Skip to content

Commit f51d23e

Browse files
add tests for white space only files not beeing classified as empty
1 parent 5b19011 commit f51d23e

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public void Bar(int i)
2929
}
3030
}";
3131

32+
private const string WhiteSpace = "\t ";
33+
3234
/// <summary>
3335
/// Verifies that blank lines at the end of the file will produce a warning.
3436
/// </summary>
@@ -49,6 +51,60 @@ internal async Task TestWithBlankLinesAtEndOfFileAsync(OptionSetting? newlineAtE
4951
await VerifyCSharpFixAsync(newlineAtEndOfFile, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
5052
}
5153

54+
/// <summary>
55+
/// Verifies file with white space only and no cr/lf at end of file will produce a warning when setting requires.
56+
/// </summary>
57+
/// <param name="newlineAtEndOfFile">The effective <see cref="OptionSetting"/> setting.</param>
58+
/// <param name="expectedText">The expected text to appear at the end of the file.</param>
59+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
60+
[Theory]
61+
[InlineData(null, null)]
62+
[InlineData(OptionSetting.Allow, null)]
63+
[InlineData(OptionSetting.Require, "\r\n")]
64+
[InlineData(OptionSetting.Omit, null)]
65+
internal async Task TestWithWhiteSpaceOnlyAsync(OptionSetting? newlineAtEndOfFile, string expectedText)
66+
{
67+
var testCode = WhiteSpace;
68+
var fixedCode = expectedText;
69+
70+
if (expectedText == null)
71+
{
72+
await VerifyCSharpDiagnosticAsync(newlineAtEndOfFile, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
73+
}
74+
else
75+
{
76+
var expected = Diagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(1, 1);
77+
await VerifyCSharpFixAsync(newlineAtEndOfFile, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
78+
}
79+
}
80+
81+
/// <summary>
82+
/// Verifies file with white space only and cr/lf at end of file will produce a warning when setting requires.
83+
/// </summary>
84+
/// <param name="newlineAtEndOfFile">The effective <see cref="OptionSetting"/> setting.</param>
85+
/// <param name="expectedText">The expected text to appear at the end of the file.</param>
86+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
87+
[Theory]
88+
[InlineData(null, null)]
89+
[InlineData(OptionSetting.Allow, null)]
90+
[InlineData(OptionSetting.Require, null)]
91+
[InlineData(OptionSetting.Omit, "")]
92+
internal async Task TestWithWhiteSpaceAndNewlineOnlyAsync(OptionSetting? newlineAtEndOfFile, string expectedText)
93+
{
94+
var testCode = WhiteSpace + "\r\n";
95+
var fixedCode = expectedText;
96+
97+
if (expectedText == null)
98+
{
99+
await VerifyCSharpDiagnosticAsync(newlineAtEndOfFile, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
100+
}
101+
else
102+
{
103+
var expected = Diagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(1, 1);
104+
await VerifyCSharpFixAsync(newlineAtEndOfFile, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
105+
}
106+
}
107+
52108
/// <summary>
53109
/// Verifies that empty files will not produce a warning.
54110
/// </summary>

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/SyntaxTreeHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static bool IsEmpty(this SyntaxTree tree, CancellationToken cancellationT
7777
var root = tree.GetRoot(cancellationToken);
7878
var firstToken = root.GetFirstToken(includeZeroWidth: true);
7979

80-
return firstToken.IsKind(SyntaxKind.EndOfFileToken) && firstToken.FullSpan.Length == 0;
80+
return firstToken.IsKind(SyntaxKind.EndOfFileToken) && firstToken.FullSpan.IsEmpty;
8181
}
8282

8383
internal static bool ContainsUsingAlias(this SyntaxTree tree, ConcurrentDictionary<SyntaxTree, bool> cache)

0 commit comments

Comments
 (0)