Skip to content

Commit 0a930b5

Browse files
committed
Expand testing of ! to include SA1002 and SA1019
1 parent abf0325 commit 0a930b5

3 files changed

Lines changed: 120 additions & 26 deletions

File tree

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,57 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.CSharp;
9+
using Microsoft.CodeAnalysis.Testing;
610
using StyleCop.Analyzers.Test.CSharp7.SpacingRules;
11+
using Xunit;
12+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
13+
StyleCop.Analyzers.SpacingRules.SA1002SemicolonsMustBeSpacedCorrectly,
14+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
715

816
public class SA1002CSharp8UnitTests : SA1002CSharp7UnitTests
917
{
18+
[Fact]
19+
[WorkItem(3052, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3052")]
20+
public async Task TestClosingSquareBracketFollowedByExclamationAsync()
21+
{
22+
var testCode = @"namespace TestNamespace
23+
{
24+
public class TestClass
25+
{
26+
public void TestMethod(object?[] arguments)
27+
{
28+
object o1 = arguments[0] !;
29+
object o2 = arguments[0]! ;
30+
object o3 = arguments[0] ! ;
31+
}
32+
}
33+
}
34+
";
35+
36+
var fixedCode = @"namespace TestNamespace
37+
{
38+
public class TestClass
39+
{
40+
public void TestMethod(object?[] arguments)
41+
{
42+
object o1 = arguments[0] !;
43+
object o2 = arguments[0]!;
44+
object o3 = arguments[0] !;
45+
}
46+
}
47+
}
48+
";
49+
50+
DiagnosticResult[] expected =
51+
{
52+
Diagnostic().WithArguments(" not", "preceded").WithLocation(8, 39),
53+
Diagnostic().WithArguments(" not", "preceded").WithLocation(9, 40),
54+
};
55+
56+
await VerifyCSharpFixAsync(LanguageVersion.CSharp8, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
57+
}
1058
}
1159
}

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

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,12 @@ public class TestClass
6767
{
6868
public void TestMethod(object?[] arguments)
6969
{
70-
object o = arguments[0]!;
71-
string s = arguments[0]!.ToString();
72-
}
73-
}
74-
}
75-
";
76-
77-
await VerifyCSharpDiagnosticAsync(LanguageVersion.CSharp8, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
78-
}
79-
80-
[Fact]
81-
[WorkItem(3052, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3052")]
82-
public async Task TestInvalidClosingSquareBracketFollowedByExclamationAsync()
83-
{
84-
var testCode = @"namespace TestNamespace
85-
{
86-
public class TestClass
87-
{
88-
public void TestMethod(object?[] arguments)
89-
{
90-
object o = arguments[0] !;
91-
string s = arguments[0] !.ToString();
70+
object o1 = arguments[0] !;
71+
object o2 = arguments[0]! ;
72+
object o3 = arguments[0] ! ;
73+
string s1 = arguments[0] !.ToString();
74+
string s2 = arguments[0]! .ToString();
75+
string s3 = arguments[0] ! .ToString();
9276
}
9377
}
9478
}
@@ -100,17 +84,23 @@ public class TestClass
10084
{
10185
public void TestMethod(object?[] arguments)
10286
{
103-
object o = arguments[0]!;
104-
string s = arguments[0]!.ToString();
87+
object o1 = arguments[0]!;
88+
object o2 = arguments[0]! ;
89+
object o3 = arguments[0]! ;
90+
string s1 = arguments[0]!.ToString();
91+
string s2 = arguments[0]! .ToString();
92+
string s3 = arguments[0]! .ToString();
10593
}
10694
}
10795
}
10896
";
10997

11098
DiagnosticResult[] expected =
11199
{
112-
Diagnostic().WithArguments(" not", "followed").WithLocation(7, 35),
113-
Diagnostic().WithArguments(" not", "followed").WithLocation(8, 35),
100+
Diagnostic().WithArguments(" not", "followed").WithLocation(7, 36),
101+
Diagnostic().WithArguments(" not", "followed").WithLocation(9, 36),
102+
Diagnostic().WithArguments(" not", "followed").WithLocation(10, 36),
103+
Diagnostic().WithArguments(" not", "followed").WithLocation(12, 36),
114104
};
115105

116106
await VerifyCSharpFixAsync(LanguageVersion.CSharp8, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,65 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.CSharp;
9+
using Microsoft.CodeAnalysis.Testing;
610
using StyleCop.Analyzers.Test.CSharp7.SpacingRules;
11+
using Xunit;
12+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
13+
StyleCop.Analyzers.SpacingRules.SA1019MemberAccessSymbolsMustBeSpacedCorrectly,
14+
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
715

816
public class SA1019CSharp8UnitTests : SA1019CSharp7UnitTests
917
{
18+
[Fact]
19+
[WorkItem(3052, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3052")]
20+
public async Task TestClosingSquareBracketFollowedByExclamationAsync()
21+
{
22+
var testCode = @"namespace TestNamespace
23+
{
24+
public class TestClass
25+
{
26+
public void TestMethod(object?[] arguments)
27+
{
28+
string s1 = arguments[0] !.ToString();
29+
string s2 = arguments[0]! .ToString();
30+
string s3 = arguments[0] ! .ToString();
31+
string s4 = arguments[0] !?.ToString();
32+
string s5 = arguments[0]! ?.ToString();
33+
string s6 = arguments[0] ! ?.ToString();
34+
}
35+
}
36+
}
37+
";
38+
39+
var fixedCode = @"namespace TestNamespace
40+
{
41+
public class TestClass
42+
{
43+
public void TestMethod(object?[] arguments)
44+
{
45+
string s1 = arguments[0] !.ToString();
46+
string s2 = arguments[0]!.ToString();
47+
string s3 = arguments[0] !.ToString();
48+
string s4 = arguments[0] !?.ToString();
49+
string s5 = arguments[0]!?.ToString();
50+
string s6 = arguments[0] !?.ToString();
51+
}
52+
}
53+
}
54+
";
55+
56+
DiagnosticResult[] expected =
57+
{
58+
Diagnostic().WithArguments(".", "preceded").WithLocation(8, 39),
59+
Diagnostic().WithArguments(".", "preceded").WithLocation(9, 40),
60+
Diagnostic().WithArguments("?", "preceded").WithLocation(11, 39),
61+
Diagnostic().WithArguments("?", "preceded").WithLocation(12, 40),
62+
};
63+
64+
await VerifyCSharpFixAsync(LanguageVersion.CSharp8, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
65+
}
1066
}
1167
}

0 commit comments

Comments
 (0)