Skip to content

Commit

Permalink
Use more collection expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianeicher committed Feb 26, 2024
1 parent fd57845 commit 42a6a8d
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/Common.AnsiCli/AnsiCli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static IRenderable Table<T>(IEnumerable<T> data)

table.AddRow(getters.Select(getter =>
{
var value = getter.Invoke(row, Array.Empty<object?>());
var value = getter.Invoke(row, []);
return new Text(
value?.ToString() ?? "",
new Style(foreground, link: (value as Uri)?.ToStringRfc()));
Expand Down
4 changes: 2 additions & 2 deletions src/Common/Collections/ArrayUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ public static T[] AsArray<T>(this ArraySegment<T> segment)
{
if (segment.Array == null)
#if NET20 || NET40 || NET45
return new T[0];
return new T[0];
#else
return Array.Empty<T>();
return [];
#endif

if (segment.Offset == 0 && segment.Count == segment.Array.Length)
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Collections/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static IEnumerable<T> Except<T>(this IEnumerable<T> enumeration, Func<T,
/// </summary>
[LinqTunnel]
public static IEnumerable<T> Except<T>(this IEnumerable<T> enumeration, T element)
=> enumeration.Except(new[] {element});
=> enumeration.Except([element]);

/// <summary>
/// Flattens a list of lists.
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Collections/MultiDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ public bool Remove(TKey key, TValue value)
/// <returns>A list of elements with the specified key. Empty list if the key was not found.</returns>
[CollectionAccess(CollectionAccessType.Read)]
public new IEnumerable<TValue> this[TKey key]
=> TryGetValue(key, out var result) ? result : Enumerable.Empty<TValue>();
=> TryGetValue(key, out var result) ? result : [];
}
2 changes: 1 addition & 1 deletion src/Common/Dispatch/ModelViewSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,6 @@ public void Register<TSpecificModel, TSpecificView>(Func<TSpecificModel, TSpecif
{
if (create == null) throw new ArgumentNullException(nameof(create));

RegisterMultiple(element => new[] {create(element)}, update);
RegisterMultiple(element => [create(element)], update);
}
}
2 changes: 1 addition & 1 deletion src/Samples/WinForms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public MainForm()
var outputGridButton = new Button {Text = "Grid", Location = new(90, 100)};
outputGridButton.Click += delegate
{
OutputGridBox.Show(this, "Test", new [] {"Test 1", "Test 2"});
OutputGridBox.Show(this, "Test", ["Test 1", "Test 2"]);
};

var errorButton = new Button {Text = "Error", Location = new(170, 100)};
Expand Down
10 changes: 5 additions & 5 deletions src/UnitTests/Collections/MultiDictionaryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public void TestAdd()
{"B", 0}
}.Should().BeEquivalentTo(new Dictionary<string, IEnumerable<int>>
{
["A"] = new[] {0, 1},
["B"] = new[] {0}
["A"] = [0, 1],
["B"] = [0]
});
}

Expand All @@ -32,7 +32,7 @@ public void TestValues()
{"A", 0},
{"B", 1},
{"C", 1}
}.Values.Should().BeEquivalentTo(new[] {0, 1, 1});
}.Values.Should().BeEquivalentTo([0, 1, 1]);
}

[Fact]
Expand All @@ -51,6 +51,6 @@ public void TestRemove()
dict.Remove("A", 2).Should().BeFalse();
dict.Remove("C", 0).Should().BeFalse();

dict.Should().BeEquivalentTo(new Dictionary<string, IEnumerable<int>> {["A"] = new[] {0}});
dict.Should().BeEquivalentTo(new Dictionary<string, IEnumerable<int>> {["A"] = [0]});
}
}
}
32 changes: 16 additions & 16 deletions src/UnitTests/Dispatch/MergeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public void TestMergeSimple()
ICollection<int> toRemove = new List<int>();
ICollection<int> toAdd = new List<int>();
Merge.TwoWay(
theirs: new[] {16, 8, 4},
mine: new[] {1, 2, 4},
theirs: [16, 8, 4],
mine: [1, 2, 4],
added: toAdd.Add, removed: toRemove.Add);

toAdd.Should().Equal(16, 8);
Expand All @@ -77,7 +77,7 @@ public void TestMergeEquals()
{
var list = new[] {new MergeTestData(mergeID: "1")};

Merge.ThreeWay(reference: Array.Empty<MergeTestData>(), theirs: list, mine: list,
Merge.ThreeWay(reference: [], theirs: list, mine: list,
added: element => throw new AssertionFailedException($"{element} should not be detected as added."),
removed: element => throw new AssertionFailedException($"{element} should not be detected as removed."));
}
Expand Down Expand Up @@ -107,21 +107,21 @@ public void TestMergeAddAndRemove()
public void TestMergeModify()
{
var reference = MergeTestData.BuildList("a", "b", "c", "d", "e");
var theirs = new[]
var theirs = new MergeTestData[]
{
new MergeTestData(mergeID: "a"),
new MergeTestData(mergeID: "b", data: "123", timestamp: new DateTime(2000, 1, 1)),
new MergeTestData(mergeID: "c"),
new MergeTestData(mergeID: "d", data: "456", timestamp: new DateTime(2000, 1, 1)),
new MergeTestData(mergeID: "e", data: "789", timestamp: new DateTime(2999, 1, 1))
new(mergeID: "a"),
new(mergeID: "b", data: "123", timestamp: new DateTime(2000, 1, 1)),
new(mergeID: "c"),
new(mergeID: "d", data: "456", timestamp: new DateTime(2000, 1, 1)),
new(mergeID: "e", data: "789", timestamp: new DateTime(2999, 1, 1))
};
var mine = new[]
var mine = new MergeTestData[]
{
new MergeTestData(mergeID: "a"),
new MergeTestData(mergeID: "b"),
new MergeTestData(mergeID: "c", data: "abc", timestamp: new DateTime(2000, 1, 1)),
new MergeTestData(mergeID: "d", data: "def", timestamp: new DateTime(2999, 1, 1)),
new MergeTestData(mergeID: "e", data: "ghi", timestamp: new DateTime(2000, 1, 1))
new(mergeID: "a"),
new(mergeID: "b"),
new(mergeID: "c", data: "abc", timestamp: new DateTime(2000, 1, 1)),
new(mergeID: "d", data: "def", timestamp: new DateTime(2999, 1, 1)),
new(mergeID: "e", data: "ghi", timestamp: new DateTime(2000, 1, 1))
};

var toRemove = new List<MergeTestData>();
Expand All @@ -131,4 +131,4 @@ public void TestMergeModify()
toRemove.Should().Equal(mine[1], mine[4]);
toAdd.Should().Equal(theirs[1], theirs[4]);
}
}
}
8 changes: 4 additions & 4 deletions src/UnitTests/Tasks/ForEachTaskTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public void TestCallback()
[Fact]
public void TestExceptionPassing()
{
ForEachTask.Create("Test task", new[] { "" }, _ => throw new IOException("Test exception"))
ForEachTask.Create("Test task", [""], _ => throw new IOException("Test exception"))
.Invoking(x => x.Run())
.Should().Throw<IOException>();
ForEachTask.Create("Test task", new[] {""}, _ => throw new WebException("Test exception"))
ForEachTask.Create("Test task", [""], _ => throw new WebException("Test exception"))
.Invoking(x => x.Run())
.Should().Throw<WebException>();
}
Expand All @@ -38,7 +38,7 @@ public void TestRollbackException()
{
var applyCalledFor = new List<int>();
var rollbackCalledFor = new List<int>();
ForEachTask.Create("Test task", new[] {1, 2, 3},
ForEachTask.Create("Test task", [1, 2, 3],
action: value =>
{
applyCalledFor.Add(value);
Expand All @@ -59,7 +59,7 @@ public void TestRollbackCancellation()

var applyCalledFor = new List<int>();
var rollbackCalledFor = new List<int>();
ForEachTask.Create("Test task", new[] {1, 2, 3},
ForEachTask.Create("Test task", [1, 2, 3],
action: value =>
{
applyCalledFor.Add(value);
Expand Down
8 changes: 4 additions & 4 deletions src/UnitTests/Threading/ResultRacerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ResultRacerTest
[Fact]
public void Sync()
{
var racer = ResultRacer.For(new[] {1, 2, 3}, (i, _) =>
var racer = ResultRacer.For([1, 2, 3], (i, _) =>
{
Thread.Sleep(i * 100);
return i;
Expand All @@ -20,7 +20,7 @@ public void Sync()
[Fact]
public void SyncCancellation()
{
var racer = ResultRacer.For(new[] {1}, (i, _) =>
var racer = ResultRacer.For([1], (i, _) =>
{
Thread.Sleep(100);
return i;
Expand All @@ -32,7 +32,7 @@ public void SyncCancellation()
[Fact]
public async Task Async()
{
var racer = ResultRacer.For(new[] {1, 2, 3}, async (i, cancellationToken) =>
var racer = ResultRacer.For([1, 2, 3], async (i, cancellationToken) =>
{
await Task.Delay(i * 100, cancellationToken);
return i;
Expand All @@ -43,7 +43,7 @@ public async Task Async()
[Fact]
public async Task AsyncCancellation()
{
var racer = ResultRacer.For(new[] {1}, async (i, cancellationToken) =>
var racer = ResultRacer.For([1], async (i, cancellationToken) =>
{
await Task.Delay(100, cancellationToken);
return i;
Expand Down
16 changes: 8 additions & 8 deletions src/UnitTests/Undo/CompositeCommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public void TestExecuteUndo()
new MockCommand(() => executeCalls.Add(2), () => undoCalls.Add(2)));

command.Execute();
executeCalls.Should().Equal(new[] {0, 1, 2}, because: "Child commands should be executed in ascending order");
executeCalls.Should().Equal([0, 1, 2], because: "Child commands should be executed in ascending order");

command.Undo();
undoCalls.Should().Equal(new[] {2, 1, 0}, because: "Child commands should be undone in descending order");
undoCalls.Should().Equal([2, 1, 0], because: "Child commands should be undone in descending order");
}

[Fact]
Expand All @@ -51,8 +51,8 @@ public void TestExecuteRollback()
new MockCommand(() => throw new OperationCanceledException(), () => undoCalls.Add(2)));

command.Invoking(x => x.Execute()).Should().Throw<OperationCanceledException>(because: "Exceptions should be passed through after rollback");
executeCalls.Should().Equal(new[] {0, 1}, because: "After an exception the rest of the commands should not be executed");
undoCalls.Should().Equal(new[] {1, 0}, because: "After an exception all successful executions should be undone");
executeCalls.Should().Equal([0, 1], because: "After an exception the rest of the commands should not be executed");
undoCalls.Should().Equal([1, 0], because: "After an exception all successful executions should be undone");
}

[Fact]
Expand All @@ -66,12 +66,12 @@ public void TestUndoRollback()
new MockCommand(() => executeCalls.Add(2), () => undoCalls.Add(2)));

command.Execute();
executeCalls.Should().Equal(new[] {0, 1, 2}, because: "Child commands should be executed in ascending order");
executeCalls.Should().Equal([0, 1, 2], because: "Child commands should be executed in ascending order");

executeCalls.Clear();
command.Invoking(x => x.Undo()).Should().Throw<OperationCanceledException>(because: "Exceptions should be passed through after rollback");
undoCalls.Should().Equal(new[] {2, 1}, because: "After an exception the rest of the undoes should not be performed");
executeCalls.Should().Equal(new[] {1, 2}, because: "After an exception all successful undoes should be re-executed");
undoCalls.Should().Equal([2, 1], because: "After an exception the rest of the undoes should not be performed");
executeCalls.Should().Equal([1, 2], because: "After an exception all successful undoes should be re-executed");
}

[Fact]
Expand All @@ -83,4 +83,4 @@ public void TestWrongOrder()
command.Execute();
command.Invoking(x => x.Execute()).Should().Throw<InvalidOperationException>(because: "Should not allow two Executes without an Undo in between");
}
}
}
6 changes: 3 additions & 3 deletions src/UnitTests/Undo/PreExecutedCompositeCommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public void TestExecuteUndo()
executeCalls.Should().BeEmpty(because: "First execution should do nothing");

command.Undo();
undoCalls.Should().Equal(new[] {2, 1, 0}, because: "Child commands should be undone in descending order");
undoCalls.Should().Equal([2, 1, 0], because: "Child commands should be undone in descending order");

command.Execute();
executeCalls.Should().Equal(new[] {0, 1, 2}, because: "Child commands should be executed in ascending order");
executeCalls.Should().Equal([0, 1, 2], because: "Child commands should be executed in ascending order");
}
}
}

0 comments on commit 42a6a8d

Please sign in to comment.