|
| 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.NamingRules |
| 5 | +{ |
| 6 | + using System.Collections.Generic; |
| 7 | + using System.Threading; |
| 8 | + using System.Threading.Tasks; |
| 9 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 10 | + using StyleCop.Analyzers.NamingRules; |
| 11 | + using TestHelper; |
| 12 | + using Xunit; |
| 13 | + |
| 14 | + public class SA1305UnitTests : DiagnosticVerifier |
| 15 | + { |
| 16 | + private const string SettingsFileName = "stylecop.json"; |
| 17 | + private const string DefaultTestSettings = @" |
| 18 | +{ |
| 19 | + ""settings"": { |
| 20 | + ""namingRules"": { |
| 21 | + ""allowCommonHungarianPrefixes"": true, |
| 22 | + ""allowedHungarianPrefixes"": [ ] |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | +"; |
| 27 | + |
| 28 | + private string customTestSettings; |
| 29 | + |
| 30 | + public static IEnumerable<object[]> CommonPrefixes |
| 31 | + { |
| 32 | + get |
| 33 | + { |
| 34 | + yield return new object[] { "as" }; |
| 35 | + yield return new object[] { "at" }; |
| 36 | + yield return new object[] { "by" }; |
| 37 | + yield return new object[] { "do" }; |
| 38 | + yield return new object[] { "go" }; |
| 39 | + yield return new object[] { "if" }; |
| 40 | + yield return new object[] { "in" }; |
| 41 | + yield return new object[] { "is" }; |
| 42 | + yield return new object[] { "it" }; |
| 43 | + yield return new object[] { "no" }; |
| 44 | + yield return new object[] { "of" }; |
| 45 | + yield return new object[] { "on" }; |
| 46 | + yield return new object[] { "or" }; |
| 47 | + yield return new object[] { "to" }; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public async Task TestValidFieldNamesAreNotReportedAsync() |
| 53 | + { |
| 54 | + var testCode = @" public class TestClass |
| 55 | +{ |
| 56 | + string bar, Car, fooBar, x, yz; |
| 57 | +} |
| 58 | +"; |
| 59 | + |
| 60 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public async Task TestValidVariableNamesAreNotReportedAsync() |
| 65 | + { |
| 66 | + var testCode = @" public class TestClass |
| 67 | +{ |
| 68 | + public void TestMethod() |
| 69 | + { |
| 70 | + string bar, Car, fooBar, x, yz; |
| 71 | + } |
| 72 | +} |
| 73 | +"; |
| 74 | + |
| 75 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public async Task TestInvalidFieldNamesAreReportedAsync() |
| 80 | + { |
| 81 | + var testCode = @" public class TestClass |
| 82 | +{ |
| 83 | + string baR, caRe, daRE, fAre; |
| 84 | +} |
| 85 | +"; |
| 86 | + |
| 87 | + DiagnosticResult[] expected = |
| 88 | + { |
| 89 | + this.CSharpDiagnostic().WithLocation(3, 12).WithArguments("field", "baR"), |
| 90 | + this.CSharpDiagnostic().WithLocation(3, 17).WithArguments("field", "caRe"), |
| 91 | + this.CSharpDiagnostic().WithLocation(3, 23).WithArguments("field", "daRE"), |
| 92 | + this.CSharpDiagnostic().WithLocation(3, 29).WithArguments("field", "fAre") |
| 93 | + }; |
| 94 | + |
| 95 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public async Task TestInvalidVariableNamesAreReportedAsync() |
| 100 | + { |
| 101 | + var testCode = @" public class TestClass |
| 102 | +{ |
| 103 | + public void TestMethod() |
| 104 | + { |
| 105 | + string baR, caRe, daRE, fAre; |
| 106 | + } |
| 107 | +} |
| 108 | +"; |
| 109 | + |
| 110 | + DiagnosticResult[] expected = |
| 111 | + { |
| 112 | + this.CSharpDiagnostic().WithLocation(5, 16).WithArguments("variable", "baR"), |
| 113 | + this.CSharpDiagnostic().WithLocation(5, 21).WithArguments("variable", "caRe"), |
| 114 | + this.CSharpDiagnostic().WithLocation(5, 27).WithArguments("variable", "daRE"), |
| 115 | + this.CSharpDiagnostic().WithLocation(5, 33).WithArguments("variable", "fAre") |
| 116 | + }; |
| 117 | + |
| 118 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 119 | + } |
| 120 | + |
| 121 | + [Fact] |
| 122 | + public async Task TestEventFieldsAreNotReportedAsync() |
| 123 | + { |
| 124 | + var testCode = @" public interface ITestInterface |
| 125 | +{ |
| 126 | + event System.Action abC; |
| 127 | +} |
| 128 | +"; |
| 129 | + |
| 130 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 131 | + } |
| 132 | + |
| 133 | + [Theory] |
| 134 | + [MemberData(nameof(CommonPrefixes))] |
| 135 | + public async Task TestAllowedCommonPrefixesAsync(string prefix) |
| 136 | + { |
| 137 | + var testCode = $@" public class TestClass |
| 138 | +{{ |
| 139 | + string {prefix}R; |
| 140 | +}} |
| 141 | +"; |
| 142 | + |
| 143 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 144 | + } |
| 145 | + |
| 146 | + [Theory] |
| 147 | + [MemberData(nameof(CommonPrefixes))] |
| 148 | + public async Task TestAllowedCommonPrefixesWhenDisabledAsync(string prefix) |
| 149 | + { |
| 150 | + this.customTestSettings = @" |
| 151 | +{ |
| 152 | + ""settings"": { |
| 153 | + ""namingRules"": { |
| 154 | + ""allowCommonHungarianPrefixes"": false, |
| 155 | + ""allowedHungarianPrefixes"": [ ] |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | +"; |
| 160 | + |
| 161 | + var testCode = $@" public class TestClass |
| 162 | +{{ |
| 163 | + string {prefix}R; |
| 164 | +}} |
| 165 | +"; |
| 166 | + |
| 167 | + var expected = this.CSharpDiagnostic().WithLocation(3, 12).WithArguments("field", $"{prefix}R"); |
| 168 | + |
| 169 | + await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); |
| 170 | + } |
| 171 | + |
| 172 | + [Fact] |
| 173 | + public async Task TestExcludedPrefixesAreNotReportedAsync() |
| 174 | + { |
| 175 | + this.customTestSettings = @" |
| 176 | +{ |
| 177 | + ""settings"": { |
| 178 | + ""namingRules"": { |
| 179 | + ""allowCommonHungarianPrefixes"": false, |
| 180 | + ""allowedHungarianPrefixes"": [ ""ba"", ""ca"", ""da"", ""f"" ] |
| 181 | + } |
| 182 | + } |
| 183 | +} |
| 184 | +"; |
| 185 | + |
| 186 | + var testCode = @" public class TestClass |
| 187 | +{ |
| 188 | + string baR, caRe, daRE, fAre; |
| 189 | +} |
| 190 | +"; |
| 191 | + |
| 192 | + await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
| 193 | + } |
| 194 | + |
| 195 | + protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers() |
| 196 | + { |
| 197 | + yield return new SA1305FieldNamesMustNotUseHungarianNotation(); |
| 198 | + } |
| 199 | + |
| 200 | + protected override string GetSettings() |
| 201 | + { |
| 202 | + return this.customTestSettings ?? DefaultTestSettings; |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments