|
| 1 | +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +namespace StyleCop.Analyzers.Test.DocumentationRules |
| 5 | +{ |
| 6 | + using System.Collections.Generic; |
| 7 | + using System.Threading; |
| 8 | + using System.Threading.Tasks; |
| 9 | + using Analyzers.DocumentationRules; |
| 10 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 11 | + using TestHelper; |
| 12 | + using Xunit; |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// This class contains unit tests for <see cref="SA1627DocumentationTextMustNotBeEmpty"/>. |
| 16 | + /// </summary> |
| 17 | + public class SA1627UnitTests : DiagnosticVerifier |
| 18 | + { |
| 19 | + public static IEnumerable<object[]> Elements |
| 20 | + { |
| 21 | + get |
| 22 | + { |
| 23 | + yield return new[] { "remarks" }; |
| 24 | + yield return new[] { "example" }; |
| 25 | + yield return new[] { "permission" }; |
| 26 | + yield return new[] { "exception" }; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Checks an element with a blank value gives an error. |
| 32 | + /// </summary> |
| 33 | + /// <param name="element">Element to check</param> |
| 34 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 35 | + [Theory] |
| 36 | + [MemberData(nameof(Elements))] |
| 37 | + public async Task TestMemberWithBlankElementAsync(string element) |
| 38 | + { |
| 39 | + var testCode = @" |
| 40 | +/// <summary> |
| 41 | +/// Class name. |
| 42 | +/// </summary> |
| 43 | +public class ClassName |
| 44 | +{ |
| 45 | + /// <summary> |
| 46 | + /// Join together two strings. |
| 47 | + /// </summary> |
| 48 | + ///<param name=""first"">First string.</param> |
| 49 | + ///<param name=""second"">Second string.</param> |
| 50 | + /// <$$> </$$> |
| 51 | + public string JoinStrings(string first, string second) { return first + second; } |
| 52 | +}"; |
| 53 | + var expectedDiagnostic = this.CSharpDiagnostic().WithLocation(12, 9).WithArguments(element); |
| 54 | + await this.VerifyCSharpDiagnosticAsync(testCode.Replace("$$", element), expectedDiagnostic, CancellationToken.None).ConfigureAwait(false); |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Checks an element with a multiple blank values give multiple errors. |
| 59 | + /// </summary> |
| 60 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 61 | + [Fact] |
| 62 | + public async Task TestMemberWithMultipleBlankElementsAsync() |
| 63 | + { |
| 64 | + var testCode = @" |
| 65 | +/// <summary> |
| 66 | +/// Class name. |
| 67 | +/// </summary> |
| 68 | +public class ClassName |
| 69 | +{ |
| 70 | + /// <summary> |
| 71 | + /// Join together two strings. |
| 72 | + /// </summary> |
| 73 | + ///<param name=""first"">First string.</param> |
| 74 | + ///<param name=""second"">Second string.</param> |
| 75 | + /// <remarks>Single line remark.</remarks> |
| 76 | + /// <example></example> |
| 77 | + /// <exception> |
| 78 | + /// |
| 79 | + /// </exception> |
| 80 | + /// <permission> |
| 81 | + /// Multi line notes. |
| 82 | + /// Multi line notes. |
| 83 | + /// </permission> |
| 84 | + public string JoinStrings(string first, string second) { return first + second; } |
| 85 | +}"; |
| 86 | + var expectedDiagnostics = new[] |
| 87 | + { |
| 88 | + this.CSharpDiagnostic().WithLocation(13, 9).WithArguments("example"), |
| 89 | + this.CSharpDiagnostic().WithLocation(14, 9).WithArguments("exception") |
| 90 | + }; |
| 91 | + await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostics, CancellationToken.None).ConfigureAwait(false); |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Checks an element with an empty element gives an error. |
| 96 | + /// </summary> |
| 97 | + /// <param name="element">Element to check</param> |
| 98 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 99 | + [Theory] |
| 100 | + [MemberData(nameof(Elements))] |
| 101 | + public async Task TestMemberWithEmptyElementAsync(string element) |
| 102 | + { |
| 103 | + var testCode = @" |
| 104 | +/// <summary> |
| 105 | +/// Class name. |
| 106 | +/// </summary> |
| 107 | +public class ClassName |
| 108 | +{ |
| 109 | + /// <summary> |
| 110 | + /// Join together two strings. |
| 111 | + /// </summary> |
| 112 | + ///<param name=""first"">First string.</param> |
| 113 | + ///<param name=""second"">Second string.</param> |
| 114 | + /// <$$ /> |
| 115 | + public string JoinStrings(string first, string second) { return first + second; } |
| 116 | +}"; |
| 117 | + var expectedDiagnostic = this.CSharpDiagnostic().WithLocation(12, 9).WithArguments(element); |
| 118 | + await this.VerifyCSharpDiagnosticAsync(testCode.Replace("$$", element), expectedDiagnostic, CancellationToken.None).ConfigureAwait(false); |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Checks an element with non blank text does not give an error. |
| 123 | + /// </summary> |
| 124 | + /// <param name="element">Element to check</param> |
| 125 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 126 | + [Theory] |
| 127 | + [MemberData(nameof(Elements))] |
| 128 | + public async Task TestMemberWithValidElementAsync(string element) |
| 129 | + { |
| 130 | + var testCode = @" |
| 131 | +/// <summary> |
| 132 | +/// Class name. |
| 133 | +/// </summary> |
| 134 | +public class ClassName |
| 135 | +{ |
| 136 | + /// <summary> |
| 137 | + /// Join together two strings. |
| 138 | + /// </summary> |
| 139 | + ///<param name=""first"">First string.</param> |
| 140 | + ///<param name=""second"">Second string.</param> |
| 141 | + /// <$$>Not blank element.</$$> |
| 142 | + public string JoinStrings(string first, string second) { return first + second; } |
| 143 | +}"; |
| 144 | + await this.VerifyCSharpDiagnosticAsync(testCode.Replace("$$", element), EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 145 | + } |
| 146 | + |
| 147 | + protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers() |
| 148 | + { |
| 149 | + yield return new SA1627DocumentationTextMustNotBeEmpty(); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments