Skip to content

Commit 4902e13

Browse files
committed
refactor: modern list initialisation syntax
1 parent 017206b commit 4902e13

41 files changed

Lines changed: 187 additions & 183 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Generator.Equals.Tests/Analyzers/CrossAssemblyEquatableTests.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public partial class ExternalType : IEquatable<ExternalType>
6666

6767
var externalCompilation = CSharpCompilation.Create(
6868
"ExternalLib",
69-
new[] { CSharpSyntaxTree.ParseText(externalSource, cancellationToken: TestContext.Current.CancellationToken) },
69+
[CSharpSyntaxTree.ParseText(externalSource, cancellationToken: TestContext.Current.CancellationToken)],
7070
CoreReferences,
7171
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
7272

@@ -97,7 +97,7 @@ public partial class Container
9797

9898
var mainCompilation = CSharpCompilation.Create(
9999
"MainApp",
100-
new[] { CSharpSyntaxTree.ParseText(mainSource, cancellationToken: TestContext.Current.CancellationToken) },
100+
[CSharpSyntaxTree.ParseText(mainSource, cancellationToken: TestContext.Current.CancellationToken)],
101101
mainReferences,
102102
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
103103

@@ -153,7 +153,7 @@ public partial class ExternalType : IEquatable<ExternalType>
153153

154154
var externalCompilation = CSharpCompilation.Create(
155155
"ExternalLib",
156-
new[] { CSharpSyntaxTree.ParseText(externalSource, cancellationToken: TestContext.Current.CancellationToken) },
156+
[CSharpSyntaxTree.ParseText(externalSource, cancellationToken: TestContext.Current.CancellationToken)],
157157
CoreReferences,
158158
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
159159

@@ -176,14 +176,14 @@ public partial class Container
176176

177177
var mainCompilation = CSharpCompilation.Create(
178178
"MainApp",
179-
new[] { CSharpSyntaxTree.ParseText(mainSource, cancellationToken: TestContext.Current.CancellationToken) },
179+
[CSharpSyntaxTree.ParseText(mainSource, cancellationToken: TestContext.Current.CancellationToken)],
180180
mainReferences,
181181
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
182182

183183
// Run the analyzer
184184
DiagnosticAnalyzer analyzer = new EquatableAnalyzer();
185185
var compilationWithAnalyzers = mainCompilation.WithAnalyzers(
186-
ImmutableArray.Create(analyzer));
186+
[analyzer]);
187187

188188
var analyzerDiagnostics = await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync(TestContext.Current.CancellationToken);
189189

@@ -214,7 +214,7 @@ public partial record ExternalSettings
214214

215215
var externalCompilation = CSharpCompilation.Create(
216216
"ExternalLib",
217-
new[] { CSharpSyntaxTree.ParseText(externalSource, cancellationToken: TestContext.Current.CancellationToken) },
217+
[CSharpSyntaxTree.ParseText(externalSource, cancellationToken: TestContext.Current.CancellationToken)],
218218
CoreReferences,
219219
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
220220

@@ -244,7 +244,7 @@ public readonly partial record struct ContainerState
244244

245245
var mainCompilation = CSharpCompilation.Create(
246246
"MainApp",
247-
new[] { CSharpSyntaxTree.ParseText(mainSource, cancellationToken: TestContext.Current.CancellationToken) },
247+
[CSharpSyntaxTree.ParseText(mainSource, cancellationToken: TestContext.Current.CancellationToken)],
248248
mainReferences,
249249
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
250250

@@ -288,7 +288,7 @@ public partial record ExternalSettings
288288

289289
var externalCompilation = CSharpCompilation.Create(
290290
"ExternalLib",
291-
new[] { CSharpSyntaxTree.ParseText(externalSource, cancellationToken: TestContext.Current.CancellationToken) },
291+
[CSharpSyntaxTree.ParseText(externalSource, cancellationToken: TestContext.Current.CancellationToken)],
292292
CoreReferences,
293293
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
294294

@@ -311,14 +311,14 @@ public readonly partial record struct ContainerState
311311

312312
var mainCompilation = CSharpCompilation.Create(
313313
"MainApp",
314-
new[] { CSharpSyntaxTree.ParseText(mainSource, cancellationToken: TestContext.Current.CancellationToken) },
314+
[CSharpSyntaxTree.ParseText(mainSource, cancellationToken: TestContext.Current.CancellationToken)],
315315
mainReferences,
316316
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
317317

318318
// Run the analyzer
319319
DiagnosticAnalyzer analyzer = new EquatableAnalyzer();
320320
var compilationWithAnalyzers = mainCompilation.WithAnalyzers(
321-
ImmutableArray.Create(analyzer));
321+
[analyzer]);
322322

323323
var analyzerDiagnostics = await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync(TestContext.Current.CancellationToken);
324324

@@ -355,7 +355,7 @@ public partial record ExternalSettings
355355

356356
var externalCompilation = CSharpCompilation.Create(
357357
"ExternalLib",
358-
new[] { CSharpSyntaxTree.ParseText(externalSource, cancellationToken: TestContext.Current.CancellationToken) },
358+
[CSharpSyntaxTree.ParseText(externalSource, cancellationToken: TestContext.Current.CancellationToken)],
359359
CoreReferences,
360360
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
361361

@@ -371,7 +371,7 @@ public partial record ExternalSettings
371371
var reflectionAssembly = System.Reflection.Assembly.LoadFrom(externalDllPath);
372372
var reflectionType = reflectionAssembly.GetType("ExternalLib.ExternalSettings");
373373
var reflectionAttrs = reflectionType?.GetCustomAttributes(false);
374-
var reflectionAttrNames = reflectionAttrs?.Select(a => a.GetType().FullName).ToList() ?? new List<string>();
374+
var reflectionAttrNames = reflectionAttrs?.Select(a => a.GetType().FullName).ToList() ?? [];
375375

376376
// Reference the emitted DLL (not the in-memory compilation)
377377
var externalReference = MetadataReference.CreateFromFile(externalDllPath);
@@ -393,7 +393,7 @@ public readonly partial record struct ContainerState
393393

394394
var mainCompilation = CSharpCompilation.Create(
395395
"MainApp",
396-
new[] { CSharpSyntaxTree.ParseText(mainSource, cancellationToken: TestContext.Current.CancellationToken) },
396+
[CSharpSyntaxTree.ParseText(mainSource, cancellationToken: TestContext.Current.CancellationToken)],
397397
mainReferences,
398398
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
399399

@@ -442,7 +442,7 @@ public readonly partial record struct ContainerState
442442
// Run the analyzer
443443
DiagnosticAnalyzer analyzer = new EquatableAnalyzer();
444444
var compilationWithAnalyzers = mainCompilation.WithAnalyzers(
445-
ImmutableArray.Create(analyzer));
445+
[analyzer]);
446446

447447
var analyzerDiagnostics = await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync(TestContext.Current.CancellationToken);
448448

@@ -485,7 +485,7 @@ public AliasAttribute(string name) { }
485485

486486
var mockPackageCompilation = CSharpCompilation.Create(
487487
"MockPackage",
488-
new[] { CSharpSyntaxTree.ParseText(mockPackageSource, cancellationToken: TestContext.Current.CancellationToken) },
488+
[CSharpSyntaxTree.ParseText(mockPackageSource, cancellationToken: TestContext.Current.CancellationToken)],
489489
CoreReferences,
490490
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
491491

@@ -509,7 +509,7 @@ public sealed partial record Settings
509509
var externalRefs = CoreReferences.Append(mockPackageReference).ToArray();
510510
var externalCompilation = CSharpCompilation.Create(
511511
"ExternalLib",
512-
new[] { CSharpSyntaxTree.ParseText(externalSource, cancellationToken: TestContext.Current.CancellationToken) },
512+
[CSharpSyntaxTree.ParseText(externalSource, cancellationToken: TestContext.Current.CancellationToken)],
513513
externalRefs,
514514
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
515515

@@ -537,7 +537,7 @@ public readonly partial record struct ContainerState
537537

538538
var mainCompilation = CSharpCompilation.Create(
539539
"MainApp",
540-
new[] { CSharpSyntaxTree.ParseText(mainSource, cancellationToken: TestContext.Current.CancellationToken) },
540+
[CSharpSyntaxTree.ParseText(mainSource, cancellationToken: TestContext.Current.CancellationToken)],
541541
mainReferences,
542542
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
543543

@@ -560,7 +560,7 @@ public readonly partial record struct ContainerState
560560
// Run the analyzer
561561
DiagnosticAnalyzer analyzer = new EquatableAnalyzer();
562562
var compilationWithAnalyzers = mainCompilation.WithAnalyzers(
563-
ImmutableArray.Create(analyzer));
563+
[analyzer]);
564564

565565
var analyzerDiagnostics = await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync(TestContext.Current.CancellationToken);
566566

@@ -592,7 +592,7 @@ public sealed partial record ExternalSettings
592592

593593
var externalCompilation = CSharpCompilation.Create(
594594
"ExternalLib",
595-
new[] { CSharpSyntaxTree.ParseText(externalSource, cancellationToken: TestContext.Current.CancellationToken) },
595+
[CSharpSyntaxTree.ParseText(externalSource, cancellationToken: TestContext.Current.CancellationToken)],
596596
CoreReferences,
597597
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
598598

@@ -605,7 +605,7 @@ public sealed partial record ExternalSettings
605605

606606
var mainCompilation = CSharpCompilation.Create(
607607
"MainApp",
608-
new[] { CSharpSyntaxTree.ParseText("// empty", cancellationToken: TestContext.Current.CancellationToken) },
608+
[CSharpSyntaxTree.ParseText("// empty", cancellationToken: TestContext.Current.CancellationToken)],
609609
CoreReferences.Append(externalReference).ToArray(),
610610
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
611611

Generator.Equals.Tests/Classes/EnumerableEqualityTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ public void DifferentContents_ShouldNotBeEqual()
4141
// Both null
4242
{ new Sample { Properties = null }, new Sample { Properties = null }, true },
4343
// Same content
44-
{ new Sample { Properties = new[] { 1, 2, 3 } }, new Sample { Properties = new[] { 1, 2, 3 } }, true },
44+
{ new Sample { Properties = [1, 2, 3] }, new Sample { Properties = [1, 2, 3] }, true },
4545
// Same content, different order (unordered equality)
46-
{ new Sample { Properties = new[] { 1, 2, 3 } }, new Sample { Properties = new[] { 3, 2, 1 } }, true },
46+
{ new Sample { Properties = [1, 2, 3] }, new Sample { Properties = [3, 2, 1] }, true },
4747
// Different content
48-
{ new Sample { Properties = new[] { 1, 2, 3 } }, new Sample { Properties = new[] { 1, 2, 4 } }, false },
48+
{ new Sample { Properties = [1, 2, 3] }, new Sample { Properties = [1, 2, 4] }, false },
4949
};
5050

5151
[Theory]

Generator.Equals.Tests/Classes/FieldEqualityTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ public Sample(string[] addresses)
2121
public static TheoryData<Sample, Sample, bool> EqualityCases => new()
2222
{
2323
// Same array content should be equal
24-
{ new Sample(new[] { "10 Downing St" }), new Sample(new[] { "10 Downing St" }), true },
24+
{ new Sample(["10 Downing St"]), new Sample(["10 Downing St"]), true },
2525
// Different array content should not be equal
26-
{ new Sample(new[] { "10 Downing St" }), new Sample(new[] { "Bricklane" }), false },
26+
{ new Sample(["10 Downing St"]), new Sample(["Bricklane"]), false },
2727
// Multiple items, same order
28-
{ new Sample(new[] { "A", "B" }), new Sample(new[] { "A", "B" }), true },
28+
{ new Sample(["A", "B"]), new Sample(["A", "B"]), true },
2929
// Multiple items, different order (ordered equality)
30-
{ new Sample(new[] { "A", "B" }), new Sample(new[] { "B", "A" }), false },
30+
{ new Sample(["A", "B"]), new Sample(["B", "A"]), false },
3131
};
3232

3333
[Theory]

Generator.Equals.Tests/Classes/InheritedEqualityAttributesTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public partial class ChildWithOwnAttribute : Parent
3434
public static TheoryData<Child, Child, bool> ChildEqualityCases => new()
3535
{
3636
// Same array content
37-
{ new Child { Ints = new[] { 1, 2, 3 } }, new Child { Ints = new[] { 1, 2, 3 } }, true },
37+
{ new Child { Ints = [1, 2, 3] }, new Child { Ints = [1, 2, 3] }, true },
3838
// Different content
39-
{ new Child { Ints = new[] { 1, 2, 3 } }, new Child { Ints = new[] { 1, 2, 4 } }, false },
39+
{ new Child { Ints = [1, 2, 3] }, new Child { Ints = [1, 2, 4] }, false },
4040
// Same content, different order (ordered equality - order matters!)
41-
{ new Child { Ints = new[] { 1, 2, 3 } }, new Child { Ints = new[] { 3, 2, 1 } }, false },
41+
{ new Child { Ints = [1, 2, 3] }, new Child { Ints = [3, 2, 1] }, false },
4242
};
4343

4444
[Theory]
@@ -49,11 +49,11 @@ public void ChildEquality(Child a, Child b, bool expected) =>
4949
public static TheoryData<ChildWithOwnAttribute, ChildWithOwnAttribute, bool> ChildWithOwnAttributeCases => new()
5050
{
5151
// Same content, different order (unordered equality - order doesn't matter!)
52-
{ new ChildWithOwnAttribute { Ints = new[] { 1, 2, 3 } }, new ChildWithOwnAttribute { Ints = new[] { 3, 2, 1 } }, true },
52+
{ new ChildWithOwnAttribute { Ints = [1, 2, 3] }, new ChildWithOwnAttribute { Ints = [3, 2, 1] }, true },
5353
// Same content
54-
{ new ChildWithOwnAttribute { Ints = new[] { 1, 2, 3 } }, new ChildWithOwnAttribute { Ints = new[] { 1, 2, 3 } }, true },
54+
{ new ChildWithOwnAttribute { Ints = [1, 2, 3] }, new ChildWithOwnAttribute { Ints = [1, 2, 3] }, true },
5555
// Different content
56-
{ new ChildWithOwnAttribute { Ints = new[] { 1, 2, 3 } }, new ChildWithOwnAttribute { Ints = new[] { 1, 2, 4 } }, false },
56+
{ new ChildWithOwnAttribute { Ints = [1, 2, 3] }, new ChildWithOwnAttribute { Ints = [1, 2, 4] }, false },
5757
};
5858

5959
[Theory]

Generator.Equals.Tests/Classes/InheritedFromNonEquatableTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public partial class Child : Parent
2626
public static TheoryData<Child, Child, bool> EqualityCases => new()
2727
{
2828
// Same array content
29-
{ new Child { Ints = new[] { 1, 2, 3 } }, new Child { Ints = new[] { 1, 2, 3 } }, true },
29+
{ new Child { Ints = [1, 2, 3] }, new Child { Ints = [1, 2, 3] }, true },
3030
// Different array content
31-
{ new Child { Ints = new[] { 1, 2, 3 } }, new Child { Ints = new[] { 1, 2, 4 } }, false },
31+
{ new Child { Ints = [1, 2, 3] }, new Child { Ints = [1, 2, 4] }, false },
3232
// Same content, different order (ordered equality - order matters!)
33-
{ new Child { Ints = new[] { 1, 2, 3 } }, new Child { Ints = new[] { 3, 2, 1 } }, false },
33+
{ new Child { Ints = [1, 2, 3] }, new Child { Ints = [3, 2, 1] }, false },
3434
};
3535

3636
[Theory]

Generator.Equals.Tests/Classes/InheritedOverrideSkippedTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public partial class Child : Parent
2727
public static TheoryData<Child, Child, bool> EqualityCases => new()
2828
{
2929
// Same array content
30-
{ new Child { Ints = new[] { 1, 2, 3 } }, new Child { Ints = new[] { 1, 2, 3 } }, true },
30+
{ new Child { Ints = [1, 2, 3] }, new Child { Ints = [1, 2, 3] }, true },
3131
// Same content, different order (ordered equality - order matters!)
32-
{ new Child { Ints = new[] { 1, 2, 3 } }, new Child { Ints = new[] { 3, 2, 1 } }, false },
32+
{ new Child { Ints = [1, 2, 3] }, new Child { Ints = [3, 2, 1] }, false },
3333
// Different content
34-
{ new Child { Ints = new[] { 1, 2, 3 } }, new Child { Ints = new[] { 1, 2, 4 } }, false },
34+
{ new Child { Ints = [1, 2, 3] }, new Child { Ints = [1, 2, 4] }, false },
3535
};
3636

3737
[Theory]

Generator.Equals.Tests/Classes/MultiplePartialsEqualityTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ public partial class Sample
2828
{
2929
// Same values (Age is ignored)
3030
{
31-
new Sample { FirstName = "Dave", Age = 35, Addresses = new[] { "10 Downing St", "Bricklane" } },
32-
new Sample { FirstName = "Dave", Age = 42, Addresses = new[] { "10 Downing St", "Bricklane" } },
31+
new Sample { FirstName = "Dave", Age = 35, Addresses = ["10 Downing St", "Bricklane"] },
32+
new Sample { FirstName = "Dave", Age = 42, Addresses = ["10 Downing St", "Bricklane"] },
3333
true
3434
},
3535
// Different FirstName
3636
{
37-
new Sample { FirstName = "Dave", Age = 35, Addresses = new[] { "10 Downing St" } },
38-
new Sample { FirstName = "John", Age = 35, Addresses = new[] { "10 Downing St" } },
37+
new Sample { FirstName = "Dave", Age = 35, Addresses = ["10 Downing St"] },
38+
new Sample { FirstName = "John", Age = 35, Addresses = ["10 Downing St"] },
3939
false
4040
},
4141
// Different Addresses
4242
{
43-
new Sample { FirstName = "Dave", Addresses = new[] { "A" } },
44-
new Sample { FirstName = "Dave", Addresses = new[] { "B" } },
43+
new Sample { FirstName = "Dave", Addresses = ["A"] },
44+
new Sample { FirstName = "Dave", Addresses = ["B"] },
4545
false
4646
},
4747
};

Generator.Equals.Tests/Classes/NullableEqualityTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ public partial class Sample
1818
// Both null should be equal
1919
{ new Sample { Addresses = null }, new Sample { Addresses = null }, true },
2020
// Both empty should be equal
21-
{ new Sample { Addresses = Array.Empty<string>() }, new Sample { Addresses = Array.Empty<string>() }, true },
21+
{ new Sample { Addresses = [] }, new Sample { Addresses = [] }, true },
2222
// Same content should be equal
23-
{ new Sample { Addresses = new[] { "A" } }, new Sample { Addresses = new[] { "A" } }, true },
23+
{ new Sample { Addresses = ["A"] }, new Sample { Addresses = ["A"] }, true },
2424
// One null, one not - should not be equal
25-
{ new Sample { Addresses = null }, new Sample { Addresses = new[] { "A" } }, false },
25+
{ new Sample { Addresses = null }, new Sample { Addresses = ["A"] }, false },
2626
// Different content should not be equal
27-
{ new Sample { Addresses = new[] { "A" } }, new Sample { Addresses = new[] { "B" } }, false },
27+
{ new Sample { Addresses = ["A"] }, new Sample { Addresses = ["B"] }, false },
2828
};
2929

3030
[Theory]

Generator.Equals.Tests/Classes/OrderedEqualityTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ public Sample(string[] addresses)
2222
public static TheoryData<Sample, Sample, bool> EqualityCases => new()
2323
{
2424
// Same content, same order
25-
{ new Sample(new[] { "10 Downing St" }), new Sample(new[] { "10 Downing St" }), true },
25+
{ new Sample(["10 Downing St"]), new Sample(["10 Downing St"]), true },
2626
// Different content
27-
{ new Sample(new[] { "10 Downing St" }), new Sample(new[] { "Bricklane" }), false },
27+
{ new Sample(["10 Downing St"]), new Sample(["Bricklane"]), false },
2828
// Same content, different order (ordered equality - order matters!)
29-
{ new Sample(new[] { "A", "B" }), new Sample(new[] { "B", "A" }), false },
29+
{ new Sample(["A", "B"]), new Sample(["B", "A"]), false },
3030
// Multiple items, same order
31-
{ new Sample(new[] { "A", "B", "C" }), new Sample(new[] { "A", "B", "C" }), true },
31+
{ new Sample(["A", "B", "C"]), new Sample(["A", "B", "C"]), true },
3232
};
3333

3434
[Theory]

0 commit comments

Comments
 (0)