@@ -16,96 +16,202 @@ namespace StyleCop.Analyzers.Test.CSharp10.LayoutRules
1616
1717 public class SA1516CSharp10UnitTests : SA1516CSharp9UnitTests
1818 {
19- private const string CorrectCode = @"extern alias corlib;
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;
2035
2136using System;
2237using System.Linq;
23- using a = System.Collections.Generic ;
38+ using a = System.Collections;
2439
2540namespace Foo;
41+ " ;
2642
27- public class Bar
28- {
29- public string Test1;
30- public string Test2;
31- public string Test3;
43+ await VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
44+ }
3245
33- public string TestProperty1 { get; set; }
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+ " ;
3459
35- public string TestProperty2 { get; set; }
36- /// <summary>
37- /// A summary.
38- /// </summary>
39- public string TestProperty3 { get; set; }
60+ var fixedCode = @"namespace Foo;
4061
41- public string TestProperty4
42- {
43- get
44- {
45- return Test1;
62+ using System;
63+ using System.Linq;
64+ using a = System.Collections;
65+ " ;
66+
67+ await VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
4668 }
4769
48- set
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 ( )
4977 {
50- Test1 = value;
51- }
52- }
78+ var testCode = @"namespace Foo;
79+ [|public|] class Bar
80+ {
81+ }
82+ [|public|] enum Foobar
83+ {
84+ }
85+ " ;
5386
54- public string FooValue, BarValue ;
87+ var fixedCode = @"namespace Foo ;
5588
56- [Obsolete]
57- public enum TestEnum
58- {
59- Value1,
60- Value2
61- }
89+ public class Bar
90+ {
6291}
6392
6493public enum Foobar
6594{
95+ }
96+ " ;
6697
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+ {
67118}
68119" ;
69120
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+
70139 /// <summary>
71- /// Verifies that SA1516 is not reported for code with correct blank lines .
140+ /// Verifies that SA1516 is reported extern alias inside a file scoped namespace .
72141 /// </summary>
73142 /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
74143 [ Fact ]
75144 [ WorkItem ( 3512 , "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512" ) ]
76- public async Task TestFileScopedNamespaceCorrectSpacingAsync ( )
145+ public async Task TestThatDiagnosticIIsReportedOnExternAliasInsideFileScopedNamespaceAsync ( )
77146 {
78- await VerifyCSharpDiagnosticAsync ( CorrectCode , DiagnosticResult . EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
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 ) ;
79157 }
80158
81159 /// <summary>
82- /// Verifies that SA1516 is reported for code with missing correct blank lines .
160+ /// Verifies that SA1516 is reported extern alias and usings inside a file scoped namespace .
83161 /// </summary>
84162 /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
85163 [ Fact ]
86164 [ WorkItem ( 3512 , "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512" ) ]
87- public async Task TestFileScopedNamespaceWrongSpacingAsync ( )
165+ public async Task TestThatDiagnosticIIsReportedOnExternAliasAndUsingsInsideFileScopedNamespaceAsync ( )
88166 {
89- var testCode = @"extern alias corlib;
90- {|#0:using|} System;
167+ var testCode = @"namespace Foo;
168+ [|extern|] alias corlib;
169+ [|using|] System;
91170using System.Linq;
92- using a = System.Collections.Generic;
93- {|#1:namespace|} Foo;
94- {|#2:public|} class Bar
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
95201{
96202}
97- {|#3: public|} enum Foobar
203+ [| public|] enum Foobar
98204{
99205}
100206" ;
101207
102- var fixedCode = @"extern alias corlib;
208+ var fixedCode = @"namespace Foo;
209+
210+ extern alias corlib;
103211
104212using System;
105213using System.Linq;
106- using a = System.Collections.Generic;
107-
108- namespace Foo;
214+ using a = System.Collections;
109215
110216public class Bar
111217{
@@ -116,6 +222,11 @@ public enum Foobar
116222}
117223" ;
118224
225+ await VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
226+ }
227+
228+ private static Task VerifyCSharpFixAsync ( string testCode , string fixedCode )
229+ {
119230 var test = new CSharpTest ( LanguageVersion . CSharp10 )
120231 {
121232 ReferenceAssemblies = ReferenceAssemblies . Net . Net50 ,
@@ -126,15 +237,8 @@ public enum Foobar
126237 } ,
127238 FixedCode = fixedCode ,
128239 } ;
129- var expectedDiagnostic = new [ ]
130- {
131- Diagnostic ( ) . WithLocation ( 0 ) ,
132- Diagnostic ( ) . WithLocation ( 1 ) ,
133- Diagnostic ( ) . WithLocation ( 2 ) ,
134- Diagnostic ( ) . WithLocation ( 3 ) ,
135- } ;
136- test . TestState . ExpectedDiagnostics . AddRange ( expectedDiagnostic ) ;
137- await test . RunAsync ( CancellationToken . None ) . ConfigureAwait ( false ) ;
240+
241+ return test . RunAsync ( CancellationToken . None ) ;
138242 }
139243 }
140244}
0 commit comments