@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.Test.LayoutRules
66 using System . Collections . Generic ;
77 using System . Threading ;
88 using System . Threading . Tasks ;
9+ using Microsoft . CodeAnalysis ;
910 using Microsoft . CodeAnalysis . CodeFixes ;
1011 using Microsoft . CodeAnalysis . Diagnostics ;
1112 using StyleCop . Analyzers . LayoutRules ;
@@ -14,7 +15,7 @@ namespace StyleCop.Analyzers.Test.LayoutRules
1415 using Xunit ;
1516
1617 /// <summary>
17- /// Unit tests for <see cref="SA1518CodeMustNotContainBlankLinesAtEndOfFile "/>.
18+ /// Unit tests for <see cref="SA1518UseLineEndingsCorrectlyAtEndOfFile "/>.
1819 /// </summary>
1920 public class SA1518UnitTests : CodeFixVerifier
2021 {
@@ -47,7 +48,7 @@ internal async Task TestWithBlankLinesAtEndOfFileAsync(EndOfFileHandling? newlin
4748 var testCode = BaseCode + "\r \n \r \n " ;
4849 var fixedCode = BaseCode + result ;
4950
50- var expected = this . CSharpDiagnostic ( ) . WithLocation ( 8 , 2 ) ;
51+ var expected = this . CSharpDiagnostic ( this . GetDescriptor ( newlineAtEndOfFile ) ) . WithLocation ( 8 , 2 ) ;
5152 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
5253 await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
5354 await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
@@ -71,7 +72,7 @@ internal async Task TestWithLineFeedOnlyBlankLinesAtEndOfFileAsync(EndOfFileHand
7172 var testCode = BaseCode + "\n \n " ;
7273 var fixedCode = BaseCode + result ;
7374
74- var expected = this . CSharpDiagnostic ( ) . WithLocation ( 8 , 2 ) ;
75+ var expected = this . CSharpDiagnostic ( this . GetDescriptor ( newlineAtEndOfFile ) ) . WithLocation ( 8 , 2 ) ;
7576 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
7677 await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
7778 await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
@@ -102,7 +103,7 @@ internal async Task TestWithSingleCarriageReturnLineFeedAtEndOfFileAsync(EndOfFi
102103 }
103104 else
104105 {
105- var expected = this . CSharpDiagnostic ( ) . WithLocation ( 8 , 2 ) ;
106+ var expected = this . CSharpDiagnostic ( this . GetDescriptor ( newlineAtEndOfFile ) ) . WithLocation ( 8 , 2 ) ;
106107 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
107108 await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
108109 await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
@@ -134,7 +135,7 @@ internal async Task TestWithSingleLineFeedAtEndOfFileAsync(EndOfFileHandling? ne
134135 }
135136 else
136137 {
137- var expected = this . CSharpDiagnostic ( ) . WithLocation ( 8 , 2 ) ;
138+ var expected = this . CSharpDiagnostic ( this . GetDescriptor ( newlineAtEndOfFile ) ) . WithLocation ( 8 , 2 ) ;
138139 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
139140 await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
140141 await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
@@ -166,7 +167,7 @@ internal async Task TestWithoutCarriageReturnLineFeedAtEndOfFileAsync(EndOfFileH
166167 }
167168 else
168169 {
169- var expected = this . CSharpDiagnostic ( ) . WithLocation ( 8 , 2 ) ;
170+ var expected = this . CSharpDiagnostic ( this . GetDescriptor ( newlineAtEndOfFile ) ) . WithLocation ( 8 , 2 ) ;
170171 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
171172 await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
172173 await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
@@ -191,7 +192,7 @@ internal async Task TestFileEndsWithSpacesAsync(EndOfFileHandling? newlineAtEndO
191192 var testCode = BaseCode + "\r \n " ;
192193 var fixedCode = BaseCode + expectedText ;
193194
194- var expected = this . CSharpDiagnostic ( ) . WithLocation ( 8 , 2 ) ;
195+ var expected = this . CSharpDiagnostic ( this . GetDescriptor ( newlineAtEndOfFile ) ) . WithLocation ( 8 , 2 ) ;
195196 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
196197 await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
197198 await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
@@ -222,7 +223,7 @@ internal async Task TestFileEndingWithCommentAsync(EndOfFileHandling? newlineAtE
222223 }
223224 else
224225 {
225- var expected = this . CSharpDiagnostic ( ) . WithLocation ( 9 , 16 ) ;
226+ var expected = this . CSharpDiagnostic ( this . GetDescriptor ( newlineAtEndOfFile ) ) . WithLocation ( 9 , 16 ) ;
226227 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
227228 await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
228229 await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
@@ -247,7 +248,7 @@ internal async Task TestFileEndingWithCommentAndSpuriousWhitespaceAsync(EndOfFil
247248 var testCode = BaseCode + "\r \n // Test comment\r \n \r \n " ;
248249 var fixedCode = BaseCode + "\r \n // Test comment" + expectedText ;
249250
250- var expected = this . CSharpDiagnostic ( ) . WithLocation ( 9 , 16 ) ;
251+ var expected = this . CSharpDiagnostic ( this . GetDescriptor ( newlineAtEndOfFile ) ) . WithLocation ( 9 , 16 ) ;
251252 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
252253 await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
253254 await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
@@ -278,7 +279,7 @@ internal async Task TestFileEndingWithEndIfAsync(EndOfFileHandling? newlineAtEnd
278279 }
279280 else
280281 {
281- var expected = this . CSharpDiagnostic ( ) . WithLocation ( 10 , 7 ) ;
282+ var expected = this . CSharpDiagnostic ( this . GetDescriptor ( newlineAtEndOfFile ) ) . WithLocation ( 10 , 7 ) ;
282283 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
283284 await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
284285 await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
@@ -303,7 +304,7 @@ internal async Task TestFileEndingWithEndIfWithSpuriousWhitespaceAsync(EndOfFile
303304 var testCode = "#if true\r \n " + BaseCode + "\r \n #endif\r \n \r \n " ;
304305 var fixedCode = "#if true\r \n " + BaseCode + "\r \n #endif" + expectedText ;
305306
306- var expected = this . CSharpDiagnostic ( ) . WithLocation ( 10 , 7 ) ;
307+ var expected = this . CSharpDiagnostic ( this . GetDescriptor ( newlineAtEndOfFile ) ) . WithLocation ( 10 , 7 ) ;
307308 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
308309 await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
309310 await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
@@ -327,7 +328,7 @@ internal async Task TestCodeFixProviderStripsTrailingBlankLinesAsync(EndOfFileHa
327328 var testCode = BaseCode + "\r \n \r \n " ;
328329 var fixedCode = BaseCode + expectedText ;
329330
330- var expected = this . CSharpDiagnostic ( ) . WithLocation ( 8 , 2 ) ;
331+ var expected = this . CSharpDiagnostic ( this . GetDescriptor ( newlineAtEndOfFile ) ) . WithLocation ( 8 , 2 ) ;
331332 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
332333 await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
333334 await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
@@ -351,7 +352,7 @@ internal async Task TestCodeFixProviderStripsTrailingBlankLinesIncludingWhitespa
351352 var testCode = BaseCode + "\r \n \r \n \r \n " ;
352353 var fixedCode = BaseCode + expectedText ;
353354
354- var expected = this . CSharpDiagnostic ( ) . WithLocation ( 8 , 2 ) ;
355+ var expected = this . CSharpDiagnostic ( this . GetDescriptor ( newlineAtEndOfFile ) ) . WithLocation ( 8 , 2 ) ;
355356 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
356357 await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
357358 await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
@@ -375,7 +376,7 @@ internal async Task TestCodeFixProviderStripsTrailingLinefeedOnlyBlankLinesInclu
375376 var testCode = BaseCode + "\n \n \n " ;
376377 var fixedCode = BaseCode + expectedText ;
377378
378- var expected = this . CSharpDiagnostic ( ) . WithLocation ( 8 , 2 ) ;
379+ var expected = this . CSharpDiagnostic ( this . GetDescriptor ( newlineAtEndOfFile ) ) . WithLocation ( 8 , 2 ) ;
379380 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
380381 await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
381382 await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
@@ -399,7 +400,7 @@ internal async Task TestCodeFixProviderOnlyStripsTrailingBlankLinesAsync(EndOfFi
399400 var testCode = "#if true\r \n " + BaseCode + "\r \n #endif\r \n \r \n " ;
400401 var fixedCode = "#if true\r \n " + BaseCode + "\r \n #endif" + expectedText ;
401402
402- var expected = this . CSharpDiagnostic ( ) . WithLocation ( 10 , 7 ) ;
403+ var expected = this . CSharpDiagnostic ( this . GetDescriptor ( newlineAtEndOfFile ) ) . WithLocation ( 10 , 7 ) ;
403404 await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
404405 await this . VerifyCSharpDiagnosticAsync ( fixedCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
405406 await this . VerifyCSharpFixAsync ( testCode , fixedCode ) . ConfigureAwait ( false ) ;
@@ -426,13 +427,30 @@ protected override string GetSettings()
426427 /// <inheritdoc/>
427428 protected override IEnumerable < DiagnosticAnalyzer > GetCSharpDiagnosticAnalyzers ( )
428429 {
429- yield return new SA1518CodeMustNotContainBlankLinesAtEndOfFile ( ) ;
430+ yield return new SA1518UseLineEndingsCorrectlyAtEndOfFile ( ) ;
430431 }
431432
432433 /// <inheritdoc/>
433434 protected override CodeFixProvider GetCSharpCodeFixProvider ( )
434435 {
435436 return new SA1518CodeFixProvider ( ) ;
436437 }
438+
439+ private DiagnosticDescriptor GetDescriptor ( EndOfFileHandling ? endOfFileHandling )
440+ {
441+ switch ( endOfFileHandling )
442+ {
443+ case EndOfFileHandling . Require :
444+ return SA1518UseLineEndingsCorrectlyAtEndOfFile . DescriptorRequire ;
445+
446+ case EndOfFileHandling . Omit :
447+ return SA1518UseLineEndingsCorrectlyAtEndOfFile . DescriptorOmit ;
448+
449+ case EndOfFileHandling . Allow :
450+ case null :
451+ default :
452+ return SA1518UseLineEndingsCorrectlyAtEndOfFile . DescriptorAllow ;
453+ }
454+ }
437455 }
438456}
0 commit comments