Skip to content

Commit

Permalink
fix(test): use new NUnit constraint API
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Nov 13, 2024
1 parent 0b978f5 commit a433244
Show file tree
Hide file tree
Showing 36 changed files with 457 additions and 312 deletions.
16 changes: 8 additions & 8 deletions X10D.Tests/src/Collections/EnumerableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ public void For_ShouldTransform_GivenTransformationDelegate()

IEnumerable<DummyClass> source = oneToTen.Select(i => new DummyClass {Value = i}).ToArray();
IEnumerable<int> values = source.Select(o => o.Value);
CollectionAssert.AreEqual(oneToTen, values.ToArray());
Assert.That(values.ToArray(), Is.EqualTo(oneToTen).AsCollection);

source.For((i, o) => o.Value *= i);
values = source.Select(o => o.Value);
CollectionAssert.AreEqual(multipliedByIndex, values.ToArray());
Assert.That(values.ToArray(), Is.EqualTo(multipliedByIndex).AsCollection);
}

[Test]
Expand All @@ -146,11 +146,11 @@ public void ForEach_ShouldTransform_GivenTransformationDelegate()

IEnumerable<DummyClass> source = oneToTen.Select(i => new DummyClass {Value = i}).ToArray();
IEnumerable<int> values = source.Select(o => o.Value);
CollectionAssert.AreEqual(oneToTen, values.ToArray());
Assert.That(values.ToArray(), Is.EqualTo(oneToTen).AsCollection);

source.ForEach(o => o.Value *= 2);
values = source.Select(o => o.Value);
CollectionAssert.AreEqual(oneToTenDoubled, values.ToArray());
Assert.That(values.ToArray(), Is.EqualTo(oneToTenDoubled).AsCollection);
}

[Test]
Expand Down Expand Up @@ -245,18 +245,18 @@ public void Shuffled_ShouldReorder_GivenNotNull()
int[] array = Enumerable.Range(1, 52).ToArray(); // 52! chance of being shuffled to the same order
int[] shuffled = array[..];

CollectionAssert.AreEqual(array, shuffled);
Assert.That(shuffled, Is.EqualTo(array).AsCollection);

shuffled = array.Shuffled().ToArray();
CollectionAssert.AreNotEqual(array, shuffled);
Assert.That(shuffled, Is.Not.EqualTo(array).AsCollection);
}

[Test]
public void WhereNot_ShouldReturnCorrectElements_GivenSequence()
{
var enumerable = new[] {2, 4, 6, 7, 8, 9, 10};
IEnumerable<int> result = enumerable.WhereNot(x => x % 2 == 0);
CollectionAssert.AreEqual(new[] {7, 9}, result.ToArray());
Assert.That(result.ToArray(), Is.EqualTo(new[] { 7, 9 }).AsCollection);
}

[Test]
Expand Down Expand Up @@ -285,7 +285,7 @@ public void WhereNotNull_ShouldContainNoNullElements()

foreach (object o in array.WhereNotNull())
{
Assert.IsNotNull(o);
Assert.That(o, Is.Not.Null);
actualCount++;
}

Expand Down
64 changes: 32 additions & 32 deletions X10D.Tests/src/Collections/ListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace X10D.Tests.Collections;
[TestFixture]
internal class ListTests
{
[Test]
[Test]
[TestCase(1)]
[TestCase(1, 2, 3)]
[TestCase(1, 2, 3, 4, 5)]
Expand All @@ -15,17 +15,17 @@ public void Fill_ShouldGiveHomogenousList_GivenValue(params int[] args)
int[] all42 = Enumerable.Repeat(42, args.Length).ToArray();
var list = new List<int>(args);

CollectionAssert.AreEqual(args, list);
Assert.That(list, Is.EqualTo(args).AsCollection);

args.Fill(42);
list.Fill(42);

CollectionAssert.AreEqual(args, list);
CollectionAssert.AreEqual(all42, args);
CollectionAssert.AreEqual(all42, list);
Assert.That(list, Is.EqualTo(args).AsCollection);
Assert.That(args, Is.EqualTo(all42).AsCollection);
Assert.That(list, Is.EqualTo(all42).AsCollection);
}

[Test]
[Test]
[TestCase(1)]
[TestCase(1, 2, 3)]
[TestCase(1, 2, 3, 4, 5)]
Expand All @@ -36,7 +36,7 @@ public void SlicedFill_ShouldLeaveFirstElement_GivenStartIndex1(params int[] arg

int[] comparison = Enumerable.Repeat(1, args.Length - 1).ToArray();
Assert.That(args[0], Is.EqualTo(first));
CollectionAssert.AreEqual(comparison, args[1..]);
Assert.That(args[1..], Is.EqualTo(comparison).AsCollection);
}

[Test]
Expand Down Expand Up @@ -80,7 +80,7 @@ public void Fill_ShouldThrow_GivenNull()
[Test]
public void IndexOf_ShouldReturnCorrectValue_FromStartOfList()
{
int[] array = {0, 1, 2, 3, 4};
int[] array = { 0, 1, 2, 3, 4 };
Assert.Multiple(() =>
{
Assert.That(array.IndexOf(2), Is.EqualTo(2));
Expand All @@ -92,7 +92,7 @@ public void IndexOf_ShouldReturnCorrectValue_FromStartOfList()
[Test]
public void IndexOf_ShouldReturnCorrectValue_GivenSubRange()
{
int[] array = {0, 1, 2, 3, 4, 0};
int[] array = { 0, 1, 2, 3, 4, 0 };
Assert.Multiple(() =>
{
Assert.That(array.IndexOf(0), Is.Zero);
Expand Down Expand Up @@ -149,7 +149,7 @@ public void IndexOf_ShouldThrowArgumentOutOfRangeException_GivenNegativeStartInd
[Test]
public void IndexOf_ShouldThrowArgumentOutOfRangeException_GivenInvalidStartIndexCountPair()
{
int[] array = {0, 1, 2};
int[] array = { 0, 1, 2 };
Assert.Throws<ArgumentOutOfRangeException>(() => array.IndexOf(0, 2, 4));
}

Expand Down Expand Up @@ -184,7 +184,7 @@ public void RemoveRange_ShouldThrowArgumentOutOfRangeException_GivenEndIndexLess
public void RemoveRange_ShouldThrowArgumentOutOfRangeException_GivenEndIndexGreaterThanOrEqualToCount()
{
Assert.Throws<ArgumentOutOfRangeException>(() => new List<int>().RemoveRange(..0));
Assert.Throws<ArgumentOutOfRangeException>(() => new List<int> {1}.RemoveRange(..2));
Assert.Throws<ArgumentOutOfRangeException>(() => new List<int> { 1 }.RemoveRange(..2));
}

[Test]
Expand All @@ -208,7 +208,7 @@ public void RemoveRange_ShouldRemoveElements_GivenList()
list.RemoveRange(2..5);

Assert.That(list, Has.Count.EqualTo(6));
CollectionAssert.AreEqual(new[] {1, 2, 7, 8, 9, 10}, list);
Assert.That(list, Is.EqualTo(new[] { 1, 2, 7, 8, 9, 10 }).AsCollection);
}

[Test]
Expand All @@ -217,11 +217,11 @@ public void Shuffle_ShouldReorder_GivenNotNull()
var list = new List<int>(Enumerable.Range(1, 52)); // 52! chance of being shuffled to the same order
var shuffled = new List<int>(list);

CollectionAssert.AreEqual(list, shuffled);
Assert.That(shuffled, Is.EqualTo(list).AsCollection);

shuffled.Shuffle();

CollectionAssert.AreNotEqual(list, shuffled);
Assert.That(shuffled, Is.Not.EqualTo(list).AsCollection);
}

[Test]
Expand All @@ -233,23 +233,23 @@ public void Shuffle_ShouldThrow_GivenNull()
[Test]
public void Slice_ShouldReturnCorrectValue_GivenStartIndex()
{
int[] array = {0, 1, 2, 3, 4, 5};
CollectionAssert.AreEqual(new[] {2, 3, 4, 5}, array.Slice(2).ToArray());
int[] array = { 0, 1, 2, 3, 4, 5 };
Assert.That(array.Slice(2).ToArray(), Is.EqualTo(new[] { 2, 3, 4, 5 }).AsCollection);
}

[Test]
public void Slice_ShouldReturnCorrectValue_GivenStartIndexAndLength()
{
int[] array = {0, 1, 2, 3, 4, 5};
CollectionAssert.AreEqual(new[] {2, 3, 4}, array.Slice(2, 3).ToArray());
int[] array = { 0, 1, 2, 3, 4, 5 };
Assert.That(array.Slice(2, 3).ToArray(), Is.EqualTo(new[] { 2, 3, 4 }).AsCollection);
}

[Test]
public void Slice_ShouldReturnEmptyList_ForEmptyList()
{
int[] array = Array.Empty<int>();
CollectionAssert.AreEqual(Array.Empty<int>(), array.Slice(0).ToArray());
CollectionAssert.AreEqual(Array.Empty<int>(), array.Slice(0, 0).ToArray());
Assert.That(array.Slice(0).ToArray(), Is.EqualTo(Array.Empty<int>()).AsCollection);
Assert.That(array.Slice(0, 0).ToArray(), Is.EqualTo(Array.Empty<int>()).AsCollection);
}

[Test]
Expand Down Expand Up @@ -278,7 +278,7 @@ public void Slice_ShouldThrowArgumentOutOfRangeException_GivenNegativeStartIndex
[Test]
public void Slice_ShouldThrowArgumentOutOfRangeException_GivenInvalidStartIndexCountPair()
{
int[] array = {0, 1, 2};
int[] array = { 0, 1, 2 };
Assert.Throws<ArgumentOutOfRangeException>(() => array.Slice(2, 4));
}

Expand All @@ -297,18 +297,18 @@ public void Swap_ShouldThrowArgumentNullException_GivenNullTarget()
[Test]
public void Swap_ShouldSwapElements_GivenMatchingElementCount()
{
var first = new List<int> {1, 2, 3};
var second = new List<int> {4, 5, 6};
var first = new List<int> { 1, 2, 3 };
var second = new List<int> { 4, 5, 6 };

first.Swap(second);

CollectionAssert.AreEqual(new[] {4, 5, 6}, first, string.Join(' ', first));
CollectionAssert.AreEqual(new[] {1, 2, 3}, second, string.Join(' ', second));
Assert.That(first, Is.EqualTo(new[] { 4, 5, 6 }).AsCollection, string.Join(' ', first));
Assert.That(second, Is.EqualTo(new[] { 1, 2, 3 }).AsCollection, string.Join(' ', second));

first.Swap(second);

CollectionAssert.AreEqual(new[] {1, 2, 3}, first, string.Join(' ', first));
CollectionAssert.AreEqual(new[] {4, 5, 6}, second, string.Join(' ', second));
Assert.That(first, Is.EqualTo(new[] { 1, 2, 3 }).AsCollection, string.Join(' ', first));
Assert.That(second, Is.EqualTo(new[] { 4, 5, 6 }).AsCollection, string.Join(' ', second));
}

[Test]
Expand All @@ -322,16 +322,16 @@ public void Swap_ShouldSwapElements_GivenDifferentElementCount()
4,
5
};
var second = new List<int> {6, 7};
var second = new List<int> { 6, 7 };

first.Swap(second);

CollectionAssert.AreEqual(new[] {6, 7}, first, string.Join(' ', first));
CollectionAssert.AreEqual(new[] {1, 2, 3, 4, 5}, second, string.Join(' ', second));
Assert.That(first, Is.EqualTo(new[] { 6, 7 }).AsCollection, string.Join(' ', first));
Assert.That(second, Is.EqualTo(new[] { 1, 2, 3, 4, 5 }).AsCollection, string.Join(' ', second));

first.Swap(second);

CollectionAssert.AreEqual(new[] {1, 2, 3, 4, 5}, first, string.Join(' ', first));
CollectionAssert.AreEqual(new[] {6, 7}, second, string.Join(' ', second));
Assert.That(first, Is.EqualTo(new[] { 1, 2, 3, 4, 5 }).AsCollection, string.Join(' ', first));
Assert.That(second, Is.EqualTo(new[] { 6, 7 }).AsCollection, string.Join(' ', second));
}
}
16 changes: 8 additions & 8 deletions X10D.Tests/src/Collections/SpanTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void Count_ShouldReturn0_GivenEmptyReadOnlySpan()
[Test]
public void Count_ShouldReturn8_GivenSpanWith8MatchingElements()
{
Span<int> span = stackalloc int[16] {1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2};
Span<int> span = stackalloc int[16] { 1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2 };

int count = span.Count(2);

Expand All @@ -41,7 +41,7 @@ public void Count_ShouldReturn8_GivenSpanWith8MatchingElements()
[Test]
public void Count_ShouldReturn8_GivenReadOnlySpanWith8MatchingElements()
{
ReadOnlySpan<int> span = stackalloc int[16] {1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2};
ReadOnlySpan<int> span = stackalloc int[16] { 1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2 };

int count = span.Count(2);

Expand All @@ -51,25 +51,25 @@ public void Count_ShouldReturn8_GivenReadOnlySpanWith8MatchingElements()
[Test]
public void Replace_ShouldReplaceAllElements_GivenSpanOfInt32()
{
Span<int> span = stackalloc int[16] {1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2};
Span<int> span = stackalloc int[16] { 1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2 };
span.Replace(2, 4);
Assert.That(span.ToArray(), Is.EqualTo(new[] {1, 4, 3, 4, 5, 4, 7, 4, 9, 4, 11, 4, 13, 4, 15, 4}));
Assert.That(span.ToArray(), Is.EqualTo(new[] { 1, 4, 3, 4, 5, 4, 7, 4, 9, 4, 11, 4, 13, 4, 15, 4 }));
}

[Test]
public void Replace_ShouldReplaceAllElements_GivenSpanOfChar()
{
Span<char> chars = stackalloc char[12] {'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!'};
Span<char> chars = stackalloc char[12] { 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!' };
chars.Replace('l', 'w');
CollectionAssert.AreEqual(chars.ToArray(), "Hewwo worwd!".ToCharArray());
Assert.That("Hewwo worwd!".ToCharArray(), Is.EqualTo(chars.ToArray()).AsCollection);
}

[Test]
public void Replace_ShouldDoNothing_GivenSpanWithNoMatchingElements()
{
Span<int> span = stackalloc int[16] {1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2};
Span<int> span = stackalloc int[16] { 1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2 };
span.Replace(4, 8);
Assert.That(span.ToArray(), Is.EqualTo(new[] {1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2}));
Assert.That(span.ToArray(), Is.EqualTo(new[] { 1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2 }));
}

#if !NET9_0_OR_GREATER
Expand Down
2 changes: 1 addition & 1 deletion X10D.Tests/src/Core/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void RepeatValue_ShouldContainRepeatedValue_GivenValue(object o)
// ReSharper disable once PossibleMultipleEnumeration
object[] array = enumerable.ToArray();
Assert.That(array, Has.Length.EqualTo(10));
CollectionAssert.AreEqual(new[] {o, o, o, o, o, o, o, o, o, o}, array);
Assert.That(array, Is.EqualTo(new[] { o, o, o, o, o, o, o, o, o, o }).AsCollection);
}

[Test]
Expand Down
1 change: 0 additions & 1 deletion X10D.Tests/src/Core/SpanTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;
using NUnit.Framework;
using X10D.Core;
Expand Down
6 changes: 3 additions & 3 deletions X10D.Tests/src/Drawing/PolygonFTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void CopyConstructor_ShouldCopyVertices_GivenPolygon()

// we cannot use CollectionAssert here for reasons I am not entirely sure of.
// it seems to dislike casting from IReadOnlyList<Point> to ICollection. but okay.
CollectionAssert.AreEqual(first.Vertices, second.Vertices);
Assert.That(second.Vertices, Is.EqualTo(first.Vertices).AsCollection);

// assert that the empty polygon was not modified
Assert.That(PolygonF.Empty.VertexCount, Is.Zero);
Expand Down Expand Up @@ -178,7 +178,7 @@ public void op_Explicit_ShouldReturnEquivalentCircle_GivenCircle()
Assert.That(converted, Is.EqualTo((Polygon)polygon));
Assert.That(converted.IsConvex, Is.EqualTo(polygon.IsConvex));
Assert.That(converted.VertexCount, Is.EqualTo(polygon.VertexCount));
CollectionAssert.AreEqual(polygon.Vertices, converted.Vertices.Select(p => (PointF)p));
Assert.That(converted.Vertices.Select(p => (PointF)p), Is.EqualTo(polygon.Vertices).AsCollection);
});
}

Expand All @@ -194,7 +194,7 @@ public void op_Implicit_ShouldReturnEquivalentCircle_GivenCircle()
Assert.That(converted == polygon);
Assert.That(converted.IsConvex, Is.EqualTo(polygon.IsConvex));
Assert.That(converted.VertexCount, Is.EqualTo(polygon.VertexCount));
CollectionAssert.AreEqual(converted.Vertices, polygon.Vertices.Select(p => (PointF)p));
Assert.That(polygon.Vertices.Select(p => (PointF)p), Is.EqualTo(converted.Vertices).AsCollection);
});
}

Expand Down
10 changes: 5 additions & 5 deletions X10D.Tests/src/Drawing/PolyhedronTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void CopyConstructor_ShouldCopyVertices_GivenPolyhedron()

// we cannot use CollectionAssert here for reasons I am not entirely sure of.
// it seems to dislike casting from IReadOnlyList<Point> to ICollection. but okay.
CollectionAssert.AreEqual(first.Vertices, second.Vertices);
Assert.That(second.Vertices, Is.EqualTo(first.Vertices).AsCollection);

// assert that the empty polyhedron was not modified
Assert.That(Polyhedron.Empty.VertexCount, Is.Zero);
Expand Down Expand Up @@ -148,11 +148,11 @@ public void op_Implicit_ShouldReturnEquivalentPolyhedron_GivenPolyhedron()
Assert.That(converted, Is.EqualTo((Polyhedron)polygon));
Assert.That(converted.VertexCount, Is.EqualTo(polygon.VertexCount));

CollectionAssert.AreEqual(converted.Vertices, polygon.Vertices.Select(p =>
Assert.That(polygon.Vertices.Select(p =>
{
var point = p.ToVector2();
return new Vector3(point.X, point.Y, 0);
}));
}), Is.EqualTo(converted.Vertices).AsCollection);
});
}

Expand All @@ -166,11 +166,11 @@ public void op_Implicit_ShouldReturnEquivalentPolyhedron_GivenPolyhedronF()
{
Assert.That(converted, Is.EqualTo((Polyhedron)polygon));
Assert.That(converted.VertexCount, Is.EqualTo(polygon.VertexCount));
CollectionAssert.AreEqual(converted.Vertices, polygon.Vertices.Select(p =>
Assert.That(converted.Vertices, Is.EqualTo(polygon.Vertices.Select(p =>
{
var point = p.ToVector2();
return new Vector3(point.X, point.Y, 0);
}));
})).AsCollection);
});
}

Expand Down
Loading

0 comments on commit a433244

Please sign in to comment.