Skip to content

Commit 250090b

Browse files
committed
Add tests for local functions in SA1127
1 parent a6f96b6 commit 250090b

1 file changed

Lines changed: 194 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/ReadabilityRules/SA1127CSharp7UnitTests.cs

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,203 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp7.ReadabilityRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.ReadabilityRules;
9+
using TestHelper;
10+
using Xunit;
711

812
public class SA1127CSharp7UnitTests : SA1127UnitTests
913
{
14+
[Fact]
15+
public async Task TestViolationWithLocalFunctionDeclarationAsync()
16+
{
17+
var testCode = $@"
18+
class Foo
19+
{{
20+
private void Method()
21+
{{
22+
void LocalFunction<T>() where T : class {{ }}
23+
}}
24+
}}";
25+
var fixedCode = $@"
26+
class Foo
27+
{{
28+
private void Method()
29+
{{
30+
void LocalFunction<T>()
31+
where T : class
32+
{{ }}
33+
}}
34+
}}";
35+
var expected = this.CSharpDiagnostic().WithLocation(6, 33);
36+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
37+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
38+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
39+
}
40+
41+
/// <summary>
42+
/// This is a regression test for DotNetAnalyzers/StyleCopAnalyzers#1476:
43+
/// https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1476
44+
/// </summary>
45+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
46+
[Fact]
47+
public async Task TestViolationWithLocalFunctionDeclarationMultiLineParametersAsync()
48+
{
49+
var testCode = @"
50+
class Foo
51+
{
52+
private void Method()
53+
{
54+
void LocalFunction<T>(
55+
int a,
56+
int b) where T : class { }
57+
}
58+
}";
59+
var fixedCode = @"
60+
class Foo
61+
{
62+
private void Method()
63+
{
64+
void LocalFunction<T>(
65+
int a,
66+
int b)
67+
where T : class
68+
{ }
69+
}
70+
}";
71+
var expected = this.CSharpDiagnostic().WithLocation(8, 20);
72+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
73+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
74+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
75+
}
76+
77+
/// <summary>
78+
/// This is a regression test for DotNetAnalyzers/StyleCopAnalyzers#1652:
79+
/// https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1652
80+
/// </summary>
81+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
82+
[Fact]
83+
public async Task TestViolationWithLocalFunctionDeclarationRegionDirectiveAsync()
84+
{
85+
var testCode = $@"
86+
class Foo
87+
{{
88+
private void Method()
89+
{{
90+
#region Test
91+
void LocalFunction<T>() where T : class {{ }}
92+
#endregion
93+
}}
94+
}}";
95+
var fixedCode = $@"
96+
class Foo
97+
{{
98+
private void Method()
99+
{{
100+
#region Test
101+
void LocalFunction<T>()
102+
where T : class
103+
{{ }}
104+
#endregion
105+
}}
106+
}}";
107+
var expected = this.CSharpDiagnostic().WithLocation(7, 33);
108+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
109+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
110+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
111+
}
112+
113+
[Fact]
114+
public async Task TestViolationWithExpressionBodiedLocalFunctionDeclarationAsync()
115+
{
116+
var testCode = $@"
117+
class Foo
118+
{{
119+
private void Method()
120+
{{
121+
string LocalFunction<T>() where T : class => typeof(T).Name;
122+
}}
123+
}}";
124+
var fixedCode = $@"
125+
class Foo
126+
{{
127+
private void Method()
128+
{{
129+
string LocalFunction<T>()
130+
where T : class
131+
=> typeof(T).Name;
132+
}}
133+
}}";
134+
var expected = this.CSharpDiagnostic().WithLocation(6, 35);
135+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
136+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
137+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
138+
}
139+
140+
[Fact]
141+
public async Task TestViolationWithLocalFunctionAndThreeTypeConstraintsOnSingleLineAsync()
142+
{
143+
var testCode = $@"
144+
class Foo
145+
{{
146+
private void Method()
147+
{{
148+
void LocalFunction<T1, T2, T3>() where T1 : class where T2 : class where T3 : class {{ }}
149+
}}
150+
}}";
151+
var fixedCode = $@"
152+
class Foo
153+
{{
154+
private void Method()
155+
{{
156+
void LocalFunction<T1, T2, T3>()
157+
where T1 : class
158+
where T2 : class
159+
where T3 : class
160+
{{ }}
161+
}}
162+
}}";
163+
var expected = this.CSharpDiagnostic().WithLocation(6, 42);
164+
var expected2 = this.CSharpDiagnostic().WithLocation(6, 59);
165+
var expected3 = this.CSharpDiagnostic().WithLocation(6, 76);
166+
await this.VerifyCSharpDiagnosticAsync(testCode, new[] { expected, expected2, expected3 }, CancellationToken.None).ConfigureAwait(false);
167+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
168+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
169+
}
170+
171+
[Fact]
172+
public async Task TestViolationWithLocalFunctionAndCommentTriviaAtEndOfLineAsync()
173+
{
174+
var testCode = $@"
175+
using System;
176+
class Foo
177+
{{
178+
private void Method()
179+
{{
180+
T GenericLocalFunction<T>() where T : class // constrain this to just classes
181+
{{
182+
throw new NotImplementedException();
183+
}}
184+
}}
185+
}}";
186+
var fixedCode = $@"
187+
using System;
188+
class Foo
189+
{{
190+
private void Method()
191+
{{
192+
T GenericLocalFunction<T>()
193+
where T : class // constrain this to just classes
194+
{{
195+
throw new NotImplementedException();
196+
}}
197+
}}
198+
}}";
199+
var expected = this.CSharpDiagnostic().WithLocation(7, 37);
200+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
201+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
202+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
203+
}
10204
}
11205
}

0 commit comments

Comments
 (0)