mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-23 00:18:47 +00:00
style(test): segment methods to partials
This changes introduces the Moq package to create mocked objects implementing IDisposable, rather than defining a concrete class.
This commit is contained in:
parent
626f1e931c
commit
62034ded75
@ -10,6 +10,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0"/>
|
||||
<PackageReference Include="Moq" Version="4.18.4"/>
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="3.0.2"/>
|
||||
<PackageReference Include="MSTest.TestFramework" Version="3.0.2"/>
|
||||
<PackageReference Include="coverlet.collector" Version="3.2.0">
|
||||
|
42
X10D.Tests/src/Collections/ArrayTests.AsReadOnly.cs
Normal file
42
X10D.Tests/src/Collections/ArrayTests.AsReadOnly.cs
Normal file
@ -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<int> result = array.AsReadOnly();
|
||||
Assert.IsInstanceOfType(result, typeof(IReadOnlyCollection<int>));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AsReadOnly_ShouldThrowArgumentNullException_WhenArrayIsNull()
|
||||
{
|
||||
int[]? array = null;
|
||||
Assert.ThrowsException<ArgumentNullException>(() => array!.AsReadOnly());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AsReadOnly_ShouldReturnCorrectCount_WhenArrayIsNotEmpty()
|
||||
{
|
||||
int[] array = {1, 2, 3};
|
||||
IReadOnlyCollection<int> result = array.AsReadOnly();
|
||||
Assert.AreEqual(array.Length, result.Count);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AsReadOnly_ShouldReturnEmptyCollection_WhenArrayIsEmpty()
|
||||
{
|
||||
int[] array = Array.Empty<int>();
|
||||
IReadOnlyCollection<int> result = array.AsReadOnly();
|
||||
Assert.AreEqual(0, result.Count);
|
||||
}
|
||||
}
|
||||
}
|
61
X10D.Tests/src/Collections/ArrayTests.Clear.cs
Normal file
61
X10D.Tests/src/Collections/ArrayTests.Clear.cs
Normal file
@ -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<int>();
|
||||
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<ArgumentNullException>(array!.Clear);
|
||||
}
|
||||
}
|
||||
}
|
@ -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<object>);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AsReadOnlyNullShouldThrow()
|
||||
{
|
||||
object[]? array = null;
|
||||
Assert.ThrowsException<ArgumentNullException>(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<ArgumentNullException>(array!.Clear);
|
||||
Assert.ThrowsException<ArgumentNullException>(() => array!.Clear(0, 0));
|
||||
}
|
||||
}
|
||||
|
@ -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<IDisposable>();
|
||||
var mock2 = new Mock<IDisposable>();
|
||||
var mock3 = new Mock<IDisposable>();
|
||||
var list = new List<IDisposable> {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<IDisposable>? list = null;
|
||||
Assert.ThrowsException<ArgumentNullException>(() => list!.ClearAndDisposeAll());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ClearAndDisposeAll_ShouldThrowInvalidOperationException_WhenCalledWithReadOnlyList()
|
||||
{
|
||||
var mock = new Mock<IDisposable>();
|
||||
var list = new ReadOnlyCollection<IDisposable>(new List<IDisposable> {mock.Object});
|
||||
|
||||
Assert.ThrowsException<InvalidOperationException>(() => list.ClearAndDisposeAll());
|
||||
}
|
||||
}
|
||||
}
|
@ -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<IAsyncDisposable>();
|
||||
var mock2 = new Mock<IAsyncDisposable>();
|
||||
var mock3 = new Mock<IAsyncDisposable>();
|
||||
var list = new List<IAsyncDisposable> {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<IAsyncDisposable>? list = null;
|
||||
await Assert.ThrowsExceptionAsync<ArgumentNullException>(list!.ClearAndDisposeAllAsync).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task ClearAndDisposeAllAsync_ShouldThrowInvalidOperationException_WhenCalledWithReadOnlyList()
|
||||
{
|
||||
var mock = new Mock<IAsyncDisposable>();
|
||||
var list = new ReadOnlyCollection<IAsyncDisposable>(new List<IAsyncDisposable> {mock.Object});
|
||||
|
||||
await Assert.ThrowsExceptionAsync<InvalidOperationException>(list.ClearAndDisposeAllAsync).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
@ -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<Disposable> {new(), new(), new()};
|
||||
var copy = new List<Disposable>(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<Disposable> {new(), new(), new()};
|
||||
var copy = new List<Disposable>(collection);
|
||||
|
||||
await collection.ClearAndDisposeAllAsync();
|
||||
|
||||
Assert.IsTrue(copy.All(x => x.IsDisposed));
|
||||
Assert.AreEqual(0, collection.Count);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ClearAndDisposeAll_ShouldThrow_GivenNull()
|
||||
{
|
||||
List<Disposable>? collection = null;
|
||||
Assert.ThrowsException<ArgumentNullException>(() => collection!.ClearAndDisposeAll());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ClearAndDisposeAllAsync_ShouldThrow_GivenNull()
|
||||
{
|
||||
List<Disposable>? collection = null;
|
||||
Assert.ThrowsExceptionAsync<ArgumentNullException>(async () => await collection!.ClearAndDisposeAllAsync());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ClearAndDisposeAll_ShouldThrow_GivenReadOnlyCollection()
|
||||
{
|
||||
var collection = new List<Disposable>().AsReadOnly();
|
||||
Assert.ThrowsException<InvalidOperationException>(() => collection.ClearAndDisposeAll());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ClearAndDisposeAllAsync_ShouldThrow_GivenReadOnlyCollection()
|
||||
{
|
||||
var collection = new List<Disposable>().AsReadOnly();
|
||||
Assert.ThrowsExceptionAsync<InvalidOperationException>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
34
X10D.Tests/src/Collections/EnumerableTests.DisposeAll.cs
Normal file
34
X10D.Tests/src/Collections/EnumerableTests.DisposeAll.cs
Normal file
@ -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<IDisposable>();
|
||||
var mock2 = new Mock<IDisposable>();
|
||||
var mock3 = new Mock<IDisposable>();
|
||||
var list = new List<IDisposable> {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<IDisposable>? list = null;
|
||||
Assert.ThrowsException<ArgumentNullException>(() => list!.DisposeAll());
|
||||
}
|
||||
}
|
||||
}
|
@ -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<IAsyncDisposable>();
|
||||
var mock2 = new Mock<IAsyncDisposable>();
|
||||
var mock3 = new Mock<IAsyncDisposable>();
|
||||
var list = new List<IAsyncDisposable> {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<IAsyncDisposable>? list = null;
|
||||
await Assert.ThrowsExceptionAsync<ArgumentNullException>(() => list!.DisposeAllAsync()).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
@ -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<OverflowException>(() => GetValues().CountWhereNot(x => x % 2 == 0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DisposeAll_ShouldDispose_GivenCollection()
|
||||
{
|
||||
var collection = new List<Disposable> {new(), new(), new()};
|
||||
collection.DisposeAll();
|
||||
Assert.IsTrue(collection.All(x => x.IsDisposed));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task DisposeAllAsync_ShouldDispose_GivenCollection()
|
||||
{
|
||||
var collection = new List<Disposable> {new(), new(), new()};
|
||||
await collection.DisposeAllAsync();
|
||||
Assert.IsTrue(collection.All(x => x.IsDisposed));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DisposeAll_ShouldThrow_GivenNull()
|
||||
{
|
||||
List<Disposable>? collection = null;
|
||||
Assert.ThrowsException<ArgumentNullException>(() => collection!.DisposeAll());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task DisposeAllAsync_ShouldThrow_GivenNull()
|
||||
{
|
||||
List<Disposable>? collection = null;
|
||||
await Assert.ThrowsExceptionAsync<ArgumentNullException>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user