Skip to content

Commit 526c5bf

Browse files
committed
Add tests for SA1018 with nullable tuple types
1 parent 898fa9f commit 526c5bf

1 file changed

Lines changed: 110 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1018CSharp7UnitTests.cs

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,118 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp7.SpacingRules
55
{
6-
using Test.SpacingRules;
6+
using System;
7+
using System.Linq;
8+
using System.Reflection;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
using Microsoft.CodeAnalysis;
12+
using StyleCop.Analyzers.Test.SpacingRules;
13+
using TestHelper;
14+
using Xunit;
715

816
public class SA1018CSharp7UnitTests : SA1018UnitTests
917
{
18+
/// <summary>
19+
/// Verifies that nullable tuple types with different kinds of spacing will report.
20+
/// </summary>
21+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
22+
[Fact]
23+
public async Task TestNullableTupleTypeSpacingAsync()
24+
{
25+
var testCode = @"namespace TestNamespace
26+
{
27+
public class TestClass
28+
{
29+
public void TestMethod()
30+
{
31+
(int, int) ? v1;
32+
(int, int) /* test */ ? v2;
33+
(int, int)
34+
? v3;
35+
(int, int)
36+
/* test */
37+
? v4;
38+
(int, int)
39+
// test
40+
? v5;
41+
42+
(int, int)
43+
#if TEST
44+
? v6a;
45+
#else
46+
? v6b;
47+
#endif
48+
}
49+
}
50+
}
51+
";
52+
53+
var fixedTestCode = @"namespace TestNamespace
54+
{
55+
public class TestClass
56+
{
57+
public void TestMethod()
58+
{
59+
(int, int)? v1;
60+
(int, int)/* test */? v2;
61+
(int, int)? v3;
62+
(int, int)/* test */? v4;
63+
(int, int)
64+
// test
65+
? v5;
66+
67+
(int, int)
68+
#if TEST
69+
? v6a;
70+
#else
71+
? v6b;
72+
#endif
73+
}
74+
}
75+
}
76+
";
77+
78+
DiagnosticResult[] expectedResults =
79+
{
80+
// v1
81+
this.CSharpDiagnostic().WithLocation(7, 24),
82+
83+
// v2
84+
this.CSharpDiagnostic().WithLocation(8, 35),
85+
86+
// v3
87+
this.CSharpDiagnostic().WithLocation(10, 1),
88+
89+
// v4
90+
this.CSharpDiagnostic().WithLocation(13, 1),
91+
92+
// v5
93+
this.CSharpDiagnostic().WithLocation(16, 1),
94+
95+
// v6
96+
this.CSharpDiagnostic().WithLocation(22, 1),
97+
};
98+
99+
// The fixed test code will have diagnostics, because not all cases can be code fixed automatically.
100+
DiagnosticResult[] fixedExpectedResults =
101+
{
102+
this.CSharpDiagnostic().WithLocation(13, 1),
103+
this.CSharpDiagnostic().WithLocation(19, 1),
104+
};
105+
106+
await this.VerifyCSharpDiagnosticAsync(testCode, expectedResults, CancellationToken.None).ConfigureAwait(false);
107+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, fixedExpectedResults, CancellationToken.None).ConfigureAwait(false);
108+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode, numberOfFixAllIterations: 2, cancellationToken: CancellationToken.None).ConfigureAwait(false);
109+
}
110+
111+
protected override Solution CreateSolution(ProjectId projectId, string language)
112+
{
113+
Solution solution = base.CreateSolution(projectId, language);
114+
Assembly systemRuntime = AppDomain.CurrentDomain.GetAssemblies().Single(x => x.GetName().Name == "System.Runtime");
115+
return solution
116+
.AddMetadataReference(projectId, MetadataReference.CreateFromFile(systemRuntime.Location))
117+
.AddMetadataReference(projectId, MetadataReference.CreateFromFile(typeof(ValueTuple).Assembly.Location));
118+
}
10119
}
11120
}

0 commit comments

Comments
 (0)