-
Notifications
You must be signed in to change notification settings - Fork 509
Expand file tree
/
Copy pathSA1204UnitTests.cs
More file actions
522 lines (445 loc) · 15.6 KB
/
SA1204UnitTests.cs
File metadata and controls
522 lines (445 loc) · 15.6 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// 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.OrderingRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.OrderingRules;
using StyleCop.Analyzers.Test.Helpers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.OrderingRules.SA1204StaticElementsMustAppearBeforeInstanceElements,
StyleCop.Analyzers.OrderingRules.ElementOrderCodeFixProvider>;
/// <summary>
/// Unit tests for <see cref="SA1204StaticElementsMustAppearBeforeInstanceElements"/>.
/// </summary>
public class SA1204UnitTests
{
/// <summary>
/// Verifies that the analyzer will properly handle valid ordering.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestValidOrderingAsync()
{
var testCode = @"public static class TestClass1 { }
public class TestClass2
{
public const int TestField1 = 1;
public static readonly int TestField2 = 1;
public static int TestField3 = 1;
public readonly int TestField4 = 1;
public int TestField5 = 1;
internal const int TestField6 = 1;
internal static readonly int TestField7 = 1;
internal static int TestField8 = 1;
internal readonly int TestField9 = 1;
internal int TestField10 = 1;
protected internal const int TestField11 = 1;
protected internal static readonly int TestField12 = 1;
protected internal static int TestField13 = 1;
protected internal readonly int TestField14 = 1;
protected internal int TestField15 = 1;
protected const int TestField16 = 1;
protected static readonly int TestField17 = 1;
protected static int TestField18 = 1;
protected readonly int TestField19 = 1;
protected int TestField20 = 1;
private const int TestField21 = 1;
private static readonly int TestField22 = 1;
private static int TestField23 = 1;
private readonly int TestField24 = 1;
private int TestField25 = 1;
public TestClass2()
{
}
private TestClass2(string a)
{
}
~TestClass2() { }
public static int TestProperty1 { get; set; }
public int TestProperty2 { get; set; }
internal static int TestProperty3 { get; set; }
internal int TestProperty4 { get; set; }
protected internal static int TestProperty5 { get; set; }
protected internal int TestProperty6 { get; set; }
protected static int TestProperty7 { get; set; }
protected int TestProperty8 { get; set; }
private static int TestProperty9 { get; set; }
private int TestProperty10 { get; set; }
public static void TestMethod1() { }
public void TestMethod2() { }
internal static void TestMethod3() { }
internal void TestMethod4() { }
protected internal static void TestMethod5() { }
protected internal void TestMethod6() { }
protected static void TestMethod7() { }
protected void TestMethod8() { }
private static void TestMethod9() { }
private void TestMethod10() { }
public static class TestClass1 { }
public class TestClass2a { }
internal static class TestClass3 { }
internal class TestClass4 { }
protected internal static class TestClass5 { }
protected internal class TestClass6 { }
protected static class TestClass7 { }
protected class TestClass8 { }
private static class TestClass9 { }
private class TestClass10 { }
}
";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that the analyzer will properly handle non-static classes before static.
/// </summary>
/// <param name="lineEnding">The line ending to use in the test code.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData("\n")]
[InlineData("\r\n")]
public async Task TestNonStaticClassBeforeStaticAsync(string lineEnding)
{
var testCode = @"public class TestClass1 { }
public static class {|#0:TestClass2|} { }
".ReplaceLineEndings(lineEnding);
DiagnosticResult[] expected =
{
Diagnostic().WithLocation(0),
};
var fixedCode = @"public static class TestClass2 { }
public class TestClass1 { }
".ReplaceLineEndings(lineEnding);
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that the analyzer will properly handle non-static elements before static in a type.
/// </summary>
/// <param name="keyword">The keyword used to declare the type.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[MemberData(nameof(CommonMemberData.DataTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
public async Task TestOrderingInClassAsync(string keyword)
{
var testCode = $@"public {keyword} TestClass
{{
public int TestField1;
public static int TestField2;
public int TestProperty1 {{ get; set; }}
public static int TestProperty2 {{ get; set; }}
public void TestMethod1() {{ }}
public static void TestMethod2() {{ }}
}}
";
DiagnosticResult[] expected =
{
Diagnostic().WithLocation(4, 23),
Diagnostic().WithLocation(6, 23),
Diagnostic().WithLocation(8, 24),
};
var fixedCode = $@"public {keyword} TestClass
{{
public static int TestField2;
public int TestField1;
public static int TestProperty2 {{ get; set; }}
public int TestProperty1 {{ get; set; }}
public static void TestMethod2() {{ }}
public void TestMethod1() {{ }}
}}
";
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that the analyzer will properly handle events.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestEventOrderingAsync()
{
var testCode = @"public class TestClass
{
public event System.Action TestEvent;
public event System.Action TestEvent2 { add { } remove { } }
public static event System.Action TestEvent3;
public static event System.Action TestEvent4 { add { } remove { } }
}
";
DiagnosticResult[] expected =
{
Diagnostic().WithLocation(5, 5),
};
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that the analyzer will properly handle instance members before const.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestConstNotReportedBeforeInstanceMembersAsync()
{
var testCode = @"public class TestClass {
public int TestField1;
public const int TestField2 = 1;
}
";
// should be reported by SA1203
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that the analyzer will properly handle nested class ordering.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestNestedClassesAsync()
{
var testCode = @"public class TestClass
{
public class TestClass1 { }
internal class TestClass2 { }
internal static class TestClass3 { }
}
";
var expected = Diagnostic().WithLocation(5, 27);
var fixedCode = @"public class TestClass
{
public class TestClass1 { }
internal static class TestClass3 { }
internal class TestClass2 { }
}
";
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that the analyzer will properly handle class ordering.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestClassesAsync()
{
var testCode = @"public class TestClass1 { }
public static class TestClass2 { }
internal class TestClass3 { }
internal static class TestClass4 { }
";
DiagnosticResult[] expected =
{
Diagnostic().WithLocation(2, 21),
Diagnostic().WithLocation(4, 23),
};
var fixedCode = @"public static class TestClass2 { }
public class TestClass1 { }
internal static class TestClass4 { }
internal class TestClass3 { }
";
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that the analyzer will properly handle unqualified members.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestMembersWithoutAccessModifiersAsync()
{
var testCode = @"class TestClass
{
string TestField1;
static string TestField2;
string TestField3;
}
";
var expected = Diagnostic().WithLocation(4, 19);
var fixedCode = @"class TestClass
{
static string TestField2;
string TestField1;
string TestField3;
}
";
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that the analyzer will properly handle unqualified classes.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestClassesWithoutAccessModifiersAsync()
{
var testCode = @"
class TestClass1 { }
static class TestClass2 { }
";
var expected = Diagnostic().WithLocation(3, 14);
var fixedCode = @"static class TestClass2 { }
class TestClass1 { }
";
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that the analyzer will properly handle wrongly ordered classes with a file header.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestWronglyOrderedClassesWithFileHeaderAsync()
{
var testCode = @"// Test header
public class TestClass1 { }
public static class TestClass2 { }
";
var expected = Diagnostic().WithLocation(4, 21);
var fixedCode = @"// Test header
public static class TestClass2 { }
public class TestClass1 { }
";
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that the analyzer will properly handle wrongly ordered classes with a file header and XMN comments.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestWronglyOrderedClassesWithFileHeaderAndXmlCommentsAsync()
{
var testCode = @"// Test header
/// <summary>
/// Test comment 1
/// </summary>
public class TestClass1 { }
/// <summary>
/// Test comment 2
/// </summary>
public static class TestClass2 { }
";
var expected = Diagnostic().WithLocation(11, 21);
var fixedCode = @"// Test header
/// <summary>
/// Test comment 2
/// </summary>
public static class TestClass2 { }
/// <summary>
/// Test comment 1
/// </summary>
public class TestClass1 { }
";
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that the analyzer will properly handle ordering within a namespace.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestNamespaceClassesAsync()
{
var testCode = @"namespace TestNamespace
{
class TestClass1 { }
static class TestClass2 { }
}
";
var expected = Diagnostic().WithLocation(4, 18);
var fixedCode = @"namespace TestNamespace
{
static class TestClass2 { }
class TestClass1 { }
}
";
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that the analyzer will properly handle static constructors.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestStaticConstructorsAsync()
{
var testCode = @"
class MyClass1
{
public MyClass1()
{
}
static MyClass1()
{
}
}
class MyClass2
{
static MyClass2()
{
}
public MyClass2()
{
}
}
";
var expected = Diagnostic().WithLocation(8, 12);
var fixedCode = @"
class MyClass1
{
static MyClass1()
{
}
public MyClass1()
{
}
}
class MyClass2
{
static MyClass2()
{
}
public MyClass2()
{
}
}
";
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestExplicitInterfaceFollowedByPrivateStaticAsync()
{
var testCode = @"
public interface TestInterface
{
void SomeMethod();
}
public class TestClass : TestInterface
{
void TestInterface.SomeMethod()
{
}
private static void ExampleMethod()
{
}
}
";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies that the analyzer will properly incomplete members.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestIncompleteMemberAsync()
{
string testCode = @"public class TestClass
{
public string Test;
public static string
public static string
}
";
// We don't care about the syntax errors.
DiagnosticResult[] expected =
{
// /0/Test0.cs(5,5): error CS1585: Member modifier 'public' must precede the member type and name
DiagnosticResult.CompilerError("CS1585").WithLocation(5, 5).WithArguments("public"),
// /0/Test0.cs(6,1): error CS1519: Invalid token '}' in class, record, struct, or interface member declaration
DiagnosticResult.CompilerError("CS1519").WithLocation(6, 1).WithArguments("}"),
};
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
}
}