@@ -7,15 +7,15 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
77 using System . Threading ;
88 using System . Threading . Tasks ;
99 using Analyzers . DocumentationRules ;
10- using Microsoft . CodeAnalysis ;
10+ using Microsoft . CodeAnalysis . CodeFixes ;
1111 using Microsoft . CodeAnalysis . Diagnostics ;
1212 using TestHelper ;
1313 using Xunit ;
1414
1515 /// <summary>
1616 /// Unit tests for file header that do not follow the XML syntax.
1717 /// </summary>
18- public class NoXmlFileHeaderUnitTests : DiagnosticVerifier
18+ public class NoXmlFileHeaderUnitTests : CodeFixVerifier
1919 {
2020 private const string SettingsFileName = "stylecop.json" ;
2121 private const string TestSettings = @"
@@ -45,10 +45,19 @@ public virtual async Task TestNoFileHeaderAsync()
4545 var testCode = @"namespace Foo
4646{
4747}
48+ " ;
49+ var fixedCode = @"// Copyright (c) FooCorp. All rights reserved.
50+ // Licensed under the ??? license. See LICENSE file in the project root for full license information.
51+
52+ namespace Foo
53+ {
54+ }
4855" ;
4956
5057 var expectedDiagnostic = this . CSharpDiagnostic ( FileHeaderAnalyzers . SA1633DescriptorMissing ) . WithLocation ( 1 , 1 ) ;
5158 await this . VerifyCSharpDiagnosticAsync ( testCode , expectedDiagnostic , CancellationToken . None ) . ConfigureAwait ( false ) ;
59+ await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
60+ await this . VerifyCSharpFixAsync ( testCode , fixedCode , cancellationToken : CancellationToken . None ) . ConfigureAwait ( false ) ;
5261 }
5362
5463 /// <summary>
@@ -87,13 +96,13 @@ namespace Bar
8796 }
8897
8998 /// <summary>
90- /// Verifies that a valid file header built using multi line comments will not produce a diagnostic message.
99+ /// Verifies that a valid file header built using multi- line comments will not produce a diagnostic message.
91100 /// </summary>
92101 /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
93102 [ Fact ]
94- public async Task TestValidFileHeaderWithMultiLineCommentsAsync ( )
103+ public async Task TestValidFileHeaderWithMultiLineComments1Async ( )
95104 {
96- var testCodeFormat1 = @"/* Copyright (c) FooCorp. All rights reserved.
105+ var testCode = @"/* Copyright (c) FooCorp. All rights reserved.
97106 * Licensed under the ??? license. See LICENSE file in the project root for full license information.
98107 */
99108
@@ -102,42 +111,55 @@ namespace Bar
102111}
103112" ;
104113
105- var testCodeFormat2 = @"/* Copyright (c) FooCorp. All rights reserved.
114+ await this . VerifyCSharpDiagnosticAsync ( testCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
115+ }
116+
117+ /// <summary>
118+ /// Verifies that a valid file header built using multi-line comments will not produce a diagnostic message.
119+ /// </summary>
120+ /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
121+ [ Fact ]
122+ public async Task TestValidFileHeaderWithMultiLineComments2Async ( )
123+ {
124+ var testCode = @"/* Copyright (c) FooCorp. All rights reserved.
106125 Licensed under the ??? license. See LICENSE file in the project root for full license information. */
107126
108127namespace Bar
109128{
110129}
111130" ;
112131
113- await this . VerifyCSharpDiagnosticAsync ( testCodeFormat1 , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
114- await this . VerifyCSharpDiagnosticAsync ( testCodeFormat2 , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
132+ await this . VerifyCSharpDiagnosticAsync ( testCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
115133 }
116134
117135 /// <summary>
118136 /// Verifies that a file header without text / only whitespace will produce the expected diagnostic message.
119137 /// </summary>
138+ /// <param name="comment">The comment text.</param>
120139 /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
121- [ Fact ]
122- public async Task TestInvalidFileHeaderWithoutTextAsync ( )
140+ [ Theory ]
141+ [ InlineData ( "//" ) ]
142+ [ InlineData ( "// " ) ]
143+ public async Task TestInvalidFileHeaderWithoutTextAsync ( string comment )
123144 {
124- var testCodeFormat1 = @"//
145+ var testCode = $@ " { comment }
125146
126147namespace Bar
127- {
128- }
148+ {{
149+ }}
129150" ;
130-
131- var testCodeFormat2 = "// " + @"
151+ var fixedCode = @"// Copyright (c) FooCorp. All rights reserved.
152+ // Licensed under the ??? license. See LICENSE file in the project root for full license information.
132153
133154namespace Bar
134155{
135156}
136157" ;
137158
138159 var expected = this . CSharpDiagnostic ( FileHeaderAnalyzers . SA1635Descriptor ) . WithLocation ( 1 , 1 ) ;
139- await this . VerifyCSharpDiagnosticAsync ( testCodeFormat1 , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
140- await this . VerifyCSharpDiagnosticAsync ( testCodeFormat2 , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
160+ await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
161+ await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
162+ await this . VerifyCSharpFixAsync ( testCode , fixedCode , cancellationToken : CancellationToken . None ) . ConfigureAwait ( false ) ;
141163 }
142164
143165 /// <summary>
@@ -150,12 +172,21 @@ public async Task TestInvalidFileHeaderWithWrongTextAsync()
150172 var testCode = @"// Copyright (c) BarCorp. All rights reserved.
151173// Licensed under the ??? license. See LICENSE file in the project root for full license information.
152174
175+ namespace Bar
176+ {
177+ }
178+ " ;
179+ var fixedCode = @"// Copyright (c) FooCorp. All rights reserved.
180+ // Licensed under the ??? license. See LICENSE file in the project root for full license information.
181+
153182namespace Bar
154183{
155184}
156185" ;
157186 var expected = this . CSharpDiagnostic ( FileHeaderAnalyzers . SA1636Descriptor ) . WithLocation ( 1 , 1 ) ;
158187 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
188+ await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
189+ await this . VerifyCSharpFixAsync ( testCode , fixedCode , cancellationToken : CancellationToken . None ) . ConfigureAwait ( false ) ;
159190 }
160191
161192 /// <inheritdoc/>
@@ -169,5 +200,11 @@ protected sealed override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAna
169200 {
170201 yield return new FileHeaderAnalyzers ( ) ;
171202 }
203+
204+ /// <inheritdoc/>
205+ protected override CodeFixProvider GetCSharpCodeFixProvider ( )
206+ {
207+ return new FileHeaderCodeFixProvider ( ) ;
208+ }
172209 }
173210}
0 commit comments