Skip to content

Commit f5fcaec

Browse files
committed
Increase strictness of Fix All iteration count tests
* Check iterations as a fixed number instead of a bound * Assume "Fix All" operations take one iteration unless specified This change helps ensure situations like #1879 do not go unnoticed.
1 parent 7be4a33 commit f5fcaec

22 files changed

Lines changed: 143 additions & 121 deletions

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/InheritdocCodeFixProviderUnitTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace StyleCop.Analyzers.Test.DocumentationRules
55
{
66
using System.Collections.Generic;
7+
using System.Threading;
78
using System.Threading.Tasks;
89
using Microsoft.CodeAnalysis.CodeFixes;
910
using Microsoft.CodeAnalysis.Diagnostics;
@@ -54,7 +55,7 @@ public override {memberData}
5455
}}
5556
";
5657

57-
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
58+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
5859
}
5960

6061
[Theory]
@@ -95,7 +96,7 @@ public class ChildClass : IParent
9596
}}
9697
";
9798

98-
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
99+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
99100
}
100101

101102
[Theory]

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1651UnitTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ public class ClassName
225225
};
226226

227227
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
228-
229-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
228+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
229+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
230230
}
231231

232232
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1501UnitTests.cs

Lines changed: 15 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ void MethodName()
512512
while (false);
513513
";
514514

515-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
515+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
516516
}
517517

518518
/// <summary>
@@ -717,7 +717,7 @@ public void Bar(int i)
717717

718718
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
719719
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
720-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
720+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 3, cancellationToken: CancellationToken.None).ConfigureAwait(false);
721721
}
722722

723723
/// <summary>
@@ -735,50 +735,6 @@ public async Task TestNoSA1503StatementWithBracesAsync(string statementText)
735735
await this.VerifyCSharpDiagnosticAsync(this.GenerateFixedTestStatement(statementText), EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
736736
}
737737

738-
/// <summary>
739-
/// Verifies that an if / else statement followed by a block without braces will produce a warning.
740-
/// </summary>
741-
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
742-
[Fact]
743-
public async Task TestNoSA1503IfElseStatementWithoutBracesAsync()
744-
{
745-
this.suppressSA1503 = true;
746-
747-
var testCode = @"using System.Diagnostics;
748-
public class TypeName
749-
{
750-
public void Bar(int i)
751-
{
752-
if (i == 0) Debug.Assert(true); else Debug.Assert(false);
753-
}
754-
}";
755-
756-
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(6, 21);
757-
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
758-
}
759-
760-
/// <summary>
761-
/// Verifies that nested if statements followed by a block without braces will produce warnings.
762-
/// </summary>
763-
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
764-
[Fact]
765-
public async Task TestNoSA1503MultipleIfStatementsWithoutBracesAsync()
766-
{
767-
this.suppressSA1503 = true;
768-
769-
var testCode = @"using System.Diagnostics;
770-
public class TypeName
771-
{
772-
public void Bar(int i)
773-
{
774-
if (i == 0) if (i == 0) Debug.Assert(true);
775-
}
776-
}";
777-
778-
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(6, 21);
779-
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
780-
}
781-
782738
/// <summary>
783739
/// Verifies that the code fix provider will work properly for an if .. else statement.
784740
/// </summary>
@@ -809,7 +765,11 @@ public void Bar(int i)
809765
}
810766
}";
811767

812-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
768+
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(6, 21);
769+
770+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
771+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
772+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
813773
}
814774

815775
/// <summary>
@@ -864,18 +824,11 @@ public void Bar(int i)
864824
}
865825
}";
866826

867-
var fixedTestCode = @"using System.Diagnostics;
868-
public class TypeName
869-
{
870-
public void Bar(int i)
871-
{
872-
#pragma warning restore
873-
if (i == 0)
874-
Debug.Assert(true);
875-
}
876-
}";
827+
// The code fix will not make any changes.
828+
var fixedTestCode = testCode;
877829

878-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
830+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
831+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, numberOfFixAllIterations: 0, cancellationToken: CancellationToken.None).ConfigureAwait(false);
879832
}
880833

881834
/// <summary>
@@ -907,7 +860,10 @@ public void Bar(int i)
907860
}
908861
}";
909862

910-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
863+
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(6, 21);
864+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
865+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
866+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
911867
}
912868

913869
/// <summary>

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1404UnitTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void Bar()
9090

9191
expected = this.CSharpDiagnostic().WithLocation(3, 66);
9292
await this.VerifyCSharpDiagnosticAsync(fixedCode, expected, CancellationToken.None).ConfigureAwait(false);
93-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
93+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
9494
}
9595

9696
[Fact]
@@ -122,7 +122,7 @@ public void Bar()
122122

123123
expected = this.CSharpDiagnostic().WithLocation(4, 34);
124124
await this.VerifyCSharpDiagnosticAsync(fixedCode, expected, CancellationToken.None).ConfigureAwait(false);
125-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
125+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
126126
}
127127

128128
[Fact]
@@ -154,7 +154,7 @@ public void Bar()
154154

155155
expected = this.CSharpDiagnostic().WithLocation(4, 32);
156156
await this.VerifyCSharpDiagnosticAsync(fixedCode, expected, CancellationToken.None).ConfigureAwait(false);
157-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
157+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
158158
}
159159

160160
[Fact]
@@ -184,7 +184,7 @@ public void Bar()
184184

185185
expected = this.CSharpDiagnostic().WithLocation(3, 66);
186186
await this.VerifyCSharpDiagnosticAsync(fixedCode, expected, CancellationToken.None).ConfigureAwait(false);
187-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
187+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
188188
}
189189

190190
[Fact]
@@ -214,7 +214,7 @@ public void Bar()
214214

215215
expected = this.CSharpDiagnostic().WithLocation(3, 66);
216216
await this.VerifyCSharpDiagnosticAsync(fixedCode, expected, CancellationToken.None).ConfigureAwait(false);
217-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
217+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
218218
}
219219

220220
[Fact]
@@ -244,7 +244,7 @@ public void Bar()
244244

245245
expected = this.CSharpDiagnostic().WithLocation(3, 66);
246246
await this.VerifyCSharpDiagnosticAsync(fixedCode, expected, CancellationToken.None).ConfigureAwait(false);
247-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
247+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
248248
}
249249

250250
[Fact]
@@ -274,7 +274,7 @@ public void Bar()
274274

275275
expected = this.CSharpDiagnostic().WithLocation(3, 66);
276276
await this.VerifyCSharpDiagnosticAsync(fixedCode, expected, CancellationToken.None).ConfigureAwait(false);
277-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
277+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
278278
}
279279

280280
[Fact]
@@ -322,7 +322,7 @@ public void Bar()
322322

323323
expected = this.CSharpDiagnostic().WithLocation(4, 66);
324324
await this.VerifyCSharpDiagnosticAsync(fixedCode, expected, CancellationToken.None).ConfigureAwait(false);
325-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
325+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
326326
}
327327

328328
[Fact]

StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1306UnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public async Task TestThatDiagnosticIsReported_SingleFieldAsync(string modifiers
183183
}}";
184184

185185
await this.VerifyCSharpDiagnosticAsync(string.Format(fixedCode, modifiers), EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
186-
await this.VerifyCSharpFixAsync(string.Format(testCode, modifiers), string.Format(fixedCode, modifiers)).ConfigureAwait(false);
186+
await this.VerifyCSharpFixAsync(string.Format(testCode, modifiers), string.Format(fixedCode, modifiers), numberOfFixAllIterations: 4, cancellationToken: CancellationToken.None).ConfigureAwait(false);
187187
}
188188

189189
[Theory]

StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1307UnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public async Task TestThatDiagnosticIsReported_MultipleFieldsWithConflictAsync(s
145145
await this.VerifyCSharpDiagnosticAsync(string.Format(testCode, modifiers), expected, CancellationToken.None).ConfigureAwait(false);
146146
await this.VerifyCSharpDiagnosticAsync(string.Format(fixedCode, modifiers), EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
147147
await this.VerifyCSharpDiagnosticAsync(string.Format(batchFixedCode, modifiers), EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
148-
await this.VerifyCSharpFixAsync(string.Format(testCode, modifiers), string.Format(fixedCode, modifiers), string.Format(batchFixedCode, modifiers), cancellationToken: CancellationToken.None).ConfigureAwait(false);
148+
await this.VerifyCSharpFixAsync(string.Format(testCode, modifiers), string.Format(fixedCode, modifiers), string.Format(batchFixedCode, modifiers), numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
149149
}
150150

151151
[Fact]

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1201UnitTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public class TestClass { }
168168
";
169169

170170
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
171-
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
171+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 3, cancellationToken: CancellationToken.None).ConfigureAwait(false);
172172
}
173173

174174
[Fact]
@@ -221,7 +221,7 @@ public class TestClass { }
221221
";
222222

223223
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
224-
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
224+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 3, cancellationToken: CancellationToken.None).ConfigureAwait(false);
225225
}
226226

227227
[Fact]

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1202UnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ public async Task TestOnlyFirstViolationReportedAsync()
504504
";
505505

506506
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
507-
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
507+
await this.VerifyCSharpFixAsync(testCode, fixedCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
508508
}
509509

510510
/// <summary>

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/UsingCodeFixProviderCombinedSystemDirectivesUnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ namespace TestNamespace2
343343
};
344344

345345
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, expected, CancellationToken.None).ConfigureAwait(false);
346-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
346+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
347347
}
348348

349349
/// <summary>

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/UsingCodeFixProviderUnitTests.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ namespace TestNamespace2
342342
};
343343

344344
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, expected, CancellationToken.None).ConfigureAwait(false);
345-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
345+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
346346
}
347347

348348
/// <summary>
@@ -566,8 +566,16 @@ public class Haddock { }
566566
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DiagnosticId).WithLocation(11, 1)
567567
};
568568

569+
DiagnosticResult[] fixedExpected =
570+
{
571+
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DiagnosticId).WithLocation(8, 1),
572+
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DiagnosticId).WithLocation(10, 1),
573+
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DiagnosticId).WithLocation(11, 1)
574+
};
575+
569576
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
570-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
577+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, fixedExpected, CancellationToken.None).ConfigureAwait(false);
578+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
571579
}
572580

573581
/// <inheritdoc/>

0 commit comments

Comments
 (0)