Skip to content

Commit b63065b

Browse files
authored
Merge pull request #3115 from pantosha/fix-3106
Make SA1117 work with multiline arguments
2 parents e5e203d + 0234741 commit b63065b

File tree

2 files changed

+113
-158
lines changed

2 files changed

+113
-158
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1117UnitTests.cs

Lines changed: 93 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,26 @@ public static IEnumerable<object[]> GetTestDeclarations(string delimiter)
2020
yield return new object[] { $"public delegate void Bar(int a, int b,{delimiter} {{|#0:string s|}});" };
2121
}
2222

23+
public static IEnumerable<object[]> GetMultilineTestDeclarations(string delimiter)
24+
{
25+
yield return new object[] { $"public Foo(int a,{delimiter} string\r\ns) {{ }}" };
26+
yield return new object[] { $"public object Bar(int a,{delimiter} string\r\ns) => null;" };
27+
yield return new object[] { $"public object this[int a,{delimiter} string\r\ns] => null;" };
28+
yield return new object[] { $"public delegate void Bar(int a,{delimiter} string\r\ns);" };
29+
}
30+
2331
public static IEnumerable<object[]> GetTestConstructorInitializers(string delimiter)
2432
{
2533
yield return new object[] { $"this(42, 43, {delimiter} {{|#0:\"hello\"|}})" };
2634
yield return new object[] { $"base(42, 43, {delimiter} {{|#0:\"hello\"|}})" };
2735
}
2836

37+
public static IEnumerable<object[]> GetMultilineTestConstructorInitializers(string delimiter)
38+
{
39+
yield return new object[] { $"this(42\r\n+ 1, {delimiter} {{|#0:43|}}, {delimiter} \"hello\")" };
40+
yield return new object[] { $"base(42\r\n+ 1, {delimiter} {{|#0:43|}}, {delimiter} \"hello\")" };
41+
}
42+
2943
public static IEnumerable<object[]> GetTestExpressions(string delimiter)
3044
{
3145
yield return new object[] { $"Bar(1, 2, {delimiter} {{|#0:2|}})" };
@@ -38,6 +52,33 @@ public static IEnumerable<object[]> GetTestExpressions(string delimiter)
3852
yield return new object[] { $"long ll = this[2, 2,{delimiter} {{|#0:2|}}];" };
3953
}
4054

55+
public static IEnumerable<object[]> GetTrailingMultilineTestExpressions(string delimiter)
56+
{
57+
yield return new object[] { $"System.Action<int, int, int> func = (int x, {delimiter} int y, {delimiter} int\r\nz) => Bar(x, y, z)" };
58+
yield return new object[] { $"System.Action<int, int, int> func = delegate(int x, {delimiter} int y, {delimiter} int\r\nz) {{ Bar(x, y, z); }}" };
59+
yield return new object[] { $"var arr = new string[2, {delimiter} 2\r\n+ 2];" };
60+
yield return new object[] { $"char cc = (new char[3, 3])[2, {delimiter} 2\r\n+ 2];" };
61+
yield return new object[] { $"char? c = (new char[3, 3])?[2, {delimiter} 2\r\n+ 2];" };
62+
yield return new object[] { $"long ll = this[2,{delimiter} 2,{delimiter} 2\r\n+ 1];" };
63+
yield return new object[] { $"var str = string.Join(\r\n\"def\",{delimiter}\"abc\"\r\n + \"cba\");" };
64+
}
65+
66+
public static IEnumerable<object[]> GetLeadingMultilineTestExpressions(string delimiter)
67+
{
68+
yield return new object[] { $"var str = string.Join(\r\n\"abc\"\r\n + \"cba\",{delimiter}{{|#0:\"def\"|}});" };
69+
yield return new object[] { $"Bar(\r\n1\r\n + 2,{delimiter}{{|#0:3|}},\r\n 4);" };
70+
}
71+
72+
public static IEnumerable<object[]> GetTestAttributes(string delimiter)
73+
{
74+
yield return new object[] { $"[MyAttribute(1, {delimiter}2, {{|#0:3|}})]" };
75+
}
76+
77+
public static IEnumerable<object[]> GetMultilineTestAttributes(string delimiter)
78+
{
79+
yield return new object[] { $"[MyAttribute(1, {delimiter}2, {delimiter}3\r\n+ 5)]" };
80+
}
81+
4182
public static IEnumerable<object[]> ValidTestExpressions()
4283
{
4384
yield return new object[] { $"System.Action func = () => Bar(0, 2, 3)" };
@@ -62,8 +103,28 @@ public static IEnumerable<object[]> ValidTestDeclarations()
62103
};
63104
}
64105

106+
public static IEnumerable<object[]> ValidTestAttribute()
107+
{
108+
// This is a regression test for https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1211
109+
yield return new object[] { @"[System.Obsolete]" };
110+
yield return new object[]
111+
{
112+
@"[MyAttribute(
113+
1, 2, 3)]",
114+
};
115+
yield return new object[]
116+
{
117+
@"[MyAttribute(
118+
1,
119+
2,
120+
3)]",
121+
};
122+
}
123+
65124
[Theory]
66125
[MemberData(nameof(GetTestDeclarations), "")]
126+
[MemberData(nameof(GetMultilineTestDeclarations), "\r\n")]
127+
[MemberData(nameof(GetMultilineTestDeclarations), "")]
67128
[MemberData(nameof(ValidTestDeclarations))]
68129
public async Task TestValidDeclarationAsync(string declaration)
69130
{
@@ -91,6 +152,7 @@ class Foo
91152

92153
[Theory]
93154
[MemberData(nameof(GetTestConstructorInitializers), "")]
155+
[MemberData(nameof(GetMultilineTestConstructorInitializers), "\r\n")]
94156
public async Task TestValidConstructorInitializerAsync(string initializer)
95157
{
96158
var testCode = $@"
@@ -119,6 +181,7 @@ public Derived(int i, int j, string z)
119181

120182
[Theory]
121183
[MemberData(nameof(GetTestConstructorInitializers), "\r\n")]
184+
[MemberData(nameof(GetMultilineTestConstructorInitializers), "")]
122185
public async Task TestInvalidConstructorInitializerAsync(string initializer)
123186
{
124187
var testCode = $@"
@@ -148,6 +211,9 @@ public Derived(int i, int j, string z)
148211

149212
[Theory]
150213
[MemberData(nameof(GetTestExpressions), "")]
214+
[MemberData(nameof(GetLeadingMultilineTestExpressions), "\r\n")]
215+
[MemberData(nameof(GetTrailingMultilineTestExpressions), "\r\n")]
216+
[MemberData(nameof(GetTrailingMultilineTestExpressions), "")]
151217
[MemberData(nameof(ValidTestExpressions))]
152218
public async Task TestValidExpressionAsync(string expression)
153219
{
@@ -171,6 +237,7 @@ public void Baz()
171237

172238
[Theory]
173239
[MemberData(nameof(GetTestExpressions), "\r\n")]
240+
[MemberData(nameof(GetLeadingMultilineTestExpressions), "")]
174241
public async Task TestInvalidExpressionAsync(string expression)
175242
{
176243
var testCode = $@"
@@ -192,52 +259,50 @@ public void Baz()
192259
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
193260
}
194261

195-
[Fact]
196-
public async Task TestValidAttributeAsync()
262+
[Theory]
263+
[MemberData(nameof(GetTestAttributes), "")]
264+
[MemberData(nameof(GetMultilineTestAttributes), "\r\n")]
265+
[MemberData(nameof(GetMultilineTestAttributes), "")]
266+
[MemberData(nameof(ValidTestAttribute))]
267+
public async Task TestValidAttributeAsync(string attribute)
197268
{
198-
var testCode = @"
269+
var testCode = $@"
199270
[System.AttributeUsage(System.AttributeTargets.Class)]
200271
public class MyAttribute : System.Attribute
201-
{
272+
{{
202273
public MyAttribute(int a, int b, int c)
203-
{
204-
}
205-
}
274+
{{
275+
}}
276+
}}
206277
207-
[MyAttribute(1, 2, 3)]
278+
{attribute}
208279
class Foo
209-
{
210-
}
211-
212-
// This is a regression test for https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1211
213-
[System.Obsolete]
214-
class ObsoleteType
215-
{
216-
}";
280+
{{
281+
}}";
217282

218283
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
219284
}
220285

221-
[Fact]
222-
public async Task TestInvalidAttributeAsync()
286+
[Theory]
287+
[MemberData(nameof(GetTestAttributes), "\r\n")]
288+
public async Task TestInvalidAttributeAsync(string attribute)
223289
{
224-
var testCode = @"
290+
var testCode = $@"
225291
[System.AttributeUsage(System.AttributeTargets.Class)]
226292
public class MyAttribute : System.Attribute
227-
{
293+
{{
228294
public MyAttribute(int a, int b, int c)
229-
{
230-
}
231-
}
295+
{{
296+
}}
297+
}}
232298
233-
[MyAttribute(
234-
1,
235-
2, {|#0:3|})]
299+
{attribute}
236300
class Foo
237-
{
238-
}";
301+
{{
302+
}}";
239303

240304
DiagnosticResult expected = Diagnostic().WithLocation(0);
305+
241306
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
242307
}
243308
}

0 commit comments

Comments
 (0)