Skip to content

Commit bd82367

Browse files
committed
Introduce helpers VerifyCSharpDiagnosticAsync and VerifyCSharpFixAsync
1 parent d01a67c commit bd82367

8 files changed

Lines changed: 150 additions & 109 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1000CSharp7UnitTests.cs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,14 @@ public void TestMethod()
136136
}
137137
";
138138

139-
await new CSharpTest
139+
DiagnosticResult[] expected =
140140
{
141-
TestCode = testCode,
142-
FixedCode = fixedCode,
143-
ExpectedDiagnostics =
144-
{
145-
// Returns
146-
Diagnostic().WithArguments("return", string.Empty, "followed").WithLocation(10, 43),
147-
Diagnostic().WithArguments("return", string.Empty, "followed").WithLocation(11, 43),
148-
},
149-
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
141+
// Returns
142+
Diagnostic().WithArguments("return", string.Empty, "followed").WithLocation(10, 43),
143+
Diagnostic().WithArguments("return", string.Empty, "followed").WithLocation(11, 43),
144+
};
145+
146+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
150147
}
151148

152149
/// <summary>
@@ -185,16 +182,13 @@ public void TestMethod()
185182
}
186183
";
187184

188-
await new CSharpTest
185+
DiagnosticResult[] expected =
189186
{
190-
TestCode = testCode,
191-
FixedCode = fixedCode,
192-
ExpectedDiagnostics =
193-
{
194-
Diagnostic().WithArguments("new", string.Empty, "followed").WithLocation(7, 21),
195-
Diagnostic().WithArguments("new", string.Empty, "followed").WithLocation(8, 21),
196-
},
197-
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
187+
Diagnostic().WithArguments("new", string.Empty, "followed").WithLocation(7, 21),
188+
Diagnostic().WithArguments("new", string.Empty, "followed").WithLocation(8, 21),
189+
};
190+
191+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
198192
}
199193

200194
/// <summary>
@@ -233,16 +227,13 @@ public void TestMethod()
233227
}
234228
";
235229

236-
await new CSharpTest
230+
DiagnosticResult[] expected =
237231
{
238-
TestCode = testCode,
239-
FixedCode = fixedCode,
240-
ExpectedDiagnostics =
241-
{
242-
Diagnostic().WithArguments("foreach", string.Empty, "followed").WithLocation(7, 13),
243-
Diagnostic().WithArguments("foreach", string.Empty, "followed").WithLocation(8, 13),
244-
},
245-
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
232+
Diagnostic().WithArguments("foreach", string.Empty, "followed").WithLocation(7, 13),
233+
Diagnostic().WithArguments("foreach", string.Empty, "followed").WithLocation(8, 13),
234+
};
235+
236+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
246237
}
247238
}
248239
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1001CSharp7UnitTests.cs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ namespace StyleCop.Analyzers.Test.CSharp7.SpacingRules
88
using StyleCop.Analyzers.Test.SpacingRules;
99
using TestHelper;
1010
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.SpacingRules.SA1001CommasMustBeSpacedCorrectly,
13+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
1114

1215
public class SA1001CSharp7UnitTests : SA1001UnitTests
1316
{
@@ -43,15 +46,13 @@ public class Foo
4346

4447
DiagnosticResult[] expected =
4548
{
46-
this.CSharpDiagnostic().WithLocation(5, 19).WithArguments(" not", "preceded"),
47-
this.CSharpDiagnostic().WithLocation(5, 47).WithArguments(" not", "preceded"),
48-
this.CSharpDiagnostic().WithLocation(7, 16).WithArguments(" not", "preceded"),
49-
this.CSharpDiagnostic().WithLocation(7, 65).WithArguments(" not", "preceded"),
49+
Diagnostic().WithLocation(5, 19).WithArguments(" not", "preceded"),
50+
Diagnostic().WithLocation(5, 47).WithArguments(" not", "preceded"),
51+
Diagnostic().WithLocation(7, 16).WithArguments(" not", "preceded"),
52+
Diagnostic().WithLocation(7, 65).WithArguments(" not", "preceded"),
5053
};
5154

52-
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
53-
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
54-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
55+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
5556
}
5657

5758
/// <summary>
@@ -82,11 +83,8 @@ public void TestMethod()
8283
}
8384
}";
8485

85-
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(7, 34).WithArguments(" not", "preceded");
86-
87-
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
88-
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
89-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
86+
DiagnosticResult expected = Diagnostic().WithLocation(7, 34).WithArguments(" not", "preceded");
87+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
9088
}
9189

9290
/// <summary>
@@ -117,11 +115,8 @@ public void TestMethod()
117115
}
118116
}";
119117

120-
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(7, 21).WithArguments(" not", "preceded");
121-
122-
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
123-
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
124-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
118+
DiagnosticResult expected = Diagnostic().WithLocation(7, 21).WithArguments(" not", "preceded");
119+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
125120
}
126121
}
127122
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1000UnitTests.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -896,11 +896,8 @@ void MethodName()
896896
}
897897
";
898898

899-
await new CSharpTest
900-
{
901-
TestCode = testCode,
902-
ExpectedDiagnostics = { CompilerError("CS0742").WithMessage("A query body must end with a select clause or a group clause").WithLocation(6, 42) },
903-
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
899+
var expected = CompilerError("CS0742").WithMessage("A query body must end with a select clause or a group clause").WithLocation(6, 42);
900+
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
904901
}
905902

906903
[Fact]
@@ -929,12 +926,8 @@ void MethodName()
929926
}
930927
";
931928

932-
await new CSharpTest
933-
{
934-
TestCode = testCode,
935-
FixedCode = fixedCode,
936-
ExpectedDiagnostics = { Diagnostic().WithArguments("if", string.Empty, "followed").WithLocation(6, 9) },
937-
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
929+
var expected = Diagnostic().WithArguments("if", string.Empty, "followed").WithLocation(6, 9);
930+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
938931
}
939932

940933
protected Task TestKeywordStatementAsync(string statement, DiagnosticResult expected, string fixedStatement, string returnType = "void", bool asyncMethod = false)

0 commit comments

Comments
 (0)