Skip to content

Commit b174ec2

Browse files
committed
Add line endings tests for spacing rules
1 parent ad91531 commit b174ec2

32 files changed

+319
-234
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/NumberSignSpacingTestBase.cs

Lines changed: 24 additions & 23 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.SpacingRules
75
{
86
using System.Collections.Generic;
@@ -12,6 +10,7 @@ namespace StyleCop.Analyzers.Test.SpacingRules
1210
using Microsoft.CodeAnalysis.CodeFixes;
1311
using Microsoft.CodeAnalysis.Diagnostics;
1412
using Microsoft.CodeAnalysis.Testing;
13+
using StyleCop.Analyzers.Test.Helpers;
1514
using StyleCop.Analyzers.Test.Verifiers;
1615
using Xunit;
1716

@@ -32,35 +31,37 @@ protected abstract CodeFixProvider CodeFix
3231
get;
3332
}
3433

35-
[Fact]
36-
public async Task TestPrefixUnaryOperatorAtEndOfLineAsync()
34+
[Theory]
35+
[InlineData("\n")]
36+
[InlineData("\r\n")]
37+
public async Task TestPrefixUnaryOperatorAtEndOfLineAsync(string lineEnding)
3738
{
38-
string testCode = @"namespace Namespace
39-
{
39+
string testCode = $@"namespace Namespace
40+
{{
4041
class Type
41-
{
42+
{{
4243
void Foo()
43-
{
44-
int x = " + this.Sign + @"
44+
{{
45+
int x = {{|#0:{this.Sign}|}}
4546
3;
46-
}
47-
}
48-
}
49-
";
47+
}}
48+
}}
49+
}}
50+
".ReplaceLineEndings(lineEnding);
5051

51-
string fixedTest = @"namespace Namespace
52-
{
52+
string fixedTest = $@"namespace Namespace
53+
{{
5354
class Type
54-
{
55+
{{
5556
void Foo()
56-
{
57-
int x = " + this.Sign + @"3;
58-
}
59-
}
60-
}
61-
";
57+
{{
58+
int x = {this.Sign}3;
59+
}}
60+
}}
61+
}}
62+
".ReplaceLineEndings(lineEnding);
6263

63-
DiagnosticResult expected = this.Diagnostic().WithArguments(" not", "followed").WithLocation(7, 21);
64+
DiagnosticResult expected = this.Diagnostic().WithArguments(" not", "followed").WithLocation(0);
6465

6566
await this.VerifyCSharpFixAsync(testCode, expected, fixedTest, CancellationToken.None).ConfigureAwait(false);
6667
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1000UnitTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
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.SpacingRules
75
{
86
using System.Threading;
97
using System.Threading.Tasks;
108
using Microsoft.CodeAnalysis.CSharp;
119
using Microsoft.CodeAnalysis.Testing;
1210
using StyleCop.Analyzers.SpacingRules;
11+
using StyleCop.Analyzers.Test.Helpers;
1312
using Xunit;
1413
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1514
StyleCop.Analyzers.SpacingRules.SA1000KeywordsMustBeSpacedCorrectly,
@@ -903,8 +902,10 @@ void MethodName()
903902
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
904903
}
905904

906-
[Fact]
907-
public async Task TestTrailingCommentAsync()
905+
[Theory]
906+
[InlineData("\n")]
907+
[InlineData("\r\n")]
908+
public async Task TestTrailingCommentAsync(string lineEnding)
908909
{
909910
string testCode = @"
910911
class ClassName
@@ -916,7 +917,7 @@ void MethodName()
916917
}
917918
}
918919
}
919-
";
920+
".ReplaceLineEndings(lineEnding);
920921
string fixedCode = @"
921922
class ClassName
922923
{
@@ -927,7 +928,7 @@ void MethodName()
927928
}
928929
}
929930
}
930-
";
931+
".ReplaceLineEndings(lineEnding);
931932

932933
var expected = Diagnostic().WithArguments("if", string.Empty, "followed").WithLocation(6, 9);
933934
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1001UnitTests.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
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.SpacingRules
75
{
86
using System;
97
using System.Threading;
108
using System.Threading.Tasks;
119
using Microsoft.CodeAnalysis.Testing;
1210
using StyleCop.Analyzers.SpacingRules;
11+
using StyleCop.Analyzers.Test.Helpers;
1312
using Xunit;
1413
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1514
StyleCop.Analyzers.SpacingRules.SA1001CommasMustBeSpacedCorrectly,
@@ -302,9 +301,11 @@ void MethodName()
302301
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
303302
}
304303

305-
[Fact]
304+
[Theory]
305+
[InlineData("\n")]
306+
[InlineData("\r\n")]
306307
[WorkItem(2468, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2468")]
307-
public async Task TestCodeFixCommaPlacementAsync()
308+
public async Task TestCodeFixCommaPlacementAsync(string lineEnding)
308309
{
309310
var testCode = @"using System;
310311
@@ -315,12 +316,12 @@ public void TestMethod()
315316
var test = (new[]
316317
{
317318
new Tuple<int, int>(1, 2)
318-
,new Tuple<int, int>(3, 4)
319-
,new Tuple<int, int>(5, 6)
319+
{|#0:,|}new Tuple<int, int>(3, 4)
320+
{|#1:,|}new Tuple<int, int>(5, 6)
320321
}).ToString();
321322
}
322323
}
323-
";
324+
".ReplaceLineEndings(lineEnding);
324325

325326
var fixedCode = @"using System;
326327
@@ -336,14 +337,14 @@ public void TestMethod()
336337
}).ToString();
337338
}
338339
}
339-
";
340+
".ReplaceLineEndings(lineEnding);
340341

341342
DiagnosticResult[] expected =
342343
{
343-
Diagnostic().WithArguments(" not", "preceded").WithLocation(10, 12),
344-
Diagnostic().WithArguments(string.Empty, "followed").WithLocation(10, 12),
345-
Diagnostic().WithArguments(" not", "preceded").WithLocation(11, 12),
346-
Diagnostic().WithArguments(string.Empty, "followed").WithLocation(11, 12),
344+
Diagnostic().WithArguments(" not", "preceded").WithLocation(0),
345+
Diagnostic().WithArguments(string.Empty, "followed").WithLocation(0),
346+
Diagnostic().WithArguments(" not", "preceded").WithLocation(1),
347+
Diagnostic().WithArguments(string.Empty, "followed").WithLocation(1),
347348
};
348349

349350
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1002UnitTests.cs

Lines changed: 10 additions & 9 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.SpacingRules
75
{
86
using System.Threading;
97
using System.Threading.Tasks;
108
using Microsoft.CodeAnalysis.Testing;
119
using StyleCop.Analyzers.SpacingRules;
10+
using StyleCop.Analyzers.Test.Helpers;
1211
using Xunit;
1312
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1413
StyleCop.Analyzers.SpacingRules.SA1002SemicolonsMustBeSpacedCorrectly,
@@ -20,20 +19,22 @@ namespace StyleCop.Analyzers.Test.SpacingRules
2019
/// </summary>
2120
public class SA1002UnitTests
2221
{
23-
[Fact]
24-
public async Task TestForLoopAsync()
22+
[Theory]
23+
[InlineData("\n")]
24+
[InlineData("\r\n")]
25+
public async Task TestForLoopAsync(string lineEnding)
2526
{
2627
string testCode = @"
2728
class ClassName
2829
{
2930
void MethodName()
3031
{
31-
for (int x =0;x<10;x++)
32+
for (int x =0{|#0:;|}x<10{|#1:;|}x++)
3233
{
3334
}
3435
}
3536
}
36-
";
37+
".ReplaceLineEndings(lineEnding);
3738
string fixedCode = @"
3839
class ClassName
3940
{
@@ -44,12 +45,12 @@ void MethodName()
4445
}
4546
}
4647
}
47-
";
48+
".ReplaceLineEndings(lineEnding);
4849

4950
DiagnosticResult[] expected =
5051
{
51-
Diagnostic().WithArguments(string.Empty, "followed").WithLocation(6, 22),
52-
Diagnostic().WithArguments(string.Empty, "followed").WithLocation(6, 27),
52+
Diagnostic().WithArguments(string.Empty, "followed").WithLocation(0),
53+
Diagnostic().WithArguments(string.Empty, "followed").WithLocation(1),
5354
};
5455

5556
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1003UnitTests.cs

Lines changed: 8 additions & 6 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.SpacingRules
75
{
86
using System.Threading;
97
using System.Threading.Tasks;
108
using Microsoft.CodeAnalysis.Testing;
119
using StyleCop.Analyzers.SpacingRules;
10+
using StyleCop.Analyzers.Test.Helpers;
1211
using Xunit;
1312

1413
using static StyleCop.Analyzers.SpacingRules.SA1003SymbolsMustBeSpacedCorrectly;
@@ -725,9 +724,12 @@ public async Task TestValidAutoPropertyInitializersAsync()
725724
/// <summary>
726725
/// Verifies that invalid auto property initializers produce the correct diagnostics and code fixes.
727726
/// </summary>
727+
/// <param name="lineEnding">The line ending to use in the test code.</param>
728728
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
729-
[Fact]
730-
public async Task TestInvalidAutoPropertyInitializersAsync()
729+
[Theory]
730+
[InlineData("\n")]
731+
[InlineData("\r\n")]
732+
public async Task TestInvalidAutoPropertyInitializersAsync(string lineEnding)
731733
{
732734
var testCode = @"public class Foo
733735
{
@@ -737,7 +739,7 @@ public async Task TestInvalidAutoPropertyInitializersAsync()
737739
738740
public int Qux { get; }=3;
739741
}
740-
";
742+
".ReplaceLineEndings(lineEnding);
741743

742744
var fixedTestCode = @"public class Foo
743745
{
@@ -747,7 +749,7 @@ public async Task TestInvalidAutoPropertyInitializersAsync()
747749
748750
public int Qux { get; } = 3;
749751
}
750-
";
752+
".ReplaceLineEndings(lineEnding);
751753

752754
DiagnosticResult[] expected =
753755
{

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1004UnitTests.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.Test.SpacingRules
1010
using Microsoft.CodeAnalysis.Testing;
1111
using StyleCop.Analyzers.Lightup;
1212
using StyleCop.Analyzers.SpacingRules;
13+
using StyleCop.Analyzers.Test.Helpers;
1314
using Xunit;
1415
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1516
StyleCop.Analyzers.SpacingRules.SA1004DocumentationLinesMustBeginWithSingleSpace,
@@ -54,8 +55,10 @@ private void Method1(int x, int y)
5455
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
5556
}
5657

57-
[Fact]
58-
public async Task TestRuleExampleAsync()
58+
[Theory]
59+
[InlineData("\n")]
60+
[InlineData("\r\n")]
61+
public async Task TestRuleExampleAsync(string lineEnding)
5962
{
6063
string testCode = @"
6164
public class TypeName
@@ -69,7 +72,7 @@ private void Method1(int x, int y)
6972
{
7073
}
7174
}
72-
";
75+
".ReplaceLineEndings(lineEnding);
7376

7477
string fixedCode = @"
7578
public class TypeName
@@ -83,7 +86,7 @@ private void Method1(int x, int y)
8386
{
8487
}
8588
}
86-
";
89+
".ReplaceLineEndings(lineEnding);
8790

8891
DiagnosticResult[] expected =
8992
{

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1005UnitTests.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
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.SpacingRules
75
{
86
using System.Threading;
97
using System.Threading.Tasks;
108
using Microsoft.CodeAnalysis;
119
using Microsoft.CodeAnalysis.Testing;
1210
using StyleCop.Analyzers.SpacingRules;
11+
using StyleCop.Analyzers.Test.Helpers;
1312
using Xunit;
1413
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1514
StyleCop.Analyzers.SpacingRules.SA1005SingleLineCommentsMustBeginWithSingleSpace,
@@ -44,9 +43,12 @@ public class Bar
4443
/// <summary>
4544
/// Verify that a single line comment without a leading space gets detected and fixed properly.
4645
/// </summary>
46+
/// <param name="lineEnding">The line ending to use in the test code.</param>
4747
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
48-
[Fact]
49-
public async Task TestNoLeadingSpaceAsync()
48+
[Theory]
49+
[InlineData("\n")]
50+
[InlineData("\r\n")]
51+
public async Task TestNoLeadingSpaceAsync(string lineEnding)
5052
{
5153
var testCode = @"public class Foo
5254
{
@@ -55,7 +57,7 @@ public class Bar
5557
{
5658
}
5759
}
58-
";
60+
".ReplaceLineEndings(lineEnding);
5961

6062
var fixedTestCode = @"public class Foo
6163
{
@@ -64,7 +66,7 @@ public class Bar
6466
{
6567
}
6668
}
67-
";
69+
".ReplaceLineEndings(lineEnding);
6870

6971
var expected = Diagnostic().WithLocation(3, 5);
7072

0 commit comments

Comments
 (0)