-
Notifications
You must be signed in to change notification settings - Fork 509
Expand file tree
/
Copy pathSA1214UnitTests.cs
More file actions
321 lines (272 loc) · 9.17 KB
/
SA1214UnitTests.cs
File metadata and controls
321 lines (272 loc) · 9.17 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
// 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.Test.Helpers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.OrderingRules.SA1214ReadonlyElementsMustAppearBeforeNonReadonlyElements,
StyleCop.Analyzers.OrderingRules.ElementOrderCodeFixProvider>;
public class SA1214UnitTests
{
[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() { }
}
";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
[Theory]
[MemberData(nameof(CommonMemberData.DataTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
public async Task TestTwoFieldsInClassStaticReadonlyFieldPlacedAfterStaticNonReadonlyAsync(string keyword)
{
var testCode = $@"
public {keyword} Foo
{{
private static int i = 0;
private static readonly int {{|#0:j|}} = 0;
}}";
var expected = new[]
{
Diagnostic().WithLocation(0),
};
var fixTestCode = $@"
public {keyword} Foo
{{
private static readonly int j = 0;
private static int i = 0;
}}";
await VerifyCSharpFixAsync(testCode, expected, fixTestCode, CancellationToken.None).ConfigureAwait(false);
}
[Theory]
[InlineData("\n")]
[InlineData("\r\n")]
public async Task TestTwoFieldsInClassStaticReadonlyFieldPlacedAfterStaticNonReadonlyWithLineEndingAsync(string lineEnding)
{
var testCode = @"
public class Foo
{
private static int i = 0;
private static readonly int {|#0:j|} = 0;
}".ReplaceLineEndings(lineEnding);
var expected = new[]
{
Diagnostic().WithLocation(0),
};
var fixTestCode = @"
public class Foo
{
private static readonly int j = 0;
private static int i = 0;
}".ReplaceLineEndings(lineEnding);
await VerifyCSharpFixAsync(testCode, expected, fixTestCode, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestTwoFieldsAllStaticReadonlyAsync()
{
var testCode = @"
public class Foo
{
private static readonly int i = 0;
private static readonly int j = 0;
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestTwoFieldsInClassStaticReadonlyFieldPlacedAfterStaticNonReadonlyOneLineAsync()
{
var testCode = @"
public class Foo
{
private static int i = 0;private static readonly int j = 0;
}";
var expected = new[]
{
Diagnostic().WithLocation(4, 58),
};
var fixTestCode = @"
public class Foo
{
private static readonly int j = 0;
private static int i = 0;}";
await VerifyCSharpFixAsync(testCode, expected, fixTestCode, CancellationToken.None).ConfigureAwait(false);
}
[Theory]
[MemberData(nameof(CommonMemberData.DataTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
public async Task TestTwoFieldsInClassNonStaticReadonlyFieldPlacedAfterNonStaticNonReadonlyAsync(string keyword)
{
var testCode = $@"
public {keyword} Foo
{{
private int i;
private readonly int {{|#0:j|}};
}}";
var fixedCode = $@"
public {keyword} Foo
{{
private readonly int j;
private int i;
}}";
var expected = Diagnostic().WithLocation(0);
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestTwoFieldsInStructStaticReadonlyFieldPlacedAfterStaticNonReadonlyAsync()
{
var testCode = @"
public struct Foo
{
private static int i = 0;
private static readonly int j = 0;
}";
var expected = new[]
{
Diagnostic().WithLocation(5, 33),
};
var fixTestCode = @"
public struct Foo
{
private static readonly int j = 0;
private static int i = 0;
}";
await VerifyCSharpFixAsync(testCode, expected, fixTestCode, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task ComplexExampleAsync()
{
var testCode = @"
public class Foo
{
public string s = ""qwe"";
private static readonly int i = 0;
public void Ff() {}
public static string s2 = ""qwe"";
public static readonly int u = 5;
public class FooInner
{
private int aa = 0;
public static readonly int t = 2;
private static int z = 999;
private static readonly int e = 1;
}
public static readonly int j = 0;
}";
var expected = new[]
{
Diagnostic().WithLocation(11, 33),
Diagnostic().WithLocation(18, 37),
// line 21 should be reported by SA1201
};
var fixTestCode = @"
public class Foo
{
public static readonly int u = 5;
public string s = ""qwe"";
private static readonly int i = 0;
public void Ff() {}
public static string s2 = ""qwe"";
public class FooInner
{
private int aa = 0;
public static readonly int t = 2;
private static readonly int e = 1;
private static int z = 999;
}
public static readonly int j = 0;
}";
await VerifyCSharpFixAsync(testCode, expected, fixTestCode, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestStaticReadonlyPrecededByClassAsync()
{
var testCode = @"
public class Foo
{
public static readonly int u = 5;
public string s = ""qwe"";
private static readonly int i = 0;
public void Ff() {}
public static string s2 = ""qwe"";
public class FooInner
{
private int aa = 0;
public static readonly int t = 2;
private static readonly int e = 1;
private static int z = 999;
}
public static readonly int j = 0;
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestStaticFollowedByReadOnlyAtDifferentAccessLevelAsync()
{
var testCode = @"class TestClass
{
public static int TestField1;
internal static readonly int TestField2;
}
";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}