|
| 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.ReadabilityRules |
| 5 | +{ |
| 6 | + using System.Collections.Generic; |
| 7 | + using System.Threading; |
| 8 | + using System.Threading.Tasks; |
| 9 | + using Analyzers.ReadabilityRules; |
| 10 | + using Microsoft.CodeAnalysis.CodeFixes; |
| 11 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 12 | + using TestHelper; |
| 13 | + using Xunit; |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// Unit tests for the <see cref="SA1129DoNotUseDefaultValueTypeConstructor"/> class. |
| 17 | + /// </summary> |
| 18 | + public class SA1129UnitTests : CodeFixVerifier |
| 19 | + { |
| 20 | + /// <summary> |
| 21 | + /// Verifies that new expressions for reference types will not generate diagnostics. |
| 22 | + /// </summary> |
| 23 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 24 | + [Fact] |
| 25 | + public async Task VerifyReferenceTypeCreationAsync() |
| 26 | + { |
| 27 | + var testCode = @"public class TestClass |
| 28 | +{ |
| 29 | + public void TestMethod() |
| 30 | + { |
| 31 | + var v1 = new TestClass2(); |
| 32 | + var v2 = new TestClass2(1); |
| 33 | + } |
| 34 | +
|
| 35 | + private class TestClass2 |
| 36 | + { |
| 37 | + public TestClass2() |
| 38 | + { |
| 39 | + } |
| 40 | +
|
| 41 | + public TestClass2(int x) |
| 42 | + { |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | +"; |
| 47 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Verifies that new expressions for value types with parameters will not generate diagnostics. |
| 52 | + /// </summary> |
| 53 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 54 | + [Fact] |
| 55 | + public async Task VerifyValueTypeWithParametersCreationAsync() |
| 56 | + { |
| 57 | + var testCode = @"public class TestClass |
| 58 | +{ |
| 59 | + public void TestMethod() |
| 60 | + { |
| 61 | + var v1 = new TestStruct(1); |
| 62 | + var v2 = new TestStruct(1) { TestProperty = 2 }; |
| 63 | + var v3 = new TestStruct() { TestProperty = 2 }; |
| 64 | + var v4 = new TestStruct { TestProperty = 2 }; |
| 65 | + } |
| 66 | +
|
| 67 | + private struct TestStruct |
| 68 | + { |
| 69 | + public TestStruct(int x) |
| 70 | + { |
| 71 | + TestProperty = x; |
| 72 | + } |
| 73 | +
|
| 74 | + public int TestProperty { get; set; } |
| 75 | + } |
| 76 | +} |
| 77 | +"; |
| 78 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Verifies that new expressions for value types without parameters will generate diagnostics. |
| 83 | + /// </summary> |
| 84 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 85 | + [Fact] |
| 86 | + public async Task VerifyInvalidValueTypeCreationAsync() |
| 87 | + { |
| 88 | + var testCode = @"public class TestClass |
| 89 | +{ |
| 90 | + public void TestMethod() |
| 91 | + { |
| 92 | + var v1 = new TestStruct(); |
| 93 | + } |
| 94 | +
|
| 95 | + private struct TestStruct |
| 96 | + { |
| 97 | + public int TestProperty { get; set; } |
| 98 | + } |
| 99 | +} |
| 100 | +"; |
| 101 | + |
| 102 | + var fixedTestCode = @"public class TestClass |
| 103 | +{ |
| 104 | + public void TestMethod() |
| 105 | + { |
| 106 | + var v1 = default(TestStruct); |
| 107 | + } |
| 108 | +
|
| 109 | + private struct TestStruct |
| 110 | + { |
| 111 | + public int TestProperty { get; set; } |
| 112 | + } |
| 113 | +} |
| 114 | +"; |
| 115 | + |
| 116 | + DiagnosticResult[] expected = |
| 117 | + { |
| 118 | + this.CSharpDiagnostic().WithLocation(5, 18) |
| 119 | + }; |
| 120 | + |
| 121 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 122 | + await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 123 | + await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false); |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Verifies that the codefix will preserve surrounding trivia. |
| 128 | + /// </summary> |
| 129 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 130 | + [Fact] |
| 131 | + public async Task VerifyTriviaPreservationAsync() |
| 132 | + { |
| 133 | + var testCode = @"public class TestClass |
| 134 | +{ |
| 135 | + public void TestMethod() |
| 136 | + { |
| 137 | + var v1 = /* c1 */ new TestStruct(); // c2 |
| 138 | +
|
| 139 | + var v2 = |
| 140 | +#if true |
| 141 | + new TestStruct(); |
| 142 | +#else |
| 143 | + new TestStruct() { TestProperty = 3 }; |
| 144 | +#endif |
| 145 | + } |
| 146 | +
|
| 147 | + private struct TestStruct |
| 148 | + { |
| 149 | + public int TestProperty { get; set; } |
| 150 | + } |
| 151 | +} |
| 152 | +"; |
| 153 | + |
| 154 | + var fixedTestCode = @"public class TestClass |
| 155 | +{ |
| 156 | + public void TestMethod() |
| 157 | + { |
| 158 | + var v1 = /* c1 */ default(TestStruct); // c2 |
| 159 | +
|
| 160 | + var v2 = |
| 161 | +#if true |
| 162 | + default(TestStruct); |
| 163 | +#else |
| 164 | + new TestStruct() { TestProperty = 3 }; |
| 165 | +#endif |
| 166 | + } |
| 167 | +
|
| 168 | + private struct TestStruct |
| 169 | + { |
| 170 | + public int TestProperty { get; set; } |
| 171 | + } |
| 172 | +} |
| 173 | +"; |
| 174 | + |
| 175 | + DiagnosticResult[] expected = |
| 176 | + { |
| 177 | + this.CSharpDiagnostic().WithLocation(5, 27), |
| 178 | + this.CSharpDiagnostic().WithLocation(9, 13) |
| 179 | + }; |
| 180 | + |
| 181 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 182 | + await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 183 | + await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false); |
| 184 | + } |
| 185 | + |
| 186 | + /// <summary> |
| 187 | + /// Verifies that new expressions for value types through generics will generate diagnostics. |
| 188 | + /// </summary> |
| 189 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 190 | + [Fact] |
| 191 | + public async Task VerifyGenericsTypeCreationAsync() |
| 192 | + { |
| 193 | + var testCode = @"public class TestClass |
| 194 | +{ |
| 195 | + public T TestMethod1<T>() |
| 196 | + where T : struct |
| 197 | + { |
| 198 | + return new T(); |
| 199 | + } |
| 200 | +
|
| 201 | + public T TestMethod2<T>() |
| 202 | + where T : new() |
| 203 | + { |
| 204 | + return new T(); |
| 205 | + } |
| 206 | +} |
| 207 | +"; |
| 208 | + |
| 209 | + var fixedTestCode = @"public class TestClass |
| 210 | +{ |
| 211 | + public T TestMethod1<T>() |
| 212 | + where T : struct |
| 213 | + { |
| 214 | + return default(T); |
| 215 | + } |
| 216 | +
|
| 217 | + public T TestMethod2<T>() |
| 218 | + where T : new() |
| 219 | + { |
| 220 | + return new T(); |
| 221 | + } |
| 222 | +} |
| 223 | +"; |
| 224 | + |
| 225 | + DiagnosticResult[] expected = |
| 226 | + { |
| 227 | + this.CSharpDiagnostic().WithLocation(6, 16) |
| 228 | + }; |
| 229 | + |
| 230 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 231 | + await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 232 | + await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false); |
| 233 | + } |
| 234 | + |
| 235 | + /// <inheritdoc/> |
| 236 | + protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers() |
| 237 | + { |
| 238 | + yield return new SA1129DoNotUseDefaultValueTypeConstructor(); |
| 239 | + } |
| 240 | + |
| 241 | + /// <inheritdoc/> |
| 242 | + protected override CodeFixProvider GetCSharpCodeFixProvider() |
| 243 | + { |
| 244 | + return new SA1129CodeFixProvider(); |
| 245 | + } |
| 246 | + } |
| 247 | +} |
0 commit comments