diff --git a/X10D.Tests/src/Collections/EnumerableTests.cs b/X10D.Tests/src/Collections/EnumerableTests.cs index 6d990ee..4e95f49 100644 --- a/X10D.Tests/src/Collections/EnumerableTests.cs +++ b/X10D.Tests/src/Collections/EnumerableTests.cs @@ -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 source = oneToTen.Select(i => new DummyClass {Value = i}).ToArray(); + IEnumerable 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? source = null; + Assert.ThrowsException(() => source!.For((_, _) => { })); + } + + [TestMethod] + public void For_ShouldThrow_GivenNullAction() + { + IEnumerable source = ArraySegment.Empty; + Assert.ThrowsException(() => 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 source = oneToTen.Select(i => new DummyClass {Value = i}).ToArray(); + IEnumerable 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? source = null; + Assert.ThrowsException(() => source!.ForEach(_ => { })); + } + + [TestMethod] + public void ForEach_ShouldThrow_GivenNullAction() + { + IEnumerable source = ArraySegment.Empty; + Assert.ThrowsException(() => 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; } + } } diff --git a/X10D.Tests/src/Core/EnumerableTests.cs b/X10D.Tests/src/Core/EnumerableTests.cs deleted file mode 100644 index 8e85c2f..0000000 --- a/X10D.Tests/src/Core/EnumerableTests.cs +++ /dev/null @@ -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 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 source = oneToTen.Select(i => new DummyClass {Value = i}).ToArray(); - IEnumerable 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? source = null; - Assert.ThrowsException(() => source!.For((_, _) => { })); - } - - [TestMethod] - public void For_ShouldThrow_GivenNullAction() - { - IEnumerable source = ArraySegment.Empty; - Assert.ThrowsException(() => 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 source = oneToTen.Select(i => new DummyClass {Value = i}).ToArray(); - IEnumerable 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? source = null; - Assert.ThrowsException(() => source!.ForEach(_ => { })); - } - - [TestMethod] - public void ForEach_ShouldThrow_GivenNullAction() - { - IEnumerable source = ArraySegment.Empty; - Assert.ThrowsException(() => source.ForEach(null!)); - } - - private class DummyClass - { - public int Value { get; set; } - } -}