Skip to content

Commit 4981654

Browse files
committed
Convert ordering rules tests to the new helpers
1 parent 54eea38 commit 4981654

15 files changed

Lines changed: 591 additions & 809 deletions

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/OrderingRules/SA1206CSharp7UnitTests.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ namespace StyleCop.Analyzers.Test.CSharp7.OrderingRules
55
{
66
using System.Threading;
77
using System.Threading.Tasks;
8-
using Microsoft.CodeAnalysis;
98
using Microsoft.CodeAnalysis.CSharp;
9+
using StyleCop.Analyzers.OrderingRules;
1010
using StyleCop.Analyzers.Test.OrderingRules;
11+
using StyleCop.Analyzers.Test.Verifiers;
1112
using TestHelper;
1213
using Xunit;
1314

1415
public class SA1206CSharp7UnitTests : SA1206UnitTests
1516
{
17+
private static DiagnosticResult[] EmptyDiagnosticResults
18+
=> StyleCopCodeFixVerifier<SA1206DeclarationKeywordsMustFollowOrder, SA1206CodeFixProvider>.EmptyDiagnosticResults;
19+
1620
[Theory]
1721
[InlineData("readonly")]
1822
[InlineData("ref")]
@@ -30,7 +34,7 @@ public async Task TestReadonlyRefKeywordInStructDeclarationAsync(string keywords
3034
}}
3135
}}
3236
";
33-
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
37+
await VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
3438
}
3539

3640
[Fact]
@@ -47,18 +51,31 @@ readonly private struct BitHelper
4751

4852
DiagnosticResult[] expected = new[]
4953
{
50-
this.CSharpDiagnostic().WithLocation(3, 14).WithArguments("private", "readonly"),
54+
Diagnostic().WithLocation(3, 14).WithArguments("private", "readonly"),
5155
};
52-
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
56+
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
5357
}
5458

55-
protected override Project ApplyCompilationOptions(Project project)
56-
{
57-
var newProject = base.ApplyCompilationOptions(project);
59+
private static DiagnosticResult Diagnostic()
60+
=> StyleCopCodeFixVerifier<SA1206DeclarationKeywordsMustFollowOrder, SA1206CodeFixProvider>.Diagnostic();
5861

59-
var parseOptions = (CSharpParseOptions)newProject.ParseOptions;
62+
private static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken)
63+
{
64+
var test = new StyleCopCodeFixVerifier<SA1206DeclarationKeywordsMustFollowOrder, SA1206CodeFixProvider>.CSharpTest
65+
{
66+
TestCode = source,
67+
SolutionTransforms =
68+
{
69+
(solution, projectId) =>
70+
{
71+
var parseOptions = (CSharpParseOptions)solution.GetProject(projectId).ParseOptions;
72+
return solution.WithProjectParseOptions(projectId, parseOptions.WithLanguageVersion(LanguageVersion.Latest));
73+
},
74+
},
75+
};
6076

61-
return newProject.WithParseOptions(parseOptions.WithLanguageVersion(LanguageVersion.Latest));
77+
test.ExpectedDiagnostics.AddRange(expected);
78+
return test.RunAsync(cancellationToken);
6279
}
6380
}
6481
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1200OutsideNamespaceUnitTests.cs

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33

44
namespace StyleCop.Analyzers.Test.OrderingRules
55
{
6-
using System.Collections.Generic;
76
using System.Threading;
87
using System.Threading.Tasks;
9-
using Microsoft.CodeAnalysis.CodeFixes;
10-
using Microsoft.CodeAnalysis.Diagnostics;
8+
using Microsoft.CodeAnalysis;
119
using StyleCop.Analyzers.OrderingRules;
1210
using StyleCop.Analyzers.Settings.ObjectModel;
11+
using StyleCop.Analyzers.Test.Verifiers;
1312
using TestHelper;
1413
using Xunit;
1514

1615
/// <summary>
1716
/// Unit tests for the <see cref="SA1200UsingDirectivesMustBePlacedCorrectly"/> when configured to use
1817
/// <see cref="UsingDirectivesPlacement.OutsideNamespace"/>.
1918
/// </summary>
20-
public class SA1200OutsideNamespaceUnitTests : CodeFixVerifier
19+
public class SA1200OutsideNamespaceUnitTests
2120
{
2221
private const string TestSettings = @"
2322
{
@@ -51,6 +50,9 @@ public class SA1200OutsideNamespaceUnitTests : CodeFixVerifier
5150

5251
private const string DelegateDefinition = @"public delegate void TestDelegate();";
5352

53+
internal static DiagnosticResult[] EmptyDiagnosticResults
54+
=> StyleCopCodeFixVerifier<SA1200UsingDirectivesMustBePlacedCorrectly, UsingCodeFixProvider>.EmptyDiagnosticResults;
55+
5456
/// <summary>
5557
/// Verifies that using statements in a namespace produces the expected diagnostics.
5658
/// </summary>
@@ -74,13 +76,11 @@ namespace TestNamespace
7476

7577
DiagnosticResult[] expectedResults =
7678
{
77-
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(3, 5),
78-
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(4, 5),
79+
Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(3, 5),
80+
Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(4, 5),
7981
};
8082

81-
await this.VerifyCSharpDiagnosticAsync(testCode, expectedResults, CancellationToken.None).ConfigureAwait(false);
82-
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
83-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
83+
await VerifyCSharpFixAsync(testCode, expectedResults, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
8484
}
8585

8686
/// <summary>
@@ -108,14 +108,12 @@ namespace System
108108

109109
DiagnosticResult[] expectedResults =
110110
{
111-
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(3, 5),
112-
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(4, 5),
113-
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(5, 5),
111+
Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(3, 5),
112+
Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(4, 5),
113+
Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(5, 5),
114114
};
115115

116-
await this.VerifyCSharpDiagnosticAsync(testCode, expectedResults, CancellationToken.None).ConfigureAwait(false);
117-
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
118-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
116+
await VerifyCSharpFixAsync(testCode, expectedResults, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
119117
}
120118

121119
/// <summary>
@@ -145,15 +143,13 @@ namespace System.MyExtension
145143

146144
DiagnosticResult[] expectedResults =
147145
{
148-
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(3, 5),
149-
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(4, 5),
150-
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(5, 5),
151-
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(6, 5),
146+
Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(3, 5),
147+
Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(4, 5),
148+
Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(5, 5),
149+
Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(6, 5),
152150
};
153151

154-
await this.VerifyCSharpDiagnosticAsync(testCode, expectedResults, CancellationToken.None).ConfigureAwait(false);
155-
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
156-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
152+
await VerifyCSharpFixAsync(testCode, expectedResults, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
157153
}
158154

159155
/// <summary>
@@ -174,7 +170,7 @@ public async Task TestValidUsingStatementsInCompilationUnitWithTypeDefinitionAsy
174170
{typeDefinition}
175171
";
176172

177-
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
173+
await VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
178174
}
179175

180176
/// <summary>
@@ -207,13 +203,11 @@ namespace TestNamespace
207203

208204
DiagnosticResult[] expectedResults =
209205
{
210-
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(7, 5),
211-
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(8, 5),
206+
Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(7, 5),
207+
Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(8, 5),
212208
};
213209

214-
await this.VerifyCSharpDiagnosticAsync(testCode, expectedResults, CancellationToken.None).ConfigureAwait(false);
215-
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
216-
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
210+
await VerifyCSharpFixAsync(testCode, expectedResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
217211
}
218212

219213
/// <summary>
@@ -231,7 +225,7 @@ namespace TestNamespace
231225
}
232226
";
233227

234-
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
228+
await VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
235229
}
236230

237231
/// <summary>
@@ -262,12 +256,10 @@ namespace TestNamespace
262256

263257
DiagnosticResult[] expectedResults =
264258
{
265-
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(6, 5),
259+
Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(6, 5),
266260
};
267261

268-
await this.VerifyCSharpDiagnosticAsync(testCode, expectedResults, CancellationToken.None).ConfigureAwait(false);
269-
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
270-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
262+
await VerifyCSharpFixAsync(testCode, expectedResults, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
271263
}
272264

273265
[Fact]
@@ -301,31 +293,39 @@ namespace TestNamespace
301293

302294
DiagnosticResult[] expectedResults =
303295
{
304-
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(8, 5),
305-
this.CSharpDiagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(10, 5),
296+
Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(8, 5),
297+
Diagnostic(SA1200UsingDirectivesMustBePlacedCorrectly.DescriptorOutside).WithLocation(10, 5),
306298
};
307299

308-
await this.VerifyCSharpDiagnosticAsync(testCode, expectedResults, CancellationToken.None).ConfigureAwait(false);
309-
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
310-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
300+
await VerifyCSharpFixAsync(testCode, expectedResults, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
311301
}
312302

313-
/// <inheritdoc/>
314-
protected override string GetSettings()
315-
{
316-
return TestSettings;
317-
}
303+
private static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor)
304+
=> StyleCopCodeFixVerifier<SA1200UsingDirectivesMustBePlacedCorrectly, UsingCodeFixProvider>.Diagnostic(descriptor);
318305

319-
/// <inheritdoc/>
320-
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
306+
private static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken)
321307
{
322-
yield return new SA1200UsingDirectivesMustBePlacedCorrectly();
308+
var test = new StyleCopCodeFixVerifier<SA1200UsingDirectivesMustBePlacedCorrectly, UsingCodeFixProvider>.CSharpTest
309+
{
310+
TestCode = source,
311+
Settings = TestSettings,
312+
};
313+
314+
test.ExpectedDiagnostics.AddRange(expected);
315+
return test.RunAsync(cancellationToken);
323316
}
324317

325-
/// <inheritdoc/>
326-
protected override CodeFixProvider GetCSharpCodeFixProvider()
318+
private static Task VerifyCSharpFixAsync(string source, DiagnosticResult[] expected, string fixedSource, CancellationToken cancellationToken)
327319
{
328-
return new UsingCodeFixProvider();
320+
var test = new StyleCopCodeFixVerifier<SA1200UsingDirectivesMustBePlacedCorrectly, UsingCodeFixProvider>.CSharpTest
321+
{
322+
TestCode = source,
323+
FixedCode = fixedSource,
324+
Settings = TestSettings,
325+
};
326+
327+
test.ExpectedDiagnostics.AddRange(expected);
328+
return test.RunAsync(cancellationToken);
329329
}
330330
}
331331
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1200PreserveUnitTests.cs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33

44
namespace StyleCop.Analyzers.Test.OrderingRules
55
{
6-
using System.Collections.Generic;
76
using System.Threading;
87
using System.Threading.Tasks;
9-
using Microsoft.CodeAnalysis.CodeFixes;
10-
using Microsoft.CodeAnalysis.Diagnostics;
8+
using Microsoft.CodeAnalysis;
119
using StyleCop.Analyzers.OrderingRules;
1210
using StyleCop.Analyzers.Settings.ObjectModel;
11+
using StyleCop.Analyzers.Test.Verifiers;
1312
using TestHelper;
1413
using Xunit;
1514

1615
/// <summary>
1716
/// Unit tests for the <see cref="SA1200UsingDirectivesMustBePlacedCorrectly"/> when configured to use
1817
/// <see cref="UsingDirectivesPlacement.Preserve"/>.
1918
/// </summary>
20-
public class SA1200PreserveUnitTests : CodeFixVerifier
19+
public class SA1200PreserveUnitTests
2120
{
2221
private const string TestSettings = @"
2322
{
@@ -48,6 +47,9 @@ public class SA1200PreserveUnitTests : CodeFixVerifier
4847

4948
private const string DelegateDefinition = @"public delegate void TestDelegate();";
5049

50+
internal static DiagnosticResult[] EmptyDiagnosticResults
51+
=> StyleCopCodeFixVerifier<SA1200UsingDirectivesMustBePlacedCorrectly, UsingCodeFixProvider>.EmptyDiagnosticResults;
52+
5153
/// <summary>
5254
/// Verifies that valid using statements in a namespace does not produce any diagnostics.
5355
/// </summary>
@@ -62,7 +64,7 @@ public async Task TestValidUsingStatementsInNamespaceAsync()
6264
}
6365
";
6466

65-
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
67+
await VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
6668
}
6769

6870
/// <summary>
@@ -83,7 +85,7 @@ public async Task TestValidUsingStatementsInCompilationUnitWithTypeDefinitionAsy
8385
{typeDefinition}
8486
";
8587

86-
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
88+
await VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
8789
}
8890

8991
/// <summary>
@@ -104,7 +106,7 @@ namespace TestNamespace
104106
}
105107
";
106108

107-
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
109+
await VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
108110
}
109111

110112
/// <summary>
@@ -123,25 +125,35 @@ namespace TestNamespace
123125
}
124126
";
125127

126-
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
128+
await VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
127129
}
128130

129-
/// <inheritdoc/>
130-
protected override string GetSettings()
131-
{
132-
return TestSettings;
133-
}
131+
private static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor)
132+
=> StyleCopCodeFixVerifier<SA1200UsingDirectivesMustBePlacedCorrectly, UsingCodeFixProvider>.Diagnostic(descriptor);
134133

135-
/// <inheritdoc/>
136-
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
134+
private static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken)
137135
{
138-
yield return new SA1200UsingDirectivesMustBePlacedCorrectly();
136+
var test = new StyleCopCodeFixVerifier<SA1200UsingDirectivesMustBePlacedCorrectly, UsingCodeFixProvider>.CSharpTest
137+
{
138+
TestCode = source,
139+
Settings = TestSettings,
140+
};
141+
142+
test.ExpectedDiagnostics.AddRange(expected);
143+
return test.RunAsync(cancellationToken);
139144
}
140145

141-
/// <inheritdoc/>
142-
protected override CodeFixProvider GetCSharpCodeFixProvider()
146+
private static Task VerifyCSharpFixAsync(string source, DiagnosticResult[] expected, string fixedSource, CancellationToken cancellationToken)
143147
{
144-
return new UsingCodeFixProvider();
148+
var test = new StyleCopCodeFixVerifier<SA1200UsingDirectivesMustBePlacedCorrectly, UsingCodeFixProvider>.CSharpTest
149+
{
150+
TestCode = source,
151+
FixedCode = fixedSource,
152+
Settings = TestSettings,
153+
};
154+
155+
test.ExpectedDiagnostics.AddRange(expected);
156+
return test.RunAsync(cancellationToken);
145157
}
146158
}
147159
}

0 commit comments

Comments
 (0)