-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSA1517UnitTests.cs
More file actions
157 lines (139 loc) · 6.63 KB
/
Copy pathSA1517UnitTests.cs
File metadata and controls
157 lines (139 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (c) Contributors to the New StyleCop Analyzers project.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#nullable disable
namespace StyleCop.Analyzers.Test.LayoutRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.LayoutRules;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.LayoutRules.SA1517CodeMustNotContainBlankLinesAtStartOfFile,
StyleCop.Analyzers.LayoutRules.SA1517CodeFixProvider>;
/// <summary>
/// Unit tests for <see cref="SA1517CodeMustNotContainBlankLinesAtStartOfFile"/>.
/// </summary>
public class SA1517UnitTests
{
private const string BaseCode = @"using System.Diagnostics;
public class Foo
{
public void Bar(int i)
{
Debug.Assert(true);
}
}";
/// <summary>
/// Verifies that blank lines at the start of the file will produce a warning.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestWithBlankLinesAtStartOfFileAsync()
{
var testCode = "\r\n\r\n" + BaseCode;
await VerifyCSharpDiagnosticAsync(testCode, this.GenerateExpectedWarning(1, 1), CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// Verifies that blank linefeed only lines at the start of the file will produce a warning.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestWithBlankLinefeedOnlyLinesAtStartOfFileAsync()
{
var testCode = "\n\n" + BaseCode;
await VerifyCSharpDiagnosticAsync(testCode, this.GenerateExpectedWarning(1, 1), CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// Verifies that non-whitespace trivia will not produce a warning.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestWithNonWhitespaceTriviaAsync()
{
var testCode = "#if true\r\n" + BaseCode + "\r\n#endif\r\n";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// Verifies that blank lines followed by non-whitespace trivia will produce a warning.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestWithNonWhitespaceTriviaAndLeadingBlankLinesAsync()
{
var testCode = "\r\n\r\n#if true\r\n" + BaseCode + "\r\n#endif\r\n";
await VerifyCSharpDiagnosticAsync(testCode, this.GenerateExpectedWarning(1, 1), CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// Verifies that no blank lines at the start of the file will not produce a warning.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestWithoutCarriageReturnLineFeedAtStartOfFileAsync()
{
await VerifyCSharpDiagnosticAsync(BaseCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// Verifies that invalid spacing will not trigger SA1517.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestWithInvalidSpacingAsync()
{
var testCode = " " + BaseCode;
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// Verifies that the code fix provider will strip leading blank lines.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestCodeFixProviderStripsLeadingBlankLinesAsync()
{
var testCode = "\r\n\r\n" + BaseCode;
var fixedTestCode = BaseCode;
var expected = Diagnostic().WithLocation(1, 1);
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// Verifies that the code fix provider will not strip leading whitespace other than blank lines.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestCodeFixProviderHandlesWhitespaceProperlyAsync()
{
var testCode = "\r\n " + BaseCode;
var fixedTestCode = " " + BaseCode;
var expected = Diagnostic().WithLocation(1, 1);
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// Verifies that the code fix provider will strip whitespace on blank lines.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestCodeFixProviderHandlesBlankLinesWithWhitespaceProperlyAsync()
{
var testCode = " \r\n \r\n" + BaseCode;
var fixedTestCode = BaseCode;
var expected = Diagnostic().WithLocation(1, 1);
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(true);
}
/// <summary>
/// Verifies that the code fix provider will not strip non-whitespace trivia.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestCodeFixProviderHandlesNonWhitespaceTriviaProperlyAsync()
{
var testCode = "\r\n\r\n#if true\r\n" + BaseCode + "\r\n#endif\r\n";
var fixedTestCode = "#if true\r\n" + BaseCode + "\r\n#endif\r\n";
var expected = Diagnostic().WithLocation(1, 1);
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(true);
}
private DiagnosticResult GenerateExpectedWarning(int line, int column)
{
return Diagnostic().WithLocation(line, column);
}
}
}