Skip to content

Commit 87297d4

Browse files
committed
Add SA1106 tests for extension foreach
1 parent 78b0d2c commit 87297d4

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

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

Lines changed: 66 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.ReadabilityRules
75
{
86
using System.Threading;
@@ -28,5 +26,71 @@ public async Task TestNoDiagnosticForEmptyRecordDeclarationAsync()
2826
TestCode = testCode,
2927
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
3028
}
29+
30+
[Fact]
31+
[WorkItem(3976, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3976")]
32+
public async Task TestForeachWithExtensionEnumeratorAsync()
33+
{
34+
var testCode = @"
35+
using System.Collections.Generic;
36+
37+
public class TestClass
38+
{
39+
public void TestMethod()
40+
{
41+
foreach (var value in new ExtensionEnumerable())
42+
[|;|]
43+
}
44+
}
45+
46+
public class ExtensionEnumerable
47+
{
48+
}
49+
50+
public struct ExtensionEnumerator
51+
{
52+
public int Current => 0;
53+
54+
public bool MoveNext() => false;
55+
}
56+
57+
public static class ExtensionEnumerableExtensions
58+
{
59+
public static ExtensionEnumerator GetEnumerator(this ExtensionEnumerable enumerable) => new();
60+
}
61+
";
62+
63+
var fixedCode = @"
64+
using System.Collections.Generic;
65+
66+
public class TestClass
67+
{
68+
public void TestMethod()
69+
{
70+
foreach (var value in new ExtensionEnumerable())
71+
{
72+
}
73+
}
74+
}
75+
76+
public class ExtensionEnumerable
77+
{
78+
}
79+
80+
public struct ExtensionEnumerator
81+
{
82+
public int Current => 0;
83+
84+
public bool MoveNext() => false;
85+
}
86+
87+
public static class ExtensionEnumerableExtensions
88+
{
89+
public static ExtensionEnumerator GetEnumerator(this ExtensionEnumerable enumerable) => new();
90+
}
91+
";
92+
93+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
94+
}
3195
}
3296
}

0 commit comments

Comments
 (0)