Skip to content

Commit a0967d0

Browse files
committed
Add tests for initializer expressions
1 parent 20ac696 commit a0967d0

1 file changed

Lines changed: 201 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1137UnitTests.cs

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,207 @@ void MethodName()
182182
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
183183
}
184184

185+
[Fact]
186+
public async Task TestInitializerExpressionAsync()
187+
{
188+
string testCode = @"
189+
using System.Collections.Generic;
190+
class ClassName
191+
{
192+
void NonZeroAlignmentMethod()
193+
{
194+
// array initializer
195+
int[] array =
196+
{
197+
0,
198+
0,
199+
0,
200+
};
201+
202+
// collection initializer
203+
List<int> list =
204+
new List<int>
205+
{
206+
0,
207+
0,
208+
0,
209+
};
210+
211+
// complex element initializer
212+
Dictionary<int, int> dictionary =
213+
new Dictionary<int, int>
214+
{
215+
{ 0, 0 },
216+
{ 0, 0 },
217+
{ 0, 0 },
218+
};
219+
220+
// object initializer
221+
var obj =
222+
new StructName
223+
{
224+
X = 0,
225+
Y = 0,
226+
Z = 0,
227+
};
228+
}
229+
230+
void ZeroAlignmentMethod()
231+
{
232+
// array initializer
233+
int[] array =
234+
{
235+
0,
236+
0,
237+
0,
238+
};
239+
240+
// collection initializer
241+
List<int> list =
242+
new List<int>
243+
{
244+
0,
245+
0,
246+
0,
247+
};
248+
249+
// complex element initializer
250+
Dictionary<int, int> dictionary =
251+
new Dictionary<int, int>
252+
{
253+
{ 0, 0 },
254+
{ 0, 0 },
255+
{ 0, 0 },
256+
};
257+
258+
// object initializer
259+
var obj =
260+
new StructName
261+
{
262+
X = 0,
263+
Y = 0,
264+
Z = 0,
265+
};
266+
}
267+
}
268+
269+
struct StructName
270+
{
271+
public int X, Y, Z;
272+
}
273+
";
274+
string fixedCode = @"
275+
using System.Collections.Generic;
276+
class ClassName
277+
{
278+
void NonZeroAlignmentMethod()
279+
{
280+
// array initializer
281+
int[] array =
282+
{
283+
0,
284+
0,
285+
0,
286+
};
287+
288+
// collection initializer
289+
List<int> list =
290+
new List<int>
291+
{
292+
0,
293+
0,
294+
0,
295+
};
296+
297+
// complex element initializer
298+
Dictionary<int, int> dictionary =
299+
new Dictionary<int, int>
300+
{
301+
{ 0, 0 },
302+
{ 0, 0 },
303+
{ 0, 0 },
304+
};
305+
306+
// object initializer
307+
var obj =
308+
new StructName
309+
{
310+
X = 0,
311+
Y = 0,
312+
Z = 0,
313+
};
314+
}
315+
316+
void ZeroAlignmentMethod()
317+
{
318+
// array initializer
319+
int[] array =
320+
{
321+
0,
322+
0,
323+
0,
324+
};
325+
326+
// collection initializer
327+
List<int> list =
328+
new List<int>
329+
{
330+
0,
331+
0,
332+
0,
333+
};
334+
335+
// complex element initializer
336+
Dictionary<int, int> dictionary =
337+
new Dictionary<int, int>
338+
{
339+
{ 0, 0 },
340+
{ 0, 0 },
341+
{ 0, 0 },
342+
};
343+
344+
// object initializer
345+
var obj =
346+
new StructName
347+
{
348+
X = 0,
349+
Y = 0,
350+
Z = 0,
351+
};
352+
}
353+
}
354+
355+
struct StructName
356+
{
357+
public int X, Y, Z;
358+
}
359+
";
360+
361+
DiagnosticResult[] expected =
362+
{
363+
this.CSharpDiagnostic().WithLocation(11, 1),
364+
this.CSharpDiagnostic().WithLocation(12, 1),
365+
this.CSharpDiagnostic().WithLocation(20, 1),
366+
this.CSharpDiagnostic().WithLocation(21, 1),
367+
this.CSharpDiagnostic().WithLocation(29, 1),
368+
this.CSharpDiagnostic().WithLocation(30, 1),
369+
this.CSharpDiagnostic().WithLocation(38, 1),
370+
this.CSharpDiagnostic().WithLocation(39, 1),
371+
this.CSharpDiagnostic().WithLocation(49, 1),
372+
this.CSharpDiagnostic().WithLocation(50, 1),
373+
this.CSharpDiagnostic().WithLocation(58, 1),
374+
this.CSharpDiagnostic().WithLocation(59, 1),
375+
this.CSharpDiagnostic().WithLocation(67, 1),
376+
this.CSharpDiagnostic().WithLocation(68, 1),
377+
this.CSharpDiagnostic().WithLocation(76, 1),
378+
this.CSharpDiagnostic().WithLocation(77, 1),
379+
};
380+
381+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
382+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
383+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
384+
}
385+
185386
/// <inheritdoc/>
186387
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
187388
{

0 commit comments

Comments
 (0)