Skip to content

Commit 8b3beda

Browse files
committed
Add regression tests for changes, improve existing ones
1 parent 1e86fc9 commit 8b3beda

1 file changed

Lines changed: 71 additions & 18 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1308UnitTests.cs

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ public class SA1308UnitTests : CodeFixVerifier
1616
{
1717
private readonly string[] modifiers = new[] { "public", "private", "protected", "public readonly", "internal readonly", "public static", "private static" };
1818

19+
public static IEnumerable<object[]> PrefixesData()
20+
{
21+
yield return new object[] { "m_" };
22+
yield return new object[] { "s_" };
23+
yield return new object[] { "t_" };
24+
yield return new object[] { "m\\u005F" };
25+
yield return new object[] { "s\\u005F" };
26+
yield return new object[] { "t\\u005F" };
27+
}
28+
29+
public static IEnumerable<object[]> MultipleDistinctPrefixesData()
30+
{
31+
yield return new object[] { "m_t_s_", "m_" };
32+
yield return new object[] { "s\\u005Fm\\u005Ft\\u005F", "s_" };
33+
}
34+
1935
[Fact]
2036
public async Task TestFieldStartingWithPrefixesToTriggerDiagnosticAsync()
2137
{
@@ -74,50 +90,87 @@ public async Task TestFieldInsideNativeMethodsClassAsync()
7490
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
7591
/// <seealso href="https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/627">#627: Code Fixes For Naming
7692
/// Rules SA1308 and SA1309 Do Not Always Fix The Name Entirely</seealso>
77-
[Fact]
78-
public async Task TestFixingMultipleIdenticalPrefixesAsync()
93+
[Theory]
94+
[MemberData(nameof(PrefixesData))]
95+
public async Task TestFixingMultipleIdenticalPrefixesAsync(string prefix)
7996
{
80-
var testCode = @"public class Foo
81-
{
82-
private string m_m_bar = ""baz"";
83-
}";
97+
var testCode = $@"public class Foo
98+
{{
99+
private string {prefix}{prefix}bar = ""baz"";
100+
}}";
84101

85102
DiagnosticResult expected =
86103
this.CSharpDiagnostic()
87-
.WithArguments("m_m_bar", "m_")
104+
.WithArguments($"{prefix}{prefix}bar", prefix)
88105
.WithLocation(3, 20);
89106

90107
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
91108

92-
var fixedCode = testCode.Replace("m_", string.Empty);
109+
var fixedCode = testCode.Replace(prefix, string.Empty);
93110
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
94111
}
95112

113+
[Theory]
114+
[MemberData(nameof(PrefixesData))]
115+
public async Task TestMultipleIdenticalPrefixesOnlyAsync(string prefix)
116+
{
117+
var testCode = $@"public class Foo
118+
{{
119+
private string {prefix}{prefix} = ""baz"";
120+
}}";
121+
122+
DiagnosticResult expected =
123+
this.CSharpDiagnostic()
124+
.WithArguments($"{prefix}{prefix}", prefix)
125+
.WithLocation(3, 20);
126+
127+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
128+
// A code fix is not offered as removing the prefixes would create an empty identifier.
129+
await this.VerifyCSharpFixAsync(testCode, testCode).ConfigureAwait(false);
130+
}
131+
96132
/// <summary>
97133
/// This is a regression test for DotNetAnalyzers/StyleCopAnalyzers#627.
98134
/// </summary>
99135
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
100136
/// <seealso href="https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/627">#627: Code Fixes For Naming
101137
/// Rules SA1308 and SA1309 Do Not Always Fix The Name Entirely</seealso>
102-
[Fact]
103-
public async Task TestFixingMultipleIndependentPrefixesAsync()
138+
[Theory]
139+
[MemberData(nameof(MultipleDistinctPrefixesData))]
140+
public async Task TestFixingMultipleDistinctPrefixesAsync(string prefixes, string diagnosticPrefix)
104141
{
105-
var testCode = @"public class Foo
106-
{
107-
private string m_t_s_bar = ""baz"";
108-
}";
142+
var testCode = $@"public class Foo
143+
{{
144+
private string {prefixes}bar = ""baz"";
145+
}}";
109146

110147
DiagnosticResult expected =
111148
this.CSharpDiagnostic()
112-
.WithArguments("m_t_s_bar", "m_")
149+
.WithArguments($"{prefixes}bar", firstPrefix)
113150
.WithLocation(3, 20);
114151

115152
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
116153

117-
var fixedCode = testCode.Replace("m_", string.Empty);
118-
fixedCode = fixedCode.Replace("s_", string.Empty);
119-
fixedCode = fixedCode.Replace("t_", string.Empty);
154+
var fixedCode = testCode.Replace(prefixes, string.Empty);
155+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
156+
}
157+
158+
[Theory]
159+
[MemberData(nameof(MultipleDistinctPrefixesData))]
160+
public async Task TestMultipleDistinctPrefixesOnlyAsync(string prefixes, string diagnosticPrefix)
161+
{
162+
var testCode = $@"public class Foo
163+
{{
164+
private string {prefixes} = ""baz"";
165+
}}";
166+
167+
DiagnosticResult expected =
168+
this.CSharpDiagnostic()
169+
.WithArguments(prefixes, firstPrefix)
170+
.WithLocation(3, 20);
120171

172+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
173+
// A code fix is not offered as removing the prefixes would create an empty identifier.
121174
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
122175
}
123176

0 commit comments

Comments
 (0)