diff --git a/X10D.Tests/X10D.Tests.csproj b/X10D.Tests/X10D.Tests.csproj index e547f07..850a144 100644 --- a/X10D.Tests/X10D.Tests.csproj +++ b/X10D.Tests/X10D.Tests.csproj @@ -10,6 +10,7 @@ + diff --git a/X10D.Tests/src/Collections/ArrayTests.AsReadOnly.cs b/X10D.Tests/src/Collections/ArrayTests.AsReadOnly.cs new file mode 100644 index 0000000..ed46847 --- /dev/null +++ b/X10D.Tests/src/Collections/ArrayTests.AsReadOnly.cs @@ -0,0 +1,42 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.Collections; + +namespace X10D.Tests.Collections; + +public partial class ArrayTests +{ + [TestClass] + public class AsReadOnlyTests + { + [TestMethod] + public void AsReadOnly_ShouldReturnReadOnlyCollection_WhenArrayIsNotNull() + { + int[] array = {1, 2, 3}; + IReadOnlyCollection result = array.AsReadOnly(); + Assert.IsInstanceOfType(result, typeof(IReadOnlyCollection)); + } + + [TestMethod] + public void AsReadOnly_ShouldThrowArgumentNullException_WhenArrayIsNull() + { + int[]? array = null; + Assert.ThrowsException(() => array!.AsReadOnly()); + } + + [TestMethod] + public void AsReadOnly_ShouldReturnCorrectCount_WhenArrayIsNotEmpty() + { + int[] array = {1, 2, 3}; + IReadOnlyCollection result = array.AsReadOnly(); + Assert.AreEqual(array.Length, result.Count); + } + + [TestMethod] + public void AsReadOnly_ShouldReturnEmptyCollection_WhenArrayIsEmpty() + { + int[] array = Array.Empty(); + IReadOnlyCollection result = array.AsReadOnly(); + Assert.AreEqual(0, result.Count); + } + } +} diff --git a/X10D.Tests/src/Collections/ArrayTests.Clear.cs b/X10D.Tests/src/Collections/ArrayTests.Clear.cs new file mode 100644 index 0000000..174e64d --- /dev/null +++ b/X10D.Tests/src/Collections/ArrayTests.Clear.cs @@ -0,0 +1,61 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.Collections; + +namespace X10D.Tests.Collections; + +public partial class ArrayTests +{ + [TestClass] + public class ClearTests + { + [TestMethod] + public void Clear_ShouldClearTheArray() + { + var array = new int?[] {1, 2, 3, null, 4}; + + array.Clear(); + + Assert.IsTrue(array.All(x => x == null)); + } + + [TestMethod] + public void Clear_ShouldDoNothing_WhenArrayIsEmpty() + { + int[] array = Array.Empty(); + array.Clear(); + } + + [TestMethod] + public void Clear_WithRange_ShouldClearTheSpecifiedRangeOfTheArray() + { + var array = new int?[] {1, 2, 3, null, 4}; + + array.Clear(1..4); + + Assert.AreEqual(5, array.Length); + Assert.AreEqual(1, array[0]); + Assert.AreEqual(4, array[4]); + Assert.IsTrue(array[1..4].All(x => x == null)); + } + + [TestMethod] + public void Clear_WithIndexAndLength_ShouldClearTheSpecifiedRangeOfTheArray() + { + var array = new int?[] {1, 2, 3, null, 4}; + + array.Clear(1, 3); + + Assert.AreEqual(5, array.Length); + Assert.AreEqual(1, array[0]); + Assert.AreEqual(4, array[4]); + Assert.IsTrue(array[1..4].All(x => x == null)); + } + + [TestMethod] + public void Clear_ShouldThrowArgumentNullException_WhenArrayIsNull() + { + int[]? array = null; + Assert.ThrowsException(array!.Clear); + } + } +} diff --git a/X10D.Tests/src/Collections/ArrayTests.cs b/X10D.Tests/src/Collections/ArrayTests.cs index d5d745f..cdeb996 100644 --- a/X10D.Tests/src/Collections/ArrayTests.cs +++ b/X10D.Tests/src/Collections/ArrayTests.cs @@ -1,49 +1,8 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using X10D.Collections; namespace X10D.Tests.Collections; [TestClass] -public class ArrayTests +public partial class ArrayTests { - [TestMethod] - public void AsReadOnlyShouldBeReadOnly() - { - var array = new object[] {1, "f", true}; - var readOnly = array.AsReadOnly(); - Assert.IsNotNull(readOnly); - Assert.IsTrue(readOnly.Count == 3); - - // ReSharper disable once ConvertTypeCheckToNullCheck - Assert.IsTrue(readOnly is IReadOnlyCollection); - } - - [TestMethod] - public void AsReadOnlyNullShouldThrow() - { - object[]? array = null; - Assert.ThrowsException(array!.AsReadOnly); - } - - [CLSCompliant(false)] - [TestMethod] - [DataRow] - [DataRow(1)] - [DataRow(1, 2, 3)] - [DataRow(1, 2, 3, 4, 5)] - public void ClearShouldFillDefault(params int[] args) - { - args.Clear(); - - int[] clearedArray = Enumerable.Repeat(0, args.Length).ToArray(); - CollectionAssert.AreEqual(clearedArray, args); - } - - [TestMethod] - public void ClearNullShouldThrow() - { - int[]? array = null; - Assert.ThrowsException(array!.Clear); - Assert.ThrowsException(() => array!.Clear(0, 0)); - } } diff --git a/X10D.Tests/src/Collections/CollectionTests.ClearAndDisposeAll.cs b/X10D.Tests/src/Collections/CollectionTests.ClearAndDisposeAll.cs new file mode 100644 index 0000000..a0bed51 --- /dev/null +++ b/X10D.Tests/src/Collections/CollectionTests.ClearAndDisposeAll.cs @@ -0,0 +1,45 @@ +using System.Collections.ObjectModel; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using X10D.Collections; + +namespace X10D.Tests.Collections; + +public partial class CollectionTests +{ + [TestClass] + public class ClearAndDisposeAllTests + { + [TestMethod] + public void ClearAndDisposeAll_ShouldClearAndDisposeAllItems_WhenCalledWithValidList() + { + var mock1 = new Mock(); + var mock2 = new Mock(); + var mock3 = new Mock(); + var list = new List {mock1.Object, mock2.Object, mock3.Object}; + + list.ClearAndDisposeAll(); + + mock1.Verify(i => i.Dispose(), Times.Once); + mock2.Verify(i => i.Dispose(), Times.Once); + mock3.Verify(i => i.Dispose(), Times.Once); + Assert.AreEqual(0, list.Count); + } + + [TestMethod] + public void ClearAndDisposeAll_ShouldThrowArgumentNullException_WhenCalledWithNullList() + { + List? list = null; + Assert.ThrowsException(() => list!.ClearAndDisposeAll()); + } + + [TestMethod] + public void ClearAndDisposeAll_ShouldThrowInvalidOperationException_WhenCalledWithReadOnlyList() + { + var mock = new Mock(); + var list = new ReadOnlyCollection(new List {mock.Object}); + + Assert.ThrowsException(() => list.ClearAndDisposeAll()); + } + } +} diff --git a/X10D.Tests/src/Collections/CollectionTests.ClearAndDisposeAllAsync.cs b/X10D.Tests/src/Collections/CollectionTests.ClearAndDisposeAllAsync.cs new file mode 100644 index 0000000..1301763 --- /dev/null +++ b/X10D.Tests/src/Collections/CollectionTests.ClearAndDisposeAllAsync.cs @@ -0,0 +1,45 @@ +using System.Collections.ObjectModel; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using X10D.Collections; + +namespace X10D.Tests.Collections; + +public partial class CollectionTests +{ + [TestClass] + public class ClearAndDisposeAllAsyncTests + { + [TestMethod] + public async Task ClearAndDisposeAllAsync_ShouldClearAndDisposeAllItems_WhenCalledWithValidList() + { + var mock1 = new Mock(); + var mock2 = new Mock(); + var mock3 = new Mock(); + var list = new List {mock1.Object, mock2.Object, mock3.Object}; + + await list.ClearAndDisposeAllAsync().ConfigureAwait(false); + + mock1.Verify(i => i.DisposeAsync(), Times.Once); + mock2.Verify(i => i.DisposeAsync(), Times.Once); + mock3.Verify(i => i.DisposeAsync(), Times.Once); + Assert.AreEqual(0, list.Count); + } + + [TestMethod] + public async Task ClearAndDisposeAllAsync_ShouldThrowArgumentNullException_WhenCalledWithNullList() + { + List? list = null; + await Assert.ThrowsExceptionAsync(list!.ClearAndDisposeAllAsync).ConfigureAwait(false); + } + + [TestMethod] + public async Task ClearAndDisposeAllAsync_ShouldThrowInvalidOperationException_WhenCalledWithReadOnlyList() + { + var mock = new Mock(); + var list = new ReadOnlyCollection(new List {mock.Object}); + + await Assert.ThrowsExceptionAsync(list.ClearAndDisposeAllAsync).ConfigureAwait(false); + } + } +} diff --git a/X10D.Tests/src/Collections/CollectionTests.cs b/X10D.Tests/src/Collections/CollectionTests.cs index 835a6d6..9642b16 100644 --- a/X10D.Tests/src/Collections/CollectionTests.cs +++ b/X10D.Tests/src/Collections/CollectionTests.cs @@ -1,77 +1,8 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using X10D.Collections; namespace X10D.Tests.Collections; [TestClass] -public class CollectionTests +public partial class CollectionTests { - [TestMethod] - public void ClearAndDisposeAll_ShouldDispose_GivenCollection() - { - var collection = new List {new(), new(), new()}; - var copy = new List(collection); - - collection.ClearAndDisposeAll(); - - Assert.IsTrue(copy.All(x => x.IsDisposed)); - Assert.AreEqual(0, collection.Count); - } - - [TestMethod] - public async Task ClearAndDisposeAllAsync_ShouldDispose_GivenCollection() - { - var collection = new List {new(), new(), new()}; - var copy = new List(collection); - - await collection.ClearAndDisposeAllAsync(); - - Assert.IsTrue(copy.All(x => x.IsDisposed)); - Assert.AreEqual(0, collection.Count); - } - - [TestMethod] - public void ClearAndDisposeAll_ShouldThrow_GivenNull() - { - List? collection = null; - Assert.ThrowsException(() => collection!.ClearAndDisposeAll()); - } - - [TestMethod] - public void ClearAndDisposeAllAsync_ShouldThrow_GivenNull() - { - List? collection = null; - Assert.ThrowsExceptionAsync(async () => await collection!.ClearAndDisposeAllAsync()); - } - - [TestMethod] - public void ClearAndDisposeAll_ShouldThrow_GivenReadOnlyCollection() - { - var collection = new List().AsReadOnly(); - Assert.ThrowsException(() => collection.ClearAndDisposeAll()); - } - - [TestMethod] - public void ClearAndDisposeAllAsync_ShouldThrow_GivenReadOnlyCollection() - { - var collection = new List().AsReadOnly(); - Assert.ThrowsExceptionAsync(async () => await collection.ClearAndDisposeAllAsync()); - } - - private class Disposable : IDisposable, IAsyncDisposable - { - public bool IsDisposed { get; private set; } - - public void Dispose() - { - Assert.IsTrue(IsDisposed = true); - } - -#pragma warning disable CS1998 - public async ValueTask DisposeAsync() -#pragma warning restore CS1998 - { - Assert.IsTrue(IsDisposed = true); - } - } } diff --git a/X10D.Tests/src/Collections/EnumerableTests.DisposeAll.cs b/X10D.Tests/src/Collections/EnumerableTests.DisposeAll.cs new file mode 100644 index 0000000..f616b62 --- /dev/null +++ b/X10D.Tests/src/Collections/EnumerableTests.DisposeAll.cs @@ -0,0 +1,34 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using X10D.Collections; + +namespace X10D.Tests.Collections; + +public partial class EnumerableTests +{ + [TestClass] + public class DisposeAllTests + { + [TestMethod] + public void DisposeAll_ShouldDisposeAllItems_WhenCalledWithValidList() + { + var mock1 = new Mock(); + var mock2 = new Mock(); + var mock3 = new Mock(); + var list = new List {mock1.Object, mock2.Object, null!, mock3.Object}; + + list.DisposeAll(); + + mock1.Verify(i => i.Dispose(), Times.Once); + mock2.Verify(i => i.Dispose(), Times.Once); + mock3.Verify(i => i.Dispose(), Times.Once); + } + + [TestMethod] + public void DisposeAll_ShouldThrowArgumentNullException_WhenCalledWithNullList() + { + List? list = null; + Assert.ThrowsException(() => list!.DisposeAll()); + } + } +} diff --git a/X10D.Tests/src/Collections/EnumerableTests.DisposeAllAsync.cs b/X10D.Tests/src/Collections/EnumerableTests.DisposeAllAsync.cs new file mode 100644 index 0000000..d683940 --- /dev/null +++ b/X10D.Tests/src/Collections/EnumerableTests.DisposeAllAsync.cs @@ -0,0 +1,34 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using X10D.Collections; + +namespace X10D.Tests.Collections; + +public partial class EnumerableTests +{ + [TestClass] + public class DisposeAllAsyncTests + { + [TestMethod] + public async Task DisposeAllAsync_ShouldDisposeAllItems_WhenCalledWithValidList() + { + var mock1 = new Mock(); + var mock2 = new Mock(); + var mock3 = new Mock(); + var list = new List {mock1.Object, mock2.Object, null!, mock3.Object}; + + await list.DisposeAllAsync().ConfigureAwait(false); + + mock1.Verify(i => i.DisposeAsync(), Times.Once); + mock2.Verify(i => i.DisposeAsync(), Times.Once); + mock3.Verify(i => i.DisposeAsync(), Times.Once); + } + + [TestMethod] + public async Task DisposeAllAsync_ShouldThrowArgumentNullException_WhenCalledWithNullList() + { + List? list = null; + await Assert.ThrowsExceptionAsync(() => list!.DisposeAllAsync()).ConfigureAwait(false); + } + } +} diff --git a/X10D.Tests/src/Collections/EnumerableTests.cs b/X10D.Tests/src/Collections/EnumerableTests.cs index 2678652..8b835a1 100644 --- a/X10D.Tests/src/Collections/EnumerableTests.cs +++ b/X10D.Tests/src/Collections/EnumerableTests.cs @@ -5,7 +5,7 @@ using X10D.Core; namespace X10D.Tests.Collections; [TestClass] -public class EnumerableTests +public partial class EnumerableTests { [TestMethod] public void CountWhereNot_ShouldReturnCorrectCount_GivenSequence() @@ -37,36 +37,6 @@ public class EnumerableTests Assert.ThrowsException(() => GetValues().CountWhereNot(x => x % 2 == 0)); } - [TestMethod] - public void DisposeAll_ShouldDispose_GivenCollection() - { - var collection = new List {new(), new(), new()}; - collection.DisposeAll(); - Assert.IsTrue(collection.All(x => x.IsDisposed)); - } - - [TestMethod] - public async Task DisposeAllAsync_ShouldDispose_GivenCollection() - { - var collection = new List {new(), new(), new()}; - await collection.DisposeAllAsync(); - Assert.IsTrue(collection.All(x => x.IsDisposed)); - } - - [TestMethod] - public void DisposeAll_ShouldThrow_GivenNull() - { - List? collection = null; - Assert.ThrowsException(() => collection!.DisposeAll()); - } - - [TestMethod] - public async Task DisposeAllAsync_ShouldThrow_GivenNull() - { - List? collection = null; - await Assert.ThrowsExceptionAsync(async () => await collection!.DisposeAllAsync()); - } - [TestMethod] public void FirstWhereNot_ShouldReturnCorrectElements_GivenSequence() { @@ -314,21 +284,4 @@ public class EnumerableTests { public int Value { get; set; } } - - private class Disposable : IDisposable, IAsyncDisposable - { - public bool IsDisposed { get; private set; } - - public void Dispose() - { - Assert.IsTrue(IsDisposed = true); - } - -#pragma warning disable CS1998 - public async ValueTask DisposeAsync() -#pragma warning restore CS1998 - { - Assert.IsTrue(IsDisposed = true); - } - } }