Skip to content

Commit a4c20e8

Browse files
Updated unit tests for SA1649 to verify both file content and file names (now using the normal VerifyCSharpFixAsync, instead of the previous specialized check)
1 parent a0484b8 commit a4c20e8

2 files changed

Lines changed: 40 additions & 65 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1649UnitTests.cs

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44
namespace StyleCop.Analyzers.Test.DocumentationRules
55
{
66
using System.Collections.Generic;
7-
using System.Collections.Immutable;
8-
using System.Linq;
97
using System.Threading;
108
using System.Threading.Tasks;
11-
using Microsoft.CodeAnalysis;
12-
using Microsoft.CodeAnalysis.CodeActions;
139
using Microsoft.CodeAnalysis.CodeFixes;
1410
using Microsoft.CodeAnalysis.Diagnostics;
1511
using StyleCop.Analyzers.DocumentationRules;
@@ -62,7 +58,7 @@ public static IEnumerable<object[]> TypeKeywords
6258
[MemberData(nameof(TypeKeywords))]
6359
public async Task VerifyWrongFileNameAsync(string typeKeyword)
6460
{
65-
var testCode = $@"namespace TestNameSpace
61+
var testCode = $@"namespace TestNamespace
6662
{{
6763
public {typeKeyword} TestType
6864
{{
@@ -81,7 +77,7 @@ public async Task VerifyWrongFileNameAsync(string typeKeyword)
8177
var expectedDiagnostic = this.CSharpDiagnostic().WithLocation("WrongFileName.cs", 3, 13 + typeKeyword.Length);
8278
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None, "WrongFileName.cs").ConfigureAwait(false);
8379
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None, "TestType.cs").ConfigureAwait(false);
84-
await this.VerifyRenameAsync(testCode, "WrongFileName.cs", "TestType.cs", CancellationToken.None).ConfigureAwait(false);
80+
await this.VerifyCSharpFixAsync(testCode, fixedCode, oldFileName: "WrongFileName.cs", newFileName: "TestType.cs", cancellationToken: CancellationToken.None).ConfigureAwait(false);
8581
}
8682

8783
/// <summary>
@@ -94,7 +90,7 @@ public async Task VerifyWrongFileNameAsync(string typeKeyword)
9490
[MemberData(nameof(TypeKeywords))]
9591
public async Task VerifyWrongFileNameMultipleExtensionsAsync(string typeKeyword)
9692
{
97-
var testCode = $@"namespace TestNameSpace
93+
var testCode = $@"namespace TestNamespace
9894
{{
9995
public {typeKeyword} TestType
10096
{{
@@ -113,7 +109,7 @@ public async Task VerifyWrongFileNameMultipleExtensionsAsync(string typeKeyword)
113109
var expectedDiagnostic = this.CSharpDiagnostic().WithLocation("WrongFileName.svc.cs", 3, 13 + typeKeyword.Length);
114110
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None, "WrongFileName.svc.cs").ConfigureAwait(false);
115111
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None, "TestType.svc.cs").ConfigureAwait(false);
116-
await this.VerifyRenameAsync(testCode, "WrongFileName.svc.cs", "TestType.svc.cs", CancellationToken.None).ConfigureAwait(false);
112+
await this.VerifyCSharpFixAsync(testCode, fixedCode, oldFileName: "WrongFileName.svc.cs", newFileName: "TestType.svc.cs", cancellationToken: CancellationToken.None).ConfigureAwait(false);
117113
}
118114

119115
/// <summary>
@@ -126,7 +122,7 @@ public async Task VerifyWrongFileNameMultipleExtensionsAsync(string typeKeyword)
126122
[MemberData(nameof(TypeKeywords))]
127123
public async Task VerifyWrongFileNameNoExtensionAsync(string typeKeyword)
128124
{
129-
var testCode = $@"namespace TestNameSpace
125+
var testCode = $@"namespace TestNamespace
130126
{{
131127
public {typeKeyword} TestType
132128
{{
@@ -145,7 +141,7 @@ public async Task VerifyWrongFileNameNoExtensionAsync(string typeKeyword)
145141
var expectedDiagnostic = this.CSharpDiagnostic().WithLocation("WrongFileName", 3, 13 + typeKeyword.Length);
146142
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None, "WrongFileName").ConfigureAwait(false);
147143
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None, "TestType").ConfigureAwait(false);
148-
await this.VerifyRenameAsync(testCode, "WrongFileName", "TestType", CancellationToken.None).ConfigureAwait(false);
144+
await this.VerifyCSharpFixAsync(testCode, fixedCode, oldFileName: "WrongFileName", newFileName: "TestType", cancellationToken: CancellationToken.None).ConfigureAwait(false);
149145
}
150146

151147
/// <summary>
@@ -157,7 +153,7 @@ public async Task VerifyWrongFileNameNoExtensionAsync(string typeKeyword)
157153
[MemberData(nameof(TypeKeywords))]
158154
public async Task VerifyCaseInsensitivityAsync(string typeKeyword)
159155
{
160-
var testCode = $@"namespace TestNameSpace
156+
var testCode = $@"namespace TestNamespace
161157
{{
162158
public {typeKeyword} TestType
163159
{{
@@ -177,7 +173,7 @@ public async Task VerifyCaseInsensitivityAsync(string typeKeyword)
177173
[MemberData(nameof(TypeKeywords))]
178174
public async Task VerifyFirstTypeIsUsedAsync(string typeKeyword)
179175
{
180-
var testCode = $@"namespace TestNameSpace
176+
var testCode = $@"namespace TestNamespace
181177
{{
182178
public {typeKeyword} TestType
183179
{{
@@ -201,7 +197,7 @@ public async Task VerifyFirstTypeIsUsedAsync(string typeKeyword)
201197
[MemberData(nameof(TypeKeywords))]
202198
public async Task VerifyThatPartialTypesAreIgnoredAsync(string typeKeyword)
203199
{
204-
var testCode = $@"namespace TestNameSpace
200+
var testCode = $@"namespace TestNamespace
205201
{{
206202
public partial {typeKeyword} TestType
207203
{{
@@ -223,7 +219,7 @@ public async Task VerifyStyleCopNamingConventionForGenericTypeAsync(string typeK
223219
{
224220
this.useMetadataSettings = false;
225221

226-
var testCode = $@"namespace TestNameSpace
222+
var testCode = $@"namespace TestNamespace
227223
{{
228224
public {typeKeyword} TestType<T1, T2, T3>
229225
{{
@@ -235,7 +231,7 @@ public async Task VerifyStyleCopNamingConventionForGenericTypeAsync(string typeK
235231
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None, "TestType`3.cs").ConfigureAwait(false);
236232
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None, "TestType.cs").ConfigureAwait(false);
237233
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None, "TestType{T1,T2,T3}.cs").ConfigureAwait(false);
238-
await this.VerifyRenameAsync(testCode, "TestType`3.cs", "TestType{T1,T2,T3}.cs", CancellationToken.None).ConfigureAwait(false);
234+
await this.VerifyCSharpFixAsync(testCode, testCode, oldFileName: "TestType`3.cs", newFileName: "TestType{T1,T2,T3}.cs", cancellationToken: CancellationToken.None).ConfigureAwait(false);
239235
}
240236

241237
/// <summary>
@@ -249,7 +245,7 @@ public async Task VerifyMetadataNamingConventionForGenericTypeAsync(string typeK
249245
{
250246
this.useMetadataSettings = true;
251247

252-
var testCode = $@"namespace TestNameSpace
248+
var testCode = $@"namespace TestNamespace
253249
{{
254250
public {typeKeyword} TestType<T1, T2, T3>
255251
{{
@@ -263,7 +259,7 @@ public async Task VerifyMetadataNamingConventionForGenericTypeAsync(string typeK
263259
expectedDiagnostic = this.CSharpDiagnostic().WithLocation("TestType.cs", 3, 13 + typeKeyword.Length);
264260
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None, "TestType.cs").ConfigureAwait(false);
265261
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None, "TestType`3.cs").ConfigureAwait(false);
266-
await this.VerifyRenameAsync(testCode, "TestType.cs", "TestType`3.cs", CancellationToken.None).ConfigureAwait(false);
262+
await this.VerifyCSharpFixAsync(testCode, testCode, oldFileName: "TestType.cs", newFileName: "TestType`3.cs", cancellationToken: CancellationToken.None).ConfigureAwait(false);
267263
}
268264

269265
/// <summary>
@@ -278,7 +274,7 @@ public async Task VerifyMetadataNamingConventionForGenericTypeMultipleExtensions
278274
{
279275
this.useMetadataSettings = true;
280276

281-
var testCode = $@"namespace TestNameSpace
277+
var testCode = $@"namespace TestNamespace
282278
{{
283279
public {typeKeyword} TestType<T>
284280
{{
@@ -297,7 +293,7 @@ public async Task VerifyMetadataNamingConventionForGenericTypeMultipleExtensions
297293
var expectedDiagnostic = this.CSharpDiagnostic().WithLocation("TestType.svc.cs", 3, 13 + typeKeyword.Length);
298294
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None, "TestType.svc.cs").ConfigureAwait(false);
299295
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None, "TestType`1.svc.cs").ConfigureAwait(false);
300-
await this.VerifyRenameAsync(testCode, "TestType.svc.cs", "TestType`1.svc.cs", CancellationToken.None).ConfigureAwait(false);
296+
await this.VerifyCSharpFixAsync(testCode, fixedCode, oldFileName: "TestType.svc.cs", newFileName: "TestType`1.svc.cs", cancellationToken: CancellationToken.None).ConfigureAwait(false);
301297
}
302298

303299
/// <summary>
@@ -307,7 +303,7 @@ public async Task VerifyMetadataNamingConventionForGenericTypeMultipleExtensions
307303
[Fact]
308304
public async Task VerifyWithoutFirstTypeAsync()
309305
{
310-
var testCode = @"namespace TestNameSpace
306+
var testCode = @"namespace TestNamespace
311307
{
312308
}
313309
";
@@ -332,39 +328,5 @@ protected override string GetSettings()
332328
{
333329
return this.useMetadataSettings ? MetadataSettings : StyleCopSettings;
334330
}
335-
336-
private async Task VerifyRenameAsync(string source, string sourceFileName, string expectedFileName, CancellationToken cancellationToken)
337-
{
338-
var analyzers = this.GetCSharpDiagnosticAnalyzers().ToImmutableArray();
339-
var document = this.CreateDocument(source, LanguageNames.CSharp, sourceFileName);
340-
var analyzerDiagnostics = await GetSortedDiagnosticsFromDocumentsAsync(analyzers, new[] { document }, cancellationToken).ConfigureAwait(false);
341-
342-
Assert.Equal(1, analyzerDiagnostics.Length);
343-
344-
var actions = new List<CodeAction>();
345-
var context = new CodeFixContext(document, analyzerDiagnostics[0], (a, d) => actions.Add(a), cancellationToken);
346-
await this.GetCSharpCodeFixProvider().RegisterCodeFixesAsync(context).ConfigureAwait(false);
347-
348-
Assert.Equal(1, actions.Count);
349-
350-
var operations = await actions[0].GetOperationsAsync(cancellationToken).ConfigureAwait(false);
351-
352-
var changedSolution = operations.OfType<ApplyChangesOperation>().Single().ChangedSolution;
353-
354-
var solutionChanges = changedSolution.GetChanges(document.Project.Solution);
355-
var projectChanges = solutionChanges.GetProjectChanges().ToArray();
356-
357-
Assert.Equal(1, projectChanges.Length);
358-
359-
var removedDocuments = projectChanges[0].GetRemovedDocuments().ToArray();
360-
Assert.Equal(1, removedDocuments.Length);
361-
Assert.Equal(document.Id, removedDocuments[0]);
362-
363-
var addedDocuments = projectChanges[0].GetAddedDocuments().ToArray();
364-
Assert.Equal(1, addedDocuments.Length);
365-
366-
var newDocument = changedSolution.GetDocument(addedDocuments[0]);
367-
Assert.Equal(expectedFileName, newDocument.Name);
368-
}
369331
}
370332
}

0 commit comments

Comments
 (0)