Skip to content

Commit

Permalink
test: bring Linq.EnumerableExtensions coverage to 100% in
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Nov 14, 2024
1 parent b74bf60 commit 465dd2e
Showing 1 changed file with 59 additions and 24 deletions.
83 changes: 59 additions & 24 deletions X10D.Tests/src/Linq/EnumerableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void Except_ShouldFilterElements_GivenMatchingElements()
int[] source = Enumerable.Range(1, 10).ToArray();
int[] result = source.Except(5).ToArray();

Assert.That(result, Is.EquivalentTo(new[] {1, 2, 3, 4, 6, 7, 8, 9, 10}));
Assert.That(result, Is.EquivalentTo(new[] { 1, 2, 3, 4, 6, 7, 8, 9, 10 }));
}

[Test]
Expand Down Expand Up @@ -54,15 +54,15 @@ public void MinMax_ShouldReturnCorrectValues_UsingDefaultComparer()
[Test]
public void MinMax_ShouldReturnCorrectSelectedValues_UsingDefaultComparer()
{
IEnumerable<Person> source = Enumerable.Range(1, 10).Select(i => new Person {Age = i});
IEnumerable<Person> source = Enumerable.Range(1, 10).Select(i => new Person { Age = i });
(int minimum, int maximum) = source.MinMax(p => p.Age);
Assert.Multiple(() =>
{
Assert.That(minimum, Is.EqualTo(1));
Assert.That(maximum, Is.EqualTo(10));
});

source = Enumerable.Range(1, 10).Select(i => new Person {Age = i}).ToArray();
source = Enumerable.Range(1, 10).Select(i => new Person { Age = i }).ToArray();
(minimum, maximum) = source.MinMax(p => p.Age);
Assert.Multiple(() =>
{
Expand All @@ -74,15 +74,15 @@ public void MinMax_ShouldReturnCorrectSelectedValues_UsingDefaultComparer()
[Test]
public void MinMax_ShouldReturnOppositeSelectedValues_UsingInverseComparer()
{
IEnumerable<Person> source = Enumerable.Range(1, 10).Select(i => new Person {Age = i});
IEnumerable<Person> source = Enumerable.Range(1, 10).Select(i => new Person { Age = i });
(int minimum, int maximum) = source.MinMax(p => p.Age, new InverseComparer<int>());
Assert.Multiple(() =>
{
Assert.That(minimum, Is.EqualTo(10));
Assert.That(maximum, Is.EqualTo(1));
});

source = Enumerable.Range(1, 10).Select(i => new Person {Age = i}).ToArray();
source = Enumerable.Range(1, 10).Select(i => new Person { Age = i }).ToArray();
(minimum, maximum) = source.MinMax(p => p.Age, new InverseComparer<int>());
Assert.Multiple(() =>
{
Expand Down Expand Up @@ -113,44 +113,73 @@ public void MinMax_ShouldReturnOppositeValues_UsingInverseComparer()
public void MinMax_ShouldThrowArgumentNullException_GivenNullSelector()
{
IEnumerable<int> source = [];
Assert.Throws<ArgumentNullException>(() => source.MinMax((Func<int, int>)(null!)));
Assert.Throws<ArgumentNullException>(() => source.MinMax((Func<int, int>)(null!), null));

Assert.Multiple(() =>
{
Assert.Throws<ArgumentNullException>(() => source.MinMax((Func<int, int>)(null!)));
Assert.Throws<ArgumentNullException>(() => source.MinMax((Func<int, int>)(null!), null));
});
}

[Test]
public void MinMax_ShouldThrowArgumentNullException_GivenNullSource()
{
IEnumerable<int>? source = null;
Assert.Throws<ArgumentNullException>(() => source!.MinMax());
Assert.Throws<ArgumentNullException>(() => source!.MinMax(v => v));
Assert.Throws<ArgumentNullException>(() => source!.MinMax(null));
Assert.Throws<ArgumentNullException>(() => source!.MinMax(v => v, null));

Assert.Multiple(() =>
{
Assert.Throws<ArgumentNullException>(() => source!.MinMax());
Assert.Throws<ArgumentNullException>(() => source!.MinMax(v => v));
Assert.Throws<ArgumentNullException>(() => source!.MinMax(null));
Assert.Throws<ArgumentNullException>(() => source!.MinMax(v => v, null));
});
}

[Test]
public void MinMax_ShouldThrowInvalidOperationException_GivenEmptySource()
{
Assert.Throws<InvalidOperationException>(() => Enumerable.Empty<int>().MinMax());
Assert.Throws<InvalidOperationException>(() => Array.Empty<int>().MinMax());
Assert.Throws<InvalidOperationException>(() => new List<int>().MinMax());

Assert.Throws<InvalidOperationException>(() => Enumerable.Empty<int>().MinMax(i => i * 2));
Assert.Throws<InvalidOperationException>(() => Array.Empty<int>().MinMax(i => i * 2));
Assert.Throws<InvalidOperationException>(() => new List<int>().MinMax(i => i * 2));
Assert.Multiple(() =>
{
Assert.Throws<InvalidOperationException>(() =>
{
Empty().MinMax();
return;

static IEnumerable<int> Empty()
{
yield break;
}
});
Assert.Throws<InvalidOperationException>(() => Array.Empty<int>().MinMax());
Assert.Throws<InvalidOperationException>(() => new List<int>().MinMax());

Assert.Throws<InvalidOperationException>(() =>
{
Empty().MinMax(i => i * 2);
return;

static IEnumerable<int> Empty()
{
yield break;
}
});
Assert.Throws<InvalidOperationException>(() => Array.Empty<int>().MinMax(i => i * 2));
Assert.Throws<InvalidOperationException>(() => new List<int>().MinMax(i => i * 2));
});
}

[Test]
public void MinMaxBy_ShouldReturnCorrectSelectedValues_UsingDefaultComparer()
{
IEnumerable<Person> source = Enumerable.Range(1, 10).Select(i => new Person {Age = i});
IEnumerable<Person> source = Enumerable.Range(1, 10).Select(i => new Person { Age = i });
(Person minimum, Person maximum) = source.MinMaxBy(p => p.Age);
Assert.Multiple(() =>
{
Assert.That(minimum.Age, Is.EqualTo(1));
Assert.That(maximum.Age, Is.EqualTo(10));
});

source = Enumerable.Range(1, 10).Select(i => new Person {Age = i}).ToArray();
source = Enumerable.Range(1, 10).Select(i => new Person { Age = i }).ToArray();
(minimum, maximum) = source.MinMaxBy(p => p.Age);
Assert.Multiple(() =>
{
Expand All @@ -162,15 +191,15 @@ public void MinMaxBy_ShouldReturnCorrectSelectedValues_UsingDefaultComparer()
[Test]
public void MinMaxBy_ShouldReturnOppositeSelectedValues_UsingInverseComparer()
{
IEnumerable<Person> source = Enumerable.Range(1, 10).Select(i => new Person {Age = i});
IEnumerable<Person> source = Enumerable.Range(1, 10).Select(i => new Person { Age = i });
(Person minimum, Person maximum) = source.MinMaxBy(p => p.Age, new InverseComparer<int>());
Assert.Multiple(() =>
{
Assert.That(minimum.Age, Is.EqualTo(10));
Assert.That(maximum.Age, Is.EqualTo(1));
});

source = Enumerable.Range(1, 10).Select(i => new Person {Age = i}).ToArray();
source = Enumerable.Range(1, 10).Select(i => new Person { Age = i }).ToArray();
(minimum, maximum) = source.MinMaxBy(p => p.Age, new InverseComparer<int>());
Assert.Multiple(() =>
{
Expand All @@ -182,7 +211,7 @@ public void MinMaxBy_ShouldReturnOppositeSelectedValues_UsingInverseComparer()
[Test]
public void MinMaxBy_ShouldThrowArgumentNullException_GivenNullSelector()
{
Person[] source = Enumerable.Range(1, 10).Select(i => new Person {Age = i}).ToArray();
Person[] source = Enumerable.Range(1, 10).Select(i => new Person { Age = i }).ToArray();

Assert.Throws<ArgumentNullException>(() => source.MinMaxBy((Func<Person, int>)null!));
Assert.Throws<ArgumentNullException>(() => source.MinMaxBy((Func<Person, int>)null!, null));
Expand All @@ -201,8 +230,14 @@ public void MinMaxBy_ShouldThrowInvalidOperationException_GivenEmptySource()
{
Assert.Throws<InvalidOperationException>(() =>
{
IEnumerable<Person> source = [];
IEnumerable<Person> source = Empty();
_ = source.MinMaxBy(p => p.Age);
return;

static IEnumerable<Person> Empty()
{
yield break;
}
});

Assert.Throws<InvalidOperationException>(() =>
Expand Down

0 comments on commit 465dd2e

Please sign in to comment.