-
Notifications
You must be signed in to change notification settings - Fork 509
Expand file tree
/
Copy pathSA1116UnitTests.cs
More file actions
318 lines (276 loc) · 10.2 KB
/
SA1116UnitTests.cs
File metadata and controls
318 lines (276 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#nullable disable
// Several test methods in this file use the same member data, but in some cases the test does not use all of the
// supported parameters. See https://github.com/xunit/xunit/issues/1556.
#pragma warning disable IDE0079 // Remove unnecessary suppression
#pragma warning disable xUnit1026 // Theory methods should use all of their parameters
namespace StyleCop.Analyzers.Test.ReadabilityRules
#pragma warning restore IDE0079 // Remove unnecessary suppression
{
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.Helpers;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.ReadabilityRules.SA1116SplitParametersMustStartOnLineAfterDeclaration,
StyleCop.Analyzers.ReadabilityRules.SA1116CodeFixProvider>;
public class SA1116UnitTests
{
public static IEnumerable<object[]> GetTestDeclarations(string delimiter, string fixDelimiter)
{
yield return new object[] { $"public Foo(int a,{delimiter} string s) {{ }}", $"public Foo({fixDelimiter}int a,{delimiter} string s) {{ }}", 16 };
yield return new object[] { $"public object Bar(int a,{delimiter} string s) => null;", $"public object Bar({fixDelimiter}int a,{delimiter} string s) => null;", 23 };
yield return new object[] { $"public static Foo operator + (Foo a,{delimiter} Foo b) => null;", $"public static Foo operator + ({fixDelimiter}Foo a,{delimiter} Foo b) => null;", 35 };
yield return new object[] { $"public object this[int a,{delimiter} string s] => null;", $"public object this[{fixDelimiter}int a,{delimiter} string s] => null;", 24 };
yield return new object[] { $"public delegate void Bar(int a,{delimiter} string s);", $"public delegate void Bar({fixDelimiter}int a,{delimiter} string s);", 30 };
}
public static IEnumerable<object[]> GetTestConstructorInitializers(string delimiter, string fixDelimiter)
{
yield return new object[] { $"this(42,{delimiter} \"hello\")", $"this({fixDelimiter}42,{delimiter} \"hello\")" };
yield return new object[] { $"base(42,{delimiter} \"hello\")", $"base({fixDelimiter}42,{delimiter} \"hello\")" };
}
public static IEnumerable<object[]> GetTestExpressions(string delimiter, string fixDelimiter)
{
yield return new object[] { $"Bar(1,{delimiter} 2)", $"Bar({fixDelimiter}1,{delimiter} 2)", 13 };
yield return new object[] { $"System.Action<int, int> func = (int x,{delimiter} int y) => Bar(x, y)", $"System.Action<int, int> func = ({fixDelimiter}int x,{delimiter} int y) => Bar(x, y)", 41 };
yield return new object[] { $"System.Action<int, int> func = delegate(int x,{delimiter} int y) {{ Bar(x, y); }}", $"System.Action<int, int> func = delegate({fixDelimiter}int x,{delimiter} int y) {{ Bar(x, y); }}", 49 };
yield return new object[] { $"new string('a',{delimiter} 2)", $"new string({fixDelimiter}'a',{delimiter} 2)", 20 };
yield return new object[] { $"var arr = new string[2,{delimiter} 2];", $"var arr = new string[{fixDelimiter}2,{delimiter} 2];", 30 };
yield return new object[] { $"char cc = (new char[3, 3])[2,{delimiter} 2];", $"char cc = (new char[3, 3])[{fixDelimiter}2,{delimiter} 2];", 36 };
yield return new object[] { $"char? c = (new char[3, 3])?[2,{delimiter} 2];", $"char? c = (new char[3, 3])?[{fixDelimiter}2,{delimiter} 2];", 37 };
yield return new object[] { $"long ll = this[2,{delimiter} 2];", $"long ll = this[{fixDelimiter}2,{delimiter} 2];", 24 };
}
public static IEnumerable<object[]> ValidTestExpressions()
{
yield return new object[] { $"System.Action func = () => Bar(0, 3)", null, 0 };
yield return new object[] { $"System.Action<int> func = x => Bar(x, 3)", null, 0 };
yield return new object[] { $"System.Action func = delegate {{ Bar(0, 0); }}", null, 0 };
}
[Theory]
[MemberData(nameof(GetTestDeclarations), "", "")]
public async Task TestValidDeclarationAsync(string declaration, string fixedDeclaration, int column)
{
// Not needed for this test
_ = fixedDeclaration;
_ = column;
var testCode = $@"
class Foo
{{
{declaration}
}}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
[Theory]
[MemberData(nameof(GetTestDeclarations), "\r\n", "\r\n ")]
public async Task TestInvalidDeclarationAsync(string declaration, string fixedDeclaration, int column)
{
var testCode = $@"
class Foo
{{
{declaration}
}}";
var fixedCode = $@"
class Foo
{{
{fixedDeclaration}
}}";
DiagnosticResult expected = Diagnostic().WithLocation(4, column);
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
[Theory]
[MemberData(nameof(GetTestConstructorInitializers), "", "")]
public async Task TestValidConstructorInitializerAsync(string initializer, string fixedInitializer)
{
// Not needed for this test
_ = fixedInitializer;
var testCode = $@"
class Base
{{
public Base(int a, string s)
{{
}}
}}
class Derived : Base
{{
public Derived()
: {initializer}
{{
}}
public Derived(int i, string z)
: base(i, z)
{{
}}
}}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
[Theory]
[MemberData(nameof(GetTestConstructorInitializers), "\r\n", "\r\n ")]
public async Task TestInvalidConstructorInitializerAsync(string initializer, string fixedInitializer)
{
var testCode = $@"
class Base
{{
public Base(int a, string s)
{{
}}
}}
class Derived : Base
{{
public Derived()
: {initializer}
{{
}}
public Derived(int i, string z)
: base(i, z)
{{
}}
}}";
var fixedCode = $@"
class Base
{{
public Base(int a, string s)
{{
}}
}}
class Derived : Base
{{
public Derived()
: {fixedInitializer}
{{
}}
public Derived(int i, string z)
: base(i, z)
{{
}}
}}";
DiagnosticResult expected = Diagnostic().WithLocation(12, 16);
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
[Theory]
[MemberData(nameof(GetTestExpressions), "", "")]
[MemberData(nameof(ValidTestExpressions))]
public async Task TestValidExpressionAsync(string expression, string fixedExpression, int column)
{
// Not needed for this test
_ = fixedExpression;
_ = column;
var testCode = $@"
class Foo
{{
public void Bar(int i, int z)
{{
}}
public void Baz()
{{
{expression};
}}
public long this[int a, int s] => a + s;
}}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
[Theory]
[MemberData(nameof(GetTestExpressions), "\r\n", "\r\n ")]
public async Task TestInvalidExpressionAsync(string expression, string fixedExpression, int column)
{
var testCode = $@"
class Foo
{{
public void Bar(int i, int z)
{{
}}
public void Baz()
{{
{expression};
}}
public long this[int a, int s] => a + s;
}}";
var fixedCode = $@"
class Foo
{{
public void Bar(int i, int z)
{{
}}
public void Baz()
{{
{fixedExpression};
}}
public long this[int a, int s] => a + s;
}}";
DiagnosticResult expected = Diagnostic().WithLocation(10, column);
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestImplicitElementAccessAsync()
{
var testCode = @"
class Foo
{
private System.Collections.Generic.Dictionary<int, string> dict = new System.Collections.Generic.Dictionary<int, string>
{
[1] = ""foo""
};
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
[Fact]
public async Task TestValidAttributeAsync()
{
var testCode = @"
[System.AttributeUsage(System.AttributeTargets.Class)]
public class MyAttribute : System.Attribute
{
public MyAttribute(int a, int b)
{
}
}
[MyAttribute(1, 2)]
class Foo
{
}
// This is a regression test for https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1211
[System.Obsolete]
class ObsoleteType
{
}";
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
[Theory]
[InlineData("\n")]
[InlineData("\r\n")]
public async Task TestInvalidAttributeAsync(string lineEnding)
{
var testCode = @"
[System.AttributeUsage(System.AttributeTargets.Class)]
public class MyAttribute : System.Attribute
{
public MyAttribute(int a, int b)
{
}
}
[MyAttribute({|#0:1|},
2)]
class Foo
{
}".ReplaceLineEndings(lineEnding);
var fixedCode = @"
[System.AttributeUsage(System.AttributeTargets.Class)]
public class MyAttribute : System.Attribute
{
public MyAttribute(int a, int b)
{
}
}
[MyAttribute(
1,
2)]
class Foo
{
}".ReplaceLineEndings(lineEnding);
DiagnosticResult expected = Diagnostic().WithLocation(0);
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
}
}