Skip to content

Commit 4f5ba0b

Browse files
Improve tests for SA1612
1 parent f5f4ca9 commit 4f5ba0b

File tree

1 file changed

+165
-40
lines changed

1 file changed

+165
-40
lines changed

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

Lines changed: 165 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;
@@ -19,16 +20,53 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
1920
/// </summary>
2021
public class SA1612UnitTests
2122
{
23+
public static IEnumerable<object[]> DeclarationsWithMemberColumn
24+
{
25+
get
26+
{
27+
yield return new object[] { " public ClassName Method(string foo, string bar, string @new) { return null; }", 22 };
28+
yield return new object[] { " public delegate ClassName Method(string foo, string bar, string @new);", 31 };
29+
yield return new object[] { " public ClassName this[string foo, string bar, string @new] { get { return null; } set { } }", 22 };
30+
}
31+
}
32+
2233
public static IEnumerable<object[]> Declarations
2334
{
2435
get
2536
{
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 { } }" };
37+
return DeclarationsWithMemberColumn.Select(x => new[] { x[0] });
2938
}
3039
}
3140

41+
[Fact]
42+
public async Task VerifyClassIsNotReportedAsync()
43+
{
44+
var testCode = @"
45+
/// <summary>
46+
/// Foo
47+
/// </summary>
48+
public class ClassName
49+
{
50+
}";
51+
52+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
53+
}
54+
55+
[Fact]
56+
public async Task VerifyMethodWithNoParametersIsNotReportedAsync()
57+
{
58+
var testCode = @"
59+
public class ClassName
60+
{
61+
/// <summary>
62+
/// Foo
63+
/// </summary>
64+
public void Method() { }
65+
}";
66+
67+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
68+
}
69+
3270
[Fact]
3371
public async Task TestMemberWithoutDocumentationAsync()
3472
{
@@ -198,7 +236,7 @@ public class ClassName
198236

199237
[Theory]
200238
[MemberData(nameof(Declarations))]
201-
public async Task TestMembersWithAllDocumentationWrongOrderAsync(string p)
239+
public async Task TestMembersWithAllDocumentationWrongOrderAsync(string declaration)
202240
{
203241
var testCode = @"
204242
/// <summary>
@@ -212,7 +250,7 @@ public class ClassName
212250
/// <param name=""bar"">Param 2</param>
213251
/// <param name=""new"">Param 3</param>
214252
/// <param name=""foo"">Param 1</param>
215-
$$
253+
$$
216254
}";
217255

218256
var diagnostic = Diagnostic()
@@ -225,12 +263,29 @@ public class ClassName
225263
diagnostic.WithLocation(12, 22).WithArguments("foo", 1),
226264
};
227265

228-
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", p), expected, CancellationToken.None).ConfigureAwait(false);
266+
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), expected, CancellationToken.None).ConfigureAwait(false);
267+
268+
var testSettings = @"
269+
{
270+
""settings"": {
271+
""documentationRules"": {
272+
""documentExposedElements"": false
273+
}
274+
}
275+
}
276+
";
277+
278+
expected = new[]
279+
{
280+
diagnostic.WithLocation(12, 22).WithArguments("foo", 1),
281+
};
282+
283+
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), testSettings, expected, CancellationToken.None).ConfigureAwait(false);
229284
}
230285

231286
[Theory]
232287
[MemberData(nameof(Declarations))]
233-
public async Task TestMembersWithTooManyDocumentationAsync(string p)
288+
public async Task TestMembersWithTooManyDocumentationAsync(string declaration)
234289
{
235290
var testCode = @"
236291
/// <summary>
@@ -245,15 +300,32 @@ public class ClassName
245300
/// <param name=""bar"">Param 2</param>
246301
/// <param name=""new"">Param 3</param>
247302
/// <param name=""bar"">Param 4</param>
248-
$$
303+
$$
249304
}";
250305

251306
var diagnostic = Diagnostic()
252307
.WithMessageFormat("The parameter documentation for '{0}' should be at position {1}");
253308

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

256-
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", p), expected, CancellationToken.None).ConfigureAwait(false);
311+
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), expected, CancellationToken.None).ConfigureAwait(false);
312+
}
313+
314+
[Fact]
315+
public async Task TestDelegateWithoutIdentifierWithTooManyDocumentationIsNotReportedAsync()
316+
{
317+
var testCode = @"
318+
public class ClassName
319+
{
320+
/// <summary>
321+
/// Foo
322+
/// </summary>
323+
/// <param name=""foo"">Param 1</param>
324+
/// <param name=""bar"">Param 2</param>
325+
public delegate void (int foo);
326+
}";
327+
328+
await VerifyCSharpDiagnosticAsync(testCode, testSettings: null, DiagnosticResult.EmptyDiagnosticResults, ignoreCompilerDiagnostics: true, CancellationToken.None).ConfigureAwait(false);
257329
}
258330

259331
[Theory]
@@ -267,11 +339,23 @@ public async Task VerifyInheritedDocumentationReportsNoDiagnosticsAsync(string d
267339
public class ClassName
268340
{
269341
/// <inheritdoc/>
270-
$$
342+
$$
271343
}";
272344
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
273345
}
274346

347+
[Fact]
348+
public async Task VerifyIncludedClassIsNotReportedAsync()
349+
{
350+
var testCode = @"
351+
/// <include file='MissingParamDocumentation.xml' path='/ClassName/Method/*' />
352+
public class ClassName
353+
{
354+
}";
355+
356+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
357+
}
358+
275359
[Fact]
276360
public async Task VerifyIncludedMemberWithoutParamsIsNotReportedAsync()
277361
{
@@ -303,8 +387,9 @@ public class ClassName
303387
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
304388
}
305389

306-
[Fact]
307-
public async Task VerifyIncludedMemberWithValidParamsIsNotReportedAsync()
390+
[Theory]
391+
[MemberData(nameof(Declarations))]
392+
public async Task VerifyIncludedMemberWithValidParamsIsNotReportedAsync(string declaration)
308393
{
309394
var testCode = @"
310395
/// <summary>
@@ -313,13 +398,14 @@ public async Task VerifyIncludedMemberWithValidParamsIsNotReportedAsync()
313398
public class ClassName
314399
{
315400
/// <include file='WithParamDocumentation.xml' path='/ClassName/Method/*' />
316-
public ClassName Method(string foo, string bar, string @new) { return null; }
401+
$$
317402
}";
318-
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
403+
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
319404
}
320405

321-
[Fact]
322-
public async Task VerifyIncludedMemberWithInvalidParamsIsReportedAsync()
406+
[Theory]
407+
[MemberData(nameof(DeclarationsWithMemberColumn))]
408+
public async Task VerifyIncludedMemberWithInvalidParamsIsReportedAsync(string declaration, int memberColumn)
323409
{
324410
var testCode = @"
325411
/// <summary>
@@ -328,17 +414,17 @@ public async Task VerifyIncludedMemberWithInvalidParamsIsReportedAsync()
328414
public class ClassName
329415
{
330416
/// <include file='WithInvalidParamDocumentation.xml' path='/ClassName/Method/*' />
331-
public ClassName Method(string foo, string bar, string @new) { return null; }
417+
$$
332418
}";
333419

334420
var expected = new[]
335421
{
336-
Diagnostic().WithLocation(8, 22).WithArguments("boo"),
337-
Diagnostic().WithLocation(8, 22).WithArguments("far"),
338-
Diagnostic().WithLocation(8, 22).WithArguments("foe"),
422+
Diagnostic().WithLocation(8, memberColumn).WithArguments("boo"),
423+
Diagnostic().WithLocation(8, memberColumn).WithArguments("far"),
424+
Diagnostic().WithLocation(8, memberColumn).WithArguments("foe"),
339425
};
340426

341-
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
427+
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), expected, CancellationToken.None).ConfigureAwait(false);
342428
}
343429

344430
[Fact]
@@ -351,35 +437,36 @@ public async Task VerifyIncludedMemberWithInvalidParamsThatShouldBeHandledBySA16
351437
public class ClassName
352438
{
353439
/// <include file='WithSA1613ParamDocumentation.xml' path='/ClassName/Method/*' />
354-
public ClassName Method(string foo, string bar, string @new) { return null; }
440+
public ClassName Method(string foo, string bar, string @new) { return null; }
355441
}";
356442
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
357443
}
358444

359-
[Fact]
360-
public async Task VerifyIncludedMemberWithAllDocumentationWrongOrderIsReportedAsync()
445+
[Theory]
446+
[MemberData(nameof(DeclarationsWithMemberColumn))]
447+
public async Task VerifyIncludedMemberWithAllDocumentationWrongOrderIsReportedAsync(string declaration, int memberColumn)
361448
{
362449
var testCode = @"
363450
/// <summary>
364451
/// Foo
365452
/// </summary>
366453
public class ClassName
367454
{
368-
/// <include file='WithParamDocumentation.xml' path='/ClassName/Method/*' />
369-
public ClassName Method(string bar, string @new, string foo) { return null; }
455+
/// <include file='WithWrongOrderParamDocumentation.xml' path='/ClassName/Method/*' />
456+
$$
370457
}";
371458

372459
var diagnostic = Diagnostic()
373460
.WithMessageFormat("The parameter documentation for '{0}' should be at position {1}");
374461

375462
var expected = new[]
376463
{
377-
diagnostic.WithLocation(8, 22).WithArguments("foo", 3),
378-
diagnostic.WithLocation(8, 22).WithArguments("bar", 1),
379-
diagnostic.WithLocation(8, 22).WithArguments("new", 2),
464+
diagnostic.WithLocation(8, memberColumn).WithArguments("new", 3),
465+
diagnostic.WithLocation(8, memberColumn).WithArguments("foo", 1),
466+
diagnostic.WithLocation(8, memberColumn).WithArguments("bar", 2),
380467
};
381468

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

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

397484
expected = new[]
398485
{
399-
diagnostic.WithLocation(8, 22).WithArguments("bar", 1),
400-
diagnostic.WithLocation(8, 22).WithArguments("new", 2),
486+
diagnostic.WithLocation(8, memberColumn).WithArguments("foo", 1),
487+
diagnostic.WithLocation(8, memberColumn).WithArguments("bar", 2),
401488
};
402489

403-
await VerifyCSharpDiagnosticAsync(testCode, testSettings, expected, CancellationToken.None).ConfigureAwait(false);
490+
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), testSettings, expected, CancellationToken.None).ConfigureAwait(false);
404491
}
405492

406-
[Fact]
407-
public async Task VerifyIncludedMemberWithTooManyDocumentationIsReportedAsync()
493+
[Theory]
494+
[MemberData(nameof(DeclarationsWithMemberColumn))]
495+
public async Task VerifyIncludedMemberWithTooManyDocumentationIsReportedAsync(string declaration, int memberColumn)
408496
{
409497
var testCode = @"
410498
/// <summary>
@@ -413,15 +501,31 @@ public async Task VerifyIncludedMemberWithTooManyDocumentationIsReportedAsync()
413501
public class ClassName
414502
{
415503
/// <include file='WithTooManyParamDocumentation.xml' path='/ClassName/Method/*' />
416-
public ClassName Method(string foo, string bar, string @new) { return null; }
504+
$$
417505
}";
418506

419507
var diagnostic = Diagnostic()
420508
.WithMessageFormat("The parameter documentation for '{0}' should be at position {1}");
421509

422-
var expected = diagnostic.WithLocation(8, 22).WithArguments("bar", 2);
510+
var expected = diagnostic.WithLocation(8, memberColumn).WithArguments("bar", 2);
511+
512+
await VerifyCSharpDiagnosticAsync(testCode.Replace("$$", declaration), expected, CancellationToken.None).ConfigureAwait(false);
513+
}
514+
515+
[Fact]
516+
public async Task VerifyIncludedDelegateWithoutIdentifierWithTooManyDocumentationIsNotReportedAsync()
517+
{
518+
var testCode = @"
519+
/// <summary>
520+
/// Foo
521+
/// </summary>
522+
public class ClassName
523+
{
524+
/// <include file='WithTooManyParamDocumentation.xml' path='/ClassName/Method/*' />
525+
public delegate void (int foo);
526+
}";
423527

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

427531
[Fact]
@@ -440,12 +544,15 @@ public class ClassName
440544
}
441545

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

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

448552
private static Task VerifyCSharpDiagnosticAsync(string source, string testSettings, DiagnosticResult[] expected, CancellationToken cancellationToken)
553+
=> VerifyCSharpDiagnosticAsync(source, testSettings, expected, false, cancellationToken);
554+
555+
private static Task VerifyCSharpDiagnosticAsync(string source, string testSettings, DiagnosticResult[] expected, bool ignoreCompilerDiagnostics, CancellationToken cancellationToken)
449556
{
450557
string contentWithoutParamDocumentation = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
451558
<ClassName>
@@ -467,6 +574,18 @@ private static Task VerifyCSharpDiagnosticAsync(string source, string testSettin
467574
<param name=""new"">Param 3</param>
468575
</Method>
469576
</ClassName>
577+
";
578+
string contentWithWrongOrderParamDocumentation = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
579+
<ClassName>
580+
<Method>
581+
<summary>
582+
Foo
583+
</summary>
584+
<param name=""new"">Param 3</param>
585+
<param name=""foo"">Param 1</param>
586+
<param name=""bar"">Param 2</param>
587+
</Method>
588+
</ClassName>
470589
";
471590
string contentWithInvalidParamDocumentation = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
472591
<ClassName>
@@ -523,13 +642,19 @@ private static Task VerifyCSharpDiagnosticAsync(string source, string testSettin
523642
{
524643
{ "MissingParamDocumentation.xml", contentWithoutParamDocumentation },
525644
{ "WithParamDocumentation.xml", contentWithParamDocumentation },
645+
{ "WithWrongOrderParamDocumentation.xml", contentWithWrongOrderParamDocumentation },
526646
{ "WithInvalidParamDocumentation.xml", contentWithInvalidParamDocumentation },
527647
{ "WithSA1613ParamDocumentation.xml", contentWithSA1613ParamDocumentation },
528648
{ "WithTooManyParamDocumentation.xml", contentWithTooManyParamDocumentation },
529649
{ "WithInheritedDocumentation.xml", contentWithInheritedDocumentation },
530650
},
531651
};
532652

653+
if (ignoreCompilerDiagnostics)
654+
{
655+
test.CompilerDiagnostics = CompilerDiagnostics.None;
656+
}
657+
533658
test.ExpectedDiagnostics.AddRange(expected);
534659
return test.RunAsync(cancellationToken);
535660
}

0 commit comments

Comments
 (0)