|
| 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 Microsoft.CodeAnalysis.Diagnostics; |
| 10 | + using StyleCop.Analyzers.DocumentationRules; |
| 11 | + using TestHelper; |
| 12 | + using Xunit; |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// This class contains the unit tests for SA1625. |
| 16 | + /// </summary> |
| 17 | + public class SA1625UnitTests : DiagnosticVerifier |
| 18 | + { |
| 19 | + public static IEnumerable<object[]> Members |
| 20 | + { |
| 21 | + get |
| 22 | + { |
| 23 | + yield return new[] { "public void Test() { }" }; |
| 24 | + yield return new[] { "public string Test { get; set; }" }; |
| 25 | + yield return new[] { "public string Test;" }; |
| 26 | + yield return new[] { "public class Test { }" }; |
| 27 | + yield return new[] { "public struct Test { }" }; |
| 28 | + yield return new[] { "public enum Test { }" }; |
| 29 | + yield return new[] { "public delegate void Test();" }; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + [Theory] |
| 34 | + [MemberData(nameof(Members))] |
| 35 | + public async Task VerifyThatCorrectDocumentationDoesNotReportADiagnosticAsync(string member) |
| 36 | + { |
| 37 | + var testCode = $@" |
| 38 | +public class TestClass |
| 39 | +{{ |
| 40 | + /// <summary> |
| 41 | + /// Some documentation. |
| 42 | + /// </summary> |
| 43 | + /// <remark>Some remark.</remark> |
| 44 | + {member} |
| 45 | +}} |
| 46 | +"; |
| 47 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 48 | + } |
| 49 | + |
| 50 | + [Theory] |
| 51 | + [MemberData(nameof(Members))] |
| 52 | + public async Task VerifyThatTheAnalyzerDoesNotCrashOnInheritDocAsync(string member) |
| 53 | + { |
| 54 | + var testCode = $@" |
| 55 | +public class TestClass |
| 56 | +{{ |
| 57 | + /// <inheritdoc/> |
| 58 | + {member} |
| 59 | +}} |
| 60 | +"; |
| 61 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 62 | + } |
| 63 | + |
| 64 | + [Theory] |
| 65 | + [MemberData(nameof(Members))] |
| 66 | + public async Task VerifyThatDublicatedDocumentationDoesReportADiagnosticAsync(string member) |
| 67 | + { |
| 68 | + var testCode = $@" |
| 69 | +public class TestClass |
| 70 | +{{ |
| 71 | + /// <summary>Some documentation.</summary> |
| 72 | + /// <remark>Some documentation.</remark> |
| 73 | + {member} |
| 74 | +}} |
| 75 | +"; |
| 76 | + var expected = this.CSharpDiagnostic().WithLocation(5, 9); |
| 77 | + |
| 78 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 79 | + } |
| 80 | + |
| 81 | + [Theory] |
| 82 | + [MemberData(nameof(Members))] |
| 83 | + public async Task VerifyThatAnalyzerIgnoresLeadingAndTrailingWhitespaceAsync(string member) |
| 84 | + { |
| 85 | + var testCode = $@" |
| 86 | +public class TestClass |
| 87 | +{{ |
| 88 | + /// <summary> |
| 89 | + /// Some documentation. |
| 90 | + /// |
| 91 | + /// |
| 92 | + /// </summary> |
| 93 | + /// <remark> Some documentation. </remark> |
| 94 | + {member} |
| 95 | +}} |
| 96 | +"; |
| 97 | + var expected = this.CSharpDiagnostic().WithLocation(9, 9); |
| 98 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 99 | + } |
| 100 | + |
| 101 | + [Theory] |
| 102 | + [MemberData(nameof(Members))] |
| 103 | + public async Task VerifyThatAnalysisIgnoresUnusedParametersAsync(string member) |
| 104 | + { |
| 105 | + var testCode = $@" |
| 106 | +public class TestClass |
| 107 | +{{ |
| 108 | + /// <summary>The parameter is not used.</summary> |
| 109 | + /// <remark>Documentation</remark> |
| 110 | + /// <remark>The parameter is not used.</remark> |
| 111 | + /// <remark>Documentation</remark> |
| 112 | + {member} |
| 113 | +}} |
| 114 | +"; |
| 115 | + var expected = this.CSharpDiagnostic().WithLocation(7, 9); |
| 116 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 117 | + } |
| 118 | + |
| 119 | + [Theory] |
| 120 | + [MemberData(nameof(Members))] |
| 121 | + public async Task VerifyThatAnalysisIgnoresEmptyElementsAsync(string member) |
| 122 | + { |
| 123 | + var testCode = $@" |
| 124 | +public class TestClass |
| 125 | +{{ |
| 126 | + /// <summary></summary> |
| 127 | + /// <remark>Documentation</remark> |
| 128 | + /// <remark></remark> |
| 129 | + /// <remark>Documentation</remark> |
| 130 | + {member} |
| 131 | +}} |
| 132 | +"; |
| 133 | + var expected = this.CSharpDiagnostic().WithLocation(7, 9); |
| 134 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 135 | + } |
| 136 | + |
| 137 | + [Theory] |
| 138 | + [MemberData(nameof(Members))] |
| 139 | + public async Task VerifyThatCorrectDocumentationDoesNotReportADiagnosticMultiLineAsync(string member) |
| 140 | + { |
| 141 | + var testCode = $@" |
| 142 | +public class TestClass |
| 143 | +{{ |
| 144 | + /** <summary> |
| 145 | + * Some documentation. |
| 146 | + * </summary> |
| 147 | + * <remark>Some remark.</remark> |
| 148 | + **/ |
| 149 | + {member} |
| 150 | +}} |
| 151 | +"; |
| 152 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 153 | + } |
| 154 | + |
| 155 | + [Theory] |
| 156 | + [MemberData(nameof(Members))] |
| 157 | + public async Task VerifyThatDublicatedDocumentationDoesReportADiagnosticMultiLineAsync(string member) |
| 158 | + { |
| 159 | + var testCode = $@" |
| 160 | +public class TestClass |
| 161 | +{{ |
| 162 | + /** <summary>Some documentation.</summary> |
| 163 | + * <remark>Some documentation.</remark> |
| 164 | + **/ |
| 165 | + {member} |
| 166 | +}} |
| 167 | +"; |
| 168 | + var expected = this.CSharpDiagnostic().WithLocation(5, 7); |
| 169 | + |
| 170 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 171 | + } |
| 172 | + |
| 173 | + [Theory] |
| 174 | + [MemberData(nameof(Members))] |
| 175 | + public async Task VerifyThatAnalyzerIgnoresLeadingAndTrailingWhitespaceMultiLineAsync(string member) |
| 176 | + { |
| 177 | + var testCode = $@" |
| 178 | +public class TestClass |
| 179 | +{{ |
| 180 | + /** <summary> |
| 181 | + * Some documentation. |
| 182 | + * |
| 183 | + * |
| 184 | + * </summary> |
| 185 | + * <remark> Some documentation. </remark> |
| 186 | + **/ |
| 187 | + {member} |
| 188 | +}} |
| 189 | +"; |
| 190 | + var expected = this.CSharpDiagnostic().WithLocation(9, 7); |
| 191 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 192 | + } |
| 193 | + |
| 194 | + [Theory] |
| 195 | + [MemberData(nameof(Members))] |
| 196 | + public async Task VerifyThatAnalysisIgnoresUnusedParametersMultiLineAsync(string member) |
| 197 | + { |
| 198 | + var testCode = $@" |
| 199 | +public class TestClass |
| 200 | +{{ |
| 201 | + /** <summary>The parameter is not used.</summary> |
| 202 | + * <remark>Documentation</remark> |
| 203 | + * <remark>The parameter is not used.</remark> |
| 204 | + * <remark>Documentation</remark> |
| 205 | + **/ |
| 206 | + {member} |
| 207 | +}} |
| 208 | +"; |
| 209 | + var expected = this.CSharpDiagnostic().WithLocation(7, 7); |
| 210 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 211 | + } |
| 212 | + |
| 213 | + [Theory] |
| 214 | + [MemberData(nameof(Members))] |
| 215 | + public async Task VerifyThatAnalysisIgnoresEmptyElementsMultiLineAsync(string member) |
| 216 | + { |
| 217 | + var testCode = $@" |
| 218 | +public class TestClass |
| 219 | +{{ |
| 220 | + /** <summary></summary> |
| 221 | + * <remark>Documentation</remark> |
| 222 | + * <remark></remark> |
| 223 | + * <remark>Documentation</remark> |
| 224 | + **/ |
| 225 | + {member} |
| 226 | +}} |
| 227 | +"; |
| 228 | + var expected = this.CSharpDiagnostic().WithLocation(7, 7); |
| 229 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 230 | + } |
| 231 | + |
| 232 | + /// <inheritdoc/> |
| 233 | + protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers() |
| 234 | + { |
| 235 | + yield return new SA1625ElementDocumentationMustNotBeCopiedAndPasted(); |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments