|
3 | 3 |
|
4 | 4 | namespace StyleCop.Analyzers.Test.CSharp10.LayoutRules |
5 | 5 | { |
| 6 | + using System.Threading; |
| 7 | + using System.Threading.Tasks; |
| 8 | + using Microsoft.CodeAnalysis; |
| 9 | + using Microsoft.CodeAnalysis.CSharp; |
| 10 | + using Microsoft.CodeAnalysis.Testing; |
6 | 11 | using StyleCop.Analyzers.Test.CSharp9.LayoutRules; |
| 12 | + using Xunit; |
| 13 | + using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< |
| 14 | + StyleCop.Analyzers.LayoutRules.SA1516ElementsMustBeSeparatedByBlankLine, |
| 15 | + StyleCop.Analyzers.LayoutRules.SA1516CodeFixProvider>; |
7 | 16 |
|
8 | 17 | public class SA1516CSharp10UnitTests : SA1516CSharp9UnitTests |
9 | 18 | { |
| 19 | + /// <summary> |
| 20 | + /// Verifies that SA1516 is reported for usings and extern alias outside a file scoped namespace. |
| 21 | + /// </summary> |
| 22 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 23 | + [Fact] |
| 24 | + [WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")] |
| 25 | + public async Task TestThatDiagnosticIIsReportedOnUsingsAndExternAliasOutsideFileScopedNamespaceAsync() |
| 26 | + { |
| 27 | + var testCode = @"extern alias corlib; |
| 28 | +[|using|] System; |
| 29 | +using System.Linq; |
| 30 | +using a = System.Collections; |
| 31 | +[|namespace|] Foo; |
| 32 | +"; |
| 33 | + |
| 34 | + var fixedCode = @"extern alias corlib; |
| 35 | +
|
| 36 | +using System; |
| 37 | +using System.Linq; |
| 38 | +using a = System.Collections; |
| 39 | +
|
| 40 | +namespace Foo; |
| 41 | +"; |
| 42 | + |
| 43 | + await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Verifies that SA1516 is reported for usings inside a file scoped namespace. |
| 48 | + /// </summary> |
| 49 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 50 | + [Fact] |
| 51 | + [WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")] |
| 52 | + public async Task TestThatDiagnosticIIsReportedOnSpacingWithUsingsInsideFileScopedNamespaceAsync() |
| 53 | + { |
| 54 | + var testCode = @"namespace Foo; |
| 55 | +[|using|] System; |
| 56 | +using System.Linq; |
| 57 | +using a = System.Collections; |
| 58 | +"; |
| 59 | + |
| 60 | + var fixedCode = @"namespace Foo; |
| 61 | +
|
| 62 | +using System; |
| 63 | +using System.Linq; |
| 64 | +using a = System.Collections; |
| 65 | +"; |
| 66 | + |
| 67 | + await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false); |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Verifies that SA1516 is reported for member declarations inside a file scoped namespace. |
| 72 | + /// </summary> |
| 73 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 74 | + [Fact] |
| 75 | + [WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")] |
| 76 | + public async Task TestThatDiagnosticIIsReportedOnMemberDeclarationsInsideFileScopedNamespaceAsync() |
| 77 | + { |
| 78 | + var testCode = @"namespace Foo; |
| 79 | +[|public|] class Bar |
| 80 | +{ |
| 81 | +} |
| 82 | +[|public|] enum Foobar |
| 83 | +{ |
| 84 | +} |
| 85 | +"; |
| 86 | + |
| 87 | + var fixedCode = @"namespace Foo; |
| 88 | +
|
| 89 | +public class Bar |
| 90 | +{ |
| 91 | +} |
| 92 | +
|
| 93 | +public enum Foobar |
| 94 | +{ |
| 95 | +} |
| 96 | +"; |
| 97 | + |
| 98 | + await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false); |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Verifies that SA1516 is reported for usings and member declarations inside a file scoped namespace. |
| 103 | + /// </summary> |
| 104 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 105 | + [Fact] |
| 106 | + [WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")] |
| 107 | + public async Task TestThatDiagnosticIIsReportedOnUsingsAndMemberDeclarationsInsideFileScopedNamespaceAsync() |
| 108 | + { |
| 109 | + var testCode = @"namespace Foo; |
| 110 | +[|using|] System; |
| 111 | +using System.Linq; |
| 112 | +using a = System.Collections; |
| 113 | +[|public|] class Bar |
| 114 | +{ |
| 115 | +} |
| 116 | +[|public|] enum Foobar |
| 117 | +{ |
| 118 | +} |
| 119 | +"; |
| 120 | + |
| 121 | + var fixedCode = @"namespace Foo; |
| 122 | +
|
| 123 | +using System; |
| 124 | +using System.Linq; |
| 125 | +using a = System.Collections; |
| 126 | +
|
| 127 | +public class Bar |
| 128 | +{ |
| 129 | +} |
| 130 | +
|
| 131 | +public enum Foobar |
| 132 | +{ |
| 133 | +} |
| 134 | +"; |
| 135 | + |
| 136 | + await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false); |
| 137 | + } |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Verifies that SA1516 is reported extern alias inside a file scoped namespace. |
| 141 | + /// </summary> |
| 142 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 143 | + [Fact] |
| 144 | + [WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")] |
| 145 | + public async Task TestThatDiagnosticIIsReportedOnExternAliasInsideFileScopedNamespaceAsync() |
| 146 | + { |
| 147 | + var testCode = @"namespace Foo; |
| 148 | +[|extern|] alias corlib; |
| 149 | +"; |
| 150 | + |
| 151 | + var fixedCode = @"namespace Foo; |
| 152 | +
|
| 153 | +extern alias corlib; |
| 154 | +"; |
| 155 | + |
| 156 | + await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false); |
| 157 | + } |
| 158 | + |
| 159 | + /// <summary> |
| 160 | + /// Verifies that SA1516 is reported extern alias and usings inside a file scoped namespace. |
| 161 | + /// </summary> |
| 162 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 163 | + [Fact] |
| 164 | + [WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")] |
| 165 | + public async Task TestThatDiagnosticIIsReportedOnExternAliasAndUsingsInsideFileScopedNamespaceAsync() |
| 166 | + { |
| 167 | + var testCode = @"namespace Foo; |
| 168 | +[|extern|] alias corlib; |
| 169 | +[|using|] System; |
| 170 | +using System.Linq; |
| 171 | +using a = System.Collections; |
| 172 | +"; |
| 173 | + |
| 174 | + var fixedCode = @"namespace Foo; |
| 175 | +
|
| 176 | +extern alias corlib; |
| 177 | +
|
| 178 | +using System; |
| 179 | +using System.Linq; |
| 180 | +using a = System.Collections; |
| 181 | +"; |
| 182 | + |
| 183 | + await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false); |
| 184 | + } |
| 185 | + |
| 186 | + /// <summary> |
| 187 | + /// Verifies that SA1516 is reported extern alias, usings and member declarations |
| 188 | + /// inside a file scoped namespace. |
| 189 | + /// </summary> |
| 190 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 191 | + [Fact] |
| 192 | + [WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")] |
| 193 | + public async Task TestThatDiagnosticIIsReportedOnExternAliasUsingsAndMemberDeclarationsInsideFileScopedNamespaceAsync() |
| 194 | + { |
| 195 | + var testCode = @"namespace Foo; |
| 196 | +[|extern|] alias corlib; |
| 197 | +[|using|] System; |
| 198 | +using System.Linq; |
| 199 | +using a = System.Collections; |
| 200 | +[|public|] class Bar |
| 201 | +{ |
| 202 | +} |
| 203 | +[|public|] enum Foobar |
| 204 | +{ |
| 205 | +} |
| 206 | +"; |
| 207 | + |
| 208 | + var fixedCode = @"namespace Foo; |
| 209 | +
|
| 210 | +extern alias corlib; |
| 211 | +
|
| 212 | +using System; |
| 213 | +using System.Linq; |
| 214 | +using a = System.Collections; |
| 215 | +
|
| 216 | +public class Bar |
| 217 | +{ |
| 218 | +} |
| 219 | +
|
| 220 | +public enum Foobar |
| 221 | +{ |
| 222 | +} |
| 223 | +"; |
| 224 | + |
| 225 | + await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false); |
| 226 | + } |
| 227 | + |
| 228 | + private static Task VerifyCSharpFixAsync(string testCode, string fixedCode) |
| 229 | + { |
| 230 | + var test = new CSharpTest |
| 231 | + { |
| 232 | + ReferenceAssemblies = ReferenceAssemblies.Net.Net60, |
| 233 | + TestState = |
| 234 | + { |
| 235 | + Sources = { testCode }, |
| 236 | + }, |
| 237 | + FixedCode = fixedCode, |
| 238 | + }; |
| 239 | + |
| 240 | + return test.RunAsync(CancellationToken.None); |
| 241 | + } |
10 | 242 | } |
11 | 243 | } |
0 commit comments