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

Move EnumerableTests to Collections namespace

This commit is contained in:
Oliver Booth 2022-05-02 22:27:21 +01:00
parent ed27044a3b
commit fd53b41610
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
2 changed files with 64 additions and 85 deletions

View File

@ -1,4 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Collections;
namespace X10D.Tests.Collections;
@ -6,6 +6,64 @@ namespace X10D.Tests.Collections;
[TestClass]
public class EnumerableTests
{
[TestMethod]
public void For_ShouldTransform_GivenTransformationDelegate()
{
var oneToTen = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
var multipliedByIndex = new[] {0, 2, 6, 12, 20, 30, 42, 56, 72, 90};
IEnumerable<DummyClass> source = oneToTen.Select(i => new DummyClass {Value = i}).ToArray();
IEnumerable<int> values = source.Select(o => o.Value);
CollectionAssert.AreEqual(oneToTen, values.ToArray());
source.For((i, o) => o.Value *= i);
values = source.Select(o => o.Value);
CollectionAssert.AreEqual(multipliedByIndex, values.ToArray());
}
[TestMethod]
public void For_ShouldThrow_GivenNullSource()
{
IEnumerable<object>? source = null;
Assert.ThrowsException<ArgumentNullException>(() => source!.For((_, _) => { }));
}
[TestMethod]
public void For_ShouldThrow_GivenNullAction()
{
IEnumerable<object> source = ArraySegment<object>.Empty;
Assert.ThrowsException<ArgumentNullException>(() => source.For(null!));
}
[TestMethod]
public void ForEach_ShouldTransform_GivenTransformationDelegate()
{
var oneToTen = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
var oneToTenDoubled = new[] {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
IEnumerable<DummyClass> source = oneToTen.Select(i => new DummyClass {Value = i}).ToArray();
IEnumerable<int> values = source.Select(o => o.Value);
CollectionAssert.AreEqual(oneToTen, values.ToArray());
source.ForEach(o => o.Value *= 2);
values = source.Select(o => o.Value);
CollectionAssert.AreEqual(oneToTenDoubled, values.ToArray());
}
[TestMethod]
public void ForEach_ShouldThrow_GivenNullSource()
{
IEnumerable<object>? source = null;
Assert.ThrowsException<ArgumentNullException>(() => source!.ForEach(_ => { }));
}
[TestMethod]
public void ForEach_ShouldThrow_GivenNullAction()
{
IEnumerable<object> source = ArraySegment<object>.Empty;
Assert.ThrowsException<ArgumentNullException>(() => source.ForEach(null!));
}
[TestMethod]
public void Shuffled_ShouldThrow_GivenNull()
{
@ -23,4 +81,9 @@ public class EnumerableTests
shuffled = array.Shuffled().ToArray();
CollectionAssert.AreNotEqual(array, shuffled);
}
private class DummyClass
{
public int Value { get; set; }
}
}

View File

@ -1,84 +0,0 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Collections;
using X10D.Core;
namespace X10D.Tests.Core;
[TestClass]
public class EnumerableTests
{
[TestMethod]
[DataRow(1)]
[DataRow("f")]
[DataRow(true)]
public void AsEnumerable_ShouldWrapElement_GivenValue(object o)
{
IEnumerable<object?> array = o.AsEnumerableValue().ToArray(); // prevent multiple enumeration of IEnumerable
Assert.IsNotNull(array);
Assert.IsTrue(array.Count() == 1);
Assert.AreEqual(o, array.ElementAt(0));
}
[TestMethod]
public void For_ShouldTransform_GivenTransformationDelegate()
{
var oneToTen = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
var multipliedByIndex = new[] {0, 2, 6, 12, 20, 30, 42, 56, 72, 90};
IEnumerable<DummyClass> source = oneToTen.Select(i => new DummyClass {Value = i}).ToArray();
IEnumerable<int> values = source.Select(o => o.Value);
CollectionAssert.AreEqual(oneToTen, values.ToArray());
source.For((i, o) => o.Value *= i);
values = source.Select(o => o.Value);
CollectionAssert.AreEqual(multipliedByIndex, values.ToArray());
}
[TestMethod]
public void For_ShouldThrow_GivenNullSource()
{
IEnumerable<object>? source = null;
Assert.ThrowsException<ArgumentNullException>(() => source!.For((_, _) => { }));
}
[TestMethod]
public void For_ShouldThrow_GivenNullAction()
{
IEnumerable<object> source = ArraySegment<object>.Empty;
Assert.ThrowsException<ArgumentNullException>(() => source.For(null!));
}
[TestMethod]
public void ForEach_ShouldTransform_GivenTransformationDelegate()
{
var oneToTen = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
var oneToTenDoubled = new[] {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
IEnumerable<DummyClass> source = oneToTen.Select(i => new DummyClass {Value = i}).ToArray();
IEnumerable<int> values = source.Select(o => o.Value);
CollectionAssert.AreEqual(oneToTen, values.ToArray());
source.ForEach(o => o.Value *= 2);
values = source.Select(o => o.Value);
CollectionAssert.AreEqual(oneToTenDoubled, values.ToArray());
}
[TestMethod]
public void ForEach_ShouldThrow_GivenNullSource()
{
IEnumerable<object>? source = null;
Assert.ThrowsException<ArgumentNullException>(() => source!.ForEach(_ => { }));
}
[TestMethod]
public void ForEach_ShouldThrow_GivenNullAction()
{
IEnumerable<object> source = ArraySegment<object>.Empty;
Assert.ThrowsException<ArgumentNullException>(() => source.ForEach(null!));
}
private class DummyClass
{
public int Value { get; set; }
}
}