Skip to content

Commit 02205f6

Browse files
committed
Convert remaining ordering rules to the new test helpers
1 parent 3206848 commit 02205f6

14 files changed

Lines changed: 501 additions & 583 deletions
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.OrderingRules
5+
{
6+
using System.Collections.Generic;
7+
using Microsoft.CodeAnalysis;
8+
using Microsoft.CodeAnalysis.Diagnostics;
9+
using StyleCop.Analyzers.OrderingRules;
10+
using StyleCop.Analyzers.Test.Verifiers;
11+
using TestHelper;
12+
13+
internal static class CombinedUsingDirectivesVerifier
14+
{
15+
internal static DiagnosticResult[] EmptyDiagnosticResults
16+
=> DiagnosticVerifier<SA1200UsingDirectivesMustBePlacedCorrectly>.EmptyDiagnosticResults;
17+
18+
internal static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor)
19+
=> new DiagnosticResult(descriptor);
20+
21+
internal class CSharpTest : StyleCopCodeFixVerifier<SA1200UsingDirectivesMustBePlacedCorrectly, UsingCodeFixProvider>.CSharpTest
22+
{
23+
protected override IEnumerable<DiagnosticAnalyzer> GetDiagnosticAnalyzers()
24+
{
25+
yield return new SA1200UsingDirectivesMustBePlacedCorrectly();
26+
yield return new SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives();
27+
yield return new SA1209UsingAliasDirectivesMustBePlacedAfterOtherUsingDirectives();
28+
yield return new SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace();
29+
yield return new SA1211UsingAliasDirectivesMustBeOrderedAlphabeticallyByAliasName();
30+
yield return new SA1216UsingStaticDirectivesMustBePlacedAtTheCorrectLocation();
31+
yield return new SA1217UsingStaticDirectivesMustBeOrderedAlphabetically();
32+
}
33+
}
34+
}
35+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1210CombinedSystemDirectivesUnitTests.cs

Lines changed: 33 additions & 50 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;
118
using StyleCop.Analyzers.OrderingRules;
129
using StyleCop.Analyzers.Settings.ObjectModel;
10+
using StyleCop.Analyzers.Test.Verifiers;
1311
using TestHelper;
1412
using Xunit;
13+
using static StyleCop.Analyzers.Test.Verifiers.CustomDiagnosticVerifier<StyleCop.Analyzers.OrderingRules.SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace>;
1514

1615
/// <summary>
1716
/// Unit tests for <see cref="SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace"/> for the special case
1817
/// where <see cref="OrderingSettings.SystemUsingDirectivesFirst"/> is <see langword="false"/>.
1918
/// </summary>
20-
public class SA1210CombinedSystemDirectivesUnitTests : CodeFixVerifier
19+
public class SA1210CombinedSystemDirectivesUnitTests
2120
{
2221
[Fact]
2322
public async Task TestProperOrderedUsingDirectivesInNamespaceDeclarationAsync()
@@ -35,7 +34,7 @@ namespace Bar
3534
}
3635
";
3736

38-
await this.VerifyCSharpDiagnosticAsync(namespaceDeclaration, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
37+
await VerifyCSharpDiagnosticAsync(namespaceDeclaration, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
3938
}
4039

4140
[Fact]
@@ -71,14 +70,12 @@ namespace Bar
7170

7271
DiagnosticResult[] expected =
7372
{
74-
this.CSharpDiagnostic().WithLocation(3, 5),
75-
this.CSharpDiagnostic().WithLocation(9, 5),
76-
this.CSharpDiagnostic().WithLocation(11, 5),
73+
Diagnostic().WithLocation(3, 5),
74+
Diagnostic().WithLocation(9, 5),
75+
Diagnostic().WithLocation(11, 5),
7776
};
7877

79-
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
80-
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
81-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
78+
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, allowNewCompilerDiagnostics: false, CancellationToken.None).ConfigureAwait(false);
8279
}
8380

8481
[Fact]
@@ -110,13 +107,11 @@ namespace Food
110107

111108
DiagnosticResult[] expected =
112109
{
113-
this.CSharpDiagnostic().WithLocation(1, 1),
114-
this.CSharpDiagnostic().WithLocation(3, 1),
110+
Diagnostic().WithLocation(1, 1),
111+
Diagnostic().WithLocation(3, 1),
115112
};
116113

117-
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
118-
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
119-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
114+
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, allowNewCompilerDiagnostics: false, CancellationToken.None).ConfigureAwait(false);
120115
}
121116

122117
[Fact]
@@ -150,15 +145,13 @@ namespace Food
150145

151146
DiagnosticResult[] expected =
152147
{
153-
this.CSharpDiagnostic().WithLocation(1, 1),
154-
this.CSharpDiagnostic().WithLocation(3, 1),
155-
this.CSharpDiagnostic().WithLocation(4, 1),
156-
this.CSharpDiagnostic().WithLocation(5, 1),
148+
Diagnostic().WithLocation(1, 1),
149+
Diagnostic().WithLocation(3, 1),
150+
Diagnostic().WithLocation(4, 1),
151+
Diagnostic().WithLocation(5, 1),
157152
};
158153

159-
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
160-
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
161-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
154+
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, allowNewCompilerDiagnostics: true, CancellationToken.None).ConfigureAwait(false);
162155
}
163156

164157
[Fact]
@@ -172,7 +165,7 @@ public async Task TestValidOrderedUsingDirectivesWithStaticUsingDirectivesAsync(
172165
using static System.Math;
173166
}";
174167

175-
await this.VerifyCSharpDiagnosticAsync(namespaceDeclaration, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
168+
await VerifyCSharpDiagnosticAsync(namespaceDeclaration, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
176169
}
177170

178171
[Fact]
@@ -207,17 +200,17 @@ public async Task TestPreprocessorDirectivesAsync()
207200
// else block is skipped
208201
DiagnosticResult[] expected =
209202
{
210-
this.CSharpDiagnostic().WithLocation(2, 1),
211-
this.CSharpDiagnostic().WithLocation(7, 1),
203+
Diagnostic().WithLocation(2, 1),
204+
Diagnostic().WithLocation(7, 1),
212205
};
213206

214-
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
215-
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
216-
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
207+
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, allowNewCompilerDiagnostics: false, CancellationToken.None).ConfigureAwait(false);
217208
}
218209

219-
/// <inheritdoc/>
220-
protected override string GetSettings()
210+
private static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken)
211+
=> VerifyCSharpFixAsync(source, expected, fixedSource: null, allowNewCompilerDiagnostics: false, cancellationToken);
212+
213+
private static Task VerifyCSharpFixAsync(string source, DiagnosticResult[] expected, string fixedSource, bool allowNewCompilerDiagnostics, CancellationToken cancellationToken)
221214
{
222215
const string CombinedUsingDirectivesTestSettings = @"
223216
{
@@ -229,26 +222,16 @@ protected override string GetSettings()
229222
}
230223
";
231224

232-
return CombinedUsingDirectivesTestSettings;
233-
}
234-
235-
/// <inheritdoc/>
236-
protected override IEnumerable<string> GetDisabledDiagnostics()
237-
{
238-
// Using directive appeared previously in this namespace
239-
yield return "CS0105";
240-
}
241-
242-
/// <inheritdoc/>
243-
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
244-
{
245-
yield return new SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace();
246-
}
225+
var test = new StyleCopCodeFixVerifier<SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace, UsingCodeFixProvider>.CSharpTest
226+
{
227+
TestCode = source,
228+
FixedCode = fixedSource,
229+
Settings = CombinedUsingDirectivesTestSettings,
230+
AllowNewCompilerDiagnostics = allowNewCompilerDiagnostics,
231+
};
247232

248-
/// <inheritdoc/>
249-
protected override CodeFixProvider GetCSharpCodeFixProvider()
250-
{
251-
return new UsingCodeFixProvider();
233+
test.ExpectedDiagnostics.AddRange(expected);
234+
return test.RunAsync(cancellationToken);
252235
}
253236
}
254237
}

0 commit comments

Comments
 (0)