1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 19:58:49 +00:00

test: bring coverage to 100% for Collections, Linq, Math, and Text

This commit is contained in:
Oliver Booth 2023-04-02 04:16:33 +01:00
parent 918b0b7612
commit 6b1dc2837a
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
18 changed files with 144 additions and 30 deletions

View File

@ -54,8 +54,10 @@ public partial class ArrayTests
[TestMethod] [TestMethod]
public void Clear_ShouldThrowArgumentNullException_WhenArrayIsNull() public void Clear_ShouldThrowArgumentNullException_WhenArrayIsNull()
{ {
int[]? array = null; int[] array = null!;
Assert.ThrowsException<ArgumentNullException>(array!.Clear); Assert.ThrowsException<ArgumentNullException>(() => array.Clear());
Assert.ThrowsException<ArgumentNullException>(() => array.Clear(0, 1));
Assert.ThrowsException<ArgumentNullException>(() => array.Clear(..1));
} }
} }
} }

View File

@ -21,6 +21,12 @@ public partial class EnumerableTests
Assert.ThrowsException<ArgumentNullException>(() => ((IEnumerable<int>?)null)!.CountWhereNot(x => x % 2 == 0)); Assert.ThrowsException<ArgumentNullException>(() => ((IEnumerable<int>?)null)!.CountWhereNot(x => x % 2 == 0));
} }
[TestMethod]
public void CountWhereNot_ShouldThrowArgumentNullException_GivenNullPredicate()
{
Assert.ThrowsException<ArgumentNullException>(() => Enumerable.Empty<int>().CountWhereNot(null!));
}
[TestMethod] [TestMethod]
public void CountWhereNot_ShouldThrowOverflowException_GivenLargeSource() public void CountWhereNot_ShouldThrowOverflowException_GivenLargeSource()
{ {
@ -54,13 +60,13 @@ public partial class EnumerableTests
[TestMethod] [TestMethod]
public void FirstWhereNot_ShouldThrowArgumentNullException_GivenNullPredicate() public void FirstWhereNot_ShouldThrowArgumentNullException_GivenNullPredicate()
{ {
Assert.ThrowsException<ArgumentNullException>(() => Array.Empty<int>().FirstWhereNotOrDefault(null!)); Assert.ThrowsException<ArgumentNullException>(() => Enumerable.Range(0, 1).FirstWhereNot(null!));
} }
[TestMethod] [TestMethod]
public void FirstWhereNot_ShouldThrowInvalidOperationException_GivenEmptySource() public void FirstWhereNot_ShouldThrowInvalidOperationException_GivenEmptySource()
{ {
Assert.ThrowsException<InvalidOperationException>(() => Array.Empty<int>().FirstWhereNot(x => x % 2 == 0)); Assert.ThrowsException<InvalidOperationException>(() => Enumerable.Empty<int>().FirstWhereNot(x => x % 2 == 0));
} }
[TestMethod] [TestMethod]
@ -86,13 +92,13 @@ public partial class EnumerableTests
[TestMethod] [TestMethod]
public void FirstWhereNotOrDefault_ShouldThrowArgumentNullException_GivenNullPredicate() public void FirstWhereNotOrDefault_ShouldThrowArgumentNullException_GivenNullPredicate()
{ {
Assert.ThrowsException<ArgumentNullException>(() => Array.Empty<int>().FirstWhereNotOrDefault(null!)); Assert.ThrowsException<ArgumentNullException>(() => Enumerable.Empty<int>().FirstWhereNotOrDefault(null!));
} }
[TestMethod] [TestMethod]
public void FirstWhereNotOrDefault_ShouldReturnDefault_GivenEmptySource() public void FirstWhereNotOrDefault_ShouldReturnDefault_GivenEmptySource()
{ {
int result = Array.Empty<int>().FirstWhereNotOrDefault(x => x % 2 == 0); int result = Enumerable.Empty<int>().FirstWhereNotOrDefault(x => x % 2 == 0);
Assert.AreEqual(default, result); Assert.AreEqual(default, result);
} }
@ -259,6 +265,12 @@ public partial class EnumerableTests
Assert.ThrowsException<ArgumentNullException>(() => ((IEnumerable<int>?)null)!.WhereNot(x => x % 2 == 0)); Assert.ThrowsException<ArgumentNullException>(() => ((IEnumerable<int>?)null)!.WhereNot(x => x % 2 == 0));
} }
[TestMethod]
public void WhereNot_ShouldThrowArgumentNullException_GivenNullPredicate()
{
Assert.ThrowsException<ArgumentNullException>(() => Enumerable.Empty<int>().WhereNot(null!));
}
[TestMethod] [TestMethod]
public void WhereNotNull_ShouldContainNoNullElements() public void WhereNotNull_ShouldContainNoNullElements()
{ {
@ -280,6 +292,13 @@ public partial class EnumerableTests
Assert.AreEqual(expectedCount, actualCount); Assert.AreEqual(expectedCount, actualCount);
} }
[TestMethod]
public void WhereNotNull_ShouldThrowArgumentNullException_GivenNullSource()
{
IEnumerable<string> source = null!;
Assert.ThrowsException<ArgumentNullException>(() => source.WhereNotNull());
}
private class DummyClass private class DummyClass
{ {
public int Value { get; set; } public int Value { get; set; }

View File

@ -34,6 +34,14 @@ public class ByteTests
// Π_(i=1)^n (2i) will overflow at i=4 for byte // Π_(i=1)^n (2i) will overflow at i=4 for byte
} }
[TestMethod]
public void Product_ShouldThrowArgumentNullException_GivenNullSource()
{
IEnumerable<byte> source = null!;
Assert.ThrowsException<ArgumentNullException>(() => source.Product());
Assert.ThrowsException<ArgumentNullException>(() => source.Product(v => v));
}
[TestMethod] [TestMethod]
public void RangeTo_Byte_ShouldYieldCorrectValues() public void RangeTo_Byte_ShouldYieldCorrectValues()
{ {

View File

@ -41,4 +41,12 @@ public class DecimalTests
Assert.AreEqual(185794560m, Enumerable.Range(1, 9).Product(Double)); Assert.AreEqual(185794560m, Enumerable.Range(1, 9).Product(Double));
Assert.AreEqual(3715891200m, Enumerable.Range(1, 10).Product(Double)); Assert.AreEqual(3715891200m, Enumerable.Range(1, 10).Product(Double));
} }
[TestMethod]
public void Product_ShouldThrowArgumentNullException_GivenNullSource()
{
IEnumerable<decimal> source = null!;
Assert.ThrowsException<ArgumentNullException>(() => source.Product());
Assert.ThrowsException<ArgumentNullException>(() => source.Product(v => v));
}
} }

View File

@ -41,4 +41,12 @@ public class DoubleTests
Assert.AreEqual(185794560.0, Enumerable.Range(1, 9).Product(Double)); Assert.AreEqual(185794560.0, Enumerable.Range(1, 9).Product(Double));
Assert.AreEqual(3715891200.0, Enumerable.Range(1, 10).Product(Double)); Assert.AreEqual(3715891200.0, Enumerable.Range(1, 10).Product(Double));
} }
[TestMethod]
public void Product_ShouldThrowArgumentNullException_GivenNullSource()
{
IEnumerable<double> source = null!;
Assert.ThrowsException<ArgumentNullException>(() => source.Product());
Assert.ThrowsException<ArgumentNullException>(() => source.Product(v => v));
}
} }

View File

@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Collections;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Linq; using X10D.Linq;
namespace X10D.Tests.Linq; namespace X10D.Tests.Linq;
@ -94,8 +95,9 @@ public class EnumerableTests
[TestMethod] [TestMethod]
public void MinMax_ShouldThrowArgumentNullException_GivenNullSelector() public void MinMax_ShouldThrowArgumentNullException_GivenNullSelector()
{ {
Assert.ThrowsException<ArgumentNullException>(() => Enumerable.Range(1, 10).MinMax((Func<int, int>?)null!)); IEnumerable<int> source = Enumerable.Empty<int>();
Assert.ThrowsException<ArgumentNullException>(() => Enumerable.Range(1, 10).ToArray().MinMax((Func<int, int>?)null!)); Assert.ThrowsException<ArgumentNullException>(() => source.MinMax((Func<int, int>)(null!)));
Assert.ThrowsException<ArgumentNullException>(() => source.MinMax((Func<int, int>)(null!), null));
} }
[TestMethod] [TestMethod]
@ -103,6 +105,9 @@ public class EnumerableTests
{ {
IEnumerable<int>? source = null; IEnumerable<int>? source = null;
Assert.ThrowsException<ArgumentNullException>(() => source!.MinMax()); Assert.ThrowsException<ArgumentNullException>(() => source!.MinMax());
Assert.ThrowsException<ArgumentNullException>(() => source!.MinMax(v => v));
Assert.ThrowsException<ArgumentNullException>(() => source!.MinMax(null));
Assert.ThrowsException<ArgumentNullException>(() => source!.MinMax(v => v, null));
} }
[TestMethod] [TestMethod]
@ -148,17 +153,10 @@ public class EnumerableTests
[TestMethod] [TestMethod]
public void MinMaxBy_ShouldThrowArgumentNullException_GivenNullSelector() public void MinMaxBy_ShouldThrowArgumentNullException_GivenNullSelector()
{ {
Assert.ThrowsException<ArgumentNullException>(() => Person[] source = Enumerable.Range(1, 10).Select(i => new Person {Age = i}).ToArray();
{
IEnumerable<Person> source = Enumerable.Range(1, 10).Select(i => new Person {Age = i});
return source.MinMaxBy((Func<Person, int>?)null!);
});
Assert.ThrowsException<ArgumentNullException>(() => Assert.ThrowsException<ArgumentNullException>(() => source.MinMaxBy((Func<Person, int>)null!));
{ Assert.ThrowsException<ArgumentNullException>(() => source.MinMaxBy((Func<Person, int>)null!, null));
Person[] source = Enumerable.Range(1, 10).Select(i => new Person {Age = i}).ToArray();
return source.MinMaxBy((Func<Person, int>?)null!);
});
} }
[TestMethod] [TestMethod]
@ -166,6 +164,7 @@ public class EnumerableTests
{ {
IEnumerable<Person>? source = null; IEnumerable<Person>? source = null;
Assert.ThrowsException<ArgumentNullException>(() => source!.MinMaxBy(p => p.Age)); Assert.ThrowsException<ArgumentNullException>(() => source!.MinMaxBy(p => p.Age));
Assert.ThrowsException<ArgumentNullException>(() => source!.MinMaxBy(p => p.Age, null));
} }
[TestMethod] [TestMethod]

View File

@ -38,6 +38,13 @@ public class Int16Tests
// Π_(i=1)^n (2i) will overflow at i=6 for short // Π_(i=1)^n (2i) will overflow at i=6 for short
} }
[TestMethod]
public void Product_ShouldThrowArgumentNullException_GivenNullSource()
{
IEnumerable<short> source = null!;
Assert.ThrowsException<ArgumentNullException>(() => source.Product());
}
[TestMethod] [TestMethod]
public void RangeTo_Int16_ShouldYieldCorrectValues() public void RangeTo_Int16_ShouldYieldCorrectValues()
{ {

View File

@ -41,6 +41,14 @@ public class Int32Tests
// Π_(i=1)^n (2i) will overflow at i=10 for int // Π_(i=1)^n (2i) will overflow at i=10 for int
} }
[TestMethod]
public void Product_ShouldThrowArgumentNullException_GivenNullSource()
{
IEnumerable<int> source = null!;
Assert.ThrowsException<ArgumentNullException>(() => source.Product());
Assert.ThrowsException<ArgumentNullException>(() => source.Product(v => v));
}
[TestMethod] [TestMethod]
public void RangeTo_Int32_ShouldYieldCorrectValues() public void RangeTo_Int32_ShouldYieldCorrectValues()
{ {

View File

@ -42,6 +42,14 @@ public class Int64Tests
Assert.AreEqual(3715891200, Enumerable.Range(1, 10).Product(Double)); Assert.AreEqual(3715891200, Enumerable.Range(1, 10).Product(Double));
} }
[TestMethod]
public void Product_ShouldThrowArgumentNullException_GivenNullSource()
{
IEnumerable<long> source = null!;
Assert.ThrowsException<ArgumentNullException>(() => source.Product());
Assert.ThrowsException<ArgumentNullException>(() => source.Product(v => v));
}
[TestMethod] [TestMethod]
public void RangeTo_Int64_ShouldYieldCorrectValues() public void RangeTo_Int64_ShouldYieldCorrectValues()
{ {

View File

@ -34,4 +34,12 @@ public class SByteTests
// Π_(i=1)^(n(i*2)) will overflow at i=4 for sbyte // Π_(i=1)^(n(i*2)) will overflow at i=4 for sbyte
} }
[TestMethod]
public void Product_ShouldThrowArgumentNullException_GivenNullSource()
{
IEnumerable<sbyte> source = null!;
Assert.ThrowsException<ArgumentNullException>(() => source.Product());
Assert.ThrowsException<ArgumentNullException>(() => source.Product(v => v));
}
} }

View File

@ -41,4 +41,12 @@ public class SingleTests
Assert.AreEqual(185794560f, Enumerable.Range(1, 9).Product(Double)); Assert.AreEqual(185794560f, Enumerable.Range(1, 9).Product(Double));
Assert.AreEqual(3715891200f, Enumerable.Range(1, 10).Product(Double)); Assert.AreEqual(3715891200f, Enumerable.Range(1, 10).Product(Double));
} }
[TestMethod]
public void Product_ShouldThrowArgumentNullException_GivenNullSource()
{
IEnumerable<float> source = null!;
Assert.ThrowsException<ArgumentNullException>(() => source.Product());
Assert.ThrowsException<ArgumentNullException>(() => source.Product(v => v));
}
} }

View File

@ -40,4 +40,12 @@ public class UInt16Tests
// Π_(i=1)^n (2i) will overflow at i=7 for ushort // Π_(i=1)^n (2i) will overflow at i=7 for ushort
} }
[TestMethod]
public void Product_ShouldThrowArgumentNullException_GivenNullSource()
{
IEnumerable<ushort> source = null!;
Assert.ThrowsException<ArgumentNullException>(() => source.Product());
Assert.ThrowsException<ArgumentNullException>(() => source.Product(v => v));
}
} }

View File

@ -42,4 +42,12 @@ public class UInt32Tests
Assert.AreEqual(185794560U, Enumerable.Range(1, 9).Product(Double)); Assert.AreEqual(185794560U, Enumerable.Range(1, 9).Product(Double));
Assert.AreEqual(3715891200U, Enumerable.Range(1, 10).Product(Double)); Assert.AreEqual(3715891200U, Enumerable.Range(1, 10).Product(Double));
} }
[TestMethod]
public void Product_ShouldThrowArgumentNullException_GivenNullSource()
{
IEnumerable<uint> source = null!;
Assert.ThrowsException<ArgumentNullException>(() => source.Product());
Assert.ThrowsException<ArgumentNullException>(() => source.Product(v => v));
}
} }

View File

@ -42,4 +42,12 @@ public class UInt64Tests
Assert.AreEqual(185794560UL, Enumerable.Range(1, 9).Product(Double)); Assert.AreEqual(185794560UL, Enumerable.Range(1, 9).Product(Double));
Assert.AreEqual(3715891200UL, Enumerable.Range(1, 10).Product(Double)); Assert.AreEqual(3715891200UL, Enumerable.Range(1, 10).Product(Double));
} }
[TestMethod]
public void Product_ShouldThrowArgumentNullException_GivenNullSource()
{
IEnumerable<ulong> source = null!;
Assert.ThrowsException<ArgumentNullException>(() => source.Product());
Assert.ThrowsException<ArgumentNullException>(() => source.Product(v => v));
}
} }

View File

@ -85,6 +85,13 @@ public class ComparableTests
Assert.ThrowsException<ArgumentException>(() => 0.Clamp(6, 5)); Assert.ThrowsException<ArgumentException>(() => 0.Clamp(6, 5));
} }
[TestMethod]
public void Clamp_ShouldThrowArgumentNullException_GivenNullValue()
{
string comparable = null!;
Assert.ThrowsException<ArgumentNullException>(() => comparable.Clamp(string.Empty, string.Empty));
}
[TestMethod] [TestMethod]
public void GreaterThan_5_6_ShouldBeFalse() public void GreaterThan_5_6_ShouldBeFalse()
{ {

View File

@ -60,14 +60,16 @@ public class EnumerableTests
public void Grep_ShouldThrowArgumentNullException_GivenNullPattern() public void Grep_ShouldThrowArgumentNullException_GivenNullPattern()
{ {
IEnumerable<string> source = Enumerable.Empty<string>(); IEnumerable<string> source = Enumerable.Empty<string>();
Assert.ThrowsException<ArgumentNullException>(() => source.Grep(null!)); Assert.ThrowsException<ArgumentNullException>(() => source.Grep(null!).ToArray());
Assert.ThrowsException<ArgumentNullException>(() => source.Grep(null!, false).ToArray());
} }
[TestMethod] [TestMethod]
public void Grep_ShouldThrowArgumentNullException_GivenNullSource() public void Grep_ShouldThrowArgumentNullException_GivenNullSource()
{ {
IEnumerable<string> source = null!; IEnumerable<string> source = null!;
Assert.ThrowsException<ArgumentNullException>(() => source.Grep("foo")); Assert.ThrowsException<ArgumentNullException>(() => source.Grep("foo").ToArray());
Assert.ThrowsException<ArgumentNullException>(() => source.Grep("foo", false).ToArray());
} }
[TestMethod] [TestMethod]

View File

@ -380,6 +380,13 @@ public class StringTests
Assert.IsFalse("World".IsEmoji()); Assert.IsFalse("World".IsEmoji());
} }
[TestMethod]
public void IsEmoji_ShouldThrowArgumentNullException_GivenNullInput()
{
string value = null!;
Assert.ThrowsException<ArgumentNullException>(() => value.IsEmoji());
}
[TestMethod] [TestMethod]
public void IsEmpty_ShouldReturnTrue_GivenEmptyString() public void IsEmpty_ShouldReturnTrue_GivenEmptyString()
{ {

View File

@ -418,15 +418,6 @@ public static class EnumerableExtensions
private static bool TryGetSpan<TSource>(this IEnumerable<TSource> source, out ReadOnlySpan<TSource> span) private static bool TryGetSpan<TSource>(this IEnumerable<TSource> source, out ReadOnlySpan<TSource> span)
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
var result = true; var result = true;
switch (source) switch (source)