-
Notifications
You must be signed in to change notification settings - Fork 470
/
Copy pathEnumWithFlagsAttributeTests.cs
597 lines (526 loc) · 19 KB
/
EnumWithFlagsAttributeTests.cs
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using Test.Utilities;
using Xunit;
using VerifyCS = Test.Utilities.CSharpCodeFixVerifier<
Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.EnumWithFlagsAttributeAnalyzer,
Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.EnumWithFlagsAttributeFixer>;
using VerifyVB = Test.Utilities.VisualBasicCodeFixVerifier<
Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.EnumWithFlagsAttributeAnalyzer,
Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.EnumWithFlagsAttributeFixer>;
namespace Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.UnitTests
{
public class EnumWithFlagsAttributeTests
{
private static string GetCSharpCode_EnumWithFlagsAttributes(string code, bool hasFlags)
{
string stringToReplace = hasFlags ? "[System.Flags]" : "";
return string.Format(CultureInfo.CurrentCulture, code, stringToReplace);
}
private static string GetBasicCode_EnumWithFlagsAttributes(string code, bool hasFlags)
{
string stringToReplace = hasFlags ? "<System.Flags>" : "";
return string.Format(CultureInfo.CurrentCulture, code, stringToReplace);
}
[Fact]
public async Task CSharp_EnumWithFlagsAttributes_SimpleCase()
{
var code = @"{0}
public enum SimpleFlagsEnumClass
{{
Zero = 0,
One = 1,
Two = 2,
Four = 4
}}
{0}
public enum HexFlagsEnumClass
{{
One = 0x1,
Two = 0x2,
Four = 0x4,
All = 0x7
}}";
// Verify CA1027: Mark enums with FlagsAttribute
string codeWithoutFlags = GetCSharpCode_EnumWithFlagsAttributes(code, hasFlags: false);
await VerifyCS.VerifyAnalyzerAsync(codeWithoutFlags,
GetCA1027CSharpResultAt(2, 13, "SimpleFlagsEnumClass"),
GetCA1027CSharpResultAt(11, 13, "HexFlagsEnumClass"));
// Verify no CA2217: Do not mark enums with FlagsAttribute
string codeWithFlags = GetCSharpCode_EnumWithFlagsAttributes(code, hasFlags: true);
await VerifyCS.VerifyAnalyzerAsync(codeWithFlags);
}
[Fact, WorkItem(1432, "https://github.com/dotnet/roslyn-analyzers/issues/1432")]
public async Task CSharp_EnumWithFlagsAttributes_SimpleCase_Internal()
{
var code = @"{0}
internal enum SimpleFlagsEnumClass
{{
Zero = 0,
One = 1,
Two = 2,
Four = 4
}}
internal class OuterClass
{{
{0}
public enum HexFlagsEnumClass
{{
One = 0x1,
Two = 0x2,
Four = 0x4,
All = 0x7
}}
}}";
// Verify no CA1027: Mark enums with FlagsAttribute
string codeWithoutFlags = GetCSharpCode_EnumWithFlagsAttributes(code, hasFlags: false);
await VerifyCS.VerifyAnalyzerAsync(codeWithoutFlags);
// Verify no CA2217: Do not mark enums with FlagsAttribute
string codeWithFlags = GetCSharpCode_EnumWithFlagsAttributes(code, hasFlags: true);
await VerifyCS.VerifyAnalyzerAsync(codeWithFlags);
}
[Fact]
public async Task CSharp_EnumWithFlagsAttributes_SimpleCaseWithScope()
{
var code = @"{0}
public enum {{|CA1027:SimpleFlagsEnumClass|}}
{{
Zero = 0,
One = 1,
Two = 2,
Four = 4
}}
{0}
public enum HexFlagsEnumClass
{{
One = 0x1,
Two = 0x2,
Four = 0x4,
All = 0x7
}}";
// Verify CA1027: Mark enums with FlagsAttribute
string codeWithoutFlags = GetCSharpCode_EnumWithFlagsAttributes(code, hasFlags: false);
await VerifyCS.VerifyAnalyzerAsync(codeWithoutFlags,
GetCA1027CSharpResultAt(11, 13, "HexFlagsEnumClass"));
}
[Fact]
public async Task VisualBasic_EnumWithFlagsAttributes_SimpleCase()
{
var code = @"{0}
Public Enum SimpleFlagsEnumClass
Zero = 0
One = 1
Two = 2
Four = 4
End Enum
{0}
Public Enum HexFlagsEnumClass
One = &H1
Two = &H2
Four = &H4
All = &H7
End Enum";
// Verify CA1027: Mark enums with FlagsAttribute
string codeWithoutFlags = GetBasicCode_EnumWithFlagsAttributes(code, hasFlags: false);
await VerifyVB.VerifyAnalyzerAsync(codeWithoutFlags,
GetCA1027BasicResultAt(2, 13, "SimpleFlagsEnumClass"),
GetCA1027BasicResultAt(10, 13, "HexFlagsEnumClass"));
// Verify no CA2217: Do not mark enums with FlagsAttribute
string codeWithFlags = GetBasicCode_EnumWithFlagsAttributes(code, hasFlags: true);
await VerifyVB.VerifyAnalyzerAsync(codeWithFlags);
}
[Fact, WorkItem(1432, "https://github.com/dotnet/roslyn-analyzers/issues/1432")]
public async Task VisualBasic_EnumWithFlagsAttributes_SimpleCase_Internal()
{
var code = @"{0}
Friend Enum SimpleFlagsEnumClass
Zero = 0
One = 1
Two = 2
Four = 4
End Enum
Friend Class OuterClass
{0}
Public Enum HexFlagsEnumClass
One = &H1
Two = &H2
Four = &H4
All = &H7
End Enum
End Class";
// Verify no CA1027: Mark enums with FlagsAttribute
string codeWithoutFlags = GetBasicCode_EnumWithFlagsAttributes(code, hasFlags: false);
await VerifyVB.VerifyAnalyzerAsync(codeWithoutFlags);
// Verify no CA2217: Do not mark enums with FlagsAttribute
string codeWithFlags = GetBasicCode_EnumWithFlagsAttributes(code, hasFlags: true);
await VerifyVB.VerifyAnalyzerAsync(codeWithFlags);
}
[Fact]
public async Task VisualBasic_EnumWithFlagsAttributes_SimpleCaseWithScope()
{
var code = @"{0}
Public Enum {{|CA1027:SimpleFlagsEnumClass|}}
Zero = 0
One = 1
Two = 2
Four = 4
End Enum
{0}
Public Enum HexFlagsEnumClass
One = &H1
Two = &H2
Four = &H4
All = &H7
End Enum";
// Verify CA1027: Mark enums with FlagsAttribute
string codeWithoutFlags = GetBasicCode_EnumWithFlagsAttributes(code, hasFlags: false);
await VerifyVB.VerifyAnalyzerAsync(codeWithoutFlags,
GetCA1027BasicResultAt(10, 13, "HexFlagsEnumClass"));
}
[Fact]
public async Task CSharp_EnumWithFlagsAttributes_DuplicateValues()
{
string code = @"{0}
public enum DuplicateValuesEnumClass
{{
Zero = 0,
One = 1,
Two = 2,
Four = 4,
AnotherFour = 4,
ThreePlusOne = Two + One + One
}}
";
// Verify CA1027: Mark enums with FlagsAttribute
string codeWithoutFlags = GetCSharpCode_EnumWithFlagsAttributes(code, hasFlags: false);
await VerifyCS.VerifyAnalyzerAsync(codeWithoutFlags,
GetCA1027CSharpResultAt(2, 13, "DuplicateValuesEnumClass"));
// Verify no CA2217: Do not mark enums with FlagsAttribute
string codeWithFlags = GetCSharpCode_EnumWithFlagsAttributes(code, hasFlags: true);
await VerifyCS.VerifyAnalyzerAsync(codeWithFlags);
}
[Fact]
public async Task VisualBasic_EnumWithFlagsAttributes_DuplicateValues()
{
string code = @"{0}
Public Enum DuplicateValuesEnumClass
Zero = 0
One = 1
Two = 2
Four = 4
AnotherFour = 4
ThreePlusOne = Two + One + One
End Enum
";
// Verify CA1027: Mark enums with FlagsAttribute
string codeWithoutFlags = GetBasicCode_EnumWithFlagsAttributes(code, hasFlags: false);
await VerifyVB.VerifyAnalyzerAsync(codeWithoutFlags,
GetCA1027BasicResultAt(2, 13, "DuplicateValuesEnumClass"));
// Verify no CA2217: Do not mark enums with FlagsAttribute
string codeWithFlags = GetBasicCode_EnumWithFlagsAttributes(code, hasFlags: true);
await VerifyVB.VerifyAnalyzerAsync(codeWithFlags);
}
[Fact]
public async Task CSharp_EnumWithFlagsAttributes_MissingPowerOfTwo()
{
string code = @"
{0}
public enum MissingPowerOfTwoEnumClass
{{
Zero = 0,
One = 1,
Two = 2,
Four = 4,
Sixteen = 16
}}
{0}
public enum MultipleMissingPowerOfTwoEnumClass
{{
Zero = 0,
One = 1,
Two = 2,
Four = 4,
ThirtyTwo = 32
}}";
// Verify CA1027: Mark enums with FlagsAttribute
string codeWithoutFlags = GetCSharpCode_EnumWithFlagsAttributes(code, hasFlags: false);
await VerifyCS.VerifyAnalyzerAsync(codeWithoutFlags,
GetCA1027CSharpResultAt(3, 13, "MissingPowerOfTwoEnumClass"),
GetCA1027CSharpResultAt(13, 13, "MultipleMissingPowerOfTwoEnumClass"));
// Verify no CA2217: Do not mark enums with FlagsAttribute
string codeWithFlags = GetCSharpCode_EnumWithFlagsAttributes(code, hasFlags: true);
await VerifyCS.VerifyAnalyzerAsync(codeWithFlags);
}
[Fact]
public async Task CSharp_EnumWithFlagsAttributes_IncorrectNumbers()
{
string code = @"
{0}
public enum AnotherTestValue
{{
Value1 = 0,
Value2 = 1,
Value3 = 1,
Value4 = 3
}}";
// Verify no CA1027: Mark enums with FlagsAttribute
string codeWithoutFlags = GetCSharpCode_EnumWithFlagsAttributes(code, hasFlags: false);
await VerifyCS.VerifyAnalyzerAsync(codeWithoutFlags);
// Verify CA2217: Do not mark enums with FlagsAttribute
string codeWithFlags = GetCSharpCode_EnumWithFlagsAttributes(code, hasFlags: true);
await VerifyCS.VerifyAnalyzerAsync(codeWithFlags,
GetCA2217CSharpResultAt(3, 13, "AnotherTestValue", "2"));
}
[Fact]
public async Task VisualBasic_EnumWithFlagsAttributes_MissingPowerOfTwo()
{
string code = @"
{0}
Public Enum MissingPowerOfTwoEnumClass
Zero = 0
One = 1
Two = 2
Four = 4
Sixteen = 16
End Enum
{0}
Public Enum MultipleMissingPowerOfTwoEnumClass
Zero = 0
One = 1
Two = 2
Four = 4
ThirtyTwo = 32
End Enum
";
// Verify CA1027: Mark enums with FlagsAttribute
string codeWithoutFlags = GetBasicCode_EnumWithFlagsAttributes(code, hasFlags: false);
await VerifyVB.VerifyAnalyzerAsync(codeWithoutFlags,
GetCA1027BasicResultAt(3, 13, "MissingPowerOfTwoEnumClass"),
GetCA1027BasicResultAt(12, 13, "MultipleMissingPowerOfTwoEnumClass"));
// Verify no CA2217: Do not mark enums with FlagsAttribute
string codeWithFlags = GetBasicCode_EnumWithFlagsAttributes(code, hasFlags: true);
await VerifyVB.VerifyAnalyzerAsync(codeWithFlags);
}
[Fact]
public async Task VisualBasic_EnumWithFlagsAttributes_IncorrectNumbers()
{
string code = @"
{0}
Public Enum AnotherTestValue
Value1 = 0
Value2 = 1
Value3 = 1
Value4 = 3
End Enum
";
// Verify no CA1027: Mark enums with FlagsAttribute
string codeWithoutFlags = GetBasicCode_EnumWithFlagsAttributes(code, hasFlags: false);
await VerifyVB.VerifyAnalyzerAsync(codeWithoutFlags);
// Verify CA2217: Do not mark enums with FlagsAttribute
string codeWithFlags = GetBasicCode_EnumWithFlagsAttributes(code, hasFlags: true);
await VerifyVB.VerifyAnalyzerAsync(codeWithFlags,
GetCA2217BasicResultAt(3, 13, "AnotherTestValue", "2"));
}
[Fact]
public async Task CSharp_EnumWithFlagsAttributes_ContiguousValues()
{
var code = @"
{0}
public enum ContiguousEnumClass
{{
Zero = 0,
One = 1,
Two = 2
}}
{0}
public enum ContiguousEnumClass2
{{
Zero = 0,
One = 1,
Two = 2,
Three = 3,
Four = 4,
Five = 5
}}
{0}
public enum ValuesNotDeclaredEnumClass
{{
Zero,
One,
Two,
Three,
Four,
Five
}}
{0}
public enum ShortUnderlyingType: short
{{
Zero = 0,
One,
Two,
Three,
Four,
Five
}}";
// Verify no CA1027: Mark enums with FlagsAttribute
string codeWithoutFlags = GetCSharpCode_EnumWithFlagsAttributes(code, hasFlags: false);
await VerifyCS.VerifyAnalyzerAsync(codeWithoutFlags);
// Verify no CA2217: Do not mark enums with FlagsAttribute
string codeWithFlags = GetCSharpCode_EnumWithFlagsAttributes(code, hasFlags: true);
await VerifyCS.VerifyAnalyzerAsync(codeWithFlags);
}
[Fact]
public async Task VisualBasic_EnumWithFlagsAttributes_ContiguousValues()
{
var code = @"
{0}
Public Enum ContiguousEnumClass
Zero = 0
One = 1
Two = 2
End Enum
{0}
Public Enum ContiguousEnumClass2
Zero = 0
One = 1
Two = 2
Three = 3
Four = 4
Five = 5
End Enum
{0}
Public Enum ValuesNotDeclaredEnumClass
Zero
One
Two
Three
Four
Five
End Enum
{0}
Public Enum ShortUnderlyingType As Short
Zero = 0
One
Two
Three
Four
Five
End Enum
";
// Verify no CA1027: Mark enums with FlagsAttribute
string codeWithoutFlags = GetBasicCode_EnumWithFlagsAttributes(code, hasFlags: false);
await VerifyVB.VerifyAnalyzerAsync(codeWithoutFlags);
// Verify no CA2217: Do not mark enums with FlagsAttribute
string codeWithFlags = GetBasicCode_EnumWithFlagsAttributes(code, hasFlags: true);
await VerifyVB.VerifyAnalyzerAsync(codeWithFlags);
}
[Fact]
public async Task CSharp_EnumWithFlagsAttributes_NonSimpleFlags()
{
var code = @"
{0}
public enum NonSimpleFlagEnumClass
{{
Zero = 0x0, // 0000
One = 0x1, // 0001
Two = 0x2, // 0010
Eight = 0x8, // 1000
Twelve = 0xC, // 1100
HighValue = -1 // will be cast to UInt32.MaxValue, then zero-extended to UInt64
}}
{0}
public enum BitValuesClass
{{
None = 0x0,
One = 0x1, // 0001
Two = 0x2, // 0010
Eight = 0x8, // 1000
Twelve = 0xC, // 1100
}}
{0}
public enum LabelsClass
{{
None = 0,
One = 1,
Four = 4,
Six = 6,
Seven = 7
}}";
// Verify no CA1027: Mark enums with FlagsAttribute
string codeWithoutFlags = GetCSharpCode_EnumWithFlagsAttributes(code, hasFlags: false);
await VerifyCS.VerifyAnalyzerAsync(codeWithoutFlags);
// Verify CA2217: Do not mark enums with FlagsAttribute
string codeWithFlags = GetCSharpCode_EnumWithFlagsAttributes(code, hasFlags: true);
await VerifyCS.VerifyAnalyzerAsync(codeWithFlags,
GetCA2217CSharpResultAt(3, 13, "NonSimpleFlagEnumClass", "4, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944, 549755813888, 1099511627776, 2199023255552, 4398046511104, 8796093022208, 17592186044416, 35184372088832, 70368744177664, 140737488355328, 281474976710656, 562949953421312, 1125899906842624, 2251799813685248, 4503599627370496, 9007199254740992, 18014398509481984, 36028797018963968, 72057594037927936, 144115188075855872, 288230376151711744, 576460752303423488, 1152921504606846976, 2305843009213693952, 4611686018427387904, 9223372036854775808"),
GetCA2217CSharpResultAt(14, 13, "BitValuesClass", "4"),
GetCA2217CSharpResultAt(24, 13, "LabelsClass", "2"));
}
[Fact]
public async Task VisualBasic_EnumWithFlagsAttributes_NonSimpleFlags()
{
var code = @"
{0}
Public Enum NonSimpleFlagEnumClass
Zero = &H0 ' 0000
One = &H1 ' 0001
Two = &H2 ' 0010
Eight = &H8 ' 1000
Twelve = &Hc ' 1100
HighValue = -1 ' will be cast to UInt32.MaxValue, then zero-extended to UInt64
End Enum
{0}
Public Enum BitValuesClass
None = &H0
One = &H1 ' 0001
Two = &H2 ' 0010
Eight = &H8 ' 1000
Twelve = &Hc ' 1100
End Enum
{0}
Public Enum LabelsClass
None = 0
One = 1
Four = 4
Six = 6
Seven = 7
End Enum
";
// Verify no CA1027: Mark enums with FlagsAttribute
string codeWithoutFlags = GetBasicCode_EnumWithFlagsAttributes(code, hasFlags: false);
await VerifyVB.VerifyAnalyzerAsync(codeWithoutFlags);
// Verify CA2217: Do not mark enums with FlagsAttribute
string codeWithFlags = GetBasicCode_EnumWithFlagsAttributes(code, hasFlags: true);
await VerifyVB.VerifyAnalyzerAsync(codeWithFlags,
GetCA2217BasicResultAt(3, 13, "NonSimpleFlagEnumClass", "4, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944, 549755813888, 1099511627776, 2199023255552, 4398046511104, 8796093022208, 17592186044416, 35184372088832, 70368744177664, 140737488355328, 281474976710656, 562949953421312, 1125899906842624, 2251799813685248, 4503599627370496, 9007199254740992, 18014398509481984, 36028797018963968, 72057594037927936, 144115188075855872, 288230376151711744, 576460752303423488, 1152921504606846976, 2305843009213693952, 4611686018427387904, 9223372036854775808"),
GetCA2217BasicResultAt(13, 13, "BitValuesClass", "4"),
GetCA2217BasicResultAt(22, 13, "LabelsClass", "2"));
}
private static DiagnosticResult GetCA1027CSharpResultAt(int line, int column, string enumTypeName)
#pragma warning disable RS0030 // Do not used banned APIs
=> VerifyCS.Diagnostic(EnumWithFlagsAttributeAnalyzer.Rule1027)
.WithLocation(line, column)
#pragma warning restore RS0030 // Do not used banned APIs
.WithArguments(enumTypeName);
private static DiagnosticResult GetCA1027BasicResultAt(int line, int column, string enumTypeName)
#pragma warning disable RS0030 // Do not used banned APIs
=> VerifyVB.Diagnostic(EnumWithFlagsAttributeAnalyzer.Rule1027)
.WithLocation(line, column)
#pragma warning restore RS0030 // Do not used banned APIs
.WithArguments(enumTypeName);
private static DiagnosticResult GetCA2217CSharpResultAt(int line, int column, string enumTypeName, string missingValuesString)
#pragma warning disable RS0030 // Do not used banned APIs
=> VerifyCS.Diagnostic(EnumWithFlagsAttributeAnalyzer.Rule2217)
.WithLocation(line, column)
#pragma warning restore RS0030 // Do not used banned APIs
.WithArguments(enumTypeName, missingValuesString);
private static DiagnosticResult GetCA2217BasicResultAt(int line, int column, string enumTypeName, string missingValuesString)
#pragma warning disable RS0030 // Do not used banned APIs
=> VerifyVB.Diagnostic(EnumWithFlagsAttributeAnalyzer.Rule2217)
.WithLocation(line, column)
#pragma warning restore RS0030 // Do not used banned APIs
.WithArguments(enumTypeName, missingValuesString);
}
}