Skip to content

Commit 34e7d1d

Browse files
committed
Add SA1501 tests for extension foreach
1 parent ff14d11 commit 34e7d1d

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,81 @@
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.SA1501StatementMustNotBeOnASingleLine,
13+
StyleCop.Analyzers.LayoutRules.SA1501CodeFixProvider>;
714

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

0 commit comments

Comments
 (0)