|
3 | 3 |
|
4 | 4 | namespace StyleCop.Analyzers.Test.CSharp7.ReadabilityRules |
5 | 5 | { |
| 6 | + using System; |
| 7 | + using System.Threading; |
| 8 | + using System.Threading.Tasks; |
6 | 9 | using StyleCop.Analyzers.Test.ReadabilityRules; |
| 10 | + using TestHelper; |
| 11 | + using Xunit; |
7 | 12 |
|
8 | 13 | public class SA1125CSharp7UnitTests : SA1125UnitTests |
9 | 14 | { |
| 15 | + /// <summary> |
| 16 | + /// This is a regression test for DotNetAnalyzers/StyleCopAnalyzers#386. |
| 17 | + /// <see href="https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/386">SA1125 |
| 18 | + /// UseShorthandForNullableTypes incorrectly reported in typeof()</see> |
| 19 | + /// </summary> |
| 20 | + /// <param name="longForm">The source code for the long form of a <c>cref</c> attribute referencing |
| 21 | + /// an instantiation of <see cref="Nullable{T}"/> in a <c>typeof</c> expression.</param> |
| 22 | + /// <param name="shortForm">The source code for the shorthand form of a <c>cref</c> attribute referencing |
| 23 | + /// an instantiation of <see cref="Nullable{T}"/> in a <c>typeof</c> expression. If no shorthand form is |
| 24 | + /// available, this argument should be the same as <paramref name="longForm"/>.</param> |
| 25 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 26 | + [Theory] |
| 27 | + |
| 28 | + [InlineData("Nullable<(int, int)>", "(int, int)?")] |
| 29 | + [InlineData("System.Nullable<(int, int)>", "(int, int)?")] |
| 30 | + [InlineData("global::System.Nullable<(int, int)>", "(int, int)?")] |
| 31 | + |
| 32 | + [InlineData("Nullable<(T, T)>", "(T, T)?")] |
| 33 | + [InlineData("System.Nullable<(T, T)>", "(T, T)?")] |
| 34 | + [InlineData("global::System.Nullable<(T, T)>", "(T, T)?")] |
| 35 | + public async Task TestTypeOfNullableValueTupleAsync(string longForm, string shortForm) |
| 36 | + { |
| 37 | + string template = @" |
| 38 | +namespace System |
| 39 | +{{ |
| 40 | + class ClassName<T> |
| 41 | + where T : struct |
| 42 | + {{ |
| 43 | + Type nullableType = typeof({0}); |
| 44 | + }} |
| 45 | +}} |
| 46 | +"; |
| 47 | + string testCode = string.Format(template, longForm); |
| 48 | + string fixedCode = string.Format(template, shortForm); |
| 49 | + |
| 50 | + if (testCode != fixedCode) |
| 51 | + { |
| 52 | + DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(7, 36); |
| 53 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 54 | + } |
| 55 | + |
| 56 | + await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 57 | + } |
| 58 | + |
| 59 | + [Theory] |
| 60 | + |
| 61 | + [InlineData("Nullable<(int, int)>", "(int, int)?")] |
| 62 | + [InlineData("System.Nullable<(int, int)>", "(int, int)?")] |
| 63 | + [InlineData("global::System.Nullable<(int, int)>", "(int, int)?")] |
| 64 | + |
| 65 | + [InlineData("Nullable<(T, T)>", "(T, T)?")] |
| 66 | + [InlineData("System.Nullable<(T, T)>", "(T, T)?")] |
| 67 | + [InlineData("global::System.Nullable<(T, T)>", "(T, T)?")] |
| 68 | + public async Task TestNullableValueTupleFieldAsync(string longForm, string shortForm) |
| 69 | + { |
| 70 | + string template = @" |
| 71 | +namespace System |
| 72 | +{{ |
| 73 | + class ClassName<T> |
| 74 | + where T : struct |
| 75 | + {{ |
| 76 | + {0} nullableField; |
| 77 | + }} |
| 78 | +}} |
| 79 | +"; |
| 80 | + string testCode = string.Format(template, longForm); |
| 81 | + string fixedCode = string.Format(template, shortForm); |
| 82 | + |
| 83 | + DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(7, 9); |
| 84 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 85 | + await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 86 | + } |
| 87 | + |
| 88 | + [Theory] |
| 89 | + |
| 90 | + [InlineData("Nullable<(int, int)>", "(int, int)?")] |
| 91 | + [InlineData("System.Nullable<(int, int)>", "(int, int)?")] |
| 92 | + [InlineData("global::System.Nullable<(int, int)>", "(int, int)?")] |
| 93 | + |
| 94 | + [InlineData("Nullable<(T, T)>", "(T, T)?")] |
| 95 | + [InlineData("System.Nullable<(T, T)>", "(T, T)?")] |
| 96 | + [InlineData("global::System.Nullable<(T, T)>", "(T, T)?")] |
| 97 | + public async Task TestDefaultNullableValueTupleAsync(string longForm, string shortForm) |
| 98 | + { |
| 99 | + string template = @" |
| 100 | +namespace System |
| 101 | +{{ |
| 102 | + class ClassName<T> |
| 103 | + where T : struct |
| 104 | + {{ |
| 105 | + void MethodName() |
| 106 | + {{ |
| 107 | + var nullableValue = default({0}); |
| 108 | + }} |
| 109 | + }} |
| 110 | +}} |
| 111 | +"; |
| 112 | + string testCode = string.Format(template, longForm); |
| 113 | + string fixedCode = string.Format(template, shortForm); |
| 114 | + |
| 115 | + DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(9, 41); |
| 116 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 117 | + await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// This is a regression test for DotNetAnalyzers/StyleCopAnalyzers#637. |
| 122 | + /// <see href="https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/637">SA1125 |
| 123 | + /// UseShorthandForNullableTypes incorrectly reported in <c>nameof</c> expression</see> |
| 124 | + /// </summary> |
| 125 | + /// <param name="form">The source code for the content of a <c>nameof</c> expression referencing |
| 126 | + /// <see cref="Nullable{T}"/>.</param> |
| 127 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 128 | + [Theory] |
| 129 | + [InlineData("Nullable<(int, int)>")] |
| 130 | + [InlineData("System.Nullable<(int, int)>")] |
| 131 | + [InlineData("global::System.Nullable<(int, int)>")] |
| 132 | + public async Task TestNameOfNullableValueTupleAsync(string form) |
| 133 | + { |
| 134 | + string template = @" |
| 135 | +namespace System |
| 136 | +{{ |
| 137 | + class ClassName<T> |
| 138 | + where T : struct |
| 139 | + {{ |
| 140 | + string nullableName = nameof({0}); |
| 141 | + }} |
| 142 | +}} |
| 143 | +"; |
| 144 | + string testCode = string.Format(template, form); |
| 145 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 146 | + } |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// This is a regression test for DotNetAnalyzers/StyleCopAnalyzers#636. |
| 150 | + /// <see href="https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/636">SA1125 |
| 151 | + /// UseShorthandForNullableTypes incorrectly reported for static access through Nullable<int></see> |
| 152 | + /// </summary> |
| 153 | + /// <remarks> |
| 154 | + /// <para>This special case of instance access through <c>Nullable<int></c> was mentioned in a |
| 155 | + /// comment.</para> |
| 156 | + /// </remarks> |
| 157 | + /// <param name="form">The source code for the content of a <c>nameof</c> expression referencing |
| 158 | + /// <see cref="Nullable{T}"/>.</param> |
| 159 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 160 | + [Theory] |
| 161 | + [InlineData("Nullable<(int, int)>")] |
| 162 | + [InlineData("System.Nullable<(int, int)>")] |
| 163 | + [InlineData("global::System.Nullable<(int, int)>")] |
| 164 | + public async Task TestNameOfNullableValueTupleValueAsync(string form) |
| 165 | + { |
| 166 | + string template = @" |
| 167 | +namespace System |
| 168 | +{{ |
| 169 | + class ClassName<T> |
| 170 | + where T : struct |
| 171 | + {{ |
| 172 | + string nullableName = nameof({0}.Value); |
| 173 | + }} |
| 174 | +}} |
| 175 | +"; |
| 176 | + string testCode = string.Format(template, form); |
| 177 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 178 | + } |
| 179 | + |
| 180 | + /// <summary> |
| 181 | + /// This is a regression test for DotNetAnalyzers/StyleCopAnalyzers#636. |
| 182 | + /// <see href="https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/636">SA1125 |
| 183 | + /// UseShorthandForNullableTypes incorrectly reported for static access through Nullable<int></see> |
| 184 | + /// </summary> |
| 185 | + /// <param name="form">The source code for an instantiation of <see cref="Nullable{T}"/> which does not use the |
| 186 | + /// shorthand syntax.</param> |
| 187 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 188 | + [Theory] |
| 189 | + [InlineData("Nullable<(int, int)>")] |
| 190 | + [InlineData("System.Nullable<(int, int)>")] |
| 191 | + [InlineData("global::System.Nullable<(int, int)>")] |
| 192 | + public async Task TestAccessObjectEqualThroughNullableValueTupleAsync(string form) |
| 193 | + { |
| 194 | + string template = @" |
| 195 | +namespace System |
| 196 | +{{ |
| 197 | + class ClassName<T> |
| 198 | + where T : struct |
| 199 | + {{ |
| 200 | + bool equal = {0}.Equals(null, null); |
| 201 | + }} |
| 202 | +}} |
| 203 | +"; |
| 204 | + string testCode = string.Format(template, form); |
| 205 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 206 | + } |
| 207 | + |
| 208 | + [Theory] |
| 209 | + |
| 210 | + [InlineData("Nullable<(int, int)>", "(int, int)?")] |
| 211 | + [InlineData("System.Nullable<(int, int)>", "(int, int)?")] |
| 212 | + [InlineData("global::System.Nullable<(int, int)>", "(int, int)?")] |
| 213 | + |
| 214 | + [InlineData("Nullable<(T, T)>", "(T, T)?")] |
| 215 | + [InlineData("System.Nullable<(T, T)>", "(T, T)?")] |
| 216 | + [InlineData("global::System.Nullable<(T, T)>", "(T, T)?")] |
| 217 | + public async Task TestNameOfListOfNullableValueTupleAsync(string longForm, string shortForm) |
| 218 | + { |
| 219 | + string template = @" |
| 220 | +using System.Collections.Generic; |
| 221 | +namespace System |
| 222 | +{{ |
| 223 | + class ClassName<T> |
| 224 | + where T : struct |
| 225 | + {{ |
| 226 | + string nullableName = nameof(List<{0}>); |
| 227 | + }} |
| 228 | +}} |
| 229 | +"; |
| 230 | + string testCode = string.Format(template, longForm); |
| 231 | + string fixedCode = string.Format(template, shortForm); |
| 232 | + |
| 233 | + DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(8, 43); |
| 234 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 235 | + await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 236 | + } |
| 237 | + |
| 238 | + // This is a regression test for issue 2284. |
| 239 | + [Theory] |
| 240 | + [InlineData("@Nullable<(int, int)>", "(int, int)?")] |
| 241 | + [InlineData("System.@Nullable<(int, int)>", "(int, int)?")] |
| 242 | + [InlineData("global::System.@Nullable<(int, int)>", "(int, int)?")] |
| 243 | + public async Task TestNullableValueTupleFieldWithAtSignPrefixInTypeAsync(string longForm, string shortForm) |
| 244 | + { |
| 245 | + string template = @" |
| 246 | +namespace System |
| 247 | +{{ |
| 248 | + class ClassName<T> |
| 249 | + where T : struct |
| 250 | + {{ |
| 251 | + {0} nullableField; |
| 252 | + }} |
| 253 | +}} |
| 254 | +"; |
| 255 | + string testCode = string.Format(template, longForm); |
| 256 | + string fixedCode = string.Format(template, shortForm); |
| 257 | + |
| 258 | + DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(7, 9); |
| 259 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 260 | + await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 261 | + } |
10 | 262 | } |
11 | 263 | } |
0 commit comments