Skip to content

Commit 1142c4a

Browse files
committed
Update tests for target-typed conditional expressions
Closes #3974
1 parent ff14d11 commit 1142c4a

File tree

4 files changed

+115
-6
lines changed

4 files changed

+115
-6
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/MaintainabilityRules/SA1119CSharp9UnitTests.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
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.CSharp9.MaintainabilityRules
75
{
86
using System.Threading;
@@ -152,5 +150,38 @@ public object TestMethod(Foo n, int a)
152150
TestCode = testCode,
153151
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
154152
}
153+
154+
[Fact]
155+
[WorkItem(3974, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3974")]
156+
public async Task TestConditionalExpressionWithTargetTypedNewAsync()
157+
{
158+
const string testCode = @"public class TestClass
159+
{
160+
public object GetValue(bool flag)
161+
{
162+
return {|#0:{|#1:(|}flag ? null : new(){|#2:)|}|};
163+
}
164+
}";
165+
166+
const string fixedCode = @"public class TestClass
167+
{
168+
public object GetValue(bool flag)
169+
{
170+
return flag ? null : new();
171+
}
172+
}";
173+
174+
await new CSharpTest()
175+
{
176+
TestCode = testCode,
177+
ExpectedDiagnostics =
178+
{
179+
Diagnostic(DiagnosticId).WithLocation(0),
180+
Diagnostic(ParenthesesDiagnosticId).WithLocation(1),
181+
Diagnostic(ParenthesesDiagnosticId).WithLocation(2),
182+
},
183+
FixedCode = fixedCode,
184+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
185+
}
155186
}
156187
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1000CSharp9UnitTests.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
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.CSharp9.SpacingRules
75
{
86
using System.Threading.Tasks;
@@ -24,6 +22,15 @@ public async Task TestTargetTypedNewAsync()
2422
await this.TestKeywordStatementAsync(statementWithoutSpace, DiagnosticResult.EmptyDiagnosticResults, statementWithoutSpace).ConfigureAwait(false);
2523
}
2624

25+
[Fact]
26+
[WorkItem(3974, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3974")]
27+
public async Task TestTargetTypedNewInConditionalExpressionAsync()
28+
{
29+
string statement = "bool flag = true; object value = flag ? null : new();";
30+
31+
await this.TestKeywordStatementAsync(statement, DiagnosticResult.EmptyDiagnosticResults, statement).ConfigureAwait(false);
32+
}
33+
2734
[Fact]
2835
[WorkItem(3508, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3508")]
2936
public async Task TestIsBeforeRelationalPatternAsync()

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1003CSharp9UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp9.SpacingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
69
using StyleCop.Analyzers.Test.CSharp8.SpacingRules;
10+
using Xunit;
11+
using static StyleCop.Analyzers.SpacingRules.SA1003SymbolsMustBeSpacedCorrectly;
12+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
13+
StyleCop.Analyzers.SpacingRules.SA1003SymbolsMustBeSpacedCorrectly,
14+
StyleCop.Analyzers.SpacingRules.SA1003CodeFixProvider>;
715

816
public partial class SA1003CSharp9UnitTests : SA1003CSharp8UnitTests
917
{
18+
[Fact]
19+
[WorkItem(3974, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3974")]
20+
public async Task TestTargetTypedConditionalExpressionSpacingAsync()
21+
{
22+
var testCode = @"
23+
class TestClass
24+
{
25+
void M(bool flag)
26+
{
27+
object value = flag{|#0:?|} null{|#1::|}new();
28+
}
29+
}";
30+
31+
var fixedCode = @"
32+
class TestClass
33+
{
34+
void M(bool flag)
35+
{
36+
object value = flag ? null : new();
37+
}
38+
}";
39+
40+
DiagnosticResult[] expected =
41+
{
42+
Diagnostic(DescriptorPrecededByWhitespace).WithArguments("?").WithLocation(0),
43+
Diagnostic(DescriptorPrecededByWhitespace).WithArguments(":").WithLocation(1),
44+
Diagnostic(DescriptorFollowedByWhitespace).WithArguments(":").WithLocation(1),
45+
};
46+
47+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
48+
}
1049
}
1150
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1024CSharp9UnitTests.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
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.CSharp9.SpacingRules
75
{
86
using System.Threading;
@@ -53,5 +51,39 @@ public record MyQuery3() : BaseQuery<object>;";
5351
FixedCode = fixedCode,
5452
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
5553
}
54+
55+
[Fact]
56+
[WorkItem(3974, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3974")]
57+
public async Task TestConditionalExpressionWithTargetTypedNewAsync()
58+
{
59+
const string testCode = @"
60+
class TestClass
61+
{
62+
void M(bool flag)
63+
{
64+
object value = flag ? null{|#0::|}new();
65+
}
66+
}";
67+
68+
const string fixedCode = @"
69+
class TestClass
70+
{
71+
void M(bool flag)
72+
{
73+
object value = flag ? null : new();
74+
}
75+
}";
76+
77+
await new CSharpTest()
78+
{
79+
ExpectedDiagnostics =
80+
{
81+
Diagnostic(DescriptorPreceded).WithLocation(0),
82+
Diagnostic(DescriptorFollowed).WithLocation(0),
83+
},
84+
TestCode = testCode,
85+
FixedCode = fixedCode,
86+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
87+
}
5688
}
5789
}

0 commit comments

Comments
 (0)