Skip to content

Commit f66d17d

Browse files
authored
Merge pull request #3801 from bjornhellander/feature/sa1612-test-improvements
Improve tests for SA1612
2 parents 834d939 + ed0ef91 commit f66d17d

File tree

1 file changed

+157
-40
lines changed

1 file changed

+157
-40
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1612UnitTests.cs

Lines changed: 157 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace StyleCop.Analyzers.Test.DocumentationRules
77
{
88
using System.Collections.Generic;
9+
using System.Linq;
910
using System.Threading;
1011
using System.Threading.Tasks;
1112
using Microsoft.CodeAnalysis.Testing;
@@ -23,12 +24,41 @@ public static IEnumerable<object[]> Declarations
2324
{
2425
get
2526
{
26-
yield return new[] { " public ClassName Method(string foo, string bar, string @new) { return null; }" };
27-
yield return new[] { " public delegate ClassName Method(string foo, string bar, string @new);" };
28-
yield return new[] { " public ClassName this[string foo, string bar, string @new] { get { return null; } set { } }" };
27+
yield return new object[] { " public ClassName {|#0:Method|}(string foo, string bar, string @new) { return null; }" };
28+
yield return new object[] { " public delegate ClassName {|#0:Method|}(string foo, string bar, string @new);" };
29+
yield return new object[] { " public ClassName {|#0:this|}[string foo, string bar, string @new] { get { return null; } set { } }" };
2930
}
3031
}
3132

33+
[Fact]
34+
public async Task VerifyClassIsNotReportedAsync()
35+
{
36+
var testCode = @"
37+
/// <summary>
38+
/// Foo
39+
/// </summary>
40+
public class ClassName
41+
{
42+
}";
43+
44+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
45+
}
46+
47+
[Fact]
48+
public async Task VerifyMethodWithNoParametersIsNotReportedAsync()
49+
{
50+
var testCode = @"
51+
public class ClassName
52+
{
53+
/// <summary>
54+
/// Foo
55+
/// </summary>
56+
public void Method() { }
57+
}";
58+
59+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
60+
}
61+
3262
[Fact]
3363
public async Task TestMemberWithoutDocumentationAsync()
3464
{
@@ -198,7 +228,7 @@ public class ClassName
198228

199229
[Theory]
200230
[MemberData(nameof(Declarations))]
201-
public async Task TestMembersWithAllDocumentationWrongOrderAsync(string p)
231+
public async Task TestMembersWithAllDocumentationWrongOrderAsync(string declaration)
202232
{
203233
var testCode = @"
204234
/// <summary>
@@ -212,7 +242,7 @@ public class ClassName
212242
/// <param name=""bar"">Param 2</param>
213243
/// <param name=""new"">Param 3</param>
214244
/// <param name=""foo"">Param 1</param>
215-
$$
245+
$$
216246
}";
217247

218248
var diagnostic = Diagnostic()
@@ -225,12 +255,29 @@ public class ClassName
225255
diagnostic.WithLocation(12, 22).WithArguments("foo", 1),
226256
};
227257

228-
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", p), expected, CancellationToken.None).ConfigureAwait(false);
258+
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), expected, CancellationToken.None).ConfigureAwait(false);
259+
260+
var testSettings = @"
261+
{
262+
""settings"": {
263+
""documentationRules"": {
264+
""documentExposedElements"": false
265+
}
266+
}
267+
}
268+
";
269+
270+
expected = new[]
271+
{
272+
diagnostic.WithLocation(12, 22).WithArguments("foo", 1),
273+
};
274+
275+
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), testSettings, expected, CancellationToken.None).ConfigureAwait(false);
229276
}
230277

231278
[Theory]
232279
[MemberData(nameof(Declarations))]
233-
public async Task TestMembersWithTooManyDocumentationAsync(string p)
280+
public async Task TestMembersWithTooManyDocumentationAsync(string declaration)
234281
{
235282
var testCode = @"
236283
/// <summary>
@@ -245,15 +292,32 @@ public class ClassName
245292
/// <param name=""bar"">Param 2</param>
246293
/// <param name=""new"">Param 3</param>
247294
/// <param name=""bar"">Param 4</param>
248-
$$
295+
$$
249296
}";
250297

251298
var diagnostic = Diagnostic()
252299
.WithMessageFormat("The parameter documentation for '{0}' should be at position {1}");
253300

254301
var expected = diagnostic.WithLocation(13, 22).WithArguments("bar", 2);
255302

256-
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", p), expected, CancellationToken.None).ConfigureAwait(false);
303+
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), expected, CancellationToken.None).ConfigureAwait(false);
304+
}
305+
306+
[Fact]
307+
public async Task TestDelegateWithoutIdentifierWithTooManyDocumentationIsNotReportedAsync()
308+
{
309+
var testCode = @"
310+
public class ClassName
311+
{
312+
/// <summary>
313+
/// Foo
314+
/// </summary>
315+
/// <param name=""foo"">Param 1</param>
316+
/// <param name=""bar"">Param 2</param>
317+
public delegate void (int foo);
318+
}";
319+
320+
await VerifyCSharpDiagnosticAsync(testCode, testSettings: null, DiagnosticResult.EmptyDiagnosticResults, ignoreCompilerDiagnostics: true, CancellationToken.None).ConfigureAwait(false);
257321
}
258322

259323
[Theory]
@@ -267,11 +331,23 @@ public async Task VerifyInheritedDocumentationReportsNoDiagnosticsAsync(string d
267331
public class ClassName
268332
{
269333
/// <inheritdoc/>
270-
$$
334+
$$
271335
}";
272336
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
273337
}
274338

339+
[Fact]
340+
public async Task VerifyIncludedClassIsNotReportedAsync()
341+
{
342+
var testCode = @"
343+
/// <include file='MissingParamDocumentation.xml' path='/ClassName/Method/*' />
344+
public class ClassName
345+
{
346+
}";
347+
348+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
349+
}
350+
275351
[Fact]
276352
public async Task VerifyIncludedMemberWithoutParamsIsNotReportedAsync()
277353
{
@@ -303,8 +379,9 @@ public class ClassName
303379
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
304380
}
305381

306-
[Fact]
307-
public async Task VerifyIncludedMemberWithValidParamsIsNotReportedAsync()
382+
[Theory]
383+
[MemberData(nameof(Declarations))]
384+
public async Task VerifyIncludedMemberWithValidParamsIsNotReportedAsync(string declaration)
308385
{
309386
var testCode = @"
310387
/// <summary>
@@ -313,13 +390,14 @@ public async Task VerifyIncludedMemberWithValidParamsIsNotReportedAsync()
313390
public class ClassName
314391
{
315392
/// <include file='WithParamDocumentation.xml' path='/ClassName/Method/*' />
316-
public ClassName Method(string foo, string bar, string @new) { return null; }
393+
$$
317394
}";
318-
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
395+
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
319396
}
320397

321-
[Fact]
322-
public async Task VerifyIncludedMemberWithInvalidParamsIsReportedAsync()
398+
[Theory]
399+
[MemberData(nameof(Declarations))]
400+
public async Task VerifyIncludedMemberWithInvalidParamsIsReportedAsync(string declaration)
323401
{
324402
var testCode = @"
325403
/// <summary>
@@ -328,17 +406,17 @@ public async Task VerifyIncludedMemberWithInvalidParamsIsReportedAsync()
328406
public class ClassName
329407
{
330408
/// <include file='WithInvalidParamDocumentation.xml' path='/ClassName/Method/*' />
331-
public ClassName Method(string foo, string bar, string @new) { return null; }
409+
$$
332410
}";
333411

334412
var expected = new[]
335413
{
336-
Diagnostic().WithLocation(8, 22).WithArguments("boo"),
337-
Diagnostic().WithLocation(8, 22).WithArguments("far"),
338-
Diagnostic().WithLocation(8, 22).WithArguments("foe"),
414+
Diagnostic().WithLocation(0).WithArguments("boo"),
415+
Diagnostic().WithLocation(0).WithArguments("far"),
416+
Diagnostic().WithLocation(0).WithArguments("foe"),
339417
};
340418

341-
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
419+
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), expected, CancellationToken.None).ConfigureAwait(false);
342420
}
343421

344422
[Fact]
@@ -351,35 +429,36 @@ public async Task VerifyIncludedMemberWithInvalidParamsThatShouldBeHandledBySA16
351429
public class ClassName
352430
{
353431
/// <include file='WithSA1613ParamDocumentation.xml' path='/ClassName/Method/*' />
354-
public ClassName Method(string foo, string bar, string @new) { return null; }
432+
public ClassName Method(string foo, string bar, string @new) { return null; }
355433
}";
356434
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
357435
}
358436

359-
[Fact]
360-
public async Task VerifyIncludedMemberWithAllDocumentationWrongOrderIsReportedAsync()
437+
[Theory]
438+
[MemberData(nameof(Declarations))]
439+
public async Task VerifyIncludedMemberWithAllDocumentationWrongOrderIsReportedAsync(string declaration)
361440
{
362441
var testCode = @"
363442
/// <summary>
364443
/// Foo
365444
/// </summary>
366445
public class ClassName
367446
{
368-
/// <include file='WithParamDocumentation.xml' path='/ClassName/Method/*' />
369-
public ClassName Method(string bar, string @new, string foo) { return null; }
447+
/// <include file='WithWrongOrderParamDocumentation.xml' path='/ClassName/Method/*' />
448+
$$
370449
}";
371450

372451
var diagnostic = Diagnostic()
373452
.WithMessageFormat("The parameter documentation for '{0}' should be at position {1}");
374453

375454
var expected = new[]
376455
{
377-
diagnostic.WithLocation(8, 22).WithArguments("foo", 3),
378-
diagnostic.WithLocation(8, 22).WithArguments("bar", 1),
379-
diagnostic.WithLocation(8, 22).WithArguments("new", 2),
456+
diagnostic.WithLocation(0).WithArguments("new", 3),
457+
diagnostic.WithLocation(0).WithArguments("foo", 1),
458+
diagnostic.WithLocation(0).WithArguments("bar", 2),
380459
};
381460

382-
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
461+
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), expected, CancellationToken.None).ConfigureAwait(false);
383462

384463
// This is even reported if the documentation is not required, except that no warning is reported for the
385464
// first param element (which is actually the last parameter) since it would otherwise be allowed to skip
@@ -396,15 +475,16 @@ public class ClassName
396475

397476
expected = new[]
398477
{
399-
diagnostic.WithLocation(8, 22).WithArguments("bar", 1),
400-
diagnostic.WithLocation(8, 22).WithArguments("new", 2),
478+
diagnostic.WithLocation(0).WithArguments("foo", 1),
479+
diagnostic.WithLocation(0).WithArguments("bar", 2),
401480
};
402481

403-
await VerifyCSharpDiagnosticAsync(testCode, testSettings, expected, CancellationToken.None).ConfigureAwait(false);
482+
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), testSettings, expected, CancellationToken.None).ConfigureAwait(false);
404483
}
405484

406-
[Fact]
407-
public async Task VerifyIncludedMemberWithTooManyDocumentationIsReportedAsync()
485+
[Theory]
486+
[MemberData(nameof(Declarations))]
487+
public async Task VerifyIncludedMemberWithTooManyDocumentationIsReportedAsync(string declaration)
408488
{
409489
var testCode = @"
410490
/// <summary>
@@ -413,15 +493,31 @@ public async Task VerifyIncludedMemberWithTooManyDocumentationIsReportedAsync()
413493
public class ClassName
414494
{
415495
/// <include file='WithTooManyParamDocumentation.xml' path='/ClassName/Method/*' />
416-
public ClassName Method(string foo, string bar, string @new) { return null; }
496+
$$
417497
}";
418498

419499
var diagnostic = Diagnostic()
420500
.WithMessageFormat("The parameter documentation for '{0}' should be at position {1}");
421501

422-
var expected = diagnostic.WithLocation(8, 22).WithArguments("bar", 2);
502+
var expected = diagnostic.WithLocation(0).WithArguments("bar", 2);
503+
504+
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), expected, CancellationToken.None).ConfigureAwait(false);
505+
}
506+
507+
[Fact]
508+
public async Task VerifyIncludedDelegateWithoutIdentifierWithTooManyDocumentationIsNotReportedAsync()
509+
{
510+
var testCode = @"
511+
/// <summary>
512+
/// Foo
513+
/// </summary>
514+
public class ClassName
515+
{
516+
/// <include file='WithTooManyParamDocumentation.xml' path='/ClassName/Method/*' />
517+
public delegate void (int foo);
518+
}";
423519

424-
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
520+
await VerifyCSharpDiagnosticAsync(testCode, testSettings: null, DiagnosticResult.EmptyDiagnosticResults, ignoreCompilerDiagnostics: true, CancellationToken.None).ConfigureAwait(false);
425521
}
426522

427523
[Fact]
@@ -440,12 +536,15 @@ public class ClassName
440536
}
441537

442538
private static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult expected, CancellationToken cancellationToken)
443-
=> VerifyCSharpDiagnosticAsync(source, testSettings: null, new[] { expected }, cancellationToken);
539+
=> VerifyCSharpDiagnosticAsync(source, testSettings: null, new[] { expected }, false, cancellationToken);
444540

445541
private static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken)
446-
=> VerifyCSharpDiagnosticAsync(source, testSettings: null, expected, cancellationToken);
542+
=> VerifyCSharpDiagnosticAsync(source, testSettings: null, expected, false, cancellationToken);
447543

448544
private static Task VerifyCSharpDiagnosticAsync(string source, string testSettings, DiagnosticResult[] expected, CancellationToken cancellationToken)
545+
=> VerifyCSharpDiagnosticAsync(source, testSettings, expected, false, cancellationToken);
546+
547+
private static Task VerifyCSharpDiagnosticAsync(string source, string testSettings, DiagnosticResult[] expected, bool ignoreCompilerDiagnostics, CancellationToken cancellationToken)
449548
{
450549
string contentWithoutParamDocumentation = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
451550
<ClassName>
@@ -467,6 +566,18 @@ private static Task VerifyCSharpDiagnosticAsync(string source, string testSettin
467566
<param name=""new"">Param 3</param>
468567
</Method>
469568
</ClassName>
569+
";
570+
string contentWithWrongOrderParamDocumentation = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
571+
<ClassName>
572+
<Method>
573+
<summary>
574+
Foo
575+
</summary>
576+
<param name=""new"">Param 3</param>
577+
<param name=""foo"">Param 1</param>
578+
<param name=""bar"">Param 2</param>
579+
</Method>
580+
</ClassName>
470581
";
471582
string contentWithInvalidParamDocumentation = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
472583
<ClassName>
@@ -523,13 +634,19 @@ private static Task VerifyCSharpDiagnosticAsync(string source, string testSettin
523634
{
524635
{ "MissingParamDocumentation.xml", contentWithoutParamDocumentation },
525636
{ "WithParamDocumentation.xml", contentWithParamDocumentation },
637+
{ "WithWrongOrderParamDocumentation.xml", contentWithWrongOrderParamDocumentation },
526638
{ "WithInvalidParamDocumentation.xml", contentWithInvalidParamDocumentation },
527639
{ "WithSA1613ParamDocumentation.xml", contentWithSA1613ParamDocumentation },
528640
{ "WithTooManyParamDocumentation.xml", contentWithTooManyParamDocumentation },
529641
{ "WithInheritedDocumentation.xml", contentWithInheritedDocumentation },
530642
},
531643
};
532644

645+
if (ignoreCompilerDiagnostics)
646+
{
647+
test.CompilerDiagnostics = CompilerDiagnostics.None;
648+
}
649+
533650
test.ExpectedDiagnostics.AddRange(expected);
534651
return test.RunAsync(cancellationToken);
535652
}

0 commit comments

Comments
 (0)