Skip to content

Commit

Permalink
Update to using C# 12
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz committed Nov 17, 2023
1 parent 9a4ea61 commit 80ab81c
Show file tree
Hide file tree
Showing 51 changed files with 260 additions and 378 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<LangVersion>11</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AnalysisLevel>8.0-all</AnalysisLevel>
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq.Test/AppendTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void AppendWithNonEmptyHeadSequence()
[Test]
public void AppendWithEmptyHeadSequence()
{
string[] head = { };
string[] head = [];
var tail = "first";
var whole = head.Append(tail);
whole.AssertSequenceEqual("first");
Expand Down
12 changes: 3 additions & 9 deletions MoreLinq.Test/AssertCountTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,10 @@ public void AssertCountShortSequenceWithErrorSelector()
.And.Count.EqualTo(4));
}

sealed class TestException : Exception
sealed class TestException(int cmp, int count) : Exception
{
public int Cmp { get; }
public int Count { get; }

public TestException(int cmp, int count)
{
Cmp = cmp;
Count = count;
}
public int Cmp { get; } = cmp;
public int Count { get; } = count;
}

[Test]
Expand Down
5 changes: 2 additions & 3 deletions MoreLinq.Test/AssertTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ public void AssertSequenceWithInvalidElementsAndCustomError()
.With.Property(nameof(ValueException.Value)).EqualTo(7));
}

sealed class ValueException : Exception
sealed class ValueException(object value) : Exception
{
public object Value { get; }
public ValueException(object value) => Value = value;
public object Value { get; } = value;
}
}
}
8 changes: 3 additions & 5 deletions MoreLinq.Test/Async/WatchableEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ partial class TestExtensions
public static WatchableEnumerator<T> AsWatchable<T>(this IAsyncEnumerator<T> source) => new(source);
}

sealed class WatchableEnumerator<T> : IAsyncEnumerator<T>
sealed class WatchableEnumerator<T>(IAsyncEnumerator<T> source) :
IAsyncEnumerator<T>
{
readonly IAsyncEnumerator<T> _source;
readonly IAsyncEnumerator<T> _source = source ?? throw new ArgumentNullException(nameof(source));

public event EventHandler? Disposed;
public event EventHandler<bool>? MoveNextCalled;

public WatchableEnumerator(IAsyncEnumerator<T> source) =>
_source = source ?? throw new ArgumentNullException(nameof(source));

public T Current => _source.Current;

public async ValueTask<bool> MoveNextAsync()
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq.Test/BatchTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public void BatchBucketSelectorCurrentList()
using var pool = new TestArrayPool<int>();
int[]? bucketSelectorItems = null;

var result = input.Batch(4, pool, current => bucketSelectorItems = current.ToArray(), _ => 0);
var result = input.Batch(4, pool, current => bucketSelectorItems = [..current], _ => 0);

using var reader = result.Read();
_ = reader.Read();
Expand Down
6 changes: 3 additions & 3 deletions MoreLinq.Test/BreakingCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ namespace MoreLinq.Test
using System;
using System.Collections.Generic;

class BreakingCollection<T> : BreakingSequence<T>, ICollection<T>
class BreakingCollection<T>(IList<T> list) :
BreakingSequence<T>, ICollection<T>
{
protected readonly IList<T> List;
protected readonly IList<T> List = list;

public BreakingCollection(params T[] values) : this((IList<T>)values) { }
public BreakingCollection(IList<T> list) => List = list;

public int Count => List.Count;

Expand Down
7 changes: 4 additions & 3 deletions MoreLinq.Test/BreakingList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ namespace MoreLinq.Test
/// expected to be lazily evaluated.
/// </summary>

sealed class BreakingList<T> : BreakingCollection<T>, IList<T>
sealed class BreakingList<T>(List<T> list) :
BreakingCollection<T>(list),
IList<T>
{
public BreakingList() : this(new List<T>()) { }
public BreakingList(List<T> list) : base(list) { }
public BreakingList() : this([]) { }

public int IndexOf(T item) => List.IndexOf(item);
public void Insert(int index, T item) => throw new NotImplementedException();
Expand Down
7 changes: 4 additions & 3 deletions MoreLinq.Test/BreakingReadOnlyCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ namespace MoreLinq.Test
{
using System.Collections.Generic;

class BreakingReadOnlyCollection<T> : BreakingSequence<T>, IReadOnlyCollection<T>
class BreakingReadOnlyCollection<T>(IReadOnlyCollection<T> collection) :
BreakingSequence<T>, IReadOnlyCollection<T>
{
readonly IReadOnlyCollection<T> _collection;
readonly IReadOnlyCollection<T> _collection = collection;

public BreakingReadOnlyCollection(params T[] values) : this((IReadOnlyCollection<T>)values) { }
public BreakingReadOnlyCollection(IReadOnlyCollection<T> collection) => _collection = collection;

public int Count => _collection.Count;
}
}
8 changes: 4 additions & 4 deletions MoreLinq.Test/BreakingReadOnlyList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ namespace MoreLinq.Test
/// expected to be lazily evaluated.
/// </summary>

sealed class BreakingReadOnlyList<T> : BreakingReadOnlyCollection<T>, IReadOnlyList<T>
sealed class BreakingReadOnlyList<T>(IReadOnlyList<T> list) :
BreakingReadOnlyCollection<T>(list),
IReadOnlyList<T>
{
readonly IReadOnlyList<T> _list;
readonly IReadOnlyList<T> _list = list;

public BreakingReadOnlyList(params T[] values) : this((IReadOnlyList<T>)values) { }
public BreakingReadOnlyList(IReadOnlyList<T> list) : base(list)
=> _list = list;

public T this[int index] => _list[index];
}
Expand Down
40 changes: 17 additions & 23 deletions MoreLinq.Test/CountDownTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public void WithNegativeCount()
static IEnumerable<T> GetData<T>(Func<int[], int, int?[], T> selector)
{
var xs = Enumerable.Range(0, 5).ToArray();
yield return selector(xs, -1, new int?[] { null, null, null, null, null });
yield return selector(xs, 0, new int?[] { null, null, null, null, null });
yield return selector(xs, 1, new int?[] { null, null, null, null, 0 });
yield return selector(xs, 2, new int?[] { null, null, null, 1, 0 });
yield return selector(xs, 3, new int?[] { null, null, 2, 1, 0 });
yield return selector(xs, 4, new int?[] { null, 3, 2, 1, 0 });
yield return selector(xs, 5, new int?[] { 4, 3, 2, 1, 0 });
yield return selector(xs, 6, new int?[] { 4, 3, 2, 1, 0 });
yield return selector(xs, 7, new int?[] { 4, 3, 2, 1, 0 });
yield return selector(xs, -1, [null, null, null, null, null]);
yield return selector(xs, 0, [null, null, null, null, null]);
yield return selector(xs, 1, [null, null, null, null, 0]);
yield return selector(xs, 2, [null, null, null, 1, 0]);
yield return selector(xs, 3, [null, null, 2, 1, 0]);
yield return selector(xs, 4, [null, 3, 2, 1, 0]);
yield return selector(xs, 5, [4, 3, 2, 1, 0]);
yield return selector(xs, 6, [4, 3, 2, 1, 0]);
yield return selector(xs, 7, [4, 3, 2, 1, 0]);
}

static readonly IEnumerable<TestCaseData> SequenceData =
Expand Down Expand Up @@ -170,14 +170,11 @@ public IEnumerator<T> GetEnumerator() =>
/// enumerator to be substituted for another.
/// </summary>

sealed class Collection<T> : Sequence<T>, ICollection<T>
sealed class Collection<T>(ICollection<T> collection,
Func<IEnumerator<T>, IEnumerator<T>>? em = null) :
Sequence<T>(em), ICollection<T>
{
readonly ICollection<T> _collection;

public Collection(ICollection<T> collection,
Func<IEnumerator<T>, IEnumerator<T>>? em = null) :
base(em) =>
_collection = collection ?? throw new ArgumentNullException(nameof(collection));
readonly ICollection<T> _collection = collection ?? throw new ArgumentNullException(nameof(collection));

public int Count => _collection.Count;
public bool IsReadOnly => _collection.IsReadOnly;
Expand All @@ -197,14 +194,11 @@ public Collection(ICollection<T> collection,
/// also permits its enumerator to be substituted for another.
/// </summary>

sealed class ReadOnlyCollection<T> : Sequence<T>, IReadOnlyCollection<T>
sealed class ReadOnlyCollection<T>(ICollection<T> collection,
Func<IEnumerator<T>, IEnumerator<T>>? em = null) :
Sequence<T>(em), IReadOnlyCollection<T>
{
readonly ICollection<T> _collection;

public ReadOnlyCollection(ICollection<T> collection,
Func<IEnumerator<T>, IEnumerator<T>>? em = null) :
base(em) =>
_collection = collection ?? throw new ArgumentNullException(nameof(collection));
readonly ICollection<T> _collection = collection ?? throw new ArgumentNullException(nameof(collection));

public int Count => _collection.Count;

Expand Down
6 changes: 3 additions & 3 deletions MoreLinq.Test/DistinctByTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class DistinctByTest
[Test]
public void DistinctBy()
{
string[] source = { "first", "second", "third", "fourth", "fifth" };
string[] source = ["first", "second", "third", "fourth", "fifth"];
var distinct = source.DistinctBy(word => word.Length);
distinct.AssertSequenceEqual("first", "second");
}
Expand All @@ -41,15 +41,15 @@ public void DistinctByIsLazy()
[Test]
public void DistinctByWithComparer()
{
string[] source = { "first", "FIRST", "second", "second", "third" };
string[] source = ["first", "FIRST", "second", "second", "third"];
var distinct = source.DistinctBy(word => word, StringComparer.OrdinalIgnoreCase);
distinct.AssertSequenceEqual("first", "second", "third");
}

[Test]
public void DistinctByNullComparer()
{
string[] source = { "first", "second", "third", "fourth", "fifth" };
string[] source = ["first", "second", "third", "fourth", "fifth"];
var distinct = source.DistinctBy(word => word.Length, null);
distinct.AssertSequenceEqual("first", "second");
}
Expand Down
16 changes: 8 additions & 8 deletions MoreLinq.Test/ExceptByTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class ExceptByTest
[Test]
public void SimpleExceptBy()
{
string[] first = { "aaa", "bb", "c", "dddd" };
string[] second = { "xx", "y" };
string[] first = ["aaa", "bb", "c", "dddd"];
string[] second = ["xx", "y"];
var result = first.ExceptBy(second, x => x.Length);
result.AssertSequenceEqual("aaa", "dddd");
}
Expand All @@ -42,26 +42,26 @@ public void ExceptByIsLazy()
[Test]
public void ExceptByDoesNotRepeatSourceElementsWithDuplicateKeys()
{
string[] first = { "aaa", "bb", "c", "a", "b", "c", "dddd" };
string[] second = { "xx" };
string[] first = ["aaa", "bb", "c", "a", "b", "c", "dddd"];
string[] second = ["xx"];
var result = first.ExceptBy(second, x => x.Length);
result.AssertSequenceEqual("aaa", "c", "dddd");
}

[Test]
public void ExceptByWithComparer()
{
string[] first = { "first", "second", "third", "fourth" };
string[] second = { "FIRST", "thiRD", "FIFTH" };
string[] first = ["first", "second", "third", "fourth"];
string[] second = ["FIRST", "thiRD", "FIFTH"];
var result = first.ExceptBy(second, word => word, StringComparer.OrdinalIgnoreCase);
result.AssertSequenceEqual("second", "fourth");
}

[Test]
public void ExceptByNullComparer()
{
string[] first = { "aaa", "bb", "c", "dddd" };
string[] second = { "xx", "y" };
string[] first = ["aaa", "bb", "c", "dddd"];
string[] second = ["xx", "y"];
var result = first.ExceptBy(second, x => x.Length, null);
result.AssertSequenceEqual("aaa", "dddd");
}
Expand Down
30 changes: 10 additions & 20 deletions MoreLinq.Test/FlattenTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,19 +309,19 @@ public void FlattenSelector()
new Series
{
Name = "series1",
Attributes = new[]
{
new Attribute { Values = new[] { 1, 2 } },
new Attribute { Values = new[] { 3, 4 } },
}
Attributes =
[
new Attribute { Values = [1, 2] },
new Attribute { Values = [3, 4] },
]
},
new Series
{
Name = "series2",
Attributes = new[]
{
new Attribute { Values = new[] { 5, 6 } },
}
Attributes =
[
new Attribute { Values = [5, 6] },
]
}
};

Expand Down Expand Up @@ -416,19 +416,9 @@ sealed class Attribute
public required int[] Values;
}

sealed class Tree<T>
sealed record Tree<T>(Tree<T>? Left, T Value, Tree<T>? Right)
{
public readonly T Value;
public readonly Tree<T>? Left;
public readonly Tree<T>? Right;

public Tree(T value) : this(null, value, null) { }
public Tree(Tree<T>? left, T value, Tree<T>? right)
{
Left = left;
Value = value;
Right = right;
}
}
}
}
2 changes: 1 addition & 1 deletion MoreLinq.Test/GroupAdjacentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public void GroupAdjacentSourceSequenceWithSomeNullKeys()
.SelectMany(x => Enumerable.Repeat((int?)x, x).Append(null))
.GroupAdjacent(x => x);

int?[] aNull = { null };
int?[] aNull = [null];

using var reader = groupings.Read();
AssertGrouping(reader, 1, 1);
Expand Down
Loading

0 comments on commit 80ab81c

Please sign in to comment.