Skip to content

Commit 14dea4d

Browse files
authored
Merge pull request #4057 from sharwell/attributed-local-functions
Add tests for attributed local functions in C# 9
2 parents 1bc718f + 1ef093e commit 14dea4d

File tree

9 files changed

+443
-8
lines changed

9 files changed

+443
-8
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1501CSharp9UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp9.LayoutRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.CSharp8.LayoutRules;
9+
using Xunit;
10+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
11+
StyleCop.Analyzers.LayoutRules.SA1501StatementMustNotBeOnASingleLine,
12+
StyleCop.Analyzers.LayoutRules.SA1501CodeFixProvider>;
713

814
public partial class SA1501CSharp9UnitTests : SA1501CSharp8UnitTests
915
{
16+
[Fact]
17+
[WorkItem(3978, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3978")]
18+
public async Task TestLocalFunctionWithAttributeOnSingleLineAsync()
19+
{
20+
var testCode = @"using System;
21+
22+
class TestClass
23+
{
24+
void Outer()
25+
{
26+
[Obsolete]
27+
void Local(){|#0:{|} int value = 0; }
28+
}
29+
}
30+
";
31+
32+
var fixedCode = @"using System;
33+
34+
class TestClass
35+
{
36+
void Outer()
37+
{
38+
[Obsolete]
39+
void Local()
40+
{
41+
int value = 0;
42+
}
43+
}
44+
}
45+
";
46+
47+
await VerifyCSharpFixAsync(testCode, Diagnostic().WithLocation(0), fixedCode, CancellationToken.None).ConfigureAwait(false);
48+
}
1049
}
1150
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1502CSharp9UnitTests.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp9.LayoutRules
75
{
86
using System.Threading;
@@ -93,5 +91,39 @@ public async Task TestMultiLineRecordWithParameterAsync(string keyword)
9391
FixedCode = testCode,
9492
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
9593
}
94+
95+
[Fact]
96+
[WorkItem(3978, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3978")]
97+
public async Task TestLocalFunctionWithAttributeOnSingleLineAsync()
98+
{
99+
var testCode = @"using System;
100+
101+
class TestClass
102+
{
103+
void Outer()
104+
{
105+
[Obsolete]
106+
void Local(){|#0:{|} int value = 0; }
107+
}
108+
}
109+
";
110+
111+
var fixedCode = @"using System;
112+
113+
class TestClass
114+
{
115+
void Outer()
116+
{
117+
[Obsolete]
118+
void Local()
119+
{
120+
int value = 0;
121+
}
122+
}
123+
}
124+
";
125+
126+
await VerifyCSharpFixAsync(testCode, Diagnostic().WithLocation(0), fixedCode, CancellationToken.None).ConfigureAwait(false);
127+
}
96128
}
97129
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1505CSharp9UnitTests.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp9.LayoutRules
75
{
86
using System.Threading;
@@ -28,5 +26,43 @@ public record TestRecord;
2826

2927
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, testCode, CancellationToken.None).ConfigureAwait(false);
3028
}
29+
30+
[Fact]
31+
[WorkItem(3978, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3978")]
32+
public async Task TestLocalFunctionWithAttributeFollowedByBlankLineAsync()
33+
{
34+
var testCode = @"using System;
35+
36+
class TestClass
37+
{
38+
void Outer()
39+
{
40+
[Obsolete]
41+
void Local()
42+
{|#0:{|}
43+
44+
int value = 0;
45+
}
46+
}
47+
}
48+
";
49+
50+
var fixedCode = @"using System;
51+
52+
class TestClass
53+
{
54+
void Outer()
55+
{
56+
[Obsolete]
57+
void Local()
58+
{
59+
int value = 0;
60+
}
61+
}
62+
}
63+
";
64+
65+
await VerifyCSharpFixAsync(testCode, Diagnostic().WithLocation(0), fixedCode, CancellationToken.None).ConfigureAwait(false);
66+
}
3167
}
3268
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1508CSharp9UnitTests.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp9.LayoutRules
75
{
86
using System.Threading;
@@ -93,5 +91,43 @@ public async Task TestMultiLineRecordWithParameterAsync(string keyword)
9391
FixedCode = testCode,
9492
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
9593
}
94+
95+
[Fact]
96+
[WorkItem(3978, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3978")]
97+
public async Task TestLocalFunctionWithAttributePrecededByBlankLineAsync()
98+
{
99+
var testCode = @"using System;
100+
101+
class TestClass
102+
{
103+
void Outer()
104+
{
105+
[Obsolete]
106+
void Local()
107+
{
108+
int value = 0;
109+
110+
{|#0:}|}
111+
}
112+
}
113+
";
114+
115+
var fixedCode = @"using System;
116+
117+
class TestClass
118+
{
119+
void Outer()
120+
{
121+
[Obsolete]
122+
void Local()
123+
{
124+
int value = 0;
125+
}
126+
}
127+
}
128+
";
129+
130+
await VerifyCSharpFixAsync(testCode, Diagnostic().WithLocation(0), fixedCode, CancellationToken.None).ConfigureAwait(false);
131+
}
96132
}
97133
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1513CSharp9UnitTests.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp9.LayoutRules
75
{
86
using System.Threading;
@@ -65,5 +63,43 @@ public int X
6563
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
6664
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
6765
}
66+
67+
[Fact]
68+
[WorkItem(3978, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3978")]
69+
public async Task TestLocalFunctionRequiresBlankLineAsync()
70+
{
71+
var testCode = @"using System;
72+
73+
class TestClass
74+
{
75+
void Outer()
76+
{
77+
[Obsolete]
78+
void Local()
79+
{
80+
}{|#0:
81+
|} int value = 0;
82+
}
83+
}
84+
";
85+
86+
var fixedCode = @"using System;
87+
88+
class TestClass
89+
{
90+
void Outer()
91+
{
92+
[Obsolete]
93+
void Local()
94+
{
95+
}
96+
97+
int value = 0;
98+
}
99+
}
100+
";
101+
102+
await VerifyCSharpFixAsync(testCode, Diagnostic().WithLocation(0), fixedCode, CancellationToken.None).ConfigureAwait(false);
103+
}
68104
}
69105
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/ReadabilityRules/SA1133CSharp9UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp9.ReadabilityRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.CSharp8.ReadabilityRules;
9+
using Xunit;
10+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
11+
StyleCop.Analyzers.ReadabilityRules.SA1133DoNotCombineAttributes,
12+
StyleCop.Analyzers.ReadabilityRules.SA1133CodeFixProvider>;
713

814
public partial class SA1133CSharp9UnitTests : SA1133CSharp8UnitTests
915
{
16+
[Fact]
17+
[WorkItem(3978, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3978")]
18+
public async Task TestLocalFunctionAttributeListAsync()
19+
{
20+
var testCode = @"using System;
21+
22+
class TestClass
23+
{
24+
void Outer()
25+
{
26+
[Attr1, {|#0:Attr2|}]
27+
void Local()
28+
{
29+
}
30+
}
31+
}
32+
33+
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
34+
class Attr1Attribute : Attribute
35+
{
36+
}
37+
38+
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
39+
class Attr2Attribute : Attribute
40+
{
41+
}
42+
";
43+
44+
var fixedCode = @"using System;
45+
46+
class TestClass
47+
{
48+
void Outer()
49+
{
50+
[Attr1]
51+
[Attr2]
52+
void Local()
53+
{
54+
}
55+
}
56+
}
57+
58+
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
59+
class Attr1Attribute : Attribute
60+
{
61+
}
62+
63+
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
64+
class Attr2Attribute : Attribute
65+
{
66+
}
67+
";
68+
69+
await VerifyCSharpFixAsync(testCode, Diagnostic().WithLocation(0), fixedCode, CancellationToken.None).ConfigureAwait(false);
70+
}
1071
}
1172
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/ReadabilityRules/SA1134CSharp9UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp9.ReadabilityRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.CSharp8.ReadabilityRules;
9+
using Xunit;
10+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
11+
StyleCop.Analyzers.ReadabilityRules.SA1134AttributesMustNotShareLine,
12+
StyleCop.Analyzers.ReadabilityRules.SA1134CodeFixProvider>;
713

814
public partial class SA1134CSharp9UnitTests : SA1134CSharp8UnitTests
915
{
16+
[Fact]
17+
[WorkItem(3978, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3978")]
18+
public async Task TestAttributeOnSameLineAsLocalFunctionAsync()
19+
{
20+
var testCode = @"using System;
21+
22+
class TestClass
23+
{
24+
void Outer()
25+
{
26+
{|#0:[|}Attr] void Local()
27+
{
28+
}
29+
}
30+
}
31+
32+
[AttributeUsage(AttributeTargets.All)]
33+
class AttrAttribute : Attribute
34+
{
35+
}
36+
";
37+
38+
var fixedCode = @"using System;
39+
40+
class TestClass
41+
{
42+
void Outer()
43+
{
44+
[Attr]
45+
void Local()
46+
{
47+
}
48+
}
49+
}
50+
51+
[AttributeUsage(AttributeTargets.All)]
52+
class AttrAttribute : Attribute
53+
{
54+
}
55+
";
56+
57+
await VerifyCSharpFixAsync(testCode, Diagnostic().WithLocation(0), fixedCode, CancellationToken.None).ConfigureAwait(false);
58+
}
1059
}
1160
}

0 commit comments

Comments
 (0)