-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathNamingMethodPascalTests.cs
More file actions
552 lines (485 loc) · 14.7 KB
/
NamingMethodPascalTests.cs
File metadata and controls
552 lines (485 loc) · 14.7 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
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestHelper;
namespace IntelliTect.Analyzer.Tests
{
[TestClass]
public class NamingMethodPascalTests : CodeFixVerifier
{
[TestMethod]
public void LocalMethodWithLowerCaseFirstChar_MethodNotPascalCase_Warning()
{
string test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TypeName
{
public string MyMethod()
{
var output = localMethod();
string localMethod() {
return string.Empty;
}
return output;
}
}
}";
var expected = new DiagnosticResult
{
Id = "INTL0003",
Message = "Method 'localMethod' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
[
new DiagnosticResultLocation("Test0.cs", 17, 24)
]
};
VerifyCSharpDiagnostic(test, expected);
}
[TestMethod]
public void ProperlyNamedMethod_PascalCasedMethod_NoDiagnosticInformationReturned()
{
string test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TypeName
{
public string MyMethod()
{
return string.Empty;
}
}
}";
VerifyCSharpDiagnostic(test);
}
[TestMethod]
public void ProperlyNamedMethod_TopLevelStatements_NoDiagnosticInformationReturned()
{
string test = @"
Console.WriteLine(""Hello World!"");";
VerifyCSharpDiagnostic(test);
}
[TestMethod]
public void AutoProperty_PascalCasedMethod_NoDiagnosticInformationReturned()
{
string test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TypeName
{
public int Number { get; set; }
}
}";
VerifyCSharpDiagnostic(test);
}
[TestMethod]
[Description("Issue 70")]
public void MethodNotPascalCase_InNativeMethodsClass_NoDiagnosticInformationReturned()
{
string test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class NativeMethods
{
public string mymethod()
{
return string.Empty;
}
}
}";
VerifyCSharpDiagnostic(test);
}
[TestMethod]
public void MethodWithNamingViolation_MethodNotPascalCase_Warning()
{
string test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TypeName
{
public string myMethod()
{
return string.Empty;
}
}
}";
var expected = new DiagnosticResult
{
Id = "INTL0003",
Message = "Method 'myMethod' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
[
new DiagnosticResultLocation("Test0.cs", 13, 27)
]
};
VerifyCSharpDiagnostic(test, expected);
}
[TestMethod]
public void MethodWithUnderScoreAsFirstChar_MethodNotPascalCase_Warning()
{
string test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TypeName
{
public string _MyMethod()
{
return string.Empty;
}
}
}";
var expected = new DiagnosticResult
{
Id = "INTL0003",
Message = "Method '_MyMethod' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
[
new DiagnosticResultLocation("Test0.cs", 13, 27)
]
};
VerifyCSharpDiagnostic(test, expected);
}
[TestMethod]
public async Task MethodNotPascalCase_CodeFix_FixNamingViolation_MethodIsNamedCorrectly()
{
string test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TypeName
{
public string myMethod()
{
return string.Empty;
}
}
}";
string fixTest = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TypeName
{
public string MyMethod()
{
return string.Empty;
}
}
}";
await VerifyCSharpFix(test, fixTest);
}
[TestMethod]
public async Task MethodNotPascalCase_CodeFix_FixNamingViolation_LocalFunctionIsNamedCorrectly()
{
string test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TypeName
{
public string getEmpty() {
var output = localFunction();
string localFunction() {
return string.Empty();
}
return output;
}
}
}";
string fixTest = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TypeName
{
public string GetEmpty() {
var output = LocalFunction();
string LocalFunction() {
return string.Empty();
}
return output;
}
}
}";
await VerifyCSharpFix(test, fixTest);
}
[TestMethod]
[Description("Issue 93")]
public void MethodWithNamingViolation_InGeneratedCode_Ignored()
{
string test = @"
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore.Views_Shared__ValidationScriptsPartial), @""mvc.1.0.view"", @""/Views/Shared/_ValidationScriptsPartial.cshtml"")]
namespace AspNetCore
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
[global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@""SHA1"", @""9eb468fb4b25bc6ad91d6b1bb06c95bd39c3b25c"", @""/Views/Shared/_ValidationScriptsPartial.cshtml"")]
public class Views_Shared__ValidationScriptsPartial : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<dynamic>
{
public void foo() { }
#pragma warning restore 1591
";
VerifyCSharpDiagnostic(test);
}
[TestMethod]
[Description("Issue 80")]
public void MethodWithNamingViolation_MethodWithUnderscore_Warning()
{
string test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TypeName
{
public string My_Method()
{
return string.Empty;
}
}
}";
var expected = new DiagnosticResult
{
Id = "INTL0003",
Message = "Method 'My_Method' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
[
new DiagnosticResultLocation("Test0.cs", 13, 27)
]
};
VerifyCSharpDiagnostic(test, expected);
}
[TestMethod]
[Description("Issue 100")]
public void ExplicitlyImplementedMethod_PascalCasedMethod_NoDiagnosticInformationReturned()
{
string test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
public interface IInterface
{
public void Foo();
}
public class TypeName : IInterface
{
void IInterface.Foo() { }
}
}";
VerifyCSharpDiagnostic(test);
}
[TestMethod]
[Description("Issue 100")]
public void ExplicitlyImplementedMethod_MethodNotPascalCase_Warnings()
{
string test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
public interface IInterface
{
public void foo();
}
public class TypeName : IInterface
{
void IInterface.foo() { }
}
}";
var expected1 = new DiagnosticResult
{
Id = "INTL0003",
Message = "Method 'foo' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
[
new DiagnosticResultLocation("Test0.cs", 13, 25)
]
};
var expected2 = new DiagnosticResult
{
Id = "INTL0003",
Message = "Method 'foo' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
[
new DiagnosticResultLocation("Test0.cs", 18, 29)
]
};
VerifyCSharpDiagnostic(test, expected1, expected2);
}
public static IEnumerable<object[]> TestFrameworks =>
TestFrameworkReferences.All.Select(f => new object[] { f.Name, f.TestAttribute, f.UsingDirective });
[TestMethod]
[DynamicData(nameof(TestFrameworks))]
[Description("Issue 371 - Test method with underscores should not trigger INTL0003 for any supported framework")]
public void TestMethodWithUnderscores_TestFrameworkAttribute_NoDiagnosticInformationReturned(
string frameworkName, string testAttribute, string usingDirective)
{
_ = frameworkName; // parameter used for test identification in output
string test = $@"
using System;
{usingDirective}
namespace ConsoleApplication1
{{
public class TypeName
{{
{testAttribute}
public void FooThing_IsFooThing_HasFooThing() {{ }}
}}
}}";
VerifyCSharpDiagnostic(test);
}
[TestMethod]
[Description("Issue 371 - Non-test method with underscores in a test class should still trigger INTL0003")]
public void NonTestMethodWithUnderscores_InTestClass_DiagnosticReturned()
{
string test = @"
using System;
using Xunit;
namespace ConsoleApplication1
{
public class TypeName
{
[Fact]
public void FooThing_IsFooThing_HasFooThing() { }
public void helper_setup() { }
}
}";
var expected = new DiagnosticResult
{
Id = "INTL0003",
Message = "Method 'helper_setup' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
[
new DiagnosticResultLocation("Test0.cs", 12, 25)
]
};
VerifyCSharpDiagnostic(test, expected);
}
[TestMethod]
[Description("Issue 371 - User-defined attribute named 'Test' in a non-framework namespace must not suppress INTL0003")]
public void MethodWithUnderscores_UserDefinedTestAttribute_DiagnosticReturned()
{
string test = @"
using System;
namespace MyApp
{
public class TestAttribute : System.Attribute { }
}
namespace ConsoleApplication1
{
public class TypeName
{
[MyApp.Test]
public void FooThing_IsFooThing_HasFooThing() { }
}
}";
var expected = new DiagnosticResult
{
Id = "INTL0003",
Message = "Method 'FooThing_IsFooThing_HasFooThing' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
[
new DiagnosticResultLocation("Test0.cs", 14, 25)
]
};
VerifyCSharpDiagnostic(test, expected);
}
protected override CodeFixProvider GetCSharpCodeFixProvider()
{
return new CodeFixes.NamingIdentifierPascal();
}
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
{
return new Analyzers.NamingMethodPascal();
}
}
}