Skip to content

Commit a0484b8

Browse files
Updated unit tests for SA1402 to verify file names after applying a code fix.
1 parent 6d80369 commit a0484b8

4 files changed

Lines changed: 38 additions & 16 deletions

File tree

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
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/CodeFixVerifier.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public abstract partial class CodeFixVerifier : DiagnosticVerifier
5050
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
5151
protected Task VerifyCSharpFixAsync(string oldSource, string newSource, string batchNewSource = null, int? codeFixIndex = null, bool allowNewCompilerDiagnostics = false, int numberOfIncrementalIterations = DefaultNumberOfIncrementalIterations, int numberOfFixAllIterations = 1, CancellationToken cancellationToken = default(CancellationToken))
5252
{
53-
return this.VerifyCSharpFixAsync(new[] { oldSource }, new[] { newSource }, batchNewSource == null ? null : new[] { batchNewSource }, codeFixIndex, allowNewCompilerDiagnostics, numberOfIncrementalIterations, numberOfFixAllIterations, cancellationToken);
53+
return this.VerifyCSharpFixAsync(new[] { oldSource }, new[] { newSource }, batchNewSource == null ? null : new[] { batchNewSource }, null, codeFixIndex, allowNewCompilerDiagnostics, numberOfIncrementalIterations, numberOfFixAllIterations, cancellationToken);
5454
}
5555

5656
/// <summary>
@@ -59,6 +59,7 @@ public abstract partial class CodeFixVerifier : DiagnosticVerifier
5959
/// <param name="oldSources">An array of sources in the form of strings before the code fix was applied to them.</param>
6060
/// <param name="newSources">An array of sources in the form of strings after the code fix was applied to them.</param>
6161
/// <param name="batchNewSources">An array of sources in the form of a strings after the batch fixer was applied to them.</param>
62+
/// <param name="newFileNames">An array of file names in the project after the code fix was applied.</param>
6263
/// <param name="codeFixIndex">Index determining which code fix to apply if there are multiple.</param>
6364
/// <param name="allowNewCompilerDiagnostics">A value indicating whether or not the test will fail if the code fix introduces other warnings after being applied.</param>
6465
/// <param name="numberOfIncrementalIterations">The number of iterations the incremental fixer will be called.
@@ -68,9 +69,9 @@ public abstract partial class CodeFixVerifier : DiagnosticVerifier
6869
/// value is less than 0, the negated value is treated as an upper limit as opposed to an exact value.</param>
6970
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param>
7071
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
71-
protected async Task VerifyCSharpFixAsync(string[] oldSources, string[] newSources, string[] batchNewSources = null, int? codeFixIndex = null, bool allowNewCompilerDiagnostics = false, int numberOfIncrementalIterations = DefaultNumberOfIncrementalIterations, int numberOfFixAllIterations = 1, CancellationToken cancellationToken = default(CancellationToken))
72+
protected async Task VerifyCSharpFixAsync(string[] oldSources, string[] newSources, string[] batchNewSources = null, string[] newFileNames = null, int? codeFixIndex = null, bool allowNewCompilerDiagnostics = false, int numberOfIncrementalIterations = DefaultNumberOfIncrementalIterations, int numberOfFixAllIterations = 1, CancellationToken cancellationToken = default(CancellationToken))
7273
{
73-
var t1 = this.VerifyFixInternalAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzers().ToImmutableArray(), this.GetCSharpCodeFixProvider(), oldSources, newSources, codeFixIndex, allowNewCompilerDiagnostics, numberOfIncrementalIterations, FixEachAnalyzerDiagnosticAsync, cancellationToken).ConfigureAwait(false);
74+
var t1 = this.VerifyFixInternalAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzers().ToImmutableArray(), this.GetCSharpCodeFixProvider(), oldSources, newSources, newFileNames, codeFixIndex, allowNewCompilerDiagnostics, numberOfIncrementalIterations, FixEachAnalyzerDiagnosticAsync, cancellationToken).ConfigureAwait(false);
7475

7576
var fixAllProvider = this.GetCSharpCodeFixProvider().GetFixAllProvider();
7677
Assert.NotEqual(WellKnownFixAllProviders.BatchFixer, fixAllProvider);
@@ -86,19 +87,19 @@ public abstract partial class CodeFixVerifier : DiagnosticVerifier
8687
await t1;
8788
}
8889

89-
var t2 = this.VerifyFixInternalAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzers().ToImmutableArray(), this.GetCSharpCodeFixProvider(), oldSources, batchNewSources ?? newSources, codeFixIndex, allowNewCompilerDiagnostics, numberOfFixAllIterations, FixAllAnalyzerDiagnosticsInDocumentAsync, cancellationToken).ConfigureAwait(false);
90+
var t2 = this.VerifyFixInternalAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzers().ToImmutableArray(), this.GetCSharpCodeFixProvider(), oldSources, batchNewSources ?? newSources, newFileNames, codeFixIndex, allowNewCompilerDiagnostics, numberOfFixAllIterations, FixAllAnalyzerDiagnosticsInDocumentAsync, cancellationToken).ConfigureAwait(false);
9091
if (Debugger.IsAttached)
9192
{
9293
await t2;
9394
}
9495

95-
var t3 = this.VerifyFixInternalAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzers().ToImmutableArray(), this.GetCSharpCodeFixProvider(), oldSources, batchNewSources ?? newSources, codeFixIndex, allowNewCompilerDiagnostics, numberOfFixAllIterations, FixAllAnalyzerDiagnosticsInProjectAsync, cancellationToken).ConfigureAwait(false);
96+
var t3 = this.VerifyFixInternalAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzers().ToImmutableArray(), this.GetCSharpCodeFixProvider(), oldSources, batchNewSources ?? newSources, newFileNames, codeFixIndex, allowNewCompilerDiagnostics, numberOfFixAllIterations, FixAllAnalyzerDiagnosticsInProjectAsync, cancellationToken).ConfigureAwait(false);
9697
if (Debugger.IsAttached)
9798
{
9899
await t3;
99100
}
100101

101-
var t4 = this.VerifyFixInternalAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzers().ToImmutableArray(), this.GetCSharpCodeFixProvider(), oldSources, batchNewSources ?? newSources, codeFixIndex, allowNewCompilerDiagnostics, numberOfFixAllIterations, FixAllAnalyzerDiagnosticsInSolutionAsync, cancellationToken).ConfigureAwait(false);
102+
var t4 = this.VerifyFixInternalAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzers().ToImmutableArray(), this.GetCSharpCodeFixProvider(), oldSources, batchNewSources ?? newSources, newFileNames, codeFixIndex, allowNewCompilerDiagnostics, numberOfFixAllIterations, FixAllAnalyzerDiagnosticsInSolutionAsync, cancellationToken).ConfigureAwait(false);
102103
if (Debugger.IsAttached)
103104
{
104105
await t4;
@@ -128,7 +129,7 @@ public abstract partial class CodeFixVerifier : DiagnosticVerifier
128129
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
129130
protected async Task VerifyCSharpFixAllFixAsync(string oldSource, string newSource, int? codeFixIndex = null, bool allowNewCompilerDiagnostics = false, int numberOfIterations = 1, CancellationToken cancellationToken = default(CancellationToken))
130131
{
131-
await this.VerifyFixInternalAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzers().ToImmutableArray(), this.GetCSharpCodeFixProvider(), new[] { oldSource }, new[] { newSource }, codeFixIndex, allowNewCompilerDiagnostics, numberOfIterations, FixAllAnalyzerDiagnosticsInDocumentAsync, cancellationToken).ConfigureAwait(false);
132+
await this.VerifyFixInternalAsync(LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzers().ToImmutableArray(), this.GetCSharpCodeFixProvider(), new[] { oldSource }, new[] { newSource }, null, codeFixIndex, allowNewCompilerDiagnostics, numberOfIterations, FixAllAnalyzerDiagnosticsInDocumentAsync, cancellationToken).ConfigureAwait(false);
132133
}
133134

134135
/// <summary>
@@ -348,12 +349,19 @@ private async Task VerifyFixInternalAsync(
348349
CodeFixProvider codeFixProvider,
349350
string[] oldSources,
350351
string[] newSources,
352+
string[] newFileNames,
351353
int? codeFixIndex,
352354
bool allowNewCompilerDiagnostics,
353355
int numberOfIterations,
354356
Func<ImmutableArray<DiagnosticAnalyzer>, CodeFixProvider, int?, Project, int, CancellationToken, Task<Project>> getFixedProject,
355357
CancellationToken cancellationToken)
356358
{
359+
if (newFileNames != null)
360+
{
361+
// Make sure the test case is consistent regarding the number of expected sources and file names
362+
Assert.Equal($"{newSources.Length} file names", $"{newFileNames.Length} file names");
363+
}
364+
357365
var project = this.CreateProject(oldSources, language);
358366
var compilerDiagnostics = await GetCompilerDiagnosticsAsync(project, cancellationToken).ConfigureAwait(false);
359367

@@ -390,6 +398,11 @@ private async Task VerifyFixInternalAsync(
390398
{
391399
var actual = await GetStringFromDocumentAsync(updatedDocuments[i], cancellationToken).ConfigureAwait(false);
392400
Assert.Equal(newSources[i], actual);
401+
402+
if (newFileNames != null)
403+
{
404+
Assert.Equal(newFileNames[i], updatedDocuments[i].Name);
405+
}
393406
}
394407
}
395408

0 commit comments

Comments
 (0)