Skip to content

Commit ff6daeb

Browse files
committed
Add line endings tests for maintainability rules
1 parent 7806f54 commit ff6daeb

File tree

10 files changed

+118
-61
lines changed

10 files changed

+118
-61
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1119UnitTests.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace StyleCop.Analyzers.Test.MaintainabilityRules
99
using System.Threading.Tasks;
1010
using Microsoft.CodeAnalysis.Testing;
1111
using StyleCop.Analyzers.MaintainabilityRules;
12+
using StyleCop.Analyzers.Test.Helpers;
1213
using Xunit;
1314
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1415
StyleCop.Analyzers.MaintainabilityRules.SA1119StatementMustNotUseUnnecessaryParenthesis,
@@ -32,22 +33,24 @@ public void Bar()
3233
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
3334
}
3435

35-
[Fact]
36-
public async Task TestLiteralParenthesisAsync()
36+
[Theory]
37+
[InlineData("\n")]
38+
[InlineData("\r\n")]
39+
public async Task TestLiteralParenthesisAsync(string lineEnding)
3740
{
3841
var testCode = @"public class Foo
3942
{
4043
public void Bar()
4144
{
42-
int x = (1);
45+
int x = {|#0:{|#1:(|}1{|#2:)|}|};
4346
}
44-
}";
47+
}".ReplaceLineEndings(lineEnding);
4548

4649
DiagnosticResult[] expected =
4750
{
48-
Diagnostic(DiagnosticId).WithSpan(5, 17, 5, 20),
49-
Diagnostic(ParenthesesDiagnosticId).WithLocation(5, 17),
50-
Diagnostic(ParenthesesDiagnosticId).WithLocation(5, 19),
51+
Diagnostic(DiagnosticId).WithLocation(0),
52+
Diagnostic(ParenthesesDiagnosticId).WithLocation(1),
53+
Diagnostic(ParenthesesDiagnosticId).WithLocation(2),
5154
};
5255

5356
var fixedCode = @"public class Foo
@@ -56,7 +59,8 @@ public void Bar()
5659
{
5760
int x = 1;
5861
}
59-
}";
62+
}".ReplaceLineEndings(lineEnding);
63+
6064
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
6165
}
6266

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1400UnitTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,33 @@ public async Task TestMethodDeclarationAsync()
183183
await this.TestNestedDeclarationAsync("private", "MemberName", "void MemberName", " ( int\n parameter\n ) { }").ConfigureAwait(false);
184184
}
185185

186+
[Theory]
187+
[InlineData("\n")]
188+
[InlineData("\r\n")]
189+
public async Task TestMethodDeclarationWithLineEndingAsync(string lineEnding)
190+
{
191+
const string keywordLine = "void {|#0:MemberName|}";
192+
const string linesAfter = " ( int\n parameter\n ) { }";
193+
194+
var testCode = @$"using System;
195+
internal class OuterTypeName
196+
{{
197+
{Tab} {keywordLine}
198+
{linesAfter}
199+
}}".ReplaceLineEndings(lineEnding);
200+
201+
DiagnosticResult expected = Diagnostic().WithArguments("MemberName").WithLocation(0);
202+
203+
var fixedTestCode = @$"using System;
204+
internal class OuterTypeName
205+
{{
206+
{Tab} private {keywordLine}
207+
{linesAfter}
208+
}}".ReplaceLineEndings(lineEnding);
209+
210+
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
211+
}
212+
186213
[Fact]
187214
public async Task TestMethodDeclarationWithAttributesAsync()
188215
{

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1402ForBlockDeclarationUnitTestsBase.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace StyleCop.Analyzers.Test.MaintainabilityRules
1212
using Microsoft.CodeAnalysis.Diagnostics;
1313
using Microsoft.CodeAnalysis.Testing;
1414
using StyleCop.Analyzers.MaintainabilityRules;
15+
using StyleCop.Analyzers.Test.Helpers;
1516
using Xunit;
1617

1718
public abstract class SA1402ForBlockDeclarationUnitTestsBase : FileMayOnlyContainTestBase
@@ -28,31 +29,33 @@ public abstract class SA1402ForBlockDeclarationUnitTestsBase : FileMayOnlyContai
2829

2930
protected abstract bool IsConfiguredAsTopLevelTypeByDefault { get; }
3031

31-
[Fact]
32-
public async Task TestTwoGenericElementsAsync()
32+
[Theory]
33+
[InlineData("\n")]
34+
[InlineData("\r\n")]
35+
public async Task TestTwoGenericElementsAsync(string lineEnding)
3336
{
3437
var testCode = @"%1 Foo<T1>
3538
{
3639
}
37-
%1 Bar<T2, T3>
40+
%1 {|#0:Bar|}<T2, T3>
3841
{
39-
}";
42+
}".ReplaceLineEndings(lineEnding);
4043

4144
var fixedCode = new[]
4245
{
4346
("/0/Test0.cs", @"%1 Foo<T1>
4447
{
4548
}
46-
"),
49+
".ReplaceLineEndings(lineEnding)),
4750
("Bar{T2,T3}.cs", @"%1 Bar<T2, T3>
4851
{
49-
}"),
52+
}".ReplaceLineEndings(lineEnding)),
5053
};
5154

5255
testCode = testCode.Replace("%1", this.Keyword);
5356
fixedCode = fixedCode.Select(c => (c.Item1, c.Item2.Replace("%1", this.Keyword))).ToArray();
5457

55-
DiagnosticResult expected = this.Diagnostic().WithLocation(4, this.Keyword.Length + 2);
58+
DiagnosticResult expected = this.Diagnostic().WithLocation(0);
5659
await this.VerifyCSharpFixAsync(testCode, this.GetSettings(), expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
5760
}
5861

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1404UnitTests.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace StyleCop.Analyzers.Test.MaintainabilityRules
99
using System.Threading.Tasks;
1010
using Microsoft.CodeAnalysis.Testing;
1111
using StyleCop.Analyzers.MaintainabilityRules;
12+
using StyleCop.Analyzers.Test.Helpers;
1213
using Xunit;
1314
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1415
StyleCop.Analyzers.MaintainabilityRules.SA1404CodeAnalysisSuppressionMustHaveJustification,
@@ -66,38 +67,40 @@ public void Bar()
6667
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
6768
}
6869

69-
[Fact]
70-
public async Task TestSuppressionWithNoJustificationAsync()
70+
[Theory]
71+
[InlineData("\n")]
72+
[InlineData("\r\n")]
73+
public async Task TestSuppressionWithNoJustificationAsync(string lineEnding)
7174
{
7275
var testCode = @"public class Foo
7376
{
74-
[System.Diagnostics.CodeAnalysis.SuppressMessage(null, null)]
77+
[{|#0:System.Diagnostics.CodeAnalysis.SuppressMessage(null, null)|}]
7578
public void Bar()
7679
{
7780
7881
}
79-
}";
82+
}".ReplaceLineEndings(lineEnding);
8083

81-
var fixedCode = @"public class Foo
82-
{
83-
[System.Diagnostics.CodeAnalysis.SuppressMessage(null, null, Justification = """ + SA1404CodeAnalysisSuppressionMustHaveJustification.JustificationPlaceholder + @""")]
84+
var fixedCode = $@"public class Foo
85+
{{
86+
[System.Diagnostics.CodeAnalysis.SuppressMessage(null, null, {{|#0:Justification = ""{SA1404CodeAnalysisSuppressionMustHaveJustification.JustificationPlaceholder}""|}})]
8487
public void Bar()
85-
{
88+
{{
8689
87-
}
88-
}";
90+
}}
91+
}}".ReplaceLineEndings(lineEnding);
8992

9093
await new CSharpTest
9194
{
9295
TestCode = testCode,
9396
ExpectedDiagnostics =
9497
{
95-
Diagnostic().WithLocation(3, 6),
98+
Diagnostic().WithLocation(0),
9699
},
97100
FixedCode = fixedCode,
98101
RemainingDiagnostics =
99102
{
100-
Diagnostic().WithLocation(3, 66),
103+
Diagnostic().WithLocation(0),
101104
},
102105
NumberOfIncrementalIterations = 2,
103106
NumberOfFixAllIterations = 2,

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1407UnitTests.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace StyleCop.Analyzers.Test.MaintainabilityRules
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.MaintainabilityRules.SA1407ArithmeticExpressionsMustDeclarePrecedence,
@@ -54,25 +55,27 @@ public void Bar()
5455
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
5556
}
5657

57-
[Fact]
58-
public async Task TestAdditionMultiplicationAsync()
58+
[Theory]
59+
[InlineData("\n")]
60+
[InlineData("\r\n")]
61+
public async Task TestAdditionMultiplicationAsync(string lineEnding)
5962
{
6063
var testCode = @"public class Foo
6164
{
6265
public void Bar()
6366
{
64-
int x = 1 + 1 * 1;
67+
int x = 1 + {|#0:1 * 1|};
6568
}
66-
}";
67-
DiagnosticResult expected = Diagnostic().WithLocation(5, 21);
69+
}".ReplaceLineEndings(lineEnding);
70+
DiagnosticResult expected = Diagnostic().WithLocation(0);
6871

6972
var fixedCode = @"public class Foo
7073
{
7174
public void Bar()
7275
{
7376
int x = 1 + (1 * 1);
7477
}
75-
}";
78+
}".ReplaceLineEndings(lineEnding);
7679

7780
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
7881
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1408UnitTests.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace StyleCop.Analyzers.Test.MaintainabilityRules
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.MaintainabilityRules.SA1408ConditionalExpressionsMustDeclarePrecedence,
@@ -41,25 +42,27 @@ public void Bar()
4142
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
4243
}
4344

44-
[Fact]
45-
public async Task TestOrAndAndAsync()
45+
[Theory]
46+
[InlineData("\n")]
47+
[InlineData("\r\n")]
48+
public async Task TestOrAndAndAsync(string lineEnding)
4649
{
4750
var testCode = @"public class Foo
4851
{
4952
public void Bar()
5053
{
51-
bool x = true || false && true;
54+
bool x = true || {|#0:false && true|};
5255
}
53-
}";
54-
DiagnosticResult expected = Diagnostic().WithLocation(5, 26);
56+
}".ReplaceLineEndings(lineEnding);
57+
DiagnosticResult expected = Diagnostic().WithLocation(0);
5558

5659
var fixedCode = @"public class Foo
5760
{
5861
public void Bar()
5962
{
6063
bool x = true || (false && true);
6164
}
62-
}";
65+
}".ReplaceLineEndings(lineEnding);
6366

6467
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
6568
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1410UnitTests.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace StyleCop.Analyzers.Test.MaintainabilityRules
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.MaintainabilityRules.SA1410RemoveDelegateParenthesisWhenPossible,
@@ -56,26 +57,28 @@ public void Bar()
5657
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
5758
}
5859

59-
[Fact]
60-
public async Task TestCodeFixAsync()
60+
[Theory]
61+
[InlineData("\n")]
62+
[InlineData("\r\n")]
63+
public async Task TestCodeFixAsync(string lineEnding)
6164
{
6265
var oldSource = @"public class Foo
6366
{
6467
public void Bar()
6568
{
66-
System.Func<int> getRandomNumber = delegate() { return 3; };
69+
System.Func<int> getRandomNumber = delegate{|#0:()|} { return 3; };
6770
}
68-
}";
71+
}".ReplaceLineEndings(lineEnding);
6972

7073
var newSource = @"public class Foo
7174
{
7275
public void Bar()
7376
{
7477
System.Func<int> getRandomNumber = delegate { return 3; };
7578
}
76-
}";
79+
}".ReplaceLineEndings(lineEnding);
7780

78-
var expected = Diagnostic().WithLocation(5, 52);
81+
var expected = Diagnostic().WithLocation(0);
7982
await VerifyCSharpFixAsync(oldSource, expected, newSource, CancellationToken.None).ConfigureAwait(false);
8083
}
8184

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1411UnitTests.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace StyleCop.Analyzers.Test.MaintainabilityRules
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.MaintainabilityRules.SA1411AttributeConstructorMustNotUseUnnecessaryParenthesis,
@@ -90,26 +91,28 @@ public void Bar()
9091
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
9192
}
9293

93-
[Fact]
94-
public async Task TestCodeFixAsync()
94+
[Theory]
95+
[InlineData("\n")]
96+
[InlineData("\r\n")]
97+
public async Task TestCodeFixAsync(string lineEnding)
9598
{
9699
var oldSource = @"public class Foo
97100
{
98-
[System.Obsolete()]
101+
[System.Obsolete{|#0:()|}]
99102
public void Bar()
100103
{
101104
}
102-
}";
105+
}".ReplaceLineEndings(lineEnding);
103106

104107
var newSource = @"public class Foo
105108
{
106109
[System.Obsolete]
107110
public void Bar()
108111
{
109112
}
110-
}";
113+
}".ReplaceLineEndings(lineEnding);
111114

112-
var expected = Diagnostic().WithLocation(3, 21);
115+
var expected = Diagnostic().WithLocation(0);
113116
await VerifyCSharpFixAsync(oldSource, expected, newSource, CancellationToken.None).ConfigureAwait(false);
114117
}
115118

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1412UnitTests.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace StyleCop.Analyzers.Test.MaintainabilityRules
1212
using Microsoft.CodeAnalysis.CodeFixes;
1313
using Microsoft.CodeAnalysis.Testing;
1414
using Microsoft.CodeAnalysis.Text;
15+
using StyleCop.Analyzers.Test.Helpers;
1516
using Xunit;
1617
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1718
StyleCop.Analyzers.MaintainabilityRules.SA1412StoreFilesAsUtf8,
@@ -65,11 +66,14 @@ public async Task TestFileWithWrongEncodingAsync(int codepage)
6566
await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
6667
}
6768

68-
[Fact]
69-
public async Task TestFileWithUtf8EncodingWithoutBOMAsync()
69+
[Theory]
70+
[InlineData("\n")]
71+
[InlineData("\r\n")]
72+
public async Task TestFileWithUtf8EncodingWithoutBOMAsync(string lineEnding)
7073
{
71-
var testCode = SourceText.From("class TypeName { }", new UTF8Encoding(false));
72-
var fixedCode = SourceText.From(testCode.ToString(), Encoding.UTF8);
74+
var source = "class TypeName\n{\n}\n".ReplaceLineEndings(lineEnding);
75+
var testCode = SourceText.From(source, new UTF8Encoding(false));
76+
var fixedCode = SourceText.From(source, Encoding.UTF8);
7377

7478
var expected = Diagnostic().WithLocation(1, 1);
7579

0 commit comments

Comments
 (0)