Skip to content

Commit be637d7

Browse files
committed
Add line endings tests for readability rules
1 parent 773eaaa commit be637d7

20 files changed

+222
-128
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/ReadabilityRules/SA1142CSharp7UnitTests.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.ReadabilityRules
77
using System.Threading.Tasks;
88
using Microsoft.CodeAnalysis.Testing;
99
using StyleCop.Analyzers.ReadabilityRules;
10+
using StyleCop.Analyzers.Test.Helpers;
1011
using StyleCop.Analyzers.Test.ReadabilityRules;
1112
using Xunit;
1213
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
@@ -43,9 +44,12 @@ public int TestMethod((int nameA, int nameB) p1, (int, int) p2, (int, int nameC)
4344
/// <summary>
4445
/// Verify that tuple names referenced by their metadata name will produce the expected diagnostics.
4546
/// </summary>
47+
/// <param name="lineEnding">The line ending to use for the test code.</param>
4648
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
47-
[Fact]
48-
public async Task ValidateMetadataNameReferencesAsync()
49+
[Theory]
50+
[InlineData("\n")]
51+
[InlineData("\r\n")]
52+
public async Task ValidateMetadataNameReferencesAsync(string lineEnding)
4953
{
5054
var testCode = @"
5155
public class TestClass
@@ -60,7 +64,7 @@ public int TestMethod2((int nameA, (int subNameA, int subNameB) nameB) p1)
6064
return p1.[|Item1|] + p1.nameB.[|Item1|] + p1.[|Item2|].[|Item2|];
6165
}
6266
}
63-
";
67+
".ReplaceLineEndings(lineEnding);
6468

6569
var fixedCode = @"
6670
public class TestClass
@@ -75,7 +79,7 @@ public int TestMethod2((int nameA, (int subNameA, int subNameB) nameB) p1)
7579
return p1.nameA + p1.nameB.subNameA + p1.nameB.subNameB;
7680
}
7781
}
78-
";
82+
".ReplaceLineEndings(lineEnding);
7983

8084
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
8185
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1100UnitTests.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ namespace StyleCop.Analyzers.Test.ReadabilityRules
88
using System.Threading;
99
using System.Threading.Tasks;
1010
using Microsoft.CodeAnalysis.Testing;
11+
using StyleCop.Analyzers.Test.Helpers;
1112
using Xunit;
1213
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1314
StyleCop.Analyzers.ReadabilityRules.SA1100DoNotPrefixCallsWithBaseUnlessLocalImplementationExists,
1415
StyleCop.Analyzers.ReadabilityRules.SA1100CodeFixProvider>;
1516

1617
public class SA1100UnitTests
1718
{
18-
[Fact]
19-
public async Task TestChildClassUsesBaseButNoOverrideAsync()
19+
[Theory]
20+
[InlineData("\n")]
21+
[InlineData("\r\n")]
22+
public async Task TestChildClassUsesBaseButNoOverrideAsync(string lineEnding)
2023
{
2124
var testCode = @"
2225
public class Foo
@@ -31,11 +34,11 @@ public class FooChild : Foo
3134
{
3235
protected void Baz()
3336
{
34-
base.Bar();
37+
{|#0:base|}.Bar();
3538
}
36-
}";
39+
}".ReplaceLineEndings(lineEnding);
3740

38-
DiagnosticResult expected = Diagnostic().WithLocation(14, 9);
41+
DiagnosticResult expected = Diagnostic().WithLocation(0);
3942

4043
var fixedTest = @"
4144
public class Foo
@@ -52,7 +55,7 @@ protected void Baz()
5255
{
5356
this.Bar();
5457
}
55-
}";
58+
}".ReplaceLineEndings(lineEnding);
5659
await VerifyCSharpFixAsync(testCode, expected, fixedTest, CancellationToken.None).ConfigureAwait(false);
5760
}
5861

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1101UnitTests.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.Test.ReadabilityRules
66
using System.Threading;
77
using System.Threading.Tasks;
88
using Microsoft.CodeAnalysis.Testing;
9+
using StyleCop.Analyzers.Test.Helpers;
910
using Xunit;
1011
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1112
StyleCop.Analyzers.ReadabilityRules.SA1101PrefixLocalCallsWithThis,
@@ -262,20 +263,22 @@ public void InstanceMethodName(int ParameterName)
262263
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
263264
}
264265

265-
[Fact]
266-
public async Task TestPrefixLocalCallsWithThisWithGenericArgumentsAsync()
266+
[Theory]
267+
[InlineData("\n")]
268+
[InlineData("\r\n")]
269+
public async Task TestPrefixLocalCallsWithThisWithGenericArgumentsAsync(string lineEnding)
267270
{
268271
string testCode = @"public class Test_SA1101
269272
{
270273
public void Foo()
271274
{
272-
ConvertAll(42); // SA1101
275+
{|#0:ConvertAll|}(42); // SA1101
273276
this.ConvertAll(42); // no SA1101
274-
ConvertAll<int>(42); // SA1101
277+
{|#1:ConvertAll<int>|}(42); // SA1101
275278
this.ConvertAll<int>(42); // no SA1101
276279
}
277280
public void ConvertAll<T>(T value) { }
278-
}";
281+
}".ReplaceLineEndings(lineEnding);
279282
string fixedCode = @"public class Test_SA1101
280283
{
281284
public void Foo()
@@ -286,12 +289,12 @@ public void Foo()
286289
this.ConvertAll<int>(42); // no SA1101
287290
}
288291
public void ConvertAll<T>(T value) { }
289-
}";
292+
}".ReplaceLineEndings(lineEnding);
290293

291294
var expected = new[]
292295
{
293-
Diagnostic().WithLocation(5, 9),
294-
Diagnostic().WithLocation(7, 9),
296+
Diagnostic().WithLocation(0),
297+
Diagnostic().WithLocation(1),
295298
};
296299

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

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1103UnitTests.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.Test.ReadabilityRules
1010
using Microsoft.CodeAnalysis;
1111
using Microsoft.CodeAnalysis.Testing;
1212
using StyleCop.Analyzers.ReadabilityRules;
13+
using StyleCop.Analyzers.Test.Helpers;
1314
using Xunit;
1415
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1516
StyleCop.Analyzers.ReadabilityRules.SA110xQueryClauses,
@@ -23,9 +24,12 @@ public class SA1103UnitTests
2324
/// <summary>
2425
/// Verifies that a select query expression produces the expected results.
2526
/// </summary>
27+
/// <param name="lineEnding">The line ending to use in the test code.</param>
2628
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
27-
[Fact]
28-
public async Task TestSelectQueryExpressionAsync()
29+
[Theory]
30+
[InlineData("\n")]
31+
[InlineData("\r\n")]
32+
public async Task TestSelectQueryExpressionAsync(string lineEnding)
2933
{
3034
var testCode = @"namespace TestNamespace
3135
{
@@ -37,12 +41,12 @@ public class TestClass
3741
3842
public void TestMethod()
3943
{
40-
var x = from element in testArray where (element > 1)
44+
var x = {|#0:from|} element in testArray where (element > 1)
4145
select element;
4246
}
4347
}
4448
}
45-
";
49+
".ReplaceLineEndings(lineEnding);
4650

4751
var fixedTestCode = @"namespace TestNamespace
4852
{
@@ -58,11 +62,11 @@ public void TestMethod()
5862
}
5963
}
6064
}
61-
";
65+
".ReplaceLineEndings(lineEnding);
6266

6367
DiagnosticResult[] expectedDiagnostics =
6468
{
65-
Diagnostic(SA110xQueryClauses.SA1103Descriptor).WithLocation(11, 21),
69+
Diagnostic(SA110xQueryClauses.SA1103Descriptor).WithLocation(0),
6670
};
6771

6872
await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedTestCode, CancellationToken.None).ConfigureAwait(false);

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1110UnitTests.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,38 @@ namespace StyleCop.Analyzers.Test.ReadabilityRules
88
using System.Threading;
99
using System.Threading.Tasks;
1010
using Microsoft.CodeAnalysis.Testing;
11+
using StyleCop.Analyzers.Test.Helpers;
1112
using Xunit;
1213
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1314
StyleCop.Analyzers.ReadabilityRules.SA1110OpeningParenthesisMustBeOnDeclarationLine,
1415
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
1516

1617
public class SA1110UnitTests
1718
{
18-
[Fact]
19-
public async Task TestMethodDeclarationOpeningParenthesisInTheNextLineAsync()
19+
[Theory]
20+
[InlineData("\n")]
21+
[InlineData("\r\n")]
22+
public async Task TestMethodDeclarationOpeningParenthesisInTheNextLineAsync(string lineEnding)
2023
{
2124
var testCode = @"
2225
class Foo
2326
{
2427
public void Bar
25-
()
28+
{|#0:(|})
2629
{
2730
2831
}
29-
}";
32+
}".ReplaceLineEndings(lineEnding);
3033
var fixedCode = @"
3134
class Foo
3235
{
3336
public void Bar()
3437
{
3538
3639
}
37-
}";
40+
}".ReplaceLineEndings(lineEnding);
3841

39-
DiagnosticResult expected = Diagnostic().WithLocation(5, 21);
42+
DiagnosticResult expected = Diagnostic().WithLocation(0);
4043

4144
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
4245
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1111UnitTests.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
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.ReadabilityRules
75
{
86
using System.Threading;
97
using System.Threading.Tasks;
108
using Microsoft.CodeAnalysis.Testing;
9+
using StyleCop.Analyzers.Test.Helpers;
1110
using Xunit;
1211
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1312
StyleCop.Analyzers.ReadabilityRules.SA1111ClosingParenthesisMustBeOnLineOfLastParameter,
1413
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
1514

1615
public class SA1111UnitTests
1716
{
18-
[Fact]
19-
public async Task TestMethodDeclarationWithOneParameterClosingParanthesisOnTheNextLineAsTheLastParameterAsync()
17+
[Theory]
18+
[InlineData("\n")]
19+
[InlineData("\r\n")]
20+
public async Task TestMethodDeclarationWithOneParameterClosingParanthesisOnTheNextLineAsTheLastParameterAsync(string lineEnding)
2021
{
2122
var testCode = @"
2223
class Foo
2324
{
2425
public void Bar(string s
25-
)
26+
{|#0:)|}
2627
{
2728
2829
}
29-
}";
30+
}".ReplaceLineEndings(lineEnding);
3031
var fixedCode = @"
3132
class Foo
3233
{
3334
public void Bar(string s)
3435
{
3536
3637
}
37-
}";
38+
}".ReplaceLineEndings(lineEnding);
3839

39-
DiagnosticResult expected = Diagnostic().WithLocation(5, 1);
40+
DiagnosticResult expected = Diagnostic().WithLocation(0);
4041

4142
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
4243
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1112UnitTests.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
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.ReadabilityRules
75
{
86
using System.Threading;
97
using System.Threading.Tasks;
108
using Microsoft.CodeAnalysis.Testing;
9+
using StyleCop.Analyzers.Test.Helpers;
1110
using Xunit;
1211
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1312
StyleCop.Analyzers.ReadabilityRules.SA1112ClosingParenthesisMustBeOnLineOfOpeningParenthesis,
1413
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
1514

1615
public class SA1112UnitTests
1716
{
18-
[Fact]
19-
public async Task TestMethodWithNoParametersClosingParanthesisOnTheNextLineAsync()
17+
[Theory]
18+
[InlineData("\n")]
19+
[InlineData("\r\n")]
20+
public async Task TestMethodWithNoParametersClosingParanthesisOnTheNextLineAsync(string lineEnding)
2021
{
2122
var testCode = @"
2223
class Foo
2324
{
2425
public void Bar(
25-
)
26+
{|#0:)|}
2627
{
2728
2829
}
29-
}";
30+
}".ReplaceLineEndings(lineEnding);
3031
var fixedCode = @"
3132
class Foo
3233
{
3334
public void Bar()
3435
{
3536
3637
}
37-
}";
38+
}".ReplaceLineEndings(lineEnding);
3839

39-
DiagnosticResult expected = Diagnostic().WithLocation(5, 1);
40+
DiagnosticResult expected = Diagnostic().WithLocation(0);
4041

4142
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
4243
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1113UnitTests.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
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.ReadabilityRules
75
{
86
using System.Threading;
97
using System.Threading.Tasks;
108
using Microsoft.CodeAnalysis.Testing;
9+
using StyleCop.Analyzers.Test.Helpers;
1110
using Xunit;
1211
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1312
StyleCop.Analyzers.ReadabilityRules.SA1113CommaMustBeOnSameLineAsPreviousParameter,
1413
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;
1514

1615
public class SA1113UnitTests
1716
{
18-
[Fact]
19-
public async Task TestMethodDeclarationWithTwoParametersCommaPlacedAtTheSameLineAsTheSecondParameterAsync()
17+
[Theory]
18+
[InlineData("\n")]
19+
[InlineData("\r\n")]
20+
public async Task TestMethodDeclarationWithTwoParametersCommaPlacedAtTheSameLineAsTheSecondParameterAsync(string lineEnding)
2021
{
2122
var testCode = @"public class Foo
2223
{
2324
public void Bar(string s
24-
, int i)
25+
{|#0:,|} int i)
2526
{
2627
}
27-
}";
28+
}".ReplaceLineEndings(lineEnding);
2829
var fixedCode = @"public class Foo
2930
{
3031
public void Bar(string s,
3132
int i)
3233
{
3334
}
34-
}";
35+
}".ReplaceLineEndings(lineEnding);
3536

36-
DiagnosticResult expected = Diagnostic().WithLocation(4, 21);
37+
DiagnosticResult expected = Diagnostic().WithLocation(0);
3738

3839
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
3940
}

0 commit comments

Comments
 (0)