|
| 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; |
| 7 | + using System.Collections.Generic; |
| 8 | + using System.Threading; |
| 9 | + using System.Threading.Tasks; |
| 10 | + using Analyzers.ReadabilityRules; |
| 11 | + using Microsoft.CodeAnalysis.CodeFixes; |
| 12 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 13 | + using TestHelper; |
| 14 | + using Xunit; |
| 15 | + |
| 16 | + public class SA1131UnitTests : CodeFixVerifier |
| 17 | + { |
| 18 | + [Fact] |
| 19 | + public async Task TestYodaComparismAsync() |
| 20 | + { |
| 21 | + var testCode = @" |
| 22 | +using System; |
| 23 | +public class TypeName |
| 24 | +{ |
| 25 | + public void Test() |
| 26 | + { |
| 27 | + int i = 5; |
| 28 | + const int j = 6; |
| 29 | + if (j == i) { } |
| 30 | + } |
| 31 | +}"; |
| 32 | + var fixedCode = @" |
| 33 | +using System; |
| 34 | +public class TypeName |
| 35 | +{ |
| 36 | + public void Test() |
| 37 | + { |
| 38 | + int i = 5; |
| 39 | + const int j = 6; |
| 40 | + if (i == j) { } |
| 41 | + } |
| 42 | +}"; |
| 43 | + |
| 44 | + var expected = new[] |
| 45 | + { |
| 46 | + this.CSharpDiagnostic().WithLocation(9, 13), |
| 47 | + }; |
| 48 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 49 | + await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 50 | + await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public async Task TestYodaComparismAsAnArgumentAsync() |
| 55 | + { |
| 56 | + var testCode = @" |
| 57 | +using System; |
| 58 | +public class TypeName |
| 59 | +{ |
| 60 | + public void Test() |
| 61 | + { |
| 62 | + int i = 5; |
| 63 | + const int j = 6; |
| 64 | + Test(j == i); |
| 65 | + } |
| 66 | + public void Test(bool arg) { } |
| 67 | +}"; |
| 68 | + var fixedCode = @" |
| 69 | +using System; |
| 70 | +public class TypeName |
| 71 | +{ |
| 72 | + public void Test() |
| 73 | + { |
| 74 | + int i = 5; |
| 75 | + const int j = 6; |
| 76 | + Test(i == j); |
| 77 | + } |
| 78 | + public void Test(bool arg) { } |
| 79 | +}"; |
| 80 | + |
| 81 | + var expected = new[] |
| 82 | + { |
| 83 | + this.CSharpDiagnostic().WithLocation(9, 14), |
| 84 | + }; |
| 85 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 86 | + await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 87 | + await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false); |
| 88 | + } |
| 89 | + |
| 90 | + [Fact] |
| 91 | + public async Task TestYodaComparismOutsideIfAsync() |
| 92 | + { |
| 93 | + var testCode = @" |
| 94 | +using System; |
| 95 | +public class TypeName |
| 96 | +{ |
| 97 | + public void Test() |
| 98 | + { |
| 99 | + int i = 5; |
| 100 | + const int j = 6; |
| 101 | + bool b = j == i; |
| 102 | + } |
| 103 | +}"; |
| 104 | + var fixedCode = @" |
| 105 | +using System; |
| 106 | +public class TypeName |
| 107 | +{ |
| 108 | + public void Test() |
| 109 | + { |
| 110 | + int i = 5; |
| 111 | + const int j = 6; |
| 112 | + bool b = i == j; |
| 113 | + } |
| 114 | +}"; |
| 115 | + var expected = new[] |
| 116 | + { |
| 117 | + this.CSharpDiagnostic().WithLocation(9, 18), |
| 118 | + }; |
| 119 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 120 | + await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 121 | + await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false); |
| 122 | + } |
| 123 | + |
| 124 | + [Fact] |
| 125 | + public async Task TestCorrectComparismAsync() |
| 126 | + { |
| 127 | + var testCode = @" |
| 128 | +using System; |
| 129 | +public class TypeName |
| 130 | +{ |
| 131 | + public void Test() |
| 132 | + { |
| 133 | + const int i = 5; |
| 134 | + int j = 6; |
| 135 | + if (j == i) { } |
| 136 | + } |
| 137 | +}"; |
| 138 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 139 | + } |
| 140 | + |
| 141 | + [Fact] |
| 142 | + public async Task TestCorrectComparismOutsideIfAsync() |
| 143 | + { |
| 144 | + var testCode = @" |
| 145 | +using System; |
| 146 | +public class TypeName |
| 147 | +{ |
| 148 | + public void Test() |
| 149 | + { |
| 150 | + const int i = 5; |
| 151 | + int j = 6; |
| 152 | + bool b = j == i; |
| 153 | + } |
| 154 | +}"; |
| 155 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 156 | + } |
| 157 | + |
| 158 | + [Fact] |
| 159 | + public async Task TestCorrectComparismNoConstantAsync() |
| 160 | + { |
| 161 | + var testCode = @" |
| 162 | +using System; |
| 163 | +public class TypeName |
| 164 | +{ |
| 165 | + public void Test() |
| 166 | + { |
| 167 | + int i = 5; |
| 168 | + int j = 6; |
| 169 | + if (j == i) { } |
| 170 | + } |
| 171 | +}"; |
| 172 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 173 | + } |
| 174 | + |
| 175 | + [Fact] |
| 176 | + public async Task TestCorrectComparismOutsideIfNoConstantAsync() |
| 177 | + { |
| 178 | + var testCode = @" |
| 179 | +using System; |
| 180 | +public class TypeName |
| 181 | +{ |
| 182 | + public void Test() |
| 183 | + { |
| 184 | + int i = 5; |
| 185 | + int j = 6; |
| 186 | + bool b = j == i; |
| 187 | + } |
| 188 | +}"; |
| 189 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 190 | + } |
| 191 | + |
| 192 | + [Fact] |
| 193 | + public async Task TestCommentsAsync() |
| 194 | + { |
| 195 | + var testCode = @" |
| 196 | +using System; |
| 197 | +public class TypeName |
| 198 | +{ |
| 199 | + public void Test() |
| 200 | + { |
| 201 | + int i = 5; |
| 202 | + const int j = 6; |
| 203 | + bool b = /*1*/j/*2*/==/*3*/i/*4*/; |
| 204 | + } |
| 205 | +}"; |
| 206 | + var fixedCode = @" |
| 207 | +using System; |
| 208 | +public class TypeName |
| 209 | +{ |
| 210 | + public void Test() |
| 211 | + { |
| 212 | + int i = 5; |
| 213 | + const int j = 6; |
| 214 | + bool b = /*1*/i/*2*/==/*3*/j/*4*/; |
| 215 | + } |
| 216 | +}"; |
| 217 | + var expected = new[] |
| 218 | + { |
| 219 | + this.CSharpDiagnostic().WithLocation(9, 23), |
| 220 | + }; |
| 221 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 222 | + await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 223 | + await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false); |
| 224 | + } |
| 225 | + |
| 226 | + /// <inheritdoc/> |
| 227 | + protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers() |
| 228 | + { |
| 229 | + yield return new SA1131UseReadableConditions(); |
| 230 | + } |
| 231 | + |
| 232 | + /// <inheritdoc/> |
| 233 | + protected override CodeFixProvider GetCSharpCodeFixProvider() |
| 234 | + { |
| 235 | + return new SA1131CodeFixProvider(); |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments