From 121c3a388afa1482138c16638dfaf65221c5737d Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Tue, 28 Mar 2023 16:28:38 +0100 Subject: [PATCH] test: 100% coverage on EnumerableExtensions --- X10D.Tests/src/Linq/EnumerableTests.cs | 71 ++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 10 deletions(-) diff --git a/X10D.Tests/src/Linq/EnumerableTests.cs b/X10D.Tests/src/Linq/EnumerableTests.cs index 2e3dc8c..838ec54 100644 --- a/X10D.Tests/src/Linq/EnumerableTests.cs +++ b/X10D.Tests/src/Linq/EnumerableTests.cs @@ -13,6 +13,11 @@ public class EnumerableTests (int minimum, int maximum) = source.MinMax(); Assert.AreEqual(1, minimum); Assert.AreEqual(10, maximum); + + source = Enumerable.Range(1, 10).ToArray(); + (minimum, maximum) = source.MinMax(); + Assert.AreEqual(1, minimum); + Assert.AreEqual(10, maximum); } [TestMethod] @@ -22,6 +27,11 @@ public class EnumerableTests (int minimum, int maximum) = source.MinMax(p => p.Age); Assert.AreEqual(1, minimum); Assert.AreEqual(10, maximum); + + source = Enumerable.Range(1, 10).Select(i => new Person {Age = i}).ToArray(); + (minimum, maximum) = source.MinMax(p => p.Age); + Assert.AreEqual(1, minimum); + Assert.AreEqual(10, maximum); } [TestMethod] @@ -31,13 +41,21 @@ public class EnumerableTests (int minimum, int maximum) = source.MinMax(p => p.Age, new InverseComparer()); Assert.AreEqual(10, minimum); Assert.AreEqual(1, maximum); + + source = Enumerable.Range(1, 10).Select(i => new Person {Age = i}).ToArray(); + (minimum, maximum) = source.MinMax(p => p.Age, new InverseComparer()); + Assert.AreEqual(10, minimum); + Assert.AreEqual(1, maximum); } [TestMethod] public void MinMax_ShouldReturnOppositeValues_UsingInverseComparer() { - IEnumerable source = Enumerable.Range(1, 10); - (int minimum, int maximum) = source.MinMax(new InverseComparer()); + (int minimum, int maximum) = Enumerable.Range(1, 10).MinMax(new InverseComparer()); + Assert.AreEqual(10, minimum); + Assert.AreEqual(1, maximum); + + (minimum, maximum) = Enumerable.Range(1, 10).ToArray().MinMax(new InverseComparer()); Assert.AreEqual(10, minimum); Assert.AreEqual(1, maximum); } @@ -45,8 +63,8 @@ public class EnumerableTests [TestMethod] public void MinMax_ShouldThrowArgumentNullException_GivenNullSelector() { - IEnumerable source = Enumerable.Range(1, 10); - Assert.ThrowsException(() => source.MinMax((Func?)null!)); + Assert.ThrowsException(() => Enumerable.Range(1, 10).MinMax((Func?)null!)); + Assert.ThrowsException(() => Enumerable.Range(1, 10).ToArray().MinMax((Func?)null!)); } [TestMethod] @@ -59,8 +77,13 @@ public class EnumerableTests [TestMethod] public void MinMax_ShouldThrowInvalidOperationException_GivenEmptySource() { - IEnumerable source = ArraySegment.Empty; - Assert.ThrowsException(() => source.MinMax()); + Assert.ThrowsException(() => Enumerable.Empty().MinMax()); + Assert.ThrowsException(() => Array.Empty().MinMax()); + Assert.ThrowsException(() => new List().MinMax()); + + Assert.ThrowsException(() => Enumerable.Empty().MinMax(i => i * 2)); + Assert.ThrowsException(() => Array.Empty().MinMax(i => i * 2)); + Assert.ThrowsException(() => new List().MinMax(i => i * 2)); } [TestMethod] @@ -70,6 +93,11 @@ public class EnumerableTests (Person minimum, Person maximum) = source.MinMaxBy(p => p.Age); Assert.AreEqual(1, minimum.Age); Assert.AreEqual(10, maximum.Age); + + source = Enumerable.Range(1, 10).Select(i => new Person {Age = i}).ToArray(); + (minimum, maximum) = source.MinMaxBy(p => p.Age); + Assert.AreEqual(1, minimum.Age); + Assert.AreEqual(10, maximum.Age); } [TestMethod] @@ -79,13 +107,27 @@ public class EnumerableTests (Person minimum, Person maximum) = source.MinMaxBy(p => p.Age, new InverseComparer()); Assert.AreEqual(10, minimum.Age); Assert.AreEqual(1, maximum.Age); + + source = Enumerable.Range(1, 10).Select(i => new Person {Age = i}).ToArray(); + (minimum, maximum) = source.MinMaxBy(p => p.Age, new InverseComparer()); + Assert.AreEqual(10, minimum.Age); + Assert.AreEqual(1, maximum.Age); } [TestMethod] public void MinMaxBy_ShouldThrowArgumentNullException_GivenNullSelector() { - IEnumerable source = Enumerable.Range(1, 10).Select(i => new Person {Age = i}); - Assert.ThrowsException(() => source.MinMaxBy((Func?)null!)); + Assert.ThrowsException(() => + { + IEnumerable source = Enumerable.Range(1, 10).Select(i => new Person {Age = i}); + return source.MinMaxBy((Func?)null!); + }); + + Assert.ThrowsException(() => + { + Person[] source = Enumerable.Range(1, 10).Select(i => new Person {Age = i}).ToArray(); + return source.MinMaxBy((Func?)null!); + }); } [TestMethod] @@ -98,8 +140,17 @@ public class EnumerableTests [TestMethod] public void MinMaxBy_ShouldThrowInvalidOperationException_GivenEmptySource() { - IEnumerable source = ArraySegment.Empty; - Assert.ThrowsException(() => source.MinMaxBy(p => p.Age)); + Assert.ThrowsException(() => + { + IEnumerable source = Enumerable.Empty(); + return source.MinMaxBy(p => p.Age); + }); + + Assert.ThrowsException(() => + { + Person[] source = Array.Empty(); + return source.MinMaxBy(p => p.Age); + }); } private struct InverseComparer : IComparer where T : IComparable