|
3 | 3 |
|
4 | 4 | namespace StyleCop.Analyzers.Test.CSharp9.SpacingRules
|
5 | 5 | {
|
| 6 | + using System.Threading; |
| 7 | + using System.Threading.Tasks; |
| 8 | + using Microsoft.CodeAnalysis.Testing; |
6 | 9 | using StyleCop.Analyzers.Test.CSharp8.SpacingRules;
|
| 10 | + using Xunit; |
| 11 | + using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< |
| 12 | + StyleCop.Analyzers.SpacingRules.SA1012OpeningBracesMustBeSpacedCorrectly, |
| 13 | + StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>; |
7 | 14 |
|
8 | 15 | public partial class SA1012CSharp9UnitTests : SA1012CSharp8UnitTests
|
9 | 16 | {
|
| 17 | + [Fact] |
| 18 | + [WorkItem(3812, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3812")] |
| 19 | + public async Task TestInAndPropertyPatternsAsync() |
| 20 | + { |
| 21 | + var testCode = @" |
| 22 | +class C |
| 23 | +{ |
| 24 | + public static bool HasValidLength(string s) |
| 25 | + { |
| 26 | + return s is ( {|#0:{|}Length: < 5 } and { Length: < 5 }); |
| 27 | + } |
| 28 | +} |
| 29 | +"; |
| 30 | + |
| 31 | + var fixedCode = @" |
| 32 | +class C |
| 33 | +{ |
| 34 | + public static bool HasValidLength(string s) |
| 35 | + { |
| 36 | + return s is ({ Length: < 5 } and { Length: < 5 }); |
| 37 | + } |
| 38 | +} |
| 39 | +"; |
| 40 | + |
| 41 | + DiagnosticResult[] expectedResults = |
| 42 | + { |
| 43 | + // SA1012: Opening brace should be followed by a space |
| 44 | + Diagnostic().WithLocation(0).WithArguments(string.Empty, "followed"), |
| 45 | + |
| 46 | + // SA1012: Opening brace should not be preceded by a space |
| 47 | + Diagnostic().WithLocation(0).WithArguments(" not", "preceded"), |
| 48 | + }; |
| 49 | + |
| 50 | + await VerifyCSharpFixAsync( |
| 51 | + testCode, |
| 52 | + expectedResults, |
| 53 | + fixedCode, |
| 54 | + CancellationToken.None).ConfigureAwait(false); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + [WorkItem(3812, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3812")] |
| 59 | + public async Task TestInOrPropertyPatternsAsync() |
| 60 | + { |
| 61 | + var testCode = @" |
| 62 | +class C |
| 63 | +{ |
| 64 | + public static bool HasValidLength(string s) |
| 65 | + { |
| 66 | + return s is ( {|#0:{|}Length: < 5 } or { Length: < 5 }); |
| 67 | + } |
| 68 | +} |
| 69 | +"; |
| 70 | + |
| 71 | + var fixedCode = @" |
| 72 | +class C |
| 73 | +{ |
| 74 | + public static bool HasValidLength(string s) |
| 75 | + { |
| 76 | + return s is ({ Length: < 5 } or { Length: < 5 }); |
| 77 | + } |
| 78 | +} |
| 79 | +"; |
| 80 | + |
| 81 | + DiagnosticResult[] expectedResults = |
| 82 | + { |
| 83 | + // SA1012: Opening brace should be followed by a space |
| 84 | + Diagnostic().WithLocation(0).WithArguments(string.Empty, "followed"), |
| 85 | + |
| 86 | + // SA1012: Opening brace should not be preceded by a space |
| 87 | + Diagnostic().WithLocation(0).WithArguments(" not", "preceded"), |
| 88 | + }; |
| 89 | + |
| 90 | + await VerifyCSharpFixAsync( |
| 91 | + testCode, |
| 92 | + expectedResults, |
| 93 | + fixedCode, |
| 94 | + CancellationToken.None).ConfigureAwait(false); |
| 95 | + } |
| 96 | + |
| 97 | + [Fact] |
| 98 | + [WorkItem(3812, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3812")] |
| 99 | + public async Task TestInParenthesizedPropertyPatternsAsync() |
| 100 | + { |
| 101 | + var testCode = @" |
| 102 | +class C |
| 103 | +{ |
| 104 | + public static bool HasValidLength(string s) |
| 105 | + { |
| 106 | + return s is ( {|#0:{|}Length: < 5 }); |
| 107 | + } |
| 108 | +} |
| 109 | +"; |
| 110 | + |
| 111 | + var fixedCode = @" |
| 112 | +class C |
| 113 | +{ |
| 114 | + public static bool HasValidLength(string s) |
| 115 | + { |
| 116 | + return s is ({ Length: < 5 }); |
| 117 | + } |
| 118 | +} |
| 119 | +"; |
| 120 | + |
| 121 | + DiagnosticResult[] expectedResults = |
| 122 | + { |
| 123 | + // SA1012: Opening brace should be followed by a space |
| 124 | + Diagnostic().WithLocation(0).WithArguments(string.Empty, "followed"), |
| 125 | + |
| 126 | + // SA1012: Opening brace should not be preceded by a space |
| 127 | + Diagnostic().WithLocation(0).WithArguments(" not", "preceded"), |
| 128 | + }; |
| 129 | + |
| 130 | + await VerifyCSharpFixAsync( |
| 131 | + testCode, |
| 132 | + expectedResults, |
| 133 | + fixedCode, |
| 134 | + CancellationToken.None).ConfigureAwait(false); |
| 135 | + } |
10 | 136 | }
|
11 | 137 | }
|
0 commit comments