|
| 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 Microsoft.CodeAnalysis.CodeFixes; |
| 10 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 11 | + using StyleCop.Analyzers.ReadabilityRules; |
| 12 | + using TestHelper; |
| 13 | + using Xunit; |
| 14 | + |
| 15 | + public class SX1101UnitTests : CodeFixVerifier |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Verifies that this prefixes are detected and removed. |
| 19 | + /// </summary> |
| 20 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 21 | + [Fact] |
| 22 | + public async Task VerifyThisPrefixesAreDetectedAndRemovedAsync() |
| 23 | + { |
| 24 | + var testCode = @"using System; |
| 25 | +
|
| 26 | +public class BaseTestClass |
| 27 | +{ |
| 28 | + protected int BaseTestField; |
| 29 | +
|
| 30 | + protected int BaseTestProperty { get; set; } |
| 31 | +
|
| 32 | + public event EventHandler BaseTestEvent; |
| 33 | +
|
| 34 | + protected void BaseTestMethod() |
| 35 | + { |
| 36 | + } |
| 37 | +} |
| 38 | +
|
| 39 | +public class TestClass : BaseTestClass |
| 40 | +{ |
| 41 | + protected int TestField; |
| 42 | + |
| 43 | + public int TestProperty { get; set; } |
| 44 | +
|
| 45 | + public event EventHandler TestEvent; |
| 46 | +
|
| 47 | + public void TestMethod1() |
| 48 | + { |
| 49 | + } |
| 50 | +
|
| 51 | + public void TestMethod2() |
| 52 | + { |
| 53 | + if ((this.BaseTestProperty == this.TestProperty) && (this.BaseTestField == this.TestField)) |
| 54 | + { |
| 55 | + this.BaseTestMethod(); |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + this.TestMethod1(); |
| 60 | + } |
| 61 | +
|
| 62 | + this.BaseTestEvent += (s, e) => { }; |
| 63 | + var listeners = this.TestEvent; |
| 64 | + if (listeners != null) |
| 65 | + { |
| 66 | + listeners(this, null); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | +"; |
| 71 | + |
| 72 | + var fixedTestCode = @"using System; |
| 73 | +
|
| 74 | +public class BaseTestClass |
| 75 | +{ |
| 76 | + protected int BaseTestField; |
| 77 | +
|
| 78 | + protected int BaseTestProperty { get; set; } |
| 79 | +
|
| 80 | + public event EventHandler BaseTestEvent; |
| 81 | +
|
| 82 | + protected void BaseTestMethod() |
| 83 | + { |
| 84 | + } |
| 85 | +} |
| 86 | +
|
| 87 | +public class TestClass : BaseTestClass |
| 88 | +{ |
| 89 | + protected int TestField; |
| 90 | + |
| 91 | + public int TestProperty { get; set; } |
| 92 | +
|
| 93 | + public event EventHandler TestEvent; |
| 94 | +
|
| 95 | + public void TestMethod1() |
| 96 | + { |
| 97 | + } |
| 98 | +
|
| 99 | + public void TestMethod2() |
| 100 | + { |
| 101 | + if ((BaseTestProperty == TestProperty) && (BaseTestField == TestField)) |
| 102 | + { |
| 103 | + BaseTestMethod(); |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + TestMethod1(); |
| 108 | + } |
| 109 | +
|
| 110 | + BaseTestEvent += (s, e) => { }; |
| 111 | + var listeners = TestEvent; |
| 112 | + if (listeners != null) |
| 113 | + { |
| 114 | + listeners(this, null); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | +"; |
| 119 | + |
| 120 | + DiagnosticResult[] expected = |
| 121 | + { |
| 122 | + this.CSharpDiagnostic().WithLocation(30, 14), |
| 123 | + this.CSharpDiagnostic().WithLocation(30, 39), |
| 124 | + this.CSharpDiagnostic().WithLocation(30, 62), |
| 125 | + this.CSharpDiagnostic().WithLocation(30, 84), |
| 126 | + this.CSharpDiagnostic().WithLocation(32, 13), |
| 127 | + this.CSharpDiagnostic().WithLocation(36, 13), |
| 128 | + this.CSharpDiagnostic().WithLocation(39, 9), |
| 129 | + this.CSharpDiagnostic().WithLocation(40, 25) |
| 130 | + }; |
| 131 | + |
| 132 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 133 | + await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 134 | + await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false); |
| 135 | + } |
| 136 | + |
| 137 | + /// <summary> |
| 138 | + /// Verifies that a constructor call using the 'this(...)' will not produce any diagnostics. |
| 139 | + /// </summary> |
| 140 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 141 | + [Fact] |
| 142 | + public async Task VerifyThatConstructorCallsDoNotProduceDiagnosticsAsync() |
| 143 | + { |
| 144 | + var testCode = @" |
| 145 | +public class TestClass |
| 146 | +{ |
| 147 | + private int _x; |
| 148 | +
|
| 149 | + public TestClass(int x) |
| 150 | + { |
| 151 | + _x = x; |
| 152 | + } |
| 153 | +
|
| 154 | + public TestClass() |
| 155 | + : this(1) |
| 156 | + { |
| 157 | + } |
| 158 | +} |
| 159 | +"; |
| 160 | + |
| 161 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 162 | + } |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Verifies that extension methods will not produce any diagnostics. |
| 166 | + /// </summary> |
| 167 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 168 | + [Fact] |
| 169 | + public async Task VerifyThatExtensionMethodsDoNotProduceDiagnosticsAsync() |
| 170 | + { |
| 171 | + var testCode = @" |
| 172 | +public static class TestClass |
| 173 | +{ |
| 174 | + public static void TestExtensionMethod(this System.AppDomain appDomain) |
| 175 | + { |
| 176 | + } |
| 177 | +} |
| 178 | +"; |
| 179 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 180 | + } |
| 181 | + |
| 182 | + /// <summary> |
| 183 | + /// Verifies indexers will not produce any diagnostics. |
| 184 | + /// </summary> |
| 185 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 186 | + [Fact] |
| 187 | + public async Task VerifyThatIndexersDoNotProduceDiagnosticsAsync() |
| 188 | + { |
| 189 | + var testCode = @" |
| 190 | +public class TestClass |
| 191 | +{ |
| 192 | + public int this[int index] |
| 193 | + { |
| 194 | + get { return index; } |
| 195 | + } |
| 196 | +} |
| 197 | +"; |
| 198 | + |
| 199 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 200 | + } |
| 201 | + |
| 202 | + /// <summary> |
| 203 | + /// Verifies that a necessary this prefix will not produce any diagnostics. |
| 204 | + /// </summary> |
| 205 | + /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> |
| 206 | + [Fact] |
| 207 | + public async Task VerifyThatNecessaryPrefixWillNotProduceDiagnosticsAsync() |
| 208 | + { |
| 209 | + var testCode = @" |
| 210 | +public class TestClass |
| 211 | +{ |
| 212 | + private int test; |
| 213 | +
|
| 214 | + public void TestMethod(int test) |
| 215 | + { |
| 216 | + this.test = test; |
| 217 | + } |
| 218 | +} |
| 219 | +"; |
| 220 | + |
| 221 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 222 | + } |
| 223 | + |
| 224 | + /// <inheritdoc/> |
| 225 | + protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers() |
| 226 | + { |
| 227 | + yield return new SX1101DoNotPrefixLocalMembersWithThis(); |
| 228 | + } |
| 229 | + |
| 230 | + /// <inheritdoc/> |
| 231 | + protected override CodeFixProvider GetCSharpCodeFixProvider() |
| 232 | + { |
| 233 | + return new SX1101CodeFixProvider(); |
| 234 | + } |
| 235 | + } |
| 236 | +} |
0 commit comments