From 6b1dc2837a47a8d686ad9c6c596786f12dfef7fe Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sun, 2 Apr 2023 04:16:33 +0100 Subject: [PATCH] test: bring coverage to 100% for Collections, Linq, Math, and Text --- .../src/Collections/ArrayTests.Clear.cs | 6 +++-- X10D.Tests/src/Collections/EnumerableTests.cs | 27 ++++++++++++++++--- X10D.Tests/src/Linq/ByteTests.cs | 8 ++++++ X10D.Tests/src/Linq/DecimalTests.cs | 8 ++++++ X10D.Tests/src/Linq/DoubleTests.cs | 8 ++++++ X10D.Tests/src/Linq/EnumerableTests.cs | 25 +++++++++-------- X10D.Tests/src/Linq/Int16Tests.cs | 7 +++++ X10D.Tests/src/Linq/Int32Tests.cs | 8 ++++++ X10D.Tests/src/Linq/Int64Tests.cs | 8 ++++++ X10D.Tests/src/Linq/SByteTests.cs | 8 ++++++ X10D.Tests/src/Linq/SingleTests.cs | 8 ++++++ X10D.Tests/src/Linq/UInt16Tests.cs | 8 ++++++ X10D.Tests/src/Linq/UInt32Tests.cs | 8 ++++++ X10D.Tests/src/Linq/UInt64Tests.cs | 8 ++++++ X10D.Tests/src/Math/ComparableTests.cs | 7 +++++ X10D.Tests/src/Text/EnumerableTests.cs | 6 +++-- X10D.Tests/src/Text/StringTests.cs | 7 +++++ X10D/src/Linq/EnumerableExtensions.cs | 9 ------- 18 files changed, 144 insertions(+), 30 deletions(-) diff --git a/X10D.Tests/src/Collections/ArrayTests.Clear.cs b/X10D.Tests/src/Collections/ArrayTests.Clear.cs index 174e64d..34c36bb 100644 --- a/X10D.Tests/src/Collections/ArrayTests.Clear.cs +++ b/X10D.Tests/src/Collections/ArrayTests.Clear.cs @@ -54,8 +54,10 @@ public partial class ArrayTests [TestMethod] public void Clear_ShouldThrowArgumentNullException_WhenArrayIsNull() { - int[]? array = null; - Assert.ThrowsException(array!.Clear); + int[] array = null!; + Assert.ThrowsException(() => array.Clear()); + Assert.ThrowsException(() => array.Clear(0, 1)); + Assert.ThrowsException(() => array.Clear(..1)); } } } diff --git a/X10D.Tests/src/Collections/EnumerableTests.cs b/X10D.Tests/src/Collections/EnumerableTests.cs index 8b835a1..e008e87 100644 --- a/X10D.Tests/src/Collections/EnumerableTests.cs +++ b/X10D.Tests/src/Collections/EnumerableTests.cs @@ -21,6 +21,12 @@ public partial class EnumerableTests Assert.ThrowsException(() => ((IEnumerable?)null)!.CountWhereNot(x => x % 2 == 0)); } + [TestMethod] + public void CountWhereNot_ShouldThrowArgumentNullException_GivenNullPredicate() + { + Assert.ThrowsException(() => Enumerable.Empty().CountWhereNot(null!)); + } + [TestMethod] public void CountWhereNot_ShouldThrowOverflowException_GivenLargeSource() { @@ -54,13 +60,13 @@ public partial class EnumerableTests [TestMethod] public void FirstWhereNot_ShouldThrowArgumentNullException_GivenNullPredicate() { - Assert.ThrowsException(() => Array.Empty().FirstWhereNotOrDefault(null!)); + Assert.ThrowsException(() => Enumerable.Range(0, 1).FirstWhereNot(null!)); } [TestMethod] public void FirstWhereNot_ShouldThrowInvalidOperationException_GivenEmptySource() { - Assert.ThrowsException(() => Array.Empty().FirstWhereNot(x => x % 2 == 0)); + Assert.ThrowsException(() => Enumerable.Empty().FirstWhereNot(x => x % 2 == 0)); } [TestMethod] @@ -86,13 +92,13 @@ public partial class EnumerableTests [TestMethod] public void FirstWhereNotOrDefault_ShouldThrowArgumentNullException_GivenNullPredicate() { - Assert.ThrowsException(() => Array.Empty().FirstWhereNotOrDefault(null!)); + Assert.ThrowsException(() => Enumerable.Empty().FirstWhereNotOrDefault(null!)); } [TestMethod] public void FirstWhereNotOrDefault_ShouldReturnDefault_GivenEmptySource() { - int result = Array.Empty().FirstWhereNotOrDefault(x => x % 2 == 0); + int result = Enumerable.Empty().FirstWhereNotOrDefault(x => x % 2 == 0); Assert.AreEqual(default, result); } @@ -259,6 +265,12 @@ public partial class EnumerableTests Assert.ThrowsException(() => ((IEnumerable?)null)!.WhereNot(x => x % 2 == 0)); } + [TestMethod] + public void WhereNot_ShouldThrowArgumentNullException_GivenNullPredicate() + { + Assert.ThrowsException(() => Enumerable.Empty().WhereNot(null!)); + } + [TestMethod] public void WhereNotNull_ShouldContainNoNullElements() { @@ -280,6 +292,13 @@ public partial class EnumerableTests Assert.AreEqual(expectedCount, actualCount); } + [TestMethod] + public void WhereNotNull_ShouldThrowArgumentNullException_GivenNullSource() + { + IEnumerable source = null!; + Assert.ThrowsException(() => source.WhereNotNull()); + } + private class DummyClass { public int Value { get; set; } diff --git a/X10D.Tests/src/Linq/ByteTests.cs b/X10D.Tests/src/Linq/ByteTests.cs index 1c269b4..e98fe31 100644 --- a/X10D.Tests/src/Linq/ByteTests.cs +++ b/X10D.Tests/src/Linq/ByteTests.cs @@ -34,6 +34,14 @@ public class ByteTests // Π_(i=1)^n (2i) will overflow at i=4 for byte } + [TestMethod] + public void Product_ShouldThrowArgumentNullException_GivenNullSource() + { + IEnumerable source = null!; + Assert.ThrowsException(() => source.Product()); + Assert.ThrowsException(() => source.Product(v => v)); + } + [TestMethod] public void RangeTo_Byte_ShouldYieldCorrectValues() { diff --git a/X10D.Tests/src/Linq/DecimalTests.cs b/X10D.Tests/src/Linq/DecimalTests.cs index abb3f7b..6fd9da4 100644 --- a/X10D.Tests/src/Linq/DecimalTests.cs +++ b/X10D.Tests/src/Linq/DecimalTests.cs @@ -41,4 +41,12 @@ public class DecimalTests Assert.AreEqual(185794560m, Enumerable.Range(1, 9).Product(Double)); Assert.AreEqual(3715891200m, Enumerable.Range(1, 10).Product(Double)); } + + [TestMethod] + public void Product_ShouldThrowArgumentNullException_GivenNullSource() + { + IEnumerable source = null!; + Assert.ThrowsException(() => source.Product()); + Assert.ThrowsException(() => source.Product(v => v)); + } } diff --git a/X10D.Tests/src/Linq/DoubleTests.cs b/X10D.Tests/src/Linq/DoubleTests.cs index 8e33bdd..1a54fbd 100644 --- a/X10D.Tests/src/Linq/DoubleTests.cs +++ b/X10D.Tests/src/Linq/DoubleTests.cs @@ -41,4 +41,12 @@ public class DoubleTests Assert.AreEqual(185794560.0, Enumerable.Range(1, 9).Product(Double)); Assert.AreEqual(3715891200.0, Enumerable.Range(1, 10).Product(Double)); } + + [TestMethod] + public void Product_ShouldThrowArgumentNullException_GivenNullSource() + { + IEnumerable source = null!; + Assert.ThrowsException(() => source.Product()); + Assert.ThrowsException(() => source.Product(v => v)); + } } diff --git a/X10D.Tests/src/Linq/EnumerableTests.cs b/X10D.Tests/src/Linq/EnumerableTests.cs index e77d4f9..40947a4 100644 --- a/X10D.Tests/src/Linq/EnumerableTests.cs +++ b/X10D.Tests/src/Linq/EnumerableTests.cs @@ -1,4 +1,5 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Collections; +using Microsoft.VisualStudio.TestTools.UnitTesting; using X10D.Linq; namespace X10D.Tests.Linq; @@ -94,8 +95,9 @@ public class EnumerableTests [TestMethod] public void MinMax_ShouldThrowArgumentNullException_GivenNullSelector() { - Assert.ThrowsException(() => Enumerable.Range(1, 10).MinMax((Func?)null!)); - Assert.ThrowsException(() => Enumerable.Range(1, 10).ToArray().MinMax((Func?)null!)); + IEnumerable source = Enumerable.Empty(); + Assert.ThrowsException(() => source.MinMax((Func)(null!))); + Assert.ThrowsException(() => source.MinMax((Func)(null!), null)); } [TestMethod] @@ -103,6 +105,9 @@ public class EnumerableTests { IEnumerable? source = null; Assert.ThrowsException(() => source!.MinMax()); + Assert.ThrowsException(() => source!.MinMax(v => v)); + Assert.ThrowsException(() => source!.MinMax(null)); + Assert.ThrowsException(() => source!.MinMax(v => v, null)); } [TestMethod] @@ -148,17 +153,10 @@ public class EnumerableTests [TestMethod] public void MinMaxBy_ShouldThrowArgumentNullException_GivenNullSelector() { - Assert.ThrowsException(() => - { - IEnumerable source = Enumerable.Range(1, 10).Select(i => new Person {Age = i}); - return source.MinMaxBy((Func?)null!); - }); + Person[] source = Enumerable.Range(1, 10).Select(i => new Person {Age = i}).ToArray(); - Assert.ThrowsException(() => - { - Person[] source = Enumerable.Range(1, 10).Select(i => new Person {Age = i}).ToArray(); - return source.MinMaxBy((Func?)null!); - }); + Assert.ThrowsException(() => source.MinMaxBy((Func)null!)); + Assert.ThrowsException(() => source.MinMaxBy((Func)null!, null)); } [TestMethod] @@ -166,6 +164,7 @@ public class EnumerableTests { IEnumerable? source = null; Assert.ThrowsException(() => source!.MinMaxBy(p => p.Age)); + Assert.ThrowsException(() => source!.MinMaxBy(p => p.Age, null)); } [TestMethod] diff --git a/X10D.Tests/src/Linq/Int16Tests.cs b/X10D.Tests/src/Linq/Int16Tests.cs index 14507f7..aa23e62 100644 --- a/X10D.Tests/src/Linq/Int16Tests.cs +++ b/X10D.Tests/src/Linq/Int16Tests.cs @@ -38,6 +38,13 @@ public class Int16Tests // Π_(i=1)^n (2i) will overflow at i=6 for short } + [TestMethod] + public void Product_ShouldThrowArgumentNullException_GivenNullSource() + { + IEnumerable source = null!; + Assert.ThrowsException(() => source.Product()); + } + [TestMethod] public void RangeTo_Int16_ShouldYieldCorrectValues() { diff --git a/X10D.Tests/src/Linq/Int32Tests.cs b/X10D.Tests/src/Linq/Int32Tests.cs index efc0870..dc45267 100644 --- a/X10D.Tests/src/Linq/Int32Tests.cs +++ b/X10D.Tests/src/Linq/Int32Tests.cs @@ -41,6 +41,14 @@ public class Int32Tests // Π_(i=1)^n (2i) will overflow at i=10 for int } + [TestMethod] + public void Product_ShouldThrowArgumentNullException_GivenNullSource() + { + IEnumerable source = null!; + Assert.ThrowsException(() => source.Product()); + Assert.ThrowsException(() => source.Product(v => v)); + } + [TestMethod] public void RangeTo_Int32_ShouldYieldCorrectValues() { diff --git a/X10D.Tests/src/Linq/Int64Tests.cs b/X10D.Tests/src/Linq/Int64Tests.cs index 5535eeb..ebbb1fa 100644 --- a/X10D.Tests/src/Linq/Int64Tests.cs +++ b/X10D.Tests/src/Linq/Int64Tests.cs @@ -42,6 +42,14 @@ public class Int64Tests Assert.AreEqual(3715891200, Enumerable.Range(1, 10).Product(Double)); } + [TestMethod] + public void Product_ShouldThrowArgumentNullException_GivenNullSource() + { + IEnumerable source = null!; + Assert.ThrowsException(() => source.Product()); + Assert.ThrowsException(() => source.Product(v => v)); + } + [TestMethod] public void RangeTo_Int64_ShouldYieldCorrectValues() { diff --git a/X10D.Tests/src/Linq/SByteTests.cs b/X10D.Tests/src/Linq/SByteTests.cs index 3e360f9..826464c 100644 --- a/X10D.Tests/src/Linq/SByteTests.cs +++ b/X10D.Tests/src/Linq/SByteTests.cs @@ -34,4 +34,12 @@ public class SByteTests // Π_(i=1)^(n(i*2)) will overflow at i=4 for sbyte } + + [TestMethod] + public void Product_ShouldThrowArgumentNullException_GivenNullSource() + { + IEnumerable source = null!; + Assert.ThrowsException(() => source.Product()); + Assert.ThrowsException(() => source.Product(v => v)); + } } diff --git a/X10D.Tests/src/Linq/SingleTests.cs b/X10D.Tests/src/Linq/SingleTests.cs index b504972..87cdb30 100644 --- a/X10D.Tests/src/Linq/SingleTests.cs +++ b/X10D.Tests/src/Linq/SingleTests.cs @@ -41,4 +41,12 @@ public class SingleTests Assert.AreEqual(185794560f, Enumerable.Range(1, 9).Product(Double)); Assert.AreEqual(3715891200f, Enumerable.Range(1, 10).Product(Double)); } + + [TestMethod] + public void Product_ShouldThrowArgumentNullException_GivenNullSource() + { + IEnumerable source = null!; + Assert.ThrowsException(() => source.Product()); + Assert.ThrowsException(() => source.Product(v => v)); + } } diff --git a/X10D.Tests/src/Linq/UInt16Tests.cs b/X10D.Tests/src/Linq/UInt16Tests.cs index bf496e8..a45eaa6 100644 --- a/X10D.Tests/src/Linq/UInt16Tests.cs +++ b/X10D.Tests/src/Linq/UInt16Tests.cs @@ -40,4 +40,12 @@ public class UInt16Tests // Π_(i=1)^n (2i) will overflow at i=7 for ushort } + + [TestMethod] + public void Product_ShouldThrowArgumentNullException_GivenNullSource() + { + IEnumerable source = null!; + Assert.ThrowsException(() => source.Product()); + Assert.ThrowsException(() => source.Product(v => v)); + } } diff --git a/X10D.Tests/src/Linq/UInt32Tests.cs b/X10D.Tests/src/Linq/UInt32Tests.cs index 060592e..cfb0ad6 100644 --- a/X10D.Tests/src/Linq/UInt32Tests.cs +++ b/X10D.Tests/src/Linq/UInt32Tests.cs @@ -42,4 +42,12 @@ public class UInt32Tests Assert.AreEqual(185794560U, Enumerable.Range(1, 9).Product(Double)); Assert.AreEqual(3715891200U, Enumerable.Range(1, 10).Product(Double)); } + + [TestMethod] + public void Product_ShouldThrowArgumentNullException_GivenNullSource() + { + IEnumerable source = null!; + Assert.ThrowsException(() => source.Product()); + Assert.ThrowsException(() => source.Product(v => v)); + } } diff --git a/X10D.Tests/src/Linq/UInt64Tests.cs b/X10D.Tests/src/Linq/UInt64Tests.cs index c9c2409..3b1c585 100644 --- a/X10D.Tests/src/Linq/UInt64Tests.cs +++ b/X10D.Tests/src/Linq/UInt64Tests.cs @@ -42,4 +42,12 @@ public class UInt64Tests Assert.AreEqual(185794560UL, Enumerable.Range(1, 9).Product(Double)); Assert.AreEqual(3715891200UL, Enumerable.Range(1, 10).Product(Double)); } + + [TestMethod] + public void Product_ShouldThrowArgumentNullException_GivenNullSource() + { + IEnumerable source = null!; + Assert.ThrowsException(() => source.Product()); + Assert.ThrowsException(() => source.Product(v => v)); + } } diff --git a/X10D.Tests/src/Math/ComparableTests.cs b/X10D.Tests/src/Math/ComparableTests.cs index dd71c7d..f0417f0 100644 --- a/X10D.Tests/src/Math/ComparableTests.cs +++ b/X10D.Tests/src/Math/ComparableTests.cs @@ -85,6 +85,13 @@ public class ComparableTests Assert.ThrowsException(() => 0.Clamp(6, 5)); } + [TestMethod] + public void Clamp_ShouldThrowArgumentNullException_GivenNullValue() + { + string comparable = null!; + Assert.ThrowsException(() => comparable.Clamp(string.Empty, string.Empty)); + } + [TestMethod] public void GreaterThan_5_6_ShouldBeFalse() { diff --git a/X10D.Tests/src/Text/EnumerableTests.cs b/X10D.Tests/src/Text/EnumerableTests.cs index 8793682..8a73cb3 100644 --- a/X10D.Tests/src/Text/EnumerableTests.cs +++ b/X10D.Tests/src/Text/EnumerableTests.cs @@ -60,14 +60,16 @@ public class EnumerableTests public void Grep_ShouldThrowArgumentNullException_GivenNullPattern() { IEnumerable source = Enumerable.Empty(); - Assert.ThrowsException(() => source.Grep(null!)); + Assert.ThrowsException(() => source.Grep(null!).ToArray()); + Assert.ThrowsException(() => source.Grep(null!, false).ToArray()); } [TestMethod] public void Grep_ShouldThrowArgumentNullException_GivenNullSource() { IEnumerable source = null!; - Assert.ThrowsException(() => source.Grep("foo")); + Assert.ThrowsException(() => source.Grep("foo").ToArray()); + Assert.ThrowsException(() => source.Grep("foo", false).ToArray()); } [TestMethod] diff --git a/X10D.Tests/src/Text/StringTests.cs b/X10D.Tests/src/Text/StringTests.cs index e3e61d8..0798b6e 100644 --- a/X10D.Tests/src/Text/StringTests.cs +++ b/X10D.Tests/src/Text/StringTests.cs @@ -380,6 +380,13 @@ public class StringTests Assert.IsFalse("World".IsEmoji()); } + [TestMethod] + public void IsEmoji_ShouldThrowArgumentNullException_GivenNullInput() + { + string value = null!; + Assert.ThrowsException(() => value.IsEmoji()); + } + [TestMethod] public void IsEmpty_ShouldReturnTrue_GivenEmptyString() { diff --git a/X10D/src/Linq/EnumerableExtensions.cs b/X10D/src/Linq/EnumerableExtensions.cs index 99b6773..178d03a 100644 --- a/X10D/src/Linq/EnumerableExtensions.cs +++ b/X10D/src/Linq/EnumerableExtensions.cs @@ -418,15 +418,6 @@ public static class EnumerableExtensions private static bool TryGetSpan(this IEnumerable source, out ReadOnlySpan span) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(source); -#else - if (source is null) - { - throw new ArgumentNullException(nameof(source)); - } -#endif - var result = true; switch (source)