Skip to content

Commit 2143ed5

Browse files
committed
Add local functions tests to SA1505, SA1508, SA1509, SA1513
1 parent 36f347e commit 2143ed5

4 files changed

Lines changed: 357 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1505CSharp7UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp7.LayoutRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.LayoutRules;
9+
using Xunit;
710

811
public class SA1505CSharp7UnitTests : SA1505UnitTests
912
{
13+
/// <summary>
14+
/// Verifies that a valid local function will not produce any diagnostics.
15+
/// </summary>
16+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
17+
[Fact]
18+
public async Task TestValidLocalFunctionAsync()
19+
{
20+
var testCode = @"namespace TestNamespace
21+
{
22+
public class TestClass
23+
{
24+
private int testField;
25+
26+
public void TestMethod()
27+
{
28+
void LocalFunction()
29+
{
30+
this.testField = -this.testField;
31+
}
32+
}
33+
}
34+
}
35+
";
36+
37+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
38+
}
39+
40+
/// <summary>
41+
/// Verifies that an invalid local function will produce the expected diagnostics.
42+
/// </summary>
43+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
44+
[Fact]
45+
public async Task TestInvalidLocalFunctionAsync()
46+
{
47+
var testCode = @"namespace TestNamespace
48+
{
49+
public class TestClass
50+
{
51+
private int testField;
52+
53+
public void TestMethod()
54+
{
55+
void LocalFunction()
56+
{
57+
58+
this.testField = -this.testField;
59+
}
60+
}
61+
}
62+
}
63+
";
64+
65+
var fixedTestCode = @"namespace TestNamespace
66+
{
67+
public class TestClass
68+
{
69+
private int testField;
70+
71+
public void TestMethod()
72+
{
73+
void LocalFunction()
74+
{
75+
this.testField = -this.testField;
76+
}
77+
}
78+
}
79+
}
80+
";
81+
82+
var expectedDiagnostic = this.CSharpDiagnostic().WithLocation(10, 13);
83+
84+
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
85+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
86+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
87+
}
1088
}
1189
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1508CSharp7UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp7.LayoutRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.LayoutRules;
9+
using Xunit;
710

811
public class SA1508CSharp7UnitTests : SA1508UnitTests
912
{
13+
/// <summary>
14+
/// Verifies that a valid local function will not produce any diagnostics.
15+
/// </summary>
16+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
17+
[Fact]
18+
public async Task TestValidLocalFunctionAsync()
19+
{
20+
var testCode = @"namespace TestNamespace
21+
{
22+
public class TestClass
23+
{
24+
private int testField;
25+
26+
public void TestMethod()
27+
{
28+
void LocalFunction()
29+
{
30+
this.testField = -this.testField;
31+
}
32+
}
33+
}
34+
}
35+
";
36+
37+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
38+
}
39+
40+
/// <summary>
41+
/// Verifies that an invalid local function will produce the expected diagnostics.
42+
/// </summary>
43+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
44+
[Fact]
45+
public async Task TestInvalidLocalFunctionAsync()
46+
{
47+
var testCode = @"namespace TestNamespace
48+
{
49+
public class TestClass
50+
{
51+
private int testField;
52+
53+
public void TestMethod()
54+
{
55+
void LocalFunction()
56+
{
57+
this.testField = -this.testField;
58+
59+
}
60+
}
61+
}
62+
}
63+
";
64+
65+
var fixedTestCode = @"namespace TestNamespace
66+
{
67+
public class TestClass
68+
{
69+
private int testField;
70+
71+
public void TestMethod()
72+
{
73+
void LocalFunction()
74+
{
75+
this.testField = -this.testField;
76+
}
77+
}
78+
}
79+
}
80+
";
81+
82+
var expectedDiagnostic = this.CSharpDiagnostic().WithLocation(13, 13);
83+
84+
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
85+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
86+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
87+
}
1088
}
1189
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1509CSharp7UnitTests.cs

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

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

812
public class SA1509CSharp7UnitTests : SA1509UnitTests
913
{
14+
[Fact]
15+
public async Task TestLocalFunctionDeclarationOpeningBraceHasBlankLineAsync()
16+
{
17+
var testCode = @"
18+
class Foo
19+
{
20+
void Method()
21+
{
22+
void Bar()
23+
24+
{
25+
}
26+
}
27+
}";
28+
var fixedCode = @"
29+
class Foo
30+
{
31+
void Method()
32+
{
33+
void Bar()
34+
{
35+
}
36+
}
37+
}";
38+
39+
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(8, 9);
40+
41+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
42+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
43+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
44+
}
45+
46+
[Fact]
47+
public async Task TestLocalFunctionDeclarationOpeningBraceHasTwoBlankLineAsync()
48+
{
49+
var testCode = @"
50+
class Foo
51+
{
52+
void Method()
53+
{
54+
void Bar()
55+
56+
57+
{
58+
}
59+
}
60+
}";
61+
62+
var fixedCode = @"
63+
class Foo
64+
{
65+
void Method()
66+
{
67+
void Bar()
68+
{
69+
}
70+
}
71+
}";
72+
73+
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(9, 9);
74+
75+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
76+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
77+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
78+
}
1079
}
1180
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1513CSharp7UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp7.LayoutRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.LayoutRules;
9+
using Xunit;
710

811
public class SA1513CSharp7UnitTests : SA1513UnitTests
912
{
13+
/// <summary>
14+
/// Verifies that all valid usages of a closing brace in new C# 7 syntax without a following blank line will
15+
/// report no diagnostic.
16+
/// </summary>
17+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
18+
[Fact]
19+
public async Task TestValidCSharp7Async()
20+
{
21+
var testCode = @"using System;
22+
23+
public class Foo
24+
{
25+
public void Baz()
26+
{
27+
// Valid #1
28+
int LocalFunction(int value)
29+
{
30+
return value * 2;
31+
}
32+
}
33+
34+
public Func<int, int> Quux()
35+
{
36+
// Valid #2
37+
#if SOMETHING
38+
return null;
39+
#else
40+
return LocalFunction;
41+
42+
int LocalFunction(int value)
43+
{
44+
return value * 2;
45+
}
46+
#endif
47+
}
48+
}
49+
";
50+
51+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
52+
}
53+
54+
/// <summary>
55+
/// Verifies that all invalid usages of a closing brace in new C# 7 syntax without a following blank line will
56+
/// report a diagnostic.
57+
/// </summary>
58+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
59+
[Fact]
60+
public async Task TestInvalidCSharp7Async()
61+
{
62+
var testCode = @"
63+
public class Foo
64+
{
65+
private int x;
66+
67+
public void Baz()
68+
{
69+
// Invalid #1
70+
switch (new object())
71+
{
72+
case int y:
73+
{
74+
this.x = 1;
75+
break;
76+
}
77+
case short z:
78+
this.x = 2;
79+
break;
80+
}
81+
82+
// Invalid #2, #3
83+
void LocalFunction1()
84+
{
85+
}
86+
void LocalFunction2()
87+
{
88+
}
89+
return;
90+
}
91+
}
92+
";
93+
var fixedCode = @"
94+
public class Foo
95+
{
96+
private int x;
97+
98+
public void Baz()
99+
{
100+
// Invalid #1
101+
switch (new object())
102+
{
103+
case int y:
104+
{
105+
this.x = 1;
106+
break;
107+
}
108+
109+
case short z:
110+
this.x = 2;
111+
break;
112+
}
113+
114+
// Invalid #2, #3
115+
void LocalFunction1()
116+
{
117+
}
118+
119+
void LocalFunction2()
120+
{
121+
}
122+
123+
return;
124+
}
125+
}
126+
";
127+
128+
var expected = new[]
129+
{
130+
// Invalid #1
131+
this.CSharpDiagnostic().WithLocation(15, 14),
132+
133+
// Invalid #2, #3
134+
this.CSharpDiagnostic().WithLocation(24, 10),
135+
this.CSharpDiagnostic().WithLocation(27, 10),
136+
};
137+
138+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
139+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
140+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
141+
}
10142
}
11143
}

0 commit comments

Comments
 (0)