Skip to content

Commit 898fa9f

Browse files
committed
Add tests for spacing around closing square brackets in tuple types and expressions
1 parent 245f725 commit 898fa9f

3 files changed

Lines changed: 161 additions & 2 deletions

File tree

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

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

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

816
public class SA1001CSharp7UnitTests : SA1001UnitTests
917
{
18+
/// <summary>
19+
/// Verifies spacing around a <c>]</c> character in tuple types and expressions.
20+
/// </summary>
21+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
22+
/// <seealso cref="SA1009CSharp7UnitTests.TestBracketsInTupleTypesNotFollowedBySpaceAsync"/>
23+
/// <seealso cref="SA1011CSharp7UnitTests.TestBracketsInTupleTypesNotFollowedBySpaceAsync"/>
24+
[Fact]
25+
public async Task TestBracketsInTupleTypesNotFollowedBySpaceAsync()
26+
{
27+
const string testCode = @"using System;
28+
29+
public class Foo
30+
{
31+
public (int[] , int[] ) TestMethod((int[] , int[] ) a)
32+
{
33+
(int[] , int[] ) ints = (new int[][] { new[] { 3 } }[0] , new int[][] { new[] { 3 } }[0] );
34+
return ints;
35+
}
36+
}";
37+
const string fixedCode = @"using System;
38+
39+
public class Foo
40+
{
41+
public (int[], int[] ) TestMethod((int[], int[] ) a)
42+
{
43+
(int[], int[] ) ints = (new int[][] { new[] { 3 } }[0], new int[][] { new[] { 3 } }[0] );
44+
return ints;
45+
}
46+
}";
47+
48+
DiagnosticResult[] expected =
49+
{
50+
this.CSharpDiagnostic().WithLocation(5, 19).WithArguments(" not", "preceded"),
51+
this.CSharpDiagnostic().WithLocation(5, 47).WithArguments(" not", "preceded"),
52+
this.CSharpDiagnostic().WithLocation(7, 16).WithArguments(" not", "preceded"),
53+
this.CSharpDiagnostic().WithLocation(7, 65).WithArguments(" not", "preceded"),
54+
};
55+
56+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
57+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
58+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
59+
}
60+
61+
protected override Solution CreateSolution(ProjectId projectId, string language)
62+
{
63+
Solution solution = base.CreateSolution(projectId, language);
64+
Assembly systemRuntime = AppDomain.CurrentDomain.GetAssemblies().Single(x => x.GetName().Name == "System.Runtime");
65+
return solution
66+
.AddMetadataReference(projectId, MetadataReference.CreateFromFile(systemRuntime.Location))
67+
.AddMetadataReference(projectId, MetadataReference.CreateFromFile(typeof(ValueTuple).Assembly.Location));
68+
}
1069
}
1170
}

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

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,68 @@
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 SA1009CSharp7UnitTests : SA1009UnitTests
917
{
18+
/// <summary>
19+
/// Verifies spacing around a <c>]</c> character in tuple types and expressions.
20+
/// </summary>
21+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
22+
/// <seealso cref="SA1001CSharp7UnitTests.TestBracketsInTupleTypesNotFollowedBySpaceAsync"/>
23+
/// <seealso cref="SA1011CSharp7UnitTests.TestBracketsInTupleTypesNotFollowedBySpaceAsync"/>
24+
[Fact]
25+
public async Task TestBracketsInTupleTypesNotFollowedBySpaceAsync()
26+
{
27+
const string testCode = @"using System;
28+
29+
public class Foo
30+
{
31+
public (int[] , int[] ) TestMethod((int[] , int[] ) a)
32+
{
33+
(int[] , int[] ) ints = (new int[][] { new[] { 3 } }[0] , new int[][] { new[] { 3 } }[0] );
34+
return ints;
35+
}
36+
}";
37+
const string fixedCode = @"using System;
38+
39+
public class Foo
40+
{
41+
public (int[] , int[]) TestMethod((int[] , int[]) a)
42+
{
43+
(int[] , int[]) ints = (new int[][] { new[] { 3 } }[0] , new int[][] { new[] { 3 } }[0]);
44+
return ints;
45+
}
46+
}";
47+
48+
DiagnosticResult[] expected =
49+
{
50+
this.CSharpDiagnostic().WithLocation(5, 27).WithArguments(" not", "preceded"),
51+
this.CSharpDiagnostic().WithLocation(5, 55).WithArguments(" not", "preceded"),
52+
this.CSharpDiagnostic().WithLocation(7, 24).WithArguments(" not", "preceded"),
53+
this.CSharpDiagnostic().WithLocation(7, 98).WithArguments(" not", "preceded"),
54+
};
55+
56+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
57+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
58+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
59+
}
60+
61+
protected override Solution CreateSolution(ProjectId projectId, string language)
62+
{
63+
Solution solution = base.CreateSolution(projectId, language);
64+
Assembly systemRuntime = AppDomain.CurrentDomain.GetAssemblies().Single(x => x.GetName().Name == "System.Runtime");
65+
return solution
66+
.AddMetadataReference(projectId, MetadataReference.CreateFromFile(systemRuntime.Location))
67+
.AddMetadataReference(projectId, MetadataReference.CreateFromFile(typeof(ValueTuple).Assembly.Location));
68+
}
1069
}
1170
}

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,50 @@
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 SA1011CSharp7UnitTests : SA1011UnitTests
917
{
18+
/// <summary>
19+
/// Verifies spacing around a <c>]</c> character in tuple types and expressions.
20+
/// </summary>
21+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
22+
/// <seealso cref="SA1001CSharp7UnitTests.TestBracketsInTupleTypesNotFollowedBySpaceAsync"/>
23+
/// <seealso cref="SA1009CSharp7UnitTests.TestBracketsInTupleTypesNotFollowedBySpaceAsync"/>
24+
[Fact]
25+
public async Task TestBracketsInTupleTypesNotFollowedBySpaceAsync()
26+
{
27+
// No diagnostics reported because the offending tokens are followed by comma (SA1001) or a closing
28+
// parenthesis (SA1009)
29+
const string testCode = @"using System;
30+
31+
public class Foo
32+
{
33+
public (int[] , int[] ) TestMethod((int[] , int[] ) a)
34+
{
35+
(int[] , int[] ) ints = (new int[][] { new[] { 3 } }[0] , new int[][] { new[] { 3 } }[0] );
36+
return ints;
37+
}
38+
}";
39+
40+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
41+
}
42+
43+
protected override Solution CreateSolution(ProjectId projectId, string language)
44+
{
45+
Solution solution = base.CreateSolution(projectId, language);
46+
Assembly systemRuntime = AppDomain.CurrentDomain.GetAssemblies().Single(x => x.GetName().Name == "System.Runtime");
47+
return solution
48+
.AddMetadataReference(projectId, MetadataReference.CreateFromFile(systemRuntime.Location))
49+
.AddMetadataReference(projectId, MetadataReference.CreateFromFile(typeof(ValueTuple).Assembly.Location));
50+
}
1051
}
1152
}

0 commit comments

Comments
 (0)