|
3 | 3 |
|
4 | 4 | namespace StyleCop.Analyzers.Test.CSharp9.LayoutRules |
5 | 5 | { |
| 6 | + using System.Threading; |
| 7 | + using System.Threading.Tasks; |
| 8 | + using Microsoft.CodeAnalysis.Testing; |
6 | 9 | 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>; |
7 | 14 |
|
8 | 15 | public partial class SA1501CSharp9UnitTests : SA1501CSharp8UnitTests |
9 | 16 | { |
| 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 | + } |
10 | 82 | } |
11 | 83 | } |
0 commit comments