@@ -6,9 +6,11 @@ namespace StyleCop.Analyzers.Test.DocumentationRules
66 using System . Collections . Generic ;
77 using System . Threading ;
88 using System . Threading . Tasks ;
9+ using Microsoft . CodeAnalysis ;
910 using Microsoft . CodeAnalysis . CodeFixes ;
1011 using Microsoft . CodeAnalysis . Diagnostics ;
1112 using StyleCop . Analyzers . DocumentationRules ;
13+ using StyleCop . Analyzers . Test . Helpers ;
1214 using TestHelper ;
1315 using Xunit ;
1416 using static StyleCop . Analyzers . DocumentationRules . SA1642ConstructorSummaryDocumentationMustBeginWithStandardText ;
@@ -424,6 +426,163 @@ protected CustomizableBlockSubscriberBase()
424426 await this . VerifyCSharpFixAsync ( testCode , fixedCode , cancellationToken : CancellationToken . None ) . ConfigureAwait ( false ) ;
425427 }
426428
429+ /// <summary>
430+ /// Verifies that a constructor with the correct summary text from included documentation will not produce any diagnostics.
431+ /// </summary>
432+ /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
433+ [ Fact ]
434+ public async Task TestConstructorWithValidSummaryInIncludedDocsAsync ( )
435+ {
436+ var testCode = @"
437+ public class TestClass
438+ {
439+ /// <include file='ValidSummary.xml' path='/TestClass/TestClass/*'/>
440+ public TestClass() { }
441+ }
442+ " ;
443+
444+ await this . VerifyCSharpDiagnosticAsync ( testCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
445+ }
446+
447+ /// <summary>
448+ /// Verifies that a constructor with the missing summary tag from included documentation will not produce any diagnostics.
449+ /// </summary>
450+ /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
451+ [ Fact ]
452+ public async Task TestConstructorWithMissingSummaryInIncludedDocsAsync ( )
453+ {
454+ var testCode = @"
455+ public class TestClass
456+ {
457+ /// <include file='MissingSummary.xml' path='/TestClass/TestClass/*'/>
458+ public TestClass() { }
459+ }
460+ " ;
461+
462+ await this . VerifyCSharpDiagnosticAsync ( testCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
463+ }
464+
465+ /// <summary>
466+ /// Verifies that a constructor with an empty summary tag from included documentation will produce a diagnostic and offer no codefix.
467+ /// </summary>
468+ /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
469+ [ Fact ]
470+ public async Task TestConstructorWithEmptySummaryInIncludedDocsAsync ( )
471+ {
472+ var testCode = @"
473+ public class TestClass
474+ {
475+ /// <include file='EmptySummary.xml' path='/TestClass/TestClass/*'/>
476+ public TestClass() { }
477+ }
478+ " ;
479+
480+ var expected = this . CSharpDiagnostic ( ) . WithLocation ( 4 , 9 ) ;
481+
482+ await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
483+ var offeredFixes = await this . GetOfferedCSharpFixesAsync ( testCode ) . ConfigureAwait ( false ) ;
484+ Assert . Empty ( offeredFixes ) ;
485+ }
486+
487+ /// <summary>
488+ /// Verifies that a constructor with an invalid summary tag from included documentation will produce a diagnostic and offer no codefix.
489+ /// </summary>
490+ /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
491+ [ Fact ]
492+ public async Task TestConstructorWithInvalidSummaryInIncludedDocsAsync ( )
493+ {
494+ var testCode = @"
495+ public class TestClass
496+ {
497+ /// <include file='InvalidSummary.xml' path='/TestClass/TestClass/*'/>
498+ public TestClass() { }
499+ }
500+ " ;
501+
502+ var expected = this . CSharpDiagnostic ( ) . WithLocation ( 4 , 9 ) ;
503+
504+ await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
505+ var offeredFixes = await this . GetOfferedCSharpFixesAsync ( testCode ) . ConfigureAwait ( false ) ;
506+ Assert . Empty ( offeredFixes ) ;
507+ }
508+
509+ /// <summary>
510+ /// Verifies that a constructor with an invalid class reference in the summary tag from included documentation will produce a diagnostic and offer no codefix.
511+ /// </summary>
512+ /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
513+ [ Fact ]
514+ public async Task TestConstructorWithInvalidReferenceInIncludedDocsAsync ( )
515+ {
516+ var testCode = @"
517+ public class TestClass
518+ {
519+ /// <include file='InvalidReference.xml' path='/TestClass/TestClass/*'/>
520+ public TestClass() { }
521+ }
522+
523+ public class WrongClass { }
524+ " ;
525+
526+ var expected = this . CSharpDiagnostic ( ) . WithLocation ( 4 , 9 ) ;
527+
528+ await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
529+ var offeredFixes = await this . GetOfferedCSharpFixesAsync ( testCode ) . ConfigureAwait ( false ) ;
530+ Assert . Empty ( offeredFixes ) ;
531+ }
532+
533+ protected override Project ApplyCompilationOptions ( Project project )
534+ {
535+ var resolver = new TestXmlReferenceResolver ( ) ;
536+
537+ string contentValidSummary = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
538+ <TestClass>
539+ <TestClass>
540+ <summary>Initializes a new instance of the <see cref=""TestClass""/> class.</summary>
541+ </TestClass>
542+ </TestClass>
543+ " ;
544+ resolver . XmlReferences . Add ( "ValidSummary.xml" , contentValidSummary ) ;
545+
546+ string contentMissingSummary = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
547+ <TestClass>
548+ <TestClass>
549+ </TestClass>
550+ </TestClass>
551+ " ;
552+ resolver . XmlReferences . Add ( "MissingSummary.xml" , contentMissingSummary ) ;
553+
554+ string contentEmptySummary = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
555+ <TestClass>
556+ <TestClass>
557+ <summary></summary>
558+ </TestClass>
559+ </TestClass>
560+ " ;
561+ resolver . XmlReferences . Add ( "EmptySummary.xml" , contentEmptySummary ) ;
562+
563+ string contentInvalidSummary = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
564+ <TestClass>
565+ <TestClass>
566+ <summary>Creates the <see cref=""TestClass""/> class.</summary>
567+ </TestClass>
568+ </TestClass>
569+ " ;
570+ resolver . XmlReferences . Add ( "InvalidSummary.xml" , contentInvalidSummary ) ;
571+
572+ string contentInvalidReference = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
573+ <TestClass>
574+ <TestClass>
575+ <summary>Initializes a new instance of the <see cref=""WrongClass""/> class.</summary>
576+ </TestClass>
577+ </TestClass>
578+ " ;
579+ resolver . XmlReferences . Add ( "InvalidReference.xml" , contentInvalidReference ) ;
580+
581+ project = base . ApplyCompilationOptions ( project ) ;
582+ project = project . WithCompilationOptions ( project . CompilationOptions . WithXmlReferenceResolver ( resolver ) ) ;
583+ return project ;
584+ }
585+
427586 protected override IEnumerable < DiagnosticAnalyzer > GetCSharpDiagnosticAnalyzers ( )
428587 {
429588 yield return new SA1642ConstructorSummaryDocumentationMustBeginWithStandardText ( ) ;
0 commit comments