-
Notifications
You must be signed in to change notification settings - Fork 509
Expand file tree
/
Copy pathSA1313UnitTests.cs
More file actions
769 lines (669 loc) · 24.6 KB
/
SA1313UnitTests.cs
File metadata and controls
769 lines (669 loc) · 24.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
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
// 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.NamingRules
{
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.NamingRules.SA1313ParameterNamesMustBeginWithLowerCaseLetter,
StyleCop.Analyzers.NamingRules.RenameToLowerCaseCodeFixProvider>;
[UseCulture("en-US")]
public class SA1313UnitTests
{
[Fact]
public async Task TestThatDiagnosticIsNotReportedForFieldsAsync()
{
var testCode = @"public class TypeName
{
const string bar = nameof(bar);
const string Bar = nameof(Bar);
string car = nameof(car);
string Car = nameof(Car);
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestThatDiagnosticIsNotReportedForEventFieldsAsync()
{
var testCode = @"using System;
public class TypeName
{
static event EventHandler bar;
static event EventHandler Bar;
event EventHandler car;
event EventHandler Car;
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestThatDiagnosticIsNotReportedForVariablesAsync()
{
var testCode = @"public class TypeName
{
public void MethodName()
{
const string bar = nameof(bar);
const string Bar = nameof(Bar);
string car = nameof(car);
string Car = nameof(Car);
}
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestThatDiagnosticIsReported_SingleParameterAsync()
{
var testCode = @"public class TypeName
{
public void MethodName(string Bar)
{
}
}";
DiagnosticResult expected = Diagnostic().WithArguments("Bar").WithLocation(3, 35);
var fixedCode = @"public class TypeName
{
public void MethodName(string bar)
{
}
}";
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestThatDiagnosticIsReported_MultipleParametersAsync()
{
var testCode = @"public class TypeName
{
public void MethodName(string Bar, string car, string Par)
{
}
}";
DiagnosticResult[] expected =
{
Diagnostic().WithArguments("Bar").WithLocation(3, 35),
Diagnostic().WithArguments("Par").WithLocation(3, 59),
};
var fixedCode = @"public class TypeName
{
public void MethodName(string bar, string car, string par)
{
}
}";
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestParameterStartingWithLetterAsync()
{
var testCode = @"public class TypeName
{
public void MethodName(string bar)
{
}
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestParameterPlacedInsideNativeMethodsClassAsync()
{
var testCode = @"public class FooNativeMethods
{
public void MethodName(string Bar)
{
}
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestRenameConflictsWithVariableAsync()
{
var testCode = @"public class TypeName
{
public void MethodName(string Parameter)
{
string parameter = ""Text"";
}
}";
DiagnosticResult expected = Diagnostic().WithArguments("Parameter").WithLocation(3, 35);
var fixedCode = @"public class TypeName
{
public void MethodName(string parameter1)
{
string parameter = ""Text"";
}
}";
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestRenameConflictsWithKeywordAsync()
{
var testCode = @"public class TypeName
{
public void MethodName(string Int)
{
}
}";
DiagnosticResult expected = Diagnostic().WithArguments("Int").WithLocation(3, 35);
var fixedCode = @"public class TypeName
{
public void MethodName(string @int)
{
}
}";
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestRenameConflictsWithLaterParameterAsync()
{
var testCode = @"public class TypeName
{
public void MethodName(string Parameter, int parameter)
{
}
}";
DiagnosticResult expected = Diagnostic().WithArguments("Parameter").WithLocation(3, 35);
var fixedCode = @"public class TypeName
{
public void MethodName(string parameter1, int parameter)
{
}
}";
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestRenameConflictsWithEarlierParameterAsync()
{
var testCode = @"public class TypeName
{
public void MethodName(string parameter, int Parameter)
{
}
}";
DiagnosticResult expected = Diagnostic().WithArguments("Parameter").WithLocation(3, 50);
var fixedCode = @"public class TypeName
{
public void MethodName(string parameter, int parameter1)
{
}
}";
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestNoViolationOnInterfaceParameterNameAsync()
{
var testCode = @"
public interface ITest
{
void Method(int Param1, int param2, int Param3);
}
public class Test : ITest
{
public void Method(int Param1, int param2, int param3)
{
}
}";
var expected = new[]
{
Diagnostic().WithLocation(4, 21).WithArguments("Param1"),
Diagnostic().WithLocation(4, 45).WithArguments("Param3"),
};
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestViolationOnRenamedInterfaceParameterNameAsync()
{
var testCode = @"
public interface ITest
{
void Method(int Param1, int param2, int Param3);
}
public class Test : ITest
{
public void Method(int Param1, int param2, int Other)
{
}
}";
var expected = new[]
{
Diagnostic().WithLocation(4, 21).WithArguments("Param1"),
Diagnostic().WithLocation(4, 45).WithArguments("Param3"),
Diagnostic().WithLocation(9, 52).WithArguments("Other"),
};
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
[WorkItem(3555, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3555")]
public async Task TestNoViolationOnExplicitlyImplementedInterfaceParameterNameAsync()
{
var testCode = @"
public interface ITest
{
void Method(int param1, int {|#0:Param2|});
}
public class Test : ITest
{
void ITest.Method(int param1, int Param2)
{
}
}";
var expected = new[]
{
Diagnostic().WithLocation(0).WithArguments("Param2"),
};
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
[WorkItem(3555, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3555")]
public async Task TestViolationOnRenamedExplicitlyImplementedInterfaceParameterNameAsync()
{
var testCode = @"
public interface ITest
{
void Method(int param1, int {|#0:Param2|});
}
public class Test : ITest
{
public void Method(int param1, int {|#1:Other|})
{
}
}";
var expected = new[]
{
Diagnostic().WithLocation(0).WithArguments("Param2"),
Diagnostic().WithLocation(1).WithArguments("Other"),
};
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestNoViolationOnAbstractParameterNameAsync()
{
var testCode = @"
public abstract class TestBase
{
public abstract void Method(int Param1, int param2, int Param3);
}
public class Test : TestBase
{
public override void Method(int Param1, int param2, int param3)
{
}
}";
var expected = new[]
{
Diagnostic().WithLocation(4, 37).WithArguments("Param1"),
Diagnostic().WithLocation(4, 61).WithArguments("Param3"),
};
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestViolationOnRenamedAbstractParameterNameAsync()
{
var testCode = @"
public abstract class Testbase
{
public abstract void Method(int Param1, int param2, int Param3);
}
public class Test : Testbase
{
public override void Method(int Param1, int param2, int Other)
{
}
}";
var expected = new[]
{
Diagnostic().WithLocation(4, 37).WithArguments("Param1"),
Diagnostic().WithLocation(4, 61).WithArguments("Param3"),
Diagnostic().WithLocation(9, 61).WithArguments("Other"),
};
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
[WorkItem(1442, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1442")]
public async Task TestSimpleLambaExpressionAsync()
{
var testCode = @"public class TypeName
{
public void MethodName()
{
System.Action<int> action = Ignored => { };
}
}";
DiagnosticResult expected = Diagnostic().WithArguments("Ignored").WithLocation(5, 37);
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
[WorkItem(1343, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1343")]
public async Task TestLambdaParameterNamedUnderscoreAsync()
{
var testCode = @"public class TypeName
{
public void MethodName()
{
System.Action<int> action1 = _ => { };
System.Action<int> action2 = (_) => { };
System.Action<int> action3 = delegate(int _) { };
}
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies this diagnostic does not check whether or not a parameter named <c>_</c> is being used.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
[Fact]
[WorkItem(1343, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1343")]
public async Task TestLambdaParameterNamedUnderscoreUsageAsync()
{
var testCode = @"public class TypeName
{
public void MethodName()
{
System.Func<int, int> function1 = _ => _;
System.Func<int, int> function2 = (_) => _;
System.Func<int, int> function3 = delegate(int _) { return _; };
}
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
[WorkItem(1606, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1606")]
[WorkItem(2974, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2974")]
public async Task TestLambdaParameterNamedDoubleUnderscoreAsync()
{
var testCode = @"public class TypeName
{
public void MethodName()
{
System.Action<int> action1 = __ => { };
System.Action<int> action2 = (__) => { };
System.Action<int> action3 = delegate(int __) { };
}
}";
DiagnosticResult[] expected =
{
Diagnostic().WithArguments("__").WithLocation(5, 38),
Diagnostic().WithArguments("__").WithLocation(6, 39),
Diagnostic().WithArguments("__").WithLocation(7, 51),
};
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verifies this diagnostic does not check whether or not a parameter named <c>__</c> is being used.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
[Fact]
[WorkItem(1606, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1606")]
[WorkItem(2974, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2974")]
public async Task TestLambdaParameterNamedDoubleUnderscoreUsageAsync()
{
var testCode = @"public class TypeName
{
public void MethodName()
{
System.Func<int, int> function1 = __ => __;
System.Func<int, int> function2 = (__) => __;
System.Func<int, int> function3 = delegate(int __) { return __; };
}
}";
DiagnosticResult[] expected =
{
Diagnostic().WithArguments("__").WithLocation(5, 43),
Diagnostic().WithArguments("__").WithLocation(6, 44),
Diagnostic().WithArguments("__").WithLocation(7, 56),
};
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
[WorkItem(1343, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1343")]
public async Task TestLambdaParameterWithThreeUnderscoresAsync()
{
var testCode = @"public class TypeName
{
public void MethodName()
{
System.Action<int> action1 = ___ => { };
System.Action<int> action2 = (___) => { };
System.Action<int> action3 = delegate(int ___) { };
}
}";
DiagnosticResult[] expected =
{
Diagnostic().WithArguments("___").WithLocation(5, 38),
Diagnostic().WithArguments("___").WithLocation(6, 39),
Diagnostic().WithArguments("___").WithLocation(7, 51),
};
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
[WorkItem(2974, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2974")]
public async Task TestLambdaParameterWithIncreasingNumberOfUnderscoresAsync()
{
var testCode = @"public class TypeName
{
public void MethodName()
{
System.Action<int> action1 = _ => { };
System.Action<int, int> action2 = (_, __) => { };
System.Action<int, int, int> action3 = (_, __, ___) => { };
System.Action<int, int, int, int> action4 = (_, __, ___, ____) => { };
System.Action<int> action5 = delegate(int _) { };
System.Action<int, int> action6 = delegate(int _, int __) { };
System.Action<int, int, int> action7 = delegate(int _, int __, int ___) { };
System.Action<int, int, int, int> action8 = delegate(int _, int __, int ___, int ____) { };
System.Action<int, int> action9 = (a, _) => { };
System.Action<int, int> action10 = (a, __) => { };
System.Action<int, int, int> action11 = (_, a, __) => { };
System.Action<int, int, int> action12 = (_, __, a) => { };
System.Action<int, int, int> action13 = (_, a, ___) => { };
}
}";
DiagnosticResult[] expected =
{
Diagnostic().WithArguments("__").WithLocation(15, 48),
Diagnostic().WithArguments("___").WithLocation(18, 56),
};
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
[WorkItem(1343, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1343")]
[WorkItem(2974, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2974")]
public async Task TestMethodParameterNamedUnderscoreAsync()
{
var testCode = @"public class TypeName
{
public void MethodName(int _)
{
++_;
}
}";
DiagnosticResult expected = Diagnostic().WithArguments("_").WithLocation(3, 32);
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
[WorkItem(2974, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2974")]
public async Task TestMethodParameterNamedUnderscoresUsedAsync()
{
var testCode = @"public class TypeName
{
public void MethodName1(int _1, short _2)
{
++_1;
++_2;
}
public void MethodName2(int _, short __)
{
++_;
++__;
}
}";
DiagnosticResult[] expected =
{
Diagnostic().WithArguments("_1").WithLocation(3, 33),
Diagnostic().WithArguments("_2").WithLocation(3, 43),
Diagnostic().WithArguments("_").WithLocation(9, 33),
Diagnostic().WithArguments("__").WithLocation(9, 42),
};
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
[WorkItem(1606, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1606")]
public async Task TestMethodParameterNamedDoubleUnderscoreAsync()
{
var testCode = @"public class TypeName
{
public void MethodName(int __, int _)
{
}
}";
DiagnosticResult[] expected =
{
Diagnostic().WithArguments("__").WithLocation(3, 32),
Diagnostic().WithArguments("_").WithLocation(3, 40),
};
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
[WorkItem(2974, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2974")]
public async Task TestMethodParameterNamedIncrementUnderscoreAsync()
{
var testCode = @"public class TypeName
{
public void MethodName(int _, int __, int ___)
{
}
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
[WorkItem(2974, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2974")]
public async Task TestMethodParameterNamedIrregularUnderscoreAsync()
{
var testCode = @"public class TypeName
{
public void MethodName1(int __, int _) { }
public void MethodName2(int _, int ___, int __) { }
}";
DiagnosticResult[] expected =
{
Diagnostic().WithArguments("__").WithLocation(3, 33),
Diagnostic().WithArguments("_").WithLocation(3, 41),
Diagnostic().WithArguments("___").WithLocation(4, 40),
Diagnostic().WithArguments("__").WithLocation(4, 49),
};
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
[WorkItem(1529, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1529")]
public async Task TestInheritedInterfacesWithOverloadedMembersAsync()
{
var testCode = @"
public interface ITest
{
void Method(int Param1, int param2, int Param3);
void Method();
}
public interface IEmptyInterface { }
public interface IDerivedTest : ITest, IEmptyInterface
{
void Method(int Param1, int param2, int param3);
}";
var expected = new[]
{
Diagnostic().WithLocation(4, 21).WithArguments("Param1"),
Diagnostic().WithLocation(4, 45).WithArguments("Param3"),
Diagnostic().WithLocation(12, 21).WithArguments("Param1"),
};
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
[WorkItem(1604, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1604")]
public async Task TestCodeFixProperlyRemovesUnderscoreAsync()
{
var testCode = @"
public class TestClass
{
public TestClass(string _text)
: this(_text, false)
{
}
public TestClass(string text, bool flag)
{
}
}
";
var fixedCode = @"
public class TestClass
{
public TestClass(string text)
: this(text, false)
{
}
public TestClass(string text, bool flag)
{
}
}
";
var expected = Diagnostic().WithLocation(4, 29).WithArguments("_text");
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
[WorkItem(2974, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2974")]
public async Task TestMethodParameterNamedUnusedAsync()
{
var testCode = @"public class TypeName
{
public int MethodName(int _used, int _unused, int _, int _1, int _2, int ___, int __)
{
return _used + _2;
}
}";
DiagnosticResult[] expected =
{
Diagnostic().WithArguments("_used").WithLocation(3, 31),
Diagnostic().WithArguments("_2").WithLocation(3, 70),
Diagnostic().WithArguments("___").WithLocation(3, 78),
Diagnostic().WithArguments("__").WithLocation(3, 87),
};
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Verify that an invalid method override will not produce a diagnostic nor crash the analyzer.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
[WorkItem(2189, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2189")]
public async Task InvalidMethodOverrideShouldNotProduceDiagnosticAsync()
{
var testCode = @"
namespace TestNamespace
{
public abstract class BaseClass
{
public abstract void TestMethod(int p1, int p2);
}
public class TestClass : BaseClass
{
public override void TestMethod(int p1, X int P2)
{
}
}
}
";
DiagnosticResult[] expected = this.GetInvalidMethodOverrideShouldNotProduceDiagnosticAsyncDiagnostics();
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}
protected virtual DiagnosticResult[] GetInvalidMethodOverrideShouldNotProduceDiagnosticAsyncDiagnostics()
{
return new DiagnosticResult[]
{
DiagnosticResult.CompilerError("CS0534").WithLocation(9, 18).WithMessage("'TestClass' does not implement inherited abstract member 'BaseClass.TestMethod(int, int)'"),
DiagnosticResult.CompilerError("CS0115").WithLocation(11, 30).WithMessage("'TestClass.TestMethod(int, X, int)': no suitable method found to override"),
DiagnosticResult.CompilerError("CS0246").WithLocation(11, 49).WithMessage("The type or namespace name 'X' could not be found (are you missing a using directive or an assembly reference?)"),
DiagnosticResult.CompilerError("CS1001").WithLocation(11, 51).WithMessage("Identifier expected"),
DiagnosticResult.CompilerError("CS1003").WithLocation(11, 51).WithMessage("Syntax error, ',' expected"),
};
}
}
}