Skip to content

Commit c91b82b

Browse files
committed
Add SA1519 tests for extension foreach
1 parent aa3a3d1 commit c91b82b

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

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

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

44
namespace StyleCop.Analyzers.Test.CSharp9.LayoutRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp8.LayoutRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.LayoutRules.SA1519BracesMustNotBeOmittedFromMultiLineChildStatement,
13+
StyleCop.Analyzers.LayoutRules.SA1503CodeFixProvider>;
714

815
public partial class SA1519CSharp9UnitTests : SA1519CSharp8UnitTests
916
{
17+
[Fact]
18+
[WorkItem(3976, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3976")]
19+
public async Task TestForeachWithExtensionEnumeratorAsync()
20+
{
21+
var testCode = @"
22+
using System;
23+
24+
public class TestClass
25+
{
26+
public void TestMethod()
27+
{
28+
foreach (var value in new ExtensionEnumerable())
29+
[|Console.WriteLine(
30+
value);|]
31+
}
32+
}
33+
34+
public class ExtensionEnumerable
35+
{
36+
}
37+
38+
public struct ExtensionEnumerator
39+
{
40+
public int Current => 0;
41+
42+
public bool MoveNext() => false;
43+
}
44+
45+
public static class ExtensionEnumerableExtensions
46+
{
47+
public static ExtensionEnumerator GetEnumerator(this ExtensionEnumerable enumerable) => new();
48+
}
49+
";
50+
51+
var fixedCode = @"
52+
using System;
53+
54+
public class TestClass
55+
{
56+
public void TestMethod()
57+
{
58+
foreach (var value in new ExtensionEnumerable())
59+
{
60+
Console.WriteLine(
61+
value);
62+
}
63+
}
64+
}
65+
66+
public class ExtensionEnumerable
67+
{
68+
}
69+
70+
public struct ExtensionEnumerator
71+
{
72+
public int Current => 0;
73+
74+
public bool MoveNext() => false;
75+
}
76+
77+
public static class ExtensionEnumerableExtensions
78+
{
79+
public static ExtensionEnumerator GetEnumerator(this ExtensionEnumerable enumerable) => new();
80+
}
81+
";
82+
83+
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
84+
}
1085
}
1186
}

0 commit comments

Comments
 (0)