-
Notifications
You must be signed in to change notification settings - Fork 509
Expand file tree
/
Copy pathSA1515UnitTests.cs
More file actions
362 lines (312 loc) · 11 KB
/
SA1515UnitTests.cs
File metadata and controls
362 lines (312 loc) · 11 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#nullable disable
namespace StyleCop.Analyzers.Test.LayoutRules
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.LayoutRules.SA1515SingleLineCommentMustBePrecededByBlankLine,
StyleCop.Analyzers.LayoutRules.SA1515CodeFixProvider>;
public class SA1515UnitTests
{
/// <summary>
/// Verifies that all known types valid single line comment lines will not produce a diagnostic.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestValidSingleLineCommentsAsync()
{
var testCode = @"// A single line comment at the start of the file is valid
namespace Foo
{
public class Bar
{
// A single line comment at the start of the scope is valid
private int field1;
// This is valid as well ofcourse
private int field2;
private int field3; // This should not trigger ofcourse
#if (SPECIALTEST)
private int field4;
#else
// this is also allowed
private double field4;
#endif
// This is valid ofcourse
private int field5;
// Multiple single line comments
// directly after each other are valid as well
public int Baz()
{
var x = field1;
////return 0;
return x;
}
public void Qux()
{
switch (this.Baz())
{
case 1:
// Single line comment after case statement is valid
break;
default:
// Single line comment after default statement is valid
return;
}
}
}
}
";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that invalid single line comment lines will produce a diagnostic.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestInvalidSingleLineCommentsAsync()
{
var testCode = @"namespace Foo
{
public class Bar
{
private int field1;
// This is invalid
private int field2;
#if (SPECIALTEST)
private int field3;
#endif
// This is invalid #2
private int field4;
}
}
";
var fixedTestCode = @"namespace Foo
{
public class Bar
{
private int field1;
// This is invalid
private int field2;
#if (SPECIALTEST)
private int field3;
#endif
// This is invalid #2
private int field4;
}
}
";
DiagnosticResult[] expectedDiagnostic =
{
Diagnostic().WithLocation(6, 9),
Diagnostic().WithLocation(12, 9),
};
await VerifyCSharpFixAsync(testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that an invalid single line comment line within a disabled conditional directive will not produce a diagnostic.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestSingleLineCommentWithinConditionalDirectiveAsync()
{
var testCode = @"// A single line comment at the start of the file is valid
namespace Foo
{
public class Bar
{
#if (SPECIALTEST)
private int field1;
// This is invalid
private int field2;
#endif
public int Baz()
{
return 0;
}
}
}
";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// This is a regression test for DotNetAnalyzers/StyleCopAnalyzers#1083 "SA1515 fires after some directives
/// when it shouldn't".
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestPreprocessorInteractionAsync()
{
string testCode = @"#region Test
// Inside region
using System;
#endregion
// After region
#pragma warning restore 1234
// After pragma
using System.Reflection;
#if DEBUG
// After if
using System.Resources;
#endif
// After endif
using System.Runtime.InteropServices;
";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies the analyzer will properly handle documentation followed by a comment.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestDocumentationFollowedByCommentAsync()
{
var testCode = @"
/// <summary>some documentation</summary>
// some comment
public class TestClass
{
/// <summary>more documentation.</summary>
// another comment
public void TestMethod() { }
}
";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that a comment between two statements with an end of line comment is handled properly.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(2176, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2176")]
public async Task TestCommentBetweenStatementsWithEndOfLineCommentAsync()
{
var testCode = @"
public class TestConstants
{
public const string Constant1 = ""1""; // my end of line comment
// my comment
public const string Constant2 = ""2""; // my second end of line comment
}
";
var fixedTestCode = @"
public class TestConstants
{
public const string Constant1 = ""1""; // my end of line comment
// my comment
public const string Constant2 = ""2""; // my second end of line comment
}
";
DiagnosticResult[] expectedDiagnostic =
{
Diagnostic().WithLocation(5, 3),
};
await VerifyCSharpFixAsync(testCode, expectedDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that the analyzer will properly handle documentation followed by a comment,
/// even if there is another non-adjacent comment earlier.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(3481, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3481")]
public async Task TestDocumentationFollowedByCommentWhenThereIsAlsoAnEarlierCommentAsync()
{
var testCode = @"
public class Class1 // Comment 1
{
public Class1()
{
}
/// <summary>
/// Gets value.
/// </summary>
// Comment 2
public double Value { get; }
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that the analyzer does not fire in file headers (i.e. one line comments at the start of the file).
/// </summary>
/// <param name="startWithPragma"><see langword="true"/> if the source code should start with a pragma; otherwise, <see langword="false"/>.</param>
/// <param name="numberOfHeaderLines">The number of lines in the header.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[CombinatorialData]
[WorkItem(3630, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3630")]
public async Task TestFileHeaderAsync(
bool startWithPragma,
[CombinatorialValues(1, 2, 3)] int numberOfHeaderLines)
{
var testCode = @"
class TestClass
{
}";
for (var i = 0; i < numberOfHeaderLines; i++)
{
testCode = "// A comment line." + Environment.NewLine + testCode;
}
if (startWithPragma)
{
testCode = "#pragma warning disable CS1591" + Environment.NewLine + testCode;
}
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that the analyzer does not fire in expression bodied properties.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(3550, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3550")]
public async Task TestExpressionBodiedPropertyAsync()
{
var testCode = @"
class TestClass
{
public int TestProperty =>
// A comment line
42;
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that the analyzer does not fire in expression bodied indexers.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(3550, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3550")]
public async Task TestExpressionBodiedIndexerAsync()
{
var testCode = @"
class TestClass
{
public int this[int i] =>
// A comment line
42;
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that the analyzer does not fire in expression bodied methods.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(3550, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3550")]
public async Task TestExpressionBodiedMethodAsync()
{
var testCode = @"
class TestClass
{
public int TestMethod(int x) =>
// A comment line
42;
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}