Skip to content

Commit da443ba

Browse files
committed
Update SA1011 for indices and ranges
1 parent 04ab8ae commit da443ba

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1011CSharp8UnitTests.cs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
75
{
86
using System.Threading;
97
using System.Threading.Tasks;
108
using Microsoft.CodeAnalysis.Testing;
119
using StyleCop.Analyzers.Test.CSharp7.SpacingRules;
10+
using StyleCop.Analyzers.Test.Helpers;
1211
using Xunit;
1312
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1413
StyleCop.Analyzers.SpacingRules.SA1011ClosingSquareBracketsMustBeSpacedCorrectly,
@@ -126,5 +125,57 @@ public void TestMethod(int[] arg)
126125

127126
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
128127
}
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+
}
129180
}
130181
}

0 commit comments

Comments
 (0)