|
1 | 1 | // Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. |
2 | 2 | // Licensed under the MIT License. See LICENSE in the project root for license information. |
3 | 3 |
|
4 | | -#nullable disable |
5 | | - |
6 | 4 | namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules |
7 | 5 | { |
8 | 6 | using System.Threading; |
9 | 7 | using System.Threading.Tasks; |
10 | 8 | using Microsoft.CodeAnalysis.Testing; |
11 | 9 | using StyleCop.Analyzers.Test.CSharp7.SpacingRules; |
| 10 | + using StyleCop.Analyzers.Test.Helpers; |
12 | 11 | using Xunit; |
13 | 12 | using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< |
14 | 13 | StyleCop.Analyzers.SpacingRules.SA1011ClosingSquareBracketsMustBeSpacedCorrectly, |
@@ -126,5 +125,57 @@ public void TestMethod(int[] arg) |
126 | 125 |
|
127 | 126 | await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); |
128 | 127 | } |
| 128 | + |
| 129 | + [Fact] |
| 130 | + [WorkItem(3008, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3008")] |
| 131 | + public async Task TestIndexAndRangeClosingBracketSpacingAsync() |
| 132 | + { |
| 133 | + var testCode = @" |
| 134 | +namespace TestNamespace |
| 135 | +{ |
| 136 | + using System; |
| 137 | +
|
| 138 | + public class TestClass |
| 139 | + { |
| 140 | + public void TestMethod(int[] values) |
| 141 | + { |
| 142 | + _ = values[^1 {|#0:]|}; |
| 143 | + _ = values[1..^2 {|#1:]|}; |
| 144 | + _ = values[.. {|#2:]|}; |
| 145 | + _ = values[^1{|#3:]|}^1; |
| 146 | + _ = values[^1] ^1; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | +"; |
| 151 | + var fixedCode = @" |
| 152 | +namespace TestNamespace |
| 153 | +{ |
| 154 | + using System; |
| 155 | +
|
| 156 | + public class TestClass |
| 157 | + { |
| 158 | + public void TestMethod(int[] values) |
| 159 | + { |
| 160 | + _ = values[^1]; |
| 161 | + _ = values[1..^2]; |
| 162 | + _ = values[..]; |
| 163 | + _ = values[^1] ^1; |
| 164 | + _ = values[^1] ^1; |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | +"; |
| 169 | + |
| 170 | + DiagnosticResult[] expected = |
| 171 | + { |
| 172 | + Diagnostic().WithArguments(" not", "preceded").WithLocation(0), |
| 173 | + Diagnostic().WithArguments(" not", "preceded").WithLocation(1), |
| 174 | + Diagnostic().WithArguments(" not", "preceded").WithLocation(2), |
| 175 | + Diagnostic().WithArguments(string.Empty, "followed").WithLocation(3), |
| 176 | + }; |
| 177 | + |
| 178 | + await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); |
| 179 | + } |
129 | 180 | } |
130 | 181 | } |
0 commit comments