Skip to content

Commit ad91531

Browse files
committed
Add line endings tests for ordering rules
1 parent 5a9be64 commit ad91531

File tree

10 files changed

+130
-48
lines changed

10 files changed

+130
-48
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1201UnitTests.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,22 +281,24 @@ public class TestClass { }
281281
await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
282282
}
283283

284-
[Fact]
285-
public async Task TestTypeMemberOrderWrongOrderInterfaceAsync()
284+
[Theory]
285+
[InlineData("\n")]
286+
[InlineData("\r\n")]
287+
public async Task TestTypeMemberOrderWrongOrderInterfaceAsync(string lineEnding)
286288
{
287289
string testCode = @"public interface OuterType
288290
{
289291
string TestProperty { get; set; }
290-
event System.Action TestEvent;
292+
{|#0:event System.Action TestEvent;|}
291293
void TestMethod ();
292-
string this[string arg] { get; set; }
294+
string {|#1:this|}[string arg] { get; set; }
293295
}
294-
";
296+
".ReplaceLineEndings(lineEnding);
295297

296298
DiagnosticResult[] expected =
297299
{
298-
Diagnostic().WithLocation(4, 5).WithArguments("event", "property"),
299-
Diagnostic().WithLocation(6, 12).WithArguments("indexer", "method"),
300+
Diagnostic().WithLocation(0).WithArguments("event", "property"),
301+
Diagnostic().WithLocation(1).WithArguments("indexer", "method"),
300302
};
301303

302304
string fixedCode = @"public interface OuterType
@@ -306,7 +308,7 @@ public async Task TestTypeMemberOrderWrongOrderInterfaceAsync()
306308
string this[string arg] { get; set; }
307309
void TestMethod ();
308310
}
309-
";
311+
".ReplaceLineEndings(lineEnding);
310312

311313
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
312314
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1202UnitTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,24 @@ public async Task TestTypeOrderingAsync(string keyword)
125125
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
126126
}
127127

128+
[Theory]
129+
[InlineData("\n")]
130+
[InlineData("\r\n")]
131+
public async Task TestTypeOrderingWithLineEndingsAsync(string lineEnding)
132+
{
133+
var testCode = @"internal class TestClass1 { }
134+
public class {|#0:TestClass2|} { }
135+
".ReplaceLineEndings(lineEnding);
136+
137+
var expected = Diagnostic().WithLocation(0).WithArguments("public", "internal");
138+
139+
var fixedCode = @"public class TestClass2 { }
140+
internal class TestClass1 { }
141+
".ReplaceLineEndings(lineEnding);
142+
143+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
144+
}
145+
128146
/// <summary>
129147
/// Verifies that the analyzer will properly handle interfaces before classes.
130148
/// </summary>

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1203UnitTests.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace StyleCop.Analyzers.Test.OrderingRules
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.OrderingRules.SA1203ConstantsMustAppearBeforeFields,
@@ -91,23 +92,25 @@ private class TestClass10 { }
9192
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
9293
}
9394

94-
[Fact]
95-
public async Task TestClassViolationAsync()
95+
[Theory]
96+
[InlineData("\n")]
97+
[InlineData("\r\n")]
98+
public async Task TestClassViolationAsync(string lineEnding)
9699
{
97100
var testCode = @"
98101
public class Foo
99102
{
100103
private int Baz = 1;
101-
private const int Bar = 2;
102-
}";
103-
var firstDiagnostic = Diagnostic().WithLocation(5, 23);
104+
private const int {|#0:Bar|} = 2;
105+
}".ReplaceLineEndings(lineEnding);
106+
var firstDiagnostic = Diagnostic().WithLocation(0);
104107

105108
var fixedTestCode = @"
106109
public class Foo
107110
{
108111
private const int Bar = 2;
109112
private int Baz = 1;
110-
}";
113+
}".ReplaceLineEndings(lineEnding);
111114
await VerifyCSharpFixAsync(testCode, firstDiagnostic, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
112115
}
113116

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1204UnitTests.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace StyleCop.Analyzers.Test.OrderingRules
99
using System.Threading.Tasks;
1010
using Microsoft.CodeAnalysis.Testing;
1111
using StyleCop.Analyzers.OrderingRules;
12+
using StyleCop.Analyzers.Test.Helpers;
1213
using Xunit;
1314
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1415
StyleCop.Analyzers.OrderingRules.SA1204StaticElementsMustAppearBeforeInstanceElements,
@@ -107,22 +108,25 @@ private class TestClass10 { }
107108
/// <summary>
108109
/// Verifies that the analyzer will properly handle non-static classes before static.
109110
/// </summary>
111+
/// <param name="lineEnding">The line ending to use in the test code.</param>
110112
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
111-
[Fact]
112-
public async Task TestNonStaticClassBeforeStaticAsync()
113+
[Theory]
114+
[InlineData("\n")]
115+
[InlineData("\r\n")]
116+
public async Task TestNonStaticClassBeforeStaticAsync(string lineEnding)
113117
{
114118
var testCode = @"public class TestClass1 { }
115-
public static class TestClass2 { }
116-
";
119+
public static class {|#0:TestClass2|} { }
120+
".ReplaceLineEndings(lineEnding);
117121

118122
DiagnosticResult[] expected =
119123
{
120-
Diagnostic().WithLocation(2, 21),
124+
Diagnostic().WithLocation(0),
121125
};
122126

123127
var fixedCode = @"public static class TestClass2 { }
124128
public class TestClass1 { }
125-
";
129+
".ReplaceLineEndings(lineEnding);
126130

127131
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
128132
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1205UnitTests.cs

Lines changed: 10 additions & 8 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.OrderingRules
75
{
86
using System.Collections.Generic;
@@ -12,6 +10,7 @@ namespace StyleCop.Analyzers.Test.OrderingRules
1210
using Microsoft.CodeAnalysis.Testing;
1311
using StyleCop.Analyzers.Lightup;
1412
using StyleCop.Analyzers.OrderingRules;
13+
using StyleCop.Analyzers.Test.Helpers;
1514
using Xunit;
1615
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1716
StyleCop.Analyzers.OrderingRules.SA1205PartialElementsMustDeclareAccess,
@@ -187,20 +186,23 @@ public async Task TestInvalidDeclarationAsync(string declaration)
187186
/// <summary>
188187
/// Verifies that the code fix will properly copy over the access modifier defined in another fragment of the partial element.
189188
/// </summary>
189+
/// <param name="lineEnding">The line ending to use in the test code.</param>
190190
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
191-
[Fact]
192-
public async Task TestProperAccessModifierPropagationAsync()
191+
[Theory]
192+
[InlineData("\n")]
193+
[InlineData("\r\n")]
194+
public async Task TestProperAccessModifierPropagationAsync(string lineEnding)
193195
{
194196
var testCode = @"public partial class Foo
195197
{
196198
private int field1;
197199
}
198200
199-
partial class Foo
201+
partial class {|#0:Foo|}
200202
{
201203
private int field2;
202204
}
203-
";
205+
".ReplaceLineEndings(lineEnding);
204206

205207
var fixedTestCode = @"public partial class Foo
206208
{
@@ -211,9 +213,9 @@ public partial class Foo
211213
{
212214
private int field2;
213215
}
214-
";
216+
".ReplaceLineEndings(lineEnding);
215217

216-
await VerifyCSharpFixAsync(testCode, Diagnostic().WithLocation(6, 15), fixedTestCode, CancellationToken.None).ConfigureAwait(false);
218+
await VerifyCSharpFixAsync(testCode, Diagnostic().WithLocation(0), fixedTestCode, CancellationToken.None).ConfigureAwait(false);
217219
}
218220

219221
/// <summary>

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1206CodeFixProviderUnitTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace StyleCop.Analyzers.Test.OrderingRules
99
using System.Threading.Tasks;
1010
using Microsoft.CodeAnalysis.Testing;
1111
using StyleCop.Analyzers.OrderingRules;
12+
using StyleCop.Analyzers.Test.Helpers;
1213
using Xunit;
1314
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1415
StyleCop.Analyzers.OrderingRules.SA1206DeclarationKeywordsMustFollowOrder,
@@ -33,6 +34,26 @@ public async Task VerifyKeywordReorderingInClassDeclarationAsync()
3334
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
3435
}
3536

37+
[Theory]
38+
[InlineData("\n")]
39+
[InlineData("\r\n")]
40+
public async Task VerifyKeywordReorderingInClassDeclarationWithLineEndingsAsync(string lineEnding)
41+
{
42+
var testCode = @"abstract
43+
{|#0:public|}
44+
class FooBar
45+
{
46+
}".ReplaceLineEndings(lineEnding);
47+
var fixedTestCode = @"public
48+
abstract
49+
class FooBar
50+
{
51+
}".ReplaceLineEndings(lineEnding);
52+
53+
var expected = Diagnostic().WithLocation(0).WithArguments("public", "abstract");
54+
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
55+
}
56+
3657
/// <summary>
3758
/// Verifies that the code fix will properly reorder keywords on a struct declaration.
3859
/// </summary>

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1207UnitTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace StyleCop.Analyzers.Test.OrderingRules
1212
using System.Threading.Tasks;
1313
using Microsoft.CodeAnalysis.Testing;
1414
using StyleCop.Analyzers.OrderingRules;
15+
using StyleCop.Analyzers.Test.Helpers;
1516
using Xunit;
1617
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1718
StyleCop.Analyzers.OrderingRules.SA1207ProtectedMustComeBeforeInternal,
@@ -134,5 +135,28 @@ public async Task TestInvalidDeclarationAsync(string invalidDeclaration, int dia
134135
DiagnosticResult expected = Diagnostic().WithArguments("protected", "internal").WithLocation(4, 4 + diagnosticColumn);
135136
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
136137
}
138+
139+
[Theory]
140+
[InlineData("\n")]
141+
[InlineData("\r\n")]
142+
public async Task TestInvalidDeclarationWithLineEndingsAsync(string lineEnding)
143+
{
144+
var invalidDeclaration = @"internal
145+
static
146+
{|#0:protected|}
147+
int
148+
Bar;";
149+
var fixedDeclaration = @"protected
150+
static
151+
internal
152+
int
153+
Bar;";
154+
155+
var testCode = TestCodeTemplate.Replace("$$", invalidDeclaration).ReplaceLineEndings(lineEnding);
156+
var fixedTestCode = TestCodeTemplate.Replace("$$", fixedDeclaration).ReplaceLineEndings(lineEnding);
157+
158+
DiagnosticResult expected = Diagnostic().WithArguments("protected", "internal").WithLocation(0);
159+
await VerifyCSharpFixAsync(testCode, expected, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
160+
}
137161
}
138162
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1213UnitTests.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
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.OrderingRules
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.OrderingRules.SA1213EventAccessorsMustFollowOrder,
1413
StyleCop.Analyzers.OrderingRules.SA1212SA1213CodeFixProvider>;
1514

1615
public class SA1213UnitTests
1716
{
18-
[Fact]
19-
public async Task TestAddAccessorAfterRemoveAccessorAsync()
17+
[Theory]
18+
[InlineData("\n")]
19+
[InlineData("\r\n")]
20+
public async Task TestAddAccessorAfterRemoveAccessorAsync(string lineEnding)
2021
{
2122
var testCode = @"
2223
using System;
@@ -26,7 +27,7 @@ public class Foo
2627
2728
public event EventHandler NameChanged
2829
{
29-
remove
30+
{|#0:remove|}
3031
{
3132
this.nameChanged -= value;
3233
}
@@ -35,9 +36,9 @@ public event EventHandler NameChanged
3536
this.nameChanged += value;
3637
}
3738
}
38-
}";
39+
}".ReplaceLineEndings(lineEnding);
3940

40-
DiagnosticResult expected = Diagnostic().WithLocation(9, 9);
41+
DiagnosticResult expected = Diagnostic().WithLocation(0);
4142

4243
var fixedCode = @"
4344
using System;
@@ -56,7 +57,7 @@ public event EventHandler NameChanged
5657
this.nameChanged -= value;
5758
}
5859
}
59-
}";
60+
}".ReplaceLineEndings(lineEnding);
6061

6162
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
6263
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1214UnitTests.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace StyleCop.Analyzers.Test.OrderingRules
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.OrderingRules.SA1214ReadonlyElementsMustAppearBeforeNonReadonlyElements,
@@ -84,27 +85,29 @@ private void TestMethod10() { }
8485
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
8586
}
8687

87-
[Fact]
88-
public async Task TestTwoFieldsInClassStaticReadonlyFieldPlacedAfterStaticNonReadonlyAsync()
88+
[Theory]
89+
[InlineData("\n")]
90+
[InlineData("\r\n")]
91+
public async Task TestTwoFieldsInClassStaticReadonlyFieldPlacedAfterStaticNonReadonlyAsync(string lineEnding)
8992
{
9093
var testCode = @"
9194
public class Foo
9295
{
9396
private static int i = 0;
94-
private static readonly int j = 0;
95-
}";
97+
private static readonly int {|#0:j|} = 0;
98+
}".ReplaceLineEndings(lineEnding);
9699

97100
var expected = new[]
98101
{
99-
Diagnostic().WithLocation(5, 33),
102+
Diagnostic().WithLocation(0),
100103
};
101104

102105
var fixTestCode = @"
103106
public class Foo
104107
{
105108
private static readonly int j = 0;
106109
private static int i = 0;
107-
}";
110+
}".ReplaceLineEndings(lineEnding);
108111
await VerifyCSharpFixAsync(testCode, expected, fixTestCode, CancellationToken.None).ConfigureAwait(false);
109112
}
110113

0 commit comments

Comments
 (0)