Skip to content

Commit 4cf3e03

Browse files
authored
Merge pull request #2237 from bjornhellander/Issue352_ImproveUnitTestsForSa1402FileMayOnlyContainASingleClass
Updated unit tests for SA1402 and SA1649 to verify both file content and file names
2 parents 594025f + a4c20e8 commit 4cf3e03

5 files changed

Lines changed: 69 additions & 72 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
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1402ForBlockDeclarationUnitTestsBase.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ public partial {this.Keyword} Bar
134134
135135
}}";
136136

137+
var fixedFileNames = new[] { "Test0.cs", "Bar.cs" };
137138
var fixedCode = new[]
138139
{
139140
$@"public partial {this.Keyword} Foo
@@ -150,7 +151,7 @@ public partial {this.Keyword} Bar
150151

151152
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
152153
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
153-
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
154+
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, newFileNames: fixedFileNames, cancellationToken: CancellationToken.None).ConfigureAwait(false);
154155
}
155156

156157
[Fact]
@@ -163,6 +164,7 @@ public async Task TestPreferFilenameTypeAsync()
163164
{{
164165
}}";
165166

167+
var fixedFileNames = new[] { "Test0.cs", "Foo.cs" };
166168
var fixedCode = new[]
167169
{
168170
$@"public {this.Keyword} Test0
@@ -178,7 +180,7 @@ public async Task TestPreferFilenameTypeAsync()
178180

179181
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
180182
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
181-
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
183+
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, newFileNames: fixedFileNames, cancellationToken: CancellationToken.None).ConfigureAwait(false);
182184
}
183185

184186
[Fact]

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1402ForDelegateUnitTests.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public async Task TestTwoElementsAsync()
2727
public delegate void Bar();
2828
";
2929

30+
var fixedFileNames = new[] { "Test0.cs", "Bar.cs" };
3031
var fixedCode = new[]
3132
{
3233
@"public delegate void Foo();
@@ -39,7 +40,7 @@ public async Task TestTwoElementsAsync()
3940

4041
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
4142
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
42-
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
43+
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, newFileNames: fixedFileNames, cancellationToken: CancellationToken.None).ConfigureAwait(false);
4344
}
4445

4546
[Fact]
@@ -49,6 +50,7 @@ public async Task TestTwoGenericElementsAsync()
4950
public delegate void Bar<T1, T2, T3>(T1 x, T2 y, T3 z);
5051
";
5152

53+
var fixedFileNames = new[] { "Test0.cs", "Bar.cs" };
5254
var fixedCode = new[]
5355
{
5456
@"public delegate void Foo();
@@ -61,7 +63,7 @@ public async Task TestTwoGenericElementsAsync()
6163

6264
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
6365
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
64-
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
66+
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, newFileNames: fixedFileNames, cancellationToken: CancellationToken.None).ConfigureAwait(false);
6567
}
6668

6769
[Fact]
@@ -96,6 +98,7 @@ public async Task TestThreeElementsAsync()
9698
public delegate void FooBar();
9799
";
98100

101+
var fixedFileNames = new[] { "Test0.cs", "Bar.cs", "FooBar.cs" };
99102
var fixedCode = new[]
100103
{
101104
@"public delegate void Foo();
@@ -114,7 +117,7 @@ public async Task TestThreeElementsAsync()
114117

115118
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
116119
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
117-
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
120+
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, newFileNames: fixedFileNames, cancellationToken: CancellationToken.None).ConfigureAwait(false);
118121
}
119122

120123
[Fact]
@@ -124,6 +127,7 @@ public async Task TestPreferFilenameTypeAsync()
124127
public delegate void Test0();
125128
";
126129

130+
var fixedFileNames = new[] { "Test0.cs", "Foo.cs" };
127131
var fixedCode = new[]
128132
{
129133
$@"public delegate void Test0();
@@ -136,7 +140,7 @@ public async Task TestPreferFilenameTypeAsync()
136140

137141
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
138142
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
139-
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
143+
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, newFileNames: fixedFileNames, cancellationToken: CancellationToken.None).ConfigureAwait(false);
140144
}
141145
}
142146
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1402ForEnumUnitTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ enum Bar
3737
}
3838
";
3939

40+
var fixedFileNames = new[] { "Test0.cs", "Bar.cs" };
4041
var fixedCode = new[]
4142
{
4243
@"enum Foo
@@ -55,7 +56,7 @@ enum Bar
5556

5657
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
5758
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
58-
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
59+
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, newFileNames: fixedFileNames, cancellationToken: CancellationToken.None).ConfigureAwait(false);
5960
}
6061

6162
[Fact]
@@ -111,6 +112,7 @@ enum FooBar
111112
}
112113
";
113114

115+
var fixedFileNames = new[] { "Test0.cs", "Bar.cs", "FooBar.cs" };
114116
var fixedCode = new[]
115117
{
116118
@"enum Foo
@@ -138,7 +140,7 @@ enum FooBar
138140

139141
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
140142
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
141-
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
143+
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, newFileNames: fixedFileNames, cancellationToken: CancellationToken.None).ConfigureAwait(false);
142144
}
143145

144146
[Fact]
@@ -154,6 +156,7 @@ enum Test0
154156
}
155157
";
156158

159+
var fixedFileNames = new[] { "Test0.cs", "Foo.cs" };
157160
var fixedCode = new[]
158161
{
159162
@"enum Test0
@@ -172,7 +175,7 @@ enum Test0
172175

173176
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
174177
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
175-
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
178+
await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, newFileNames: fixedFileNames, cancellationToken: CancellationToken.None).ConfigureAwait(false);
176179
}
177180
}
178181
}

0 commit comments

Comments
 (0)