|
4 | 4 | namespace StyleCop.Analyzers.Test.MaintainabilityRules |
5 | 5 | { |
6 | 6 | using System.Collections.Generic; |
| 7 | + using System.Linq; |
7 | 8 | using System.Threading; |
8 | 9 | using System.Threading.Tasks; |
| 10 | + using Analyzers.MaintainabilityRules; |
9 | 11 | using Microsoft.CodeAnalysis.CodeFixes; |
10 | 12 | using Microsoft.CodeAnalysis.Diagnostics; |
11 | | - using StyleCop.Analyzers.MaintainabilityRules; |
12 | 13 | using TestHelper; |
13 | 14 | using Xunit; |
14 | 15 |
|
15 | | - public class SA1402UnitTests : FileMayOnlyContainTestBase |
| 16 | + public abstract class SA1402ForBlockDeclarationUnitTestsBase : FileMayOnlyContainTestBase |
16 | 17 | { |
17 | | - public override string Keyword |
18 | | - { |
19 | | - get |
20 | | - { |
21 | | - return "class"; |
22 | | - } |
23 | | - } |
24 | | - |
25 | 18 | public override bool SupportsCodeFix => true; |
26 | 19 |
|
| 20 | + private bool ConfigureAsNonTopLevelType { get; set; } = false; |
| 21 | + |
27 | 22 | [Fact] |
28 | | - public async Task TestPartialClassesAsync() |
| 23 | + public async Task TestTwoElementsWithRuleDisabledAsync() |
29 | 24 | { |
30 | | - var testCode = @"public partial class Foo |
| 25 | + this.ConfigureAsNonTopLevelType = true; |
| 26 | + |
| 27 | + var testCode = @"%1 Foo |
31 | 28 | { |
32 | 29 | } |
33 | | -public partial class Foo |
| 30 | +%1 Bar |
34 | 31 | { |
35 | | -
|
36 | 32 | }"; |
37 | 33 |
|
| 34 | + testCode = testCode.Replace("%1", this.Keyword); |
| 35 | + |
38 | 36 | await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
39 | 37 | } |
40 | 38 |
|
41 | 39 | [Fact] |
42 | | - public async Task TestDifferentPartialClassesAsync() |
| 40 | + public async Task TestPartialTypesAsync() |
43 | 41 | { |
44 | | - var testCode = @"public partial class Foo |
45 | | -{ |
46 | | -} |
47 | | -public partial class Bar |
48 | | -{ |
| 42 | + var testCode = $@"public partial {this.Keyword} Foo |
| 43 | +{{ |
| 44 | +}} |
| 45 | +public partial {this.Keyword} Foo |
| 46 | +{{ |
49 | 47 |
|
50 | | -}"; |
| 48 | +}}"; |
| 49 | + |
| 50 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public async Task TestDifferentPartialTypesAsync() |
| 55 | + { |
| 56 | + var testCode = $@"public partial {this.Keyword} Foo |
| 57 | +{{ |
| 58 | +}} |
| 59 | +public partial {this.Keyword} Bar |
| 60 | +{{ |
| 61 | +
|
| 62 | +}}"; |
51 | 63 |
|
52 | 64 | var fixedCode = new[] |
53 | 65 | { |
54 | | - @"public partial class Foo |
55 | | -{ |
56 | | -} |
| 66 | + $@"public partial {this.Keyword} Foo |
| 67 | +{{ |
| 68 | +}} |
57 | 69 | ", |
58 | | - @"public partial class Bar |
59 | | -{ |
| 70 | + $@"public partial {this.Keyword} Bar |
| 71 | +{{ |
60 | 72 |
|
61 | | -}" |
| 73 | +}}" |
62 | 74 | }; |
63 | 75 |
|
64 | | - DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(4, 22); |
| 76 | + DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(4, 17 + this.Keyword.Length); |
65 | 77 |
|
66 | 78 | await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
67 | 79 | await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
68 | 80 | await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false); |
69 | 81 | } |
70 | 82 |
|
71 | 83 | [Fact] |
72 | | - public async Task TestPreferFilenameClassAsync() |
| 84 | + public async Task TestPreferFilenameTypeAsync() |
73 | 85 | { |
74 | | - var testCode = @"public class Foo |
75 | | -{ |
76 | | -} |
77 | | -public class Test0 |
78 | | -{ |
79 | | -}"; |
| 86 | + var testCode = $@"public {this.Keyword} Foo |
| 87 | +{{ |
| 88 | +}} |
| 89 | +public {this.Keyword} Test0 |
| 90 | +{{ |
| 91 | +}}"; |
80 | 92 |
|
81 | 93 | var fixedCode = new[] |
82 | 94 | { |
83 | | - @"public class Test0 |
84 | | -{ |
85 | | -}", |
86 | | - @"public class Foo |
87 | | -{ |
88 | | -} |
| 95 | + $@"public {this.Keyword} Test0 |
| 96 | +{{ |
| 97 | +}}", |
| 98 | + $@"public {this.Keyword} Foo |
| 99 | +{{ |
| 100 | +}} |
89 | 101 | " |
90 | 102 | }; |
91 | 103 |
|
92 | | - DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(1, 14); |
| 104 | + DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(1, 9 + this.Keyword.Length); |
93 | 105 |
|
94 | 106 | await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
95 | 107 | await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
96 | 108 | await this.VerifyCSharpFixAsync(new[] { testCode }, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false); |
97 | 109 | } |
98 | 110 |
|
99 | 111 | [Fact] |
100 | | - public async Task TestNestedClassesAsync() |
| 112 | + public async Task TestNestedTypesAsync() |
101 | 113 | { |
102 | | - var testCode = @"public class Foo |
103 | | -{ |
104 | | - public class Bar |
105 | | - { |
| 114 | + var testCode = $@"public class Foo |
| 115 | +{{ |
| 116 | + public {this.Keyword} Bar |
| 117 | + {{ |
106 | 118 | |
107 | | - } |
108 | | -}"; |
| 119 | + }} |
| 120 | +}}"; |
109 | 121 |
|
110 | 122 | await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
111 | 123 | } |
112 | 124 |
|
| 125 | + protected override string GetSettings() |
| 126 | + { |
| 127 | + var keywords = new List<string> { "class", "interface", "struct", "enum", "delegate" }; |
| 128 | + if (this.ConfigureAsNonTopLevelType) |
| 129 | + { |
| 130 | + keywords.Remove(this.Keyword); |
| 131 | + } |
| 132 | + |
| 133 | + var keywordsStr = string.Join(", ", keywords.Select(x => "\"" + x + "\"")); |
| 134 | + |
| 135 | + var settings = $@" |
| 136 | +{{ |
| 137 | + ""settings"": {{ |
| 138 | + ""maintainabilityRules"": {{ |
| 139 | + ""topLevelTypes"": [{keywordsStr}] |
| 140 | + }} |
| 141 | + }} |
| 142 | +}}"; |
| 143 | + |
| 144 | + return settings; |
| 145 | + } |
| 146 | + |
113 | 147 | protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers() |
114 | 148 | { |
115 | | - yield return new SA1402FileMayOnlyContainASingleClass(); |
| 149 | + yield return new SA1402FileMayOnlyContainASingleType(); |
116 | 150 | } |
117 | 151 |
|
118 | 152 | protected override CodeFixProvider GetCSharpCodeFixProvider() |
|
0 commit comments