Skip to content

Commit 17f4945

Browse files
committed
Fixed failing SA1518 unit tests by making them to verify the correct descriptor messages
1 parent 56fdaa5 commit 17f4945

2 files changed

Lines changed: 40 additions & 23 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1518UnitTests.cs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace StyleCop.Analyzers.Test.LayoutRules
66
using System.Collections.Generic;
77
using System.Threading;
88
using System.Threading.Tasks;
9+
10+
using Microsoft.CodeAnalysis;
911
using Microsoft.CodeAnalysis.CodeFixes;
1012
using Microsoft.CodeAnalysis.Diagnostics;
1113
using StyleCop.Analyzers.LayoutRules;
@@ -47,7 +49,7 @@ internal async Task TestWithBlankLinesAtEndOfFileAsync(EndOfFileHandling? newlin
4749
var testCode = BaseCode + "\r\n\r\n";
4850
var fixedCode = BaseCode + result;
4951

50-
var expected = this.CSharpDiagnostic().WithLocation(8, 2);
52+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
5153
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
5254
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
5355
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -71,7 +73,7 @@ internal async Task TestWithLineFeedOnlyBlankLinesAtEndOfFileAsync(EndOfFileHand
7173
var testCode = BaseCode + "\n\n";
7274
var fixedCode = BaseCode + result;
7375

74-
var expected = this.CSharpDiagnostic().WithLocation(8, 2);
76+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
7577
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
7678
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
7779
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -102,7 +104,7 @@ internal async Task TestWithSingleCarriageReturnLineFeedAtEndOfFileAsync(EndOfFi
102104
}
103105
else
104106
{
105-
var expected = this.CSharpDiagnostic().WithLocation(8, 2);
107+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
106108
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
107109
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
108110
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -134,7 +136,7 @@ internal async Task TestWithSingleLineFeedAtEndOfFileAsync(EndOfFileHandling? ne
134136
}
135137
else
136138
{
137-
var expected = this.CSharpDiagnostic().WithLocation(8, 2);
139+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
138140
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
139141
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
140142
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -166,7 +168,7 @@ internal async Task TestWithoutCarriageReturnLineFeedAtEndOfFileAsync(EndOfFileH
166168
}
167169
else
168170
{
169-
var expected = this.CSharpDiagnostic().WithLocation(8, 2);
171+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
170172
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
171173
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
172174
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -191,7 +193,7 @@ internal async Task TestFileEndsWithSpacesAsync(EndOfFileHandling? newlineAtEndO
191193
var testCode = BaseCode + "\r\n ";
192194
var fixedCode = BaseCode + expectedText;
193195

194-
var expected = this.CSharpDiagnostic().WithLocation(8, 2);
196+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
195197
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
196198
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
197199
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -222,7 +224,7 @@ internal async Task TestFileEndingWithCommentAsync(EndOfFileHandling? newlineAtE
222224
}
223225
else
224226
{
225-
var expected = this.CSharpDiagnostic().WithLocation(9, 16);
227+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(9, 16);
226228
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
227229
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
228230
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -247,7 +249,7 @@ internal async Task TestFileEndingWithCommentAndSpuriousWhitespaceAsync(EndOfFil
247249
var testCode = BaseCode + "\r\n// Test comment\r\n \r\n";
248250
var fixedCode = BaseCode + "\r\n// Test comment" + expectedText;
249251

250-
var expected = this.CSharpDiagnostic().WithLocation(9, 16);
252+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(9, 16);
251253
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
252254
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
253255
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -278,7 +280,7 @@ internal async Task TestFileEndingWithEndIfAsync(EndOfFileHandling? newlineAtEnd
278280
}
279281
else
280282
{
281-
var expected = this.CSharpDiagnostic().WithLocation(10, 7);
283+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(10, 7);
282284
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
283285
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
284286
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -303,7 +305,7 @@ internal async Task TestFileEndingWithEndIfWithSpuriousWhitespaceAsync(EndOfFile
303305
var testCode = "#if true\r\n" + BaseCode + "\r\n#endif\r\n \r\n";
304306
var fixedCode = "#if true\r\n" + BaseCode + "\r\n#endif" + expectedText;
305307

306-
var expected = this.CSharpDiagnostic().WithLocation(10, 7);
308+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(10, 7);
307309
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
308310
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
309311
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -327,7 +329,7 @@ internal async Task TestCodeFixProviderStripsTrailingBlankLinesAsync(EndOfFileHa
327329
var testCode = BaseCode + "\r\n\r\n";
328330
var fixedCode = BaseCode + expectedText;
329331

330-
var expected = this.CSharpDiagnostic().WithLocation(8, 2);
332+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
331333
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
332334
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
333335
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -351,7 +353,7 @@ internal async Task TestCodeFixProviderStripsTrailingBlankLinesIncludingWhitespa
351353
var testCode = BaseCode + "\r\n \r\n \r\n";
352354
var fixedCode = BaseCode + expectedText;
353355

354-
var expected = this.CSharpDiagnostic().WithLocation(8, 2);
356+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
355357
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
356358
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
357359
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -375,7 +377,7 @@ internal async Task TestCodeFixProviderStripsTrailingLinefeedOnlyBlankLinesInclu
375377
var testCode = BaseCode + "\n \n \n";
376378
var fixedCode = BaseCode + expectedText;
377379

378-
var expected = this.CSharpDiagnostic().WithLocation(8, 2);
380+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
379381
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
380382
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
381383
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -399,7 +401,7 @@ internal async Task TestCodeFixProviderOnlyStripsTrailingBlankLinesAsync(EndOfFi
399401
var testCode = "#if true\r\n" + BaseCode + "\r\n#endif\r\n \r\n";
400402
var fixedCode = "#if true\r\n" + BaseCode + "\r\n#endif" + expectedText;
401403

402-
var expected = this.CSharpDiagnostic().WithLocation(10, 7);
404+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(10, 7);
403405
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
404406
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
405407
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -434,5 +436,20 @@ protected override CodeFixProvider GetCSharpCodeFixProvider()
434436
{
435437
return new SA1518CodeFixProvider();
436438
}
439+
440+
private DiagnosticDescriptor GetDescriptor(EndOfFileHandling? endOfFileHandling)
441+
{
442+
switch (endOfFileHandling)
443+
{
444+
case EndOfFileHandling.Require:
445+
return SA1518CodeMustNotContainBlankLinesAtEndOfFile.DescriptorForRequireSetting;
446+
case EndOfFileHandling.Omit:
447+
return SA1518CodeMustNotContainBlankLinesAtEndOfFile.DescriptorForOmitSetting;
448+
case EndOfFileHandling.Allow:
449+
case null:
450+
default:
451+
return SA1518CodeMustNotContainBlankLinesAtEndOfFile.DescriptorForAllowSetting;
452+
}
453+
}
437454
}
438455
}

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1518CodeMustNotContainBlankLinesAtEndOfFile.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ internal class SA1518CodeMustNotContainBlankLinesAtEndOfFile : DiagnosticAnalyze
2929
/// </summary>
3030
public const string DiagnosticId = "SA1518";
3131

32+
internal static readonly DiagnosticDescriptor DescriptorForAllowSetting =
33+
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormatForAllowSetting, AnalyzerCategory.LayoutRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, DescriptionForAllowSetting, HelpLink);
34+
35+
internal static readonly DiagnosticDescriptor DescriptorForRequireSetting =
36+
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormatForRequireSetting, AnalyzerCategory.LayoutRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, DescriptionForRequireSetting, HelpLink);
37+
38+
internal static readonly DiagnosticDescriptor DescriptorForOmitSetting =
39+
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormatForOmitSetting, AnalyzerCategory.LayoutRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, DescriptionForOmitSetting, HelpLink);
40+
3241
private const string Title = "Fix blank lines at the end of the file";
3342

3443
private const string MessageFormatForAllowSetting = "Code must not contain blank lines at end of file";
@@ -42,15 +51,6 @@ internal class SA1518CodeMustNotContainBlankLinesAtEndOfFile : DiagnosticAnalyze
4251

4352
private const string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1518.md";
4453

45-
private static readonly DiagnosticDescriptor DescriptorForAllowSetting =
46-
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormatForAllowSetting, AnalyzerCategory.LayoutRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, DescriptionForAllowSetting, HelpLink);
47-
48-
private static readonly DiagnosticDescriptor DescriptorForRequireSetting =
49-
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormatForRequireSetting, AnalyzerCategory.LayoutRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, DescriptionForRequireSetting, HelpLink);
50-
51-
private static readonly DiagnosticDescriptor DescriptorForOmitSetting =
52-
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormatForOmitSetting, AnalyzerCategory.LayoutRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, DescriptionForOmitSetting, HelpLink);
53-
5454
private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
5555
private static readonly Action<SyntaxTreeAnalysisContext, StyleCopSettings> SyntaxTreeAction = HandleSyntaxTree;
5656

0 commit comments

Comments
 (0)