Skip to content

Commit 78b0d2c

Browse files
committed
Add SA1101 tests for extension foreach
1 parent 839ed93 commit 78b0d2c

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,76 @@ public A UpdateA(A value)
3333

3434
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
3535
}
36+
37+
[Fact]
38+
[WorkItem(3976, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3976")]
39+
public async Task TestForeachWithExtensionEnumeratorAsync()
40+
{
41+
var testCode = @"
42+
using System.Collections.Generic;
43+
44+
public class TestClass
45+
{
46+
private readonly ExtensionEnumerable values = new();
47+
48+
public void Test()
49+
{
50+
foreach (var value in [|values|])
51+
{
52+
}
53+
}
54+
}
55+
56+
public class ExtensionEnumerable
57+
{
58+
}
59+
60+
public struct ExtensionEnumerator
61+
{
62+
public int Current => 0;
63+
64+
public bool MoveNext() => false;
65+
}
66+
67+
public static class ExtensionEnumerableExtensions
68+
{
69+
public static ExtensionEnumerator GetEnumerator(this ExtensionEnumerable enumerable) => new();
70+
}
71+
";
72+
73+
var fixedCode = @"
74+
using System.Collections.Generic;
75+
76+
public class TestClass
77+
{
78+
private readonly ExtensionEnumerable values = new();
79+
80+
public void Test()
81+
{
82+
foreach (var value in this.values)
83+
{
84+
}
85+
}
86+
}
87+
88+
public class ExtensionEnumerable
89+
{
90+
}
91+
92+
public struct ExtensionEnumerator
93+
{
94+
public int Current => 0;
95+
96+
public bool MoveNext() => false;
97+
}
98+
99+
public static class ExtensionEnumerableExtensions
100+
{
101+
public static ExtensionEnumerator GetEnumerator(this ExtensionEnumerable enumerable) => new();
102+
}
103+
";
104+
105+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
106+
}
36107
}
37108
}

0 commit comments

Comments
 (0)