diff --git a/tests/CommunityToolkit.Common.UnitTests/CommunityToolkit.Common.UnitTests.csproj b/tests/CommunityToolkit.Common.UnitTests/CommunityToolkit.Common.UnitTests.csproj index 04243a107..3899341d0 100644 --- a/tests/CommunityToolkit.Common.UnitTests/CommunityToolkit.Common.UnitTests.csproj +++ b/tests/CommunityToolkit.Common.UnitTests/CommunityToolkit.Common.UnitTests.csproj @@ -5,8 +5,8 @@ - - + + diff --git a/tests/CommunityToolkit.Common.UnitTests/Extensions/Test_ArrayExtensions.cs b/tests/CommunityToolkit.Common.UnitTests/Extensions/Test_ArrayExtensions.cs index 35a2a5144..fd2813693 100644 --- a/tests/CommunityToolkit.Common.UnitTests/Extensions/Test_ArrayExtensions.cs +++ b/tests/CommunityToolkit.Common.UnitTests/Extensions/Test_ArrayExtensions.cs @@ -37,12 +37,12 @@ public void Test_ArrayExtensions_Jagged_GetColumn_Exception() new int[] { 7 } }; - _ = Assert.ThrowsException(() => + _ = Assert.ThrowsExactly(() => { _ = array.GetColumn(-1).ToArray(); }); - _ = Assert.ThrowsException(() => + _ = Assert.ThrowsExactly(() => { _ = array.GetColumn(3).ToArray(); }); diff --git a/tests/CommunityToolkit.Diagnostics.UnitTests/CommunityToolkit.Diagnostics.UnitTests.csproj b/tests/CommunityToolkit.Diagnostics.UnitTests/CommunityToolkit.Diagnostics.UnitTests.csproj index 7135ee3eb..97eca8abe 100644 --- a/tests/CommunityToolkit.Diagnostics.UnitTests/CommunityToolkit.Diagnostics.UnitTests.csproj +++ b/tests/CommunityToolkit.Diagnostics.UnitTests/CommunityToolkit.Diagnostics.UnitTests.csproj @@ -5,8 +5,8 @@ - - + + diff --git a/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.Array.cs b/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.Array.cs index d571105ae..41997cdcb 100644 --- a/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.Array.cs +++ b/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.Array.cs @@ -16,10 +16,9 @@ public void Test_Guard_IsEmpty_ArrayOk() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsEmpty_ArrayFail() { - Guard.IsEmpty(new int[1], nameof(Test_Guard_IsEmpty_ArrayFail)); + _ = Assert.ThrowsExactly(() => Guard.IsEmpty(new int[1], nameof(Test_Guard_IsEmpty_ArrayFail))); } [TestMethod] @@ -29,10 +28,9 @@ public void Test_Guard_IsNotEmpty_ArrayOk() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsNotEmpty_ArrayFail() { - Guard.IsNotEmpty(new int[0], nameof(Test_Guard_IsNotEmpty_ArrayFail)); + _ = Assert.ThrowsExactly(() => Guard.IsNotEmpty(new int[0], nameof(Test_Guard_IsNotEmpty_ArrayFail))); } [TestMethod] @@ -42,10 +40,9 @@ public void Test_Guard_HasSizeEqualTo_ArrayOk() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_HasSizeEqualTo_ArrayFail() { - Guard.HasSizeEqualTo(new int[3], 4, nameof(Test_Guard_HasSizeEqualTo_ArrayOk)); + _ = Assert.ThrowsExactly(() => Guard.HasSizeEqualTo(new int[3], 4, nameof(Test_Guard_HasSizeEqualTo_ArrayOk))); } [TestMethod] @@ -55,10 +52,9 @@ public void Test_Guard_HasSizeNotEqualTo_ArrayOk() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_HasSizeNotEqualTo_ArrayFail() { - Guard.HasSizeNotEqualTo(new int[4], 4, nameof(Test_Guard_HasSizeNotEqualTo_ArrayFail)); + _ = Assert.ThrowsExactly(() => Guard.HasSizeNotEqualTo(new int[4], 4, nameof(Test_Guard_HasSizeNotEqualTo_ArrayFail))); } [TestMethod] @@ -68,17 +64,15 @@ public void Test_Guard_HasSizeGreaterThan_ArrayOk() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_HasSizeGreaterThan_ArrayEqualFail() { - Guard.HasSizeGreaterThan(new int[4], 4, nameof(Test_Guard_HasSizeGreaterThan_ArrayEqualFail)); + _ = Assert.ThrowsExactly(() => Guard.HasSizeGreaterThan(new int[4], 4, nameof(Test_Guard_HasSizeGreaterThan_ArrayEqualFail))); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_HasSizeGreaterThan_ArraySmallerFail() { - Guard.HasSizeGreaterThan(new int[1], 4, nameof(Test_Guard_HasSizeGreaterThan_ArraySmallerFail)); + _ = Assert.ThrowsExactly(() => Guard.HasSizeGreaterThan(new int[1], 4, nameof(Test_Guard_HasSizeGreaterThan_ArraySmallerFail))); } [TestMethod] @@ -89,10 +83,9 @@ public void Test_Guard_HasSizeGreaterThanOrEqualTo_ArrayOk() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_HasSizeGreaterThanOrEqualTo_ArrayFail() { - Guard.HasSizeGreaterThan(new int[1], 4, nameof(Test_Guard_HasSizeGreaterThanOrEqualTo_ArrayFail)); + _ = Assert.ThrowsExactly(() => Guard.HasSizeGreaterThan(new int[1], 4, nameof(Test_Guard_HasSizeGreaterThanOrEqualTo_ArrayFail))); } [TestMethod] @@ -102,17 +95,15 @@ public void Test_Guard_HasSizeLessThan_ArrayOk() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_HasSizeLessThan_ArrayEqualFail() { - Guard.HasSizeLessThan(new int[4], 4, nameof(Test_Guard_HasSizeLessThan_ArrayEqualFail)); + _ = Assert.ThrowsExactly(() => Guard.HasSizeLessThan(new int[4], 4, nameof(Test_Guard_HasSizeLessThan_ArrayEqualFail))); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_HasSizeLessThan_ArrayGreaterFail() { - Guard.HasSizeLessThan(new int[6], 4, nameof(Test_Guard_HasSizeLessThan_ArrayGreaterFail)); + _ = Assert.ThrowsExactly(() => Guard.HasSizeLessThan(new int[6], 4, nameof(Test_Guard_HasSizeLessThan_ArrayGreaterFail))); } [TestMethod] @@ -123,10 +114,9 @@ public void Test_Guard_HasSizeLessThanOrEqualTo_ArrayOk() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_HasSizeLessThanOrEqualTo_ArrayFail() { - Guard.HasSizeLessThanOrEqualTo(new int[8], 4, nameof(Test_Guard_HasSizeLessThanOrEqualTo_ArrayFail)); + _ = Assert.ThrowsExactly(() => Guard.HasSizeLessThanOrEqualTo(new int[8], 4, nameof(Test_Guard_HasSizeLessThanOrEqualTo_ArrayFail))); } [TestMethod] @@ -136,10 +126,9 @@ public void Test_Guard_HasSizeEqualToArray_ArrayOk() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_HasSizeEqualToArray_ArrayFail() { - Guard.HasSizeEqualTo(new int[8], new int[2], nameof(Test_Guard_HasSizeEqualToArray_ArrayFail)); + _ = Assert.ThrowsExactly(() => Guard.HasSizeEqualTo(new int[8], new int[2], nameof(Test_Guard_HasSizeEqualToArray_ArrayFail))); } [TestMethod] @@ -150,9 +139,8 @@ public void Test_Guard_HasSizeLessThanOrEqualToArray_ArrayOk() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_HasSizeLessThanOrEqualToArray_ArrayFail() { - Guard.HasSizeLessThanOrEqualTo(new int[8], new int[2], nameof(Test_Guard_HasSizeLessThanOrEqualToArray_ArrayFail)); + _ = Assert.ThrowsExactly(() => Guard.HasSizeLessThanOrEqualTo(new int[8], new int[2], nameof(Test_Guard_HasSizeLessThanOrEqualToArray_ArrayFail))); } } diff --git a/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs b/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs index 482df6fe8..7cbe156a9 100644 --- a/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs +++ b/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs @@ -26,21 +26,18 @@ static void Test(T? obj) } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsNull_ClassFail() { - Guard.IsNull(new object(), nameof(Test_Guard_IsNull_ClassFail)); + _ = Assert.ThrowsExactly(() => Guard.IsNull(new object(), nameof(Test_Guard_IsNull_ClassFail))); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsNull_StructFail() { - Guard.IsNull(7, nameof(Test_Guard_IsNull_StructFail)); + _ = Assert.ThrowsExactly(() => Guard.IsNull(7, nameof(Test_Guard_IsNull_StructFail))); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsNull_GenericClassFail() { static void Test(T? obj) @@ -48,11 +45,10 @@ static void Test(T? obj) Guard.IsNull(obj, nameof(Test_Guard_IsNull_GenericClassFail)); } - Test("Hi!"); + _ = Assert.ThrowsExactly(() => Test("Hi!")); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsNull_GenericStructFail() { static void Test(T? obj) @@ -60,7 +56,7 @@ static void Test(T? obj) Guard.IsNull(obj, nameof(Test_Guard_IsNull_GenericStructFail)); } - Test(42); + _ = Assert.ThrowsExactly(() => Test(42)); } [TestMethod] @@ -80,21 +76,18 @@ static void Test(T? obj) } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_Guard_IsNotNull_ClassFail() { - Guard.IsNotNull(null, nameof(Test_Guard_IsNotNull_ClassFail)); + _ = Assert.ThrowsExactly(() => Guard.IsNotNull(null, nameof(Test_Guard_IsNotNull_ClassFail))); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_Guard_IsNotNull_StructFail() { - Guard.IsNotNull(null, nameof(Test_Guard_IsNotNull_StructFail)); + _ = Assert.ThrowsExactly(() => Guard.IsNotNull(null, nameof(Test_Guard_IsNotNull_StructFail))); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_Guard_IsNotNull_GenericClassFail() { static void Test(T? obj) @@ -102,11 +95,10 @@ static void Test(T? obj) Guard.IsNotNull(obj, nameof(Test_Guard_IsNotNull_GenericClassFail)); } - Test(null); + _ = Assert.ThrowsExactly(() => Test(null)); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_Guard_IsNotNull_GenericStructFail() { static void Test(T? obj) @@ -114,7 +106,7 @@ static void Test(T? obj) Guard.IsNotNull(obj, nameof(Test_Guard_IsNotNull_GenericClassFail)); } - Test(null); + _ = Assert.ThrowsExactly(() => Test(null)); } [TestMethod] @@ -125,10 +117,9 @@ public void Test_Guard_IsOfT_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsOfT_Fail() { - Guard.IsOfType(7, nameof(Test_Guard_IsOfT_Fail)); + _ = Assert.ThrowsExactly(() => Guard.IsOfType(7, nameof(Test_Guard_IsOfT_Fail))); } [TestMethod] @@ -139,10 +130,9 @@ public void Test_Guard_IsOfType_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsOfType_Fail() { - Guard.IsOfType(7, typeof(string), nameof(Test_Guard_IsOfType_Fail)); + _ = Assert.ThrowsExactly(() => Guard.IsOfType(7, typeof(string), nameof(Test_Guard_IsOfType_Fail))); } [TestMethod] @@ -152,10 +142,9 @@ public void Test_Guard_IsAssignableToT_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsAssignableToT_Fail() { - Guard.IsAssignableToType(7, nameof(Test_Guard_IsAssignableToT_Fail)); + _ = Assert.ThrowsExactly(() => Guard.IsAssignableToType(7, nameof(Test_Guard_IsAssignableToT_Fail))); } [TestMethod] @@ -165,10 +154,9 @@ public void Test_Guard_IsAssignableToType_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsAssignableToType_Fail() { - Guard.IsAssignableToType(7, typeof(string), nameof(Test_Guard_IsAssignableToType_Fail)); + _ = Assert.ThrowsExactly(() => Guard.IsAssignableToType(7, typeof(string), nameof(Test_Guard_IsAssignableToType_Fail))); } [TestMethod] @@ -179,10 +167,9 @@ public void Test_Guard_IsNullOrEmpty_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsNullOrEmpty_Fail() { - Guard.IsNullOrEmpty("Hello", nameof(Test_Guard_IsNullOrEmpty_Fail)); + _ = Assert.ThrowsExactly(() => Guard.IsNullOrEmpty("Hello", nameof(Test_Guard_IsNullOrEmpty_Fail))); } [TestMethod] @@ -192,17 +179,15 @@ public void Test_Guard_IsNotNullOrEmpty_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_Guard_IsNotNullOrEmpty_Null() { - Guard.IsNotNullOrEmpty(null, nameof(Test_Guard_IsNotNullOrEmpty_Null)); + _ = Assert.ThrowsExactly(() => Guard.IsNotNullOrEmpty(null, nameof(Test_Guard_IsNotNullOrEmpty_Null))); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsNotNullOrEmpty_Empty() { - Guard.IsNotNullOrEmpty(string.Empty, nameof(Test_Guard_IsNotNullOrEmpty_Empty)); + _ = Assert.ThrowsExactly(() => Guard.IsNotNullOrEmpty(string.Empty, nameof(Test_Guard_IsNotNullOrEmpty_Empty))); } [TestMethod] @@ -212,17 +197,15 @@ public void Test_Guard_IsNotNullOrWhiteSpace_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_Guard_IsNotNullOrWhiteSpace_Null() { - Guard.IsNotNullOrWhiteSpace(null, nameof(Test_Guard_IsNotNullOrWhiteSpace_Null)); + _ = Assert.ThrowsExactly(() => Guard.IsNotNullOrWhiteSpace(null, nameof(Test_Guard_IsNotNullOrWhiteSpace_Null))); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsNotNullOrWhiteSpace_Empty() { - Guard.IsNotNullOrWhiteSpace(" ", nameof(Test_Guard_IsNotNullOrWhiteSpace_Empty)); + _ = Assert.ThrowsExactly(() => Guard.IsNotNullOrWhiteSpace(" ", nameof(Test_Guard_IsNotNullOrWhiteSpace_Empty))); } [TestMethod] @@ -232,10 +215,9 @@ public void Test_Guard_IsEqualTo_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsEqualTo_Fail() { - Guard.IsEqualTo("Hello", "World", nameof(Test_Guard_IsEqualTo_Fail)); + _ = Assert.ThrowsExactly(() => Guard.IsEqualTo("Hello", "World", nameof(Test_Guard_IsEqualTo_Fail))); } [TestMethod] @@ -245,10 +227,9 @@ public void Test_Guard_IsNotEqualTo_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsNotEqualTo_Fail() { - Guard.IsNotEqualTo("Hello", "Hello", nameof(Test_Guard_IsNotEqualTo_Fail)); + _ = Assert.ThrowsExactly(() => Guard.IsNotEqualTo("Hello", "Hello", nameof(Test_Guard_IsNotEqualTo_Fail))); } [TestMethod] @@ -267,19 +248,27 @@ public void Test_Guard_IsBitwiseEqualTo_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] - public void Test_Guard_IsBitwiseEqualTo_Size8Fail() + public void Test_Guard_IsBitwiseEqualTo_Size8Fail_Double() + { + _ = Assert.ThrowsExactly(() => Guard.IsBitwiseEqualTo(double.PositiveInfinity, double.Epsilon, nameof(Test_Guard_IsBitwiseEqualTo_Size8Fail_Double))); + } + + [TestMethod] + public void Test_Guard_IsBitwiseEqualTo_Size8Fail_DateTime() + { + _ = Assert.ThrowsExactly(() => Guard.IsBitwiseEqualTo(DateTime.Now, DateTime.Today, nameof(Test_Guard_IsBitwiseEqualTo_Size8Fail_DateTime))); + } + + [TestMethod] + public void Test_Guard_IsBitwiseEqualTo_Size16Fail_Decimal() { - Guard.IsBitwiseEqualTo(double.PositiveInfinity, double.Epsilon, nameof(Test_Guard_IsBitwiseEqualTo_Size8Fail)); - Guard.IsBitwiseEqualTo(DateTime.Now, DateTime.Today, nameof(Test_Guard_IsBitwiseEqualTo_Size8Fail)); + _ = Assert.ThrowsExactly(() => Guard.IsBitwiseEqualTo(decimal.MaxValue, decimal.MinusOne, nameof(Test_Guard_IsBitwiseEqualTo_Size16Fail_Decimal))); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] - public void Test_Guard_IsBitwiseEqualTo_Size16Fail() + public void Test_Guard_IsBitwiseEqualTo_Size16Fail_Guid() { - Guard.IsBitwiseEqualTo(decimal.MaxValue, decimal.MinusOne, nameof(Test_Guard_IsBitwiseEqualTo_Size16Fail)); - Guard.IsBitwiseEqualTo(Guid.NewGuid(), Guid.NewGuid(), nameof(Test_Guard_IsBitwiseEqualTo_Size16Fail)); + _ = Assert.ThrowsExactly(() => Guard.IsBitwiseEqualTo(Guid.NewGuid(), Guid.NewGuid(), nameof(Test_Guard_IsBitwiseEqualTo_Size16Fail_Guid))); } // a >16 byte struct for testing IsBitwiseEqual's pathway for >16 byte types @@ -303,14 +292,13 @@ public BiggerThanLimit(ulong a, ulong b, ulong c, ulong d) } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsBitwiseEqualTo_SequenceEqualFail() { // tests the >16 byte case where the loop is called BiggerThanLimit biggerThanLimit0 = new(0, 3, ulong.MaxValue, ulong.MinValue); BiggerThanLimit biggerThanLimit1 = new(long.MaxValue + 1UL, 99, ulong.MaxValue ^ 0xF7UL, ulong.MinValue ^ 5555UL); - Guard.IsBitwiseEqualTo(biggerThanLimit0, biggerThanLimit1, nameof(Test_Guard_IsBitwiseEqualTo_SequenceEqualFail)); + _ = Assert.ThrowsExactly(() => Guard.IsBitwiseEqualTo(biggerThanLimit0, biggerThanLimit1, nameof(Test_Guard_IsBitwiseEqualTo_SequenceEqualFail))); } [TestMethod] @@ -322,10 +310,9 @@ public void Test_Guard_IsReferenceEqualTo_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsReferenceEqualTo_Fail() { - Guard.IsReferenceEqualTo(new object(), new object(), nameof(Test_Guard_IsReferenceEqualTo_Fail)); + _ = Assert.ThrowsExactly(() => Guard.IsReferenceEqualTo(new object(), new object(), nameof(Test_Guard_IsReferenceEqualTo_Fail))); } [TestMethod] @@ -335,12 +322,11 @@ public void Test_Guard_IsReferenceNotEqualTo_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsReferenceNotEqualTo_Fail() { object? obj = new(); - Guard.IsReferenceNotEqualTo(obj, obj, nameof(Test_Guard_IsReferenceEqualTo_Ok)); + _ = Assert.ThrowsExactly(() => Guard.IsReferenceNotEqualTo(obj, obj, nameof(Test_Guard_IsReferenceEqualTo_Ok))); } [TestMethod] @@ -351,10 +337,9 @@ public void Test_Guard_IsTrue_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsTrue_Fail() { - Guard.IsTrue(false, nameof(Test_Guard_IsTrue_Fail)); + _ = Assert.ThrowsExactly(() => Guard.IsTrue(false, nameof(Test_Guard_IsTrue_Fail))); } [TestMethod] @@ -411,10 +396,9 @@ public void Test_Guard_IsFalse_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsFalse_Fail() { - Guard.IsFalse(true, nameof(Test_Guard_IsFalse_Fail)); + _ = Assert.ThrowsExactly(() => Guard.IsFalse(true, nameof(Test_Guard_IsFalse_Fail))); } [TestMethod] @@ -470,17 +454,15 @@ public void Test_Guard_IsLessThan_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsLessThan_EqualsFalse() { - Guard.IsLessThan(1, 1, nameof(Test_Guard_IsLessThan_EqualsFalse)); + _ = Assert.ThrowsExactly(() => Guard.IsLessThan(1, 1, nameof(Test_Guard_IsLessThan_EqualsFalse))); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsLessThan_GreaterFalse() { - Guard.IsLessThan(2, 1, nameof(Test_Guard_IsLessThan_GreaterFalse)); + _ = Assert.ThrowsExactly(() => Guard.IsLessThan(2, 1, nameof(Test_Guard_IsLessThan_GreaterFalse))); } [TestMethod] @@ -495,10 +477,9 @@ public void Test_Guard_IsLessThanOrEqualTo_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsLessThanOrEqualTo_False() { - Guard.IsLessThanOrEqualTo(2, 1, nameof(Test_Guard_IsLessThanOrEqualTo_False)); + _ = Assert.ThrowsExactly(() => Guard.IsLessThanOrEqualTo(2, 1, nameof(Test_Guard_IsLessThanOrEqualTo_False))); } [TestMethod] @@ -510,17 +491,15 @@ public void Test_Guard_IsGreaterThan_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsGreaterThan_EqualsFalse() { - Guard.IsGreaterThan(1, 1, nameof(Test_Guard_IsGreaterThan_EqualsFalse)); + _ = Assert.ThrowsExactly(() => Guard.IsGreaterThan(1, 1, nameof(Test_Guard_IsGreaterThan_EqualsFalse))); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsGreaterThan_LowerFalse() { - Guard.IsGreaterThan(1, 2, nameof(Test_Guard_IsGreaterThan_LowerFalse)); + _ = Assert.ThrowsExactly(() => Guard.IsGreaterThan(1, 2, nameof(Test_Guard_IsGreaterThan_LowerFalse))); } [TestMethod] @@ -535,10 +514,9 @@ public void Test_Guard_IsGreaterThanOrEqualTo_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsGreaterThanOrEqualTo_False() { - Guard.IsGreaterThanOrEqualTo(1, 2, nameof(Test_Guard_IsGreaterThanOrEqualTo_False)); + _ = Assert.ThrowsExactly(() => Guard.IsGreaterThanOrEqualTo(1, 2, nameof(Test_Guard_IsGreaterThanOrEqualTo_False))); } [TestMethod] @@ -555,24 +533,21 @@ public void Test_Guard_IsInRange_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsInRange_LowerFail() { - Guard.IsInRange(-3, 0, 4, nameof(Test_Guard_IsInRange_LowerFail)); + _ = Assert.ThrowsExactly(() => Guard.IsInRange(-3, 0, 4, nameof(Test_Guard_IsInRange_LowerFail))); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsInRange_EqualFail() { - Guard.IsInRange(0, 4, 4, nameof(Test_Guard_IsInRange_EqualFail)); + _ = Assert.ThrowsExactly(() => Guard.IsInRange(0, 4, 4, nameof(Test_Guard_IsInRange_EqualFail))); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsInRange_HigherFail() { - Guard.IsInRange(0, 20, 4, nameof(Test_Guard_IsInRange_HigherFail)); + _ = Assert.ThrowsExactly(() => Guard.IsInRange(0, 20, 4, nameof(Test_Guard_IsInRange_HigherFail))); } [TestMethod] @@ -585,17 +560,15 @@ public void Test_Guard_IsNotInRange_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsNotInRange_LowerEqualFail() { - Guard.IsNotInRange(0, 0, 4, nameof(Test_Guard_IsNotInRange_LowerEqualFail)); + _ = Assert.ThrowsExactly(() => Guard.IsNotInRange(0, 0, 4, nameof(Test_Guard_IsNotInRange_LowerEqualFail))); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsNotInRange_InnerFail() { - Guard.IsNotInRange(2, 0, 4, nameof(Test_Guard_IsNotInRange_InnerFail)); + _ = Assert.ThrowsExactly(() => Guard.IsNotInRange(2, 0, 4, nameof(Test_Guard_IsNotInRange_InnerFail))); } [TestMethod] @@ -609,30 +582,36 @@ public void Test_Guard_IsInRangeFor_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsInRangeFor_LowerFail() { - Span span = stackalloc int[10]; + _ = Assert.ThrowsExactly(() => + { + Span span = stackalloc int[10]; - Guard.IsInRangeFor(-2, span, nameof(Test_Guard_IsInRangeFor_LowerFail)); + Guard.IsInRangeFor(-2, span, nameof(Test_Guard_IsInRangeFor_LowerFail)); + }); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsInRangeFor_EqualFail() { - Span span = stackalloc int[10]; + _ = Assert.ThrowsExactly(() => + { + Span span = stackalloc int[10]; - Guard.IsInRangeFor(10, span, nameof(Test_Guard_IsInRangeFor_EqualFail)); + Guard.IsInRangeFor(10, span, nameof(Test_Guard_IsInRangeFor_EqualFail)); + }); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsInRangeFor_HigherFail() { - Span span = stackalloc int[10]; + _ = Assert.ThrowsExactly(() => + { + Span span = stackalloc int[10]; - Guard.IsInRangeFor(99, span, nameof(Test_Guard_IsInRangeFor_HigherFail)); + Guard.IsInRangeFor(99, span, nameof(Test_Guard_IsInRangeFor_HigherFail)); + }); } [TestMethod] @@ -646,21 +625,25 @@ public void Test_Guard_IsNotInRangeFor_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsNotInRangeFor_LowerFail() { - Span span = stackalloc int[10]; + _ = Assert.ThrowsExactly(() => + { + Span span = stackalloc int[10]; - Guard.IsNotInRangeFor(0, span, nameof(Test_Guard_IsNotInRangeFor_LowerFail)); + Guard.IsNotInRangeFor(0, span, nameof(Test_Guard_IsNotInRangeFor_LowerFail)); + }); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsNotInRangeFor_MiddleFail() { - Span span = stackalloc int[10]; + _ = Assert.ThrowsExactly(() => + { + Span span = stackalloc int[10]; - Guard.IsNotInRangeFor(6, span, nameof(Test_Guard_IsNotInRangeFor_MiddleFail)); + Guard.IsNotInRangeFor(6, span, nameof(Test_Guard_IsNotInRangeFor_MiddleFail)); + }); } [TestMethod] @@ -672,24 +655,21 @@ public void Test_Guard_IsBetween_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsBetween_LowerFail() { - Guard.IsBetween(-1, 0, 4, nameof(Test_Guard_IsBetween_LowerFail)); + _ = Assert.ThrowsExactly(() => Guard.IsBetween(-1, 0, 4, nameof(Test_Guard_IsBetween_LowerFail))); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsBetween_EqualFail() { - Guard.IsBetween(0, 0, 4, nameof(Test_Guard_IsBetween_EqualFail)); + _ = Assert.ThrowsExactly(() => Guard.IsBetween(0, 0, 4, nameof(Test_Guard_IsBetween_EqualFail))); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsBetween_HigherFail() { - Guard.IsBetween(6, 0, 4, nameof(Test_Guard_IsBetween_HigherFail)); + _ = Assert.ThrowsExactly(() => Guard.IsBetween(6, 0, 4, nameof(Test_Guard_IsBetween_HigherFail))); } [TestMethod] @@ -701,10 +681,9 @@ public void Test_Guard_IsNotBetween_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsNotBetween_Fail() { - Guard.IsNotBetween(1, 0, 4, nameof(Test_Guard_IsNotBetween_Fail)); + _ = Assert.ThrowsExactly(() => Guard.IsNotBetween(1, 0, 4, nameof(Test_Guard_IsNotBetween_Fail))); } [TestMethod] @@ -716,17 +695,15 @@ public void Test_Guard_IsBetweenOrEqualTo_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsBetweenOrEqualTo_LowerFail() { - Guard.IsBetweenOrEqualTo(-1, 0, 4, nameof(Test_Guard_IsBetweenOrEqualTo_LowerFail)); + _ = Assert.ThrowsExactly(() => Guard.IsBetweenOrEqualTo(-1, 0, 4, nameof(Test_Guard_IsBetweenOrEqualTo_LowerFail))); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsBetweenOrEqualTo_HigherFail() { - Guard.IsBetweenOrEqualTo(6, 0, 4, nameof(Test_Guard_IsBetweenOrEqualTo_HigherFail)); + _ = Assert.ThrowsExactly(() => Guard.IsBetweenOrEqualTo(6, 0, 4, nameof(Test_Guard_IsBetweenOrEqualTo_HigherFail))); } [TestMethod] @@ -737,10 +714,9 @@ public void Test_Guard_IsNotBetweenOrEqualTo_Ok() } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_Guard_IsNotBetweenOrEqualTo_Fail() { - Guard.IsNotBetweenOrEqualTo(3, 0, 4, nameof(Test_Guard_IsNotBetweenOrEqualTo_Fail)); + _ = Assert.ThrowsExactly(() => Guard.IsNotBetweenOrEqualTo(3, 0, 4, nameof(Test_Guard_IsNotBetweenOrEqualTo_Fail))); } [TestCategory("Guard")] diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_ArrayPoolBufferWriter{T}.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_ArrayPoolBufferWriter{T}.cs index 47f977b17..7c4e40015 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_ArrayPoolBufferWriter{T}.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_ArrayPoolBufferWriter{T}.cs @@ -78,18 +78,18 @@ public void Test_ArrayPoolBufferWriterOfT_AllocateAndGetMemoryAndSpan() Assert.AreEqual(writer.WrittenMemory.Length, 43); Assert.AreEqual(writer.WrittenSpan.Length, 43); - _ = Assert.ThrowsException(() => writer.Advance(-1)); - _ = Assert.ThrowsException(() => writer.GetMemory(-1)); - _ = Assert.ThrowsException(() => writer.Advance(1024)); + _ = Assert.ThrowsExactly(() => writer.Advance(-1)); + _ = Assert.ThrowsExactly(() => writer.GetMemory(-1)); + _ = Assert.ThrowsExactly(() => writer.Advance(1024)); writer.Dispose(); - _ = Assert.ThrowsException(() => writer.WrittenMemory); - _ = Assert.ThrowsException(() => writer.WrittenSpan.Length); - _ = Assert.ThrowsException(() => writer.Capacity); - _ = Assert.ThrowsException(() => writer.FreeCapacity); - _ = Assert.ThrowsException(() => writer.Clear()); - _ = Assert.ThrowsException(() => writer.Advance(1)); + _ = Assert.ThrowsExactly(() => writer.WrittenMemory); + _ = Assert.ThrowsExactly(() => writer.WrittenSpan.Length); + _ = Assert.ThrowsExactly(() => writer.Capacity); + _ = Assert.ThrowsExactly(() => writer.FreeCapacity); + _ = Assert.ThrowsExactly(() => writer.Clear()); + _ = Assert.ThrowsExactly(() => writer.Advance(1)); } [TestMethod] @@ -119,30 +119,27 @@ public void Test_ArrayPoolBufferWriterOfT_AllocateFromCustomPoolAndGetMemoryAndS Assert.AreEqual(writer.WrittenMemory.Length, 43); Assert.AreEqual(writer.WrittenSpan.Length, 43); - _ = Assert.ThrowsException(() => writer.Advance(-1)); - _ = Assert.ThrowsException(() => writer.GetMemory(-1)); - _ = Assert.ThrowsException(() => writer.Advance(1024)); + _ = Assert.ThrowsExactly(() => writer.Advance(-1)); + _ = Assert.ThrowsExactly(() => writer.GetMemory(-1)); + _ = Assert.ThrowsExactly(() => writer.Advance(1024)); writer.Dispose(); - _ = Assert.ThrowsException(() => writer.WrittenMemory); - _ = Assert.ThrowsException(() => writer.WrittenSpan.Length); - _ = Assert.ThrowsException(() => writer.Capacity); - _ = Assert.ThrowsException(() => writer.FreeCapacity); - _ = Assert.ThrowsException(() => writer.Clear()); - _ = Assert.ThrowsException(() => writer.Advance(1)); + _ = Assert.ThrowsExactly(() => writer.WrittenMemory); + _ = Assert.ThrowsExactly(() => writer.WrittenSpan.Length); + _ = Assert.ThrowsExactly(() => writer.Capacity); + _ = Assert.ThrowsExactly(() => writer.FreeCapacity); + _ = Assert.ThrowsExactly(() => writer.Clear()); + _ = Assert.ThrowsExactly(() => writer.Advance(1)); } Assert.AreEqual(pool.RentedArrays.Count, 0); } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_ArrayPoolBufferWriterOfT_InvalidRequestedSize() { - _ = new ArrayPoolBufferWriter(-1); - - Assert.Fail("You shouldn't be here"); + _ = Assert.ThrowsExactly(() => new ArrayPoolBufferWriter(-1)); } [TestMethod] @@ -218,7 +215,7 @@ public void Test_ArrayPoolBufferWriterOfT_AsStream() writer.Dispose(); // Now check that the writer is actually disposed instead - _ = Assert.ThrowsException(() => writer.Capacity); + _ = Assert.ThrowsExactly(() => writer.Capacity); } [TestMethod] @@ -244,6 +241,6 @@ public void Test_ArrayPoolBufferWriterOfT_AllocateAndGetArray() bufferWriter.Dispose(); - _ = Assert.ThrowsException(() => bufferWriter.DangerousGetArray()); + _ = Assert.ThrowsExactly(() => bufferWriter.DangerousGetArray()); } } diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_MemoryBufferWriter{T}.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_MemoryBufferWriter{T}.cs index 6a6bf4ee8..bc0eb6740 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_MemoryBufferWriter{T}.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_MemoryBufferWriter{T}.cs @@ -42,11 +42,11 @@ public void Test_MemoryBufferWriterOfT_AllocateAndGetMemoryAndSpan() Assert.AreEqual(memory.Length - 43, writer.GetSpan(22).Length); Assert.AreEqual(memory.Length - 43, writer.GetMemory(22).Length); - _ = Assert.ThrowsException(() => writer.Advance(-1)); - _ = Assert.ThrowsException(() => writer.GetMemory(-1)); - _ = Assert.ThrowsException(() => writer.GetSpan(1024)); - _ = Assert.ThrowsException(() => writer.GetMemory(1024)); - _ = Assert.ThrowsException(() => writer.Advance(1024)); + _ = Assert.ThrowsExactly(() => writer.Advance(-1)); + _ = Assert.ThrowsExactly(() => writer.GetMemory(-1)); + _ = Assert.ThrowsExactly(() => writer.GetSpan(1024)); + _ = Assert.ThrowsExactly(() => writer.GetMemory(1024)); + _ = Assert.ThrowsExactly(() => writer.Advance(1024)); } [TestMethod] diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_MemoryOwner{T}.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_MemoryOwner{T}.cs index 80030bc1a..f256557e5 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_MemoryOwner{T}.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_MemoryOwner{T}.cs @@ -51,34 +51,32 @@ public void Test_MemoryOwnerOfT_AllocateFromCustomPoolAndGetMemoryAndSpan() } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_MemoryOwnerOfT_InvalidRequestedSize() { - using MemoryOwner? buffer = MemoryOwner.Allocate(-1); - - Assert.Fail("You shouldn't be here"); + _ = Assert.ThrowsExactly(() => + { + using MemoryOwner? buffer = MemoryOwner.Allocate(-1); + }); } [TestMethod] - [ExpectedException(typeof(ObjectDisposedException))] public void Test_MemoryOwnerOfT_DisposedMemory() { MemoryOwner? buffer = MemoryOwner.Allocate(127); buffer.Dispose(); - _ = buffer.Memory; + _ = Assert.ThrowsExactly(() => _ = buffer.Memory); } [TestMethod] - [ExpectedException(typeof(ObjectDisposedException))] public void Test_MemoryOwnerOfT_DisposedSpan() { MemoryOwner? buffer = MemoryOwner.Allocate(127); buffer.Dispose(); - _ = buffer.Span; + _ = Assert.ThrowsExactly(() => _ = buffer.Span); } [TestMethod] @@ -138,7 +136,7 @@ public void Test_MemoryOwnerOfT_AllocateAndGetArray() // The original buffer instance is disposed here, because calling Slice transfers // the ownership of the internal buffer to the new instance (this is documented in // XML docs for the MemoryOwner.Slice method). - _ = Assert.ThrowsException(() => buffer.DangerousGetArray()); + _ = Assert.ThrowsExactly(() => buffer.DangerousGetArray()); segment = second.DangerousGetArray(); @@ -150,6 +148,6 @@ public void Test_MemoryOwnerOfT_AllocateAndGetArray() second.Dispose(); - _ = Assert.ThrowsException(() => second.DangerousGetArray()); + _ = Assert.ThrowsExactly(() => second.DangerousGetArray()); } } diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_SpanOwner{T}.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_SpanOwner{T}.cs index 8fcaedd4e..3ba918805 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_SpanOwner{T}.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_SpanOwner{T}.cs @@ -47,12 +47,12 @@ public void Test_SpanOwnerOfT_AllocateFromCustomPoolAndGetMemoryAndSpan() } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_SpanOwnerOfT_InvalidRequestedSize() { - using SpanOwner buffer = SpanOwner.Allocate(-1); - - Assert.Fail("You shouldn't be here"); + _ = Assert.ThrowsExactly(() => + { + using SpanOwner buffer = SpanOwner.Allocate(-1); + }); } [TestMethod] diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/CommunityToolkit.HighPerformance.UnitTests.csproj b/tests/CommunityToolkit.HighPerformance.UnitTests/CommunityToolkit.HighPerformance.UnitTests.csproj index 3e5f44fb7..6614792ac 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/CommunityToolkit.HighPerformance.UnitTests.csproj +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/CommunityToolkit.HighPerformance.UnitTests.csproj @@ -11,8 +11,8 @@ - - + + diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_ReadOnlyRefEnumerable{T}.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_ReadOnlyRefEnumerable{T}.cs index 964914d6a..b1bc711ee 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_ReadOnlyRefEnumerable{T}.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_ReadOnlyRefEnumerable{T}.cs @@ -39,10 +39,9 @@ public void Test_ReadOnlyRefEnumerable_DangerousCreate_Ok(int length, int step, [DataRow(-44, 10)] [DataRow(10, -14)] [DataRow(-32, -1)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public unsafe void Test_ReadOnlyRefEnumerable_DangerousCreate_BelowZero(int length, int step) { - _ = ReadOnlyRefEnumerable.DangerousCreate(in *(int*)null, length, step); + _ = Assert.ThrowsExactly(() => _ = ReadOnlyRefEnumerable.DangerousCreate(in *(int*)null, length, step)); } [TestMethod] @@ -76,8 +75,8 @@ public void Test_ReadOnlyRefEnumerable_Indexer_ThrowsIndexOutOfRange() 0, 0, 0, 0 }; - _ = Assert.ThrowsException(() => ReadOnlyRefEnumerable.DangerousCreate(in array[0], array.Length, 1)[-1]); - _ = Assert.ThrowsException(() => ReadOnlyRefEnumerable.DangerousCreate(in array[0], array.Length, 1)[array.Length]); + _ = Assert.ThrowsExactly(() => ReadOnlyRefEnumerable.DangerousCreate(in array[0], array.Length, 1)[-1]); + _ = Assert.ThrowsExactly(() => ReadOnlyRefEnumerable.DangerousCreate(in array[0], array.Length, 1)[array.Length]); } #if NET6_0_OR_GREATER @@ -112,8 +111,8 @@ public void Test_ReadOnlyRefEnumerable_Index_Indexer_ThrowsIndexOutOfRange() 0, 0, 0, 0 }; - _ = Assert.ThrowsException(() => ReadOnlyRefEnumerable.DangerousCreate(in array[0], array.Length, 1)[new Index(array.Length)]); - _ = Assert.ThrowsException(() => ReadOnlyRefEnumerable.DangerousCreate(in array[0], array.Length, 1)[^0]); + _ = Assert.ThrowsExactly(() => ReadOnlyRefEnumerable.DangerousCreate(in array[0], array.Length, 1)[new Index(array.Length)]); + _ = Assert.ThrowsExactly(() => ReadOnlyRefEnumerable.DangerousCreate(in array[0], array.Length, 1)[^0]); } #endif } diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_RefEnumerable{T}.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_RefEnumerable{T}.cs index 589e7d650..ba5227430 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_RefEnumerable{T}.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_RefEnumerable{T}.cs @@ -39,10 +39,9 @@ public void Test_RefEnumerable_DangerousCreate_Ok(int length, int step, int[] va [DataRow(-44, 10)] [DataRow(10, -14)] [DataRow(-32, -1)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public unsafe void Test_RefEnumerable_DangerousCreate_BelowZero(int length, int step) { - _ = RefEnumerable.DangerousCreate(ref *(int*)null, length, step); + _ = Assert.ThrowsExactly(() => _ = RefEnumerable.DangerousCreate(ref *(int*)null, length, step)); } [TestMethod] @@ -76,8 +75,8 @@ public void Test_RefEnumerable_Indexer_ThrowsIndexOutOfRange() 0, 0, 0, 0 }; - _ = Assert.ThrowsException(() => RefEnumerable.DangerousCreate(ref array[0], array.Length, 1)[-1]); - _ = Assert.ThrowsException(() => RefEnumerable.DangerousCreate(ref array[0], array.Length, 1)[array.Length]); + _ = Assert.ThrowsExactly(() => RefEnumerable.DangerousCreate(ref array[0], array.Length, 1)[-1]); + _ = Assert.ThrowsExactly(() => RefEnumerable.DangerousCreate(ref array[0], array.Length, 1)[array.Length]); } #if NET6_0_OR_GREATER @@ -112,8 +111,8 @@ public void Test_RefEnumerable_Index_Indexer_ThrowsIndexOutOfRange() 0, 0, 0, 0 }; - _ = Assert.ThrowsException(() => RefEnumerable.DangerousCreate(ref array[0], array.Length, 1)[new Index(array.Length)]); - _ = Assert.ThrowsException(() => RefEnumerable.DangerousCreate(ref array[0], array.Length, 1)[^0]); + _ = Assert.ThrowsExactly(() => RefEnumerable.DangerousCreate(ref array[0], array.Length, 1)[new Index(array.Length)]); + _ = Assert.ThrowsExactly(() => RefEnumerable.DangerousCreate(ref array[0], array.Length, 1)[^0]); } #endif } diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_ArrayExtensions.2D.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_ArrayExtensions.2D.cs index 5d41cf9b0..d6d5bf8e3 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_ArrayExtensions.2D.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_ArrayExtensions.2D.cs @@ -179,10 +179,10 @@ public void Test_ArrayExtensions_2D_GetRow_Rectangle() // Test an empty array Assert.AreSame(new int[1, 0].GetRow(0).ToArray(), Array.Empty()); - _ = Assert.ThrowsException(() => array.GetRow(-1)); - _ = Assert.ThrowsException(() => array.GetRow(3)); + _ = Assert.ThrowsExactly(() => array.GetRow(-1)); + _ = Assert.ThrowsExactly(() => array.GetRow(3)); - _ = Assert.ThrowsException(() => array.GetRow(20)); + _ = Assert.ThrowsExactly(() => array.GetRow(20)); } [TestMethod] @@ -204,10 +204,10 @@ public void Test_ArrayExtensions_2D_GetColumn_Rectangle() CollectionAssert.AreEqual(array.GetColumn(1).ToArray(), new[] { 2, 6, 10 }); - _ = Assert.ThrowsException(() => array.GetColumn(-1)); - _ = Assert.ThrowsException(() => array.GetColumn(4)); + _ = Assert.ThrowsExactly(() => array.GetColumn(-1)); + _ = Assert.ThrowsExactly(() => array.GetColumn(4)); - _ = Assert.ThrowsException(() => array.GetColumn(20)); + _ = Assert.ThrowsExactly(() => array.GetColumn(20)); } [TestMethod] @@ -216,7 +216,7 @@ public void Test_ArrayExtensions_2D_GetRow_Empty() int[,] array = new int[0, 0]; // Try to get a row from an empty array (the row index isn't in range) - _ = Assert.ThrowsException(() => array.GetRow(0).ToArray()); + _ = Assert.ThrowsExactly(() => array.GetRow(0).ToArray()); } [TestMethod] @@ -274,11 +274,11 @@ public void Test_ArrayExtensions_2D_GetRowOrColumn_Helpers() CollectionAssert.AreEqual(copy, result); // Some invalid attempts to copy to an empty span or sequence - _ = Assert.ThrowsException(() => array.GetRow(0).CopyTo(default(RefEnumerable))); - _ = Assert.ThrowsException(() => array.GetRow(0).CopyTo(default(Span))); + _ = Assert.ThrowsExactly(() => array.GetRow(0).CopyTo(default(RefEnumerable))); + _ = Assert.ThrowsExactly(() => array.GetRow(0).CopyTo(default(Span))); - _ = Assert.ThrowsException(() => array.GetColumn(0).CopyTo(default(RefEnumerable))); - _ = Assert.ThrowsException(() => array.GetColumn(0).CopyTo(default(Span))); + _ = Assert.ThrowsExactly(() => array.GetColumn(0).CopyTo(default(RefEnumerable))); + _ = Assert.ThrowsExactly(() => array.GetColumn(0).CopyTo(default(Span))); // Same as CopyTo, but this will fail gracefully with an invalid target Assert.IsTrue(array.GetRow(2).TryCopyTo(copy)); @@ -344,11 +344,11 @@ public void Test_ArrayExtensions_2D_ReadOnlyGetRowOrColumn_Helpers() CollectionAssert.AreEqual(copy, result); - _ = Assert.ThrowsException(() => ((ReadOnlySpan2D)array).GetRow(0).CopyTo(default(RefEnumerable))); - _ = Assert.ThrowsException(() => ((ReadOnlySpan2D)array).GetRow(0).CopyTo(default(Span))); + _ = Assert.ThrowsExactly(() => ((ReadOnlySpan2D)array).GetRow(0).CopyTo(default(RefEnumerable))); + _ = Assert.ThrowsExactly(() => ((ReadOnlySpan2D)array).GetRow(0).CopyTo(default(Span))); - _ = Assert.ThrowsException(() => ((ReadOnlySpan2D)array).GetColumn(0).CopyTo(default(RefEnumerable))); - _ = Assert.ThrowsException(() => ((ReadOnlySpan2D)array).GetColumn(0).CopyTo(default(Span))); + _ = Assert.ThrowsExactly(() => ((ReadOnlySpan2D)array).GetColumn(0).CopyTo(default(RefEnumerable))); + _ = Assert.ThrowsExactly(() => ((ReadOnlySpan2D)array).GetColumn(0).CopyTo(default(Span))); Assert.IsTrue(span2D.GetRow(2).TryCopyTo(copy)); Assert.IsFalse(span2D.GetRow(2).TryCopyTo(default(Span))); @@ -416,7 +416,7 @@ public void Test_ArrayExtensions_2D_GetColumn_Empty() { int[,] array = new int[0, 0]; - _ = Assert.ThrowsException(() => array.GetColumn(0).ToArray()); + _ = Assert.ThrowsExactly(() => array.GetColumn(0).ToArray()); } #if NET6_0_OR_GREATER diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_ArrayPoolExtensions.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_ArrayPoolExtensions.cs index a215c082e..c31fe6817 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_ArrayPoolExtensions.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_ArrayPoolExtensions.cs @@ -14,12 +14,11 @@ namespace CommunityToolkit.HighPerformance.UnitTests.Extensions; public class Test_ArrayPoolExtensions { [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_ArrayPoolExtensions_Resize_InvalidSize() { int[]? array = null; - ArrayPool.Shared.Resize(ref array, -1); + _ = Assert.ThrowsExactly(() => ArrayPool.Shared.Resize(ref array, -1)); } [TestMethod] @@ -89,12 +88,11 @@ public void Test_ArrayPoolExtensions_Resize_Clear() } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_ArrayPoolExtensions_EnsureCapacity_InvalidCapacity() { int[]? array = null; - ArrayPool.Shared.EnsureCapacity(ref array, -1); + _ = Assert.ThrowsExactly(() => ArrayPool.Shared.EnsureCapacity(ref array, -1)); } [TestMethod] diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_MemoryExtensions.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_MemoryExtensions.cs index 3c33ee981..849bde106 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_MemoryExtensions.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_MemoryExtensions.cs @@ -430,7 +430,6 @@ public void Test_MemoryExtensions_FromMemoryManager_CastFromByteAndBack_WithSlic [DataRow(64, 4, 0)] [DataRow(64, 0, 4)] [DataRow(64, 4, 4)] - [DataRow(64, 4, 0)] [DataRow(256, 16, 0)] [DataRow(256, 4, 16)] [DataRow(256, 64, 0)] @@ -460,7 +459,6 @@ public unsafe void Test_MemoryExtensions_FromArray_CastFromByte_Pin(int size, in [DataRow(64, 4, 0)] [DataRow(64, 0, 4)] [DataRow(64, 4, 4)] - [DataRow(64, 4, 0)] [DataRow(256, 16, 0)] [DataRow(256, 4, 16)] [DataRow(256, 64, 0)] @@ -511,7 +509,6 @@ public void Test_MemoryExtensions_FromString_CastFromByteAndBack() [DataRow(64, 4, 0)] [DataRow(64, 0, 4)] [DataRow(64, 4, 4)] - [DataRow(64, 4, 0)] [DataRow(256, 16, 0)] [DataRow(256, 4, 16)] [DataRow(256, 64, 0)] diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_StreamExtensions.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_StreamExtensions.cs index 4977176ef..3664d49c3 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_StreamExtensions.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_StreamExtensions.cs @@ -25,7 +25,7 @@ public void Test_StreamExtensions_ReadWrite() Assert.AreEqual(stream.Position, 17); - _ = Assert.ThrowsException(() => stream.Write(long.MaxValue)); + _ = Assert.ThrowsExactly(() => stream.Write(long.MaxValue)); stream.Position = 0; @@ -34,7 +34,7 @@ public void Test_StreamExtensions_ReadWrite() Assert.AreEqual(3.14f, stream.Read()); Assert.AreEqual(unchecked(uint.MaxValue * 324823489204ul), stream.Read()); - _ = Assert.ThrowsException(() => stream.Read()); + _ = Assert.ThrowsExactly(() => stream.Read()); } // See https://github.com/CommunityToolkit/dotnet/issues/513 @@ -55,7 +55,7 @@ public void Test_StreamExtensions_ReadWrite_WithBufferedStream() Assert.AreEqual(3.14f, stream.Read()); Assert.AreEqual(unchecked(uint.MaxValue * 324823489204ul), stream.Read()); - _ = Assert.ThrowsException(() => stream.Read()); + _ = Assert.ThrowsExactly(() => stream.Read()); } private sealed class BufferedStream : MemoryStream diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Helpers/Test_ParallelHelper.For.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Helpers/Test_ParallelHelper.For.cs index 152527604..0bf56560a 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Helpers/Test_ParallelHelper.For.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Helpers/Test_ParallelHelper.For.cs @@ -40,17 +40,15 @@ public unsafe void Test_ParallelHelper_ForWithIndices() #if NET6_0_OR_GREATER [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_ParallelHelper_ForInvalidRange_FromEnd() { - ParallelHelper.For(..^1); + _ = Assert.ThrowsExactly(() => ParallelHelper.For(..^1)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_ParallelHelper_ForInvalidRange_RangeAll() { - ParallelHelper.For(..); + _ = Assert.ThrowsExactly(() => ParallelHelper.For(..)); } [TestMethod] diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Helpers/Test_ParallelHelper.For2D.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Helpers/Test_ParallelHelper.For2D.cs index 157b6a4a4..fd5b01963 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Helpers/Test_ParallelHelper.For2D.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Helpers/Test_ParallelHelper.For2D.cs @@ -52,17 +52,15 @@ public unsafe void Test_ParallelHelper_For2DWithIndices() #if NET6_0_OR_GREATER [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_ParallelHelper_For2DInvalidRange_FromEnd() { - ParallelHelper.For2D(..^1, ..4); + _ = Assert.ThrowsExactly(() => ParallelHelper.For2D(..^1, ..4)); } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void Test_ParallelHelper_For2DInvalidRange_RangeAll() { - ParallelHelper.For2D(..5, ..); + _ = Assert.ThrowsExactly(() => ParallelHelper.For2D(..5, ..)); } [TestMethod] diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_Memory2D{T}.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_Memory2D{T}.cs index 337663c71..399556c2d 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_Memory2D{T}.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_Memory2D{T}.cs @@ -66,15 +66,15 @@ public void Test_Memory2DT_Array1DConstructor() // Also ensure the right exceptions are thrown with invalid parameters, such as // negative indices, indices out of range, values that are too big, etc. - _ = Assert.ThrowsException(() => new Memory2D(new string[1], 1, 1)); - _ = Assert.ThrowsException(() => new Memory2D(array, -99, 1, 1, 1)); - _ = Assert.ThrowsException(() => new Memory2D(array, 0, -10, 1, 1)); - _ = Assert.ThrowsException(() => new Memory2D(array, 0, 1, 1, -1)); - _ = Assert.ThrowsException(() => new Memory2D(array, 0, 1, -100, 1)); - _ = Assert.ThrowsException(() => new Memory2D(array, 0, 2, 4, 0)); - _ = Assert.ThrowsException(() => new Memory2D(array, 0, 3, 3, 0)); - _ = Assert.ThrowsException(() => new Memory2D(array, 1, 2, 3, 0)); - _ = Assert.ThrowsException(() => new Memory2D(array, 0, 10, 1, 120)); + _ = Assert.ThrowsExactly(() => new Memory2D(new string[1], 1, 1)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, -99, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, 0, -10, 1, 1)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, 0, 1, 1, -1)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, 0, 1, -100, 1)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, 0, 2, 4, 0)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, 0, 3, 3, 0)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, 1, 2, 3, 0)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, 0, 10, 1, 120)); } [TestMethod] @@ -98,7 +98,7 @@ public void Test_Memory2DT_Array2DConstructor_1() // Here we test the check for covariance: we can't create a Memory2D from a U[,] array // where U is assignable to T (as in, U : T). This would cause a type safety violation on write. - _ = Assert.ThrowsException(() => new Memory2D(new string[1, 2])); + _ = Assert.ThrowsExactly(() => new Memory2D(new string[1, 2])); } [TestMethod] @@ -120,7 +120,7 @@ public void Test_Memory2DT_Array2DConstructor_2() Assert.AreEqual(memory2d.Span[0, 0], 2); Assert.AreEqual(memory2d.Span[1, 1], 6); - _ = Assert.ThrowsException(() => new Memory2D(new string[1, 2], 0, 0, 2, 2)); + _ = Assert.ThrowsExactly(() => new Memory2D(new string[1, 2], 0, 0, 2, 2)); } [TestMethod] @@ -149,9 +149,9 @@ public void Test_Memory2DT_Array3DConstructor_1() Assert.AreEqual(memory2d.Span[1, 2], 60); // A couple of tests for invalid parameters, ie. layers out of range - _ = Assert.ThrowsException(() => new Memory2D(array, -1)); - _ = Assert.ThrowsException(() => new Memory2D(array, 2)); - _ = Assert.ThrowsException(() => new Memory2D(array, 20)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, -1)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, 2)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, 20)); } [TestMethod] @@ -183,15 +183,15 @@ public void Test_Memory2DT_Array3DConstructor_2() Assert.AreEqual(memory2d.Span[1, 1], 60); // Same as above, testing a few cases with invalid parameters - _ = Assert.ThrowsException(() => new Memory2D(array, -1, 1, 1, 1, 1)); - _ = Assert.ThrowsException(() => new Memory2D(array, 1, -1, 1, 1, 1)); - _ = Assert.ThrowsException(() => new Memory2D(array, 1, 1, -1, 1, 1)); - _ = Assert.ThrowsException(() => new Memory2D(array, 1, 1, 1, -1, 1)); - _ = Assert.ThrowsException(() => new Memory2D(array, 1, 1, 1, 1, -1)); - _ = Assert.ThrowsException(() => new Memory2D(array, 2, 0, 0, 2, 3)); - _ = Assert.ThrowsException(() => new Memory2D(array, 0, 0, 1, 2, 3)); - _ = Assert.ThrowsException(() => new Memory2D(array, 0, 0, 0, 2, 4)); - _ = Assert.ThrowsException(() => new Memory2D(array, 0, 0, 0, 3, 3)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, -1, 1, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, 1, -1, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, 1, 1, -1, 1, 1)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, 1, 1, 1, -1, 1)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, 1, 1, 1, 1, -1)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, 2, 0, 0, 2, 3)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, 0, 0, 1, 2, 3)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, 0, 0, 0, 2, 4)); + _ = Assert.ThrowsExactly(() => new Memory2D(array, 0, 0, 0, 3, 3)); } #if NET6_0_OR_GREATER @@ -216,14 +216,14 @@ public void Test_Memory2DT_MemoryConstructor() Assert.AreEqual(memory2d.Span[0, 0], 2); Assert.AreEqual(memory2d.Span[1, 1], 6); - _ = Assert.ThrowsException(() => memory.AsMemory2D(-99, 1, 1, 1)); - _ = Assert.ThrowsException(() => memory.AsMemory2D(0, -10, 1, 1)); - _ = Assert.ThrowsException(() => memory.AsMemory2D(0, 1, 1, -1)); - _ = Assert.ThrowsException(() => memory.AsMemory2D(0, 1, -100, 1)); - _ = Assert.ThrowsException(() => memory.AsMemory2D(0, 2, 4, 0)); - _ = Assert.ThrowsException(() => memory.AsMemory2D(0, 3, 3, 0)); - _ = Assert.ThrowsException(() => memory.AsMemory2D(1, 2, 3, 0)); - _ = Assert.ThrowsException(() => memory.AsMemory2D(0, 10, 1, 120)); + _ = Assert.ThrowsExactly(() => memory.AsMemory2D(-99, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => memory.AsMemory2D(0, -10, 1, 1)); + _ = Assert.ThrowsExactly(() => memory.AsMemory2D(0, 1, 1, -1)); + _ = Assert.ThrowsExactly(() => memory.AsMemory2D(0, 1, -100, 1)); + _ = Assert.ThrowsExactly(() => memory.AsMemory2D(0, 2, 4, 0)); + _ = Assert.ThrowsExactly(() => memory.AsMemory2D(0, 3, 3, 0)); + _ = Assert.ThrowsExactly(() => memory.AsMemory2D(1, 2, 3, 0)); + _ = Assert.ThrowsExactly(() => memory.AsMemory2D(0, 10, 1, 120)); } #endif @@ -260,17 +260,17 @@ public void Test_Memory2DT_Slice_1() Assert.AreEqual(slice2.Span[1, 1], 6); // A few invalid slicing operations, with out of range parameters - _ = Assert.ThrowsException(() => new Memory2D(array).Slice(-1, 1, 1, 1)); - _ = Assert.ThrowsException(() => new Memory2D(array).Slice(1, -1, 1, 1)); - _ = Assert.ThrowsException(() => new Memory2D(array).Slice(1, 1, 1, -1)); - _ = Assert.ThrowsException(() => new Memory2D(array).Slice(1, 1, -1, 1)); - _ = Assert.ThrowsException(() => new Memory2D(array).Slice(10, 1, 1, 1)); - _ = Assert.ThrowsException(() => new Memory2D(array).Slice(1, 12, 1, 12)); - _ = Assert.ThrowsException(() => new Memory2D(array).Slice(1, 1, 55, 1)); - _ = Assert.ThrowsException(() => new Memory2D(array).Slice(0, 0, 2, 4)); - _ = Assert.ThrowsException(() => new Memory2D(array).Slice(0, 0, 3, 3)); - _ = Assert.ThrowsException(() => new Memory2D(array).Slice(0, 1, 2, 3)); - _ = Assert.ThrowsException(() => new Memory2D(array).Slice(1, 0, 2, 3)); + _ = Assert.ThrowsExactly(() => new Memory2D(array).Slice(-1, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new Memory2D(array).Slice(1, -1, 1, 1)); + _ = Assert.ThrowsExactly(() => new Memory2D(array).Slice(1, 1, 1, -1)); + _ = Assert.ThrowsExactly(() => new Memory2D(array).Slice(1, 1, -1, 1)); + _ = Assert.ThrowsExactly(() => new Memory2D(array).Slice(10, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new Memory2D(array).Slice(1, 12, 1, 12)); + _ = Assert.ThrowsExactly(() => new Memory2D(array).Slice(1, 1, 55, 1)); + _ = Assert.ThrowsExactly(() => new Memory2D(array).Slice(0, 0, 2, 4)); + _ = Assert.ThrowsExactly(() => new Memory2D(array).Slice(0, 0, 3, 3)); + _ = Assert.ThrowsExactly(() => new Memory2D(array).Slice(0, 1, 2, 3)); + _ = Assert.ThrowsExactly(() => new Memory2D(array).Slice(1, 0, 2, 3)); } [TestMethod] diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_ReadOnlyMemory2D{T}.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_ReadOnlyMemory2D{T}.cs index dc8673771..d5cfd04be 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_ReadOnlyMemory2D{T}.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_ReadOnlyMemory2D{T}.cs @@ -56,14 +56,14 @@ public void Test_ReadOnlyMemory2DT_Array1DConstructor() // Here we check to ensure a covariant array conversion is allowed for ReadOnlyMemory2D _ = new ReadOnlyMemory2D(new string[1], 1, 1); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, -99, 1, 1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, 0, -10, 1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, 0, 1, 1, -1)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, 0, 1, -100, 1)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, 0, 2, 4, 0)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, 0, 3, 3, 0)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, 1, 2, 3, 0)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, 0, 10, 1, 120)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, -99, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, 0, -10, 1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, 0, 1, 1, -1)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, 0, 1, -100, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, 0, 2, 4, 0)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, 0, 3, 3, 0)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, 1, 2, 3, 0)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, 0, 10, 1, 120)); } [TestMethod] @@ -107,7 +107,7 @@ public void Test_ReadOnlyMemory2DT_Array2DConstructor_2() _ = new ReadOnlyMemory2D(new string[1, 2]); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(new string[1, 2], 0, 0, 2, 2)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(new string[1, 2], 0, 0, 2, 2)); } [TestMethod] @@ -134,9 +134,9 @@ public void Test_ReadOnlyMemory2DT_Array3DConstructor_1() Assert.AreEqual(memory2d.Span[0, 1], 20); Assert.AreEqual(memory2d.Span[1, 2], 60); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, -1)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, 20)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, 2)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, -1)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, 20)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, 2)); } [TestMethod] @@ -163,15 +163,15 @@ public void Test_ReadOnlyMemory2DT_Array3DConstructor_2() Assert.AreEqual(memory2d.Span[0, 0], 20); Assert.AreEqual(memory2d.Span[1, 1], 60); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, -1, 1, 1, 1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, 1, -1, 1, 1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, 1, 1, -1, 1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, 1, 1, 1, -1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, 1, 1, 1, 1, -1)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, 2, 0, 0, 2, 3)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, 0, 0, 1, 2, 3)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, 0, 0, 0, 2, 4)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array, 0, 0, 0, 3, 3)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, -1, 1, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, 1, -1, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, 1, 1, -1, 1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, 1, 1, 1, -1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, 1, 1, 1, 1, -1)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, 2, 0, 0, 2, 3)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, 0, 0, 1, 2, 3)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, 0, 0, 0, 2, 4)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array, 0, 0, 0, 3, 3)); } #if NET6_0_OR_GREATER @@ -192,14 +192,14 @@ public void Test_ReadOnlyMemory2DT_ReadOnlyMemoryConstructor() Assert.AreEqual(memory2d.Span[0, 0], 2); Assert.AreEqual(memory2d.Span[1, 1], 6); - _ = Assert.ThrowsException(() => memory.AsMemory2D(-99, 1, 1, 1)); - _ = Assert.ThrowsException(() => memory.AsMemory2D(0, -10, 1, 1)); - _ = Assert.ThrowsException(() => memory.AsMemory2D(0, 1, 1, -1)); - _ = Assert.ThrowsException(() => memory.AsMemory2D(0, 1, -100, 1)); - _ = Assert.ThrowsException(() => memory.AsMemory2D(0, 2, 4, 0)); - _ = Assert.ThrowsException(() => memory.AsMemory2D(0, 3, 3, 0)); - _ = Assert.ThrowsException(() => memory.AsMemory2D(1, 2, 3, 0)); - _ = Assert.ThrowsException(() => memory.AsMemory2D(0, 10, 1, 120)); + _ = Assert.ThrowsExactly(() => memory.AsMemory2D(-99, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => memory.AsMemory2D(0, -10, 1, 1)); + _ = Assert.ThrowsExactly(() => memory.AsMemory2D(0, 1, 1, -1)); + _ = Assert.ThrowsExactly(() => memory.AsMemory2D(0, 1, -100, 1)); + _ = Assert.ThrowsExactly(() => memory.AsMemory2D(0, 2, 4, 0)); + _ = Assert.ThrowsExactly(() => memory.AsMemory2D(0, 3, 3, 0)); + _ = Assert.ThrowsExactly(() => memory.AsMemory2D(1, 2, 3, 0)); + _ = Assert.ThrowsExactly(() => memory.AsMemory2D(0, 10, 1, 120)); } #endif @@ -231,17 +231,17 @@ public void Test_ReadOnlyMemory2DT_Slice_1() Assert.AreEqual(slice2.Span[1, 0], 5); Assert.AreEqual(slice2.Span[1, 1], 6); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array).Slice(-1, 1, 1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array).Slice(1, -1, 1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array).Slice(1, 1, 1, -1)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array).Slice(1, 1, -1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array).Slice(10, 1, 1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array).Slice(1, 12, 1, 12)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array).Slice(1, 1, 55, 1)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array).Slice(0, 0, 2, 4)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array).Slice(0, 0, 3, 3)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array).Slice(0, 1, 2, 3)); - _ = Assert.ThrowsException(() => new ReadOnlyMemory2D(array).Slice(1, 0, 2, 3)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array).Slice(-1, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array).Slice(1, -1, 1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array).Slice(1, 1, 1, -1)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array).Slice(1, 1, -1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array).Slice(10, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array).Slice(1, 12, 1, 12)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array).Slice(1, 1, 55, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array).Slice(0, 0, 2, 4)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array).Slice(0, 0, 3, 3)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array).Slice(0, 1, 2, 3)); + _ = Assert.ThrowsExactly(() => new ReadOnlyMemory2D(array).Slice(1, 0, 2, 3)); } [TestMethod] diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_ReadOnlySpan2D{T}.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_ReadOnlySpan2D{T}.cs index b65c83075..c46c78e51 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_ReadOnlySpan2D{T}.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_ReadOnlySpan2D{T}.cs @@ -61,9 +61,9 @@ public unsafe void Test_ReadOnlySpan2DT_RefConstructor() Assert.AreEqual(span2d[0, 0], 1); Assert.AreEqual(span2d[1, 2], 6); - _ = Assert.ThrowsException(() => ReadOnlySpan2D.DangerousCreate(Unsafe.AsRef(null), -1, 0, 0)); - _ = Assert.ThrowsException(() => ReadOnlySpan2D.DangerousCreate(Unsafe.AsRef(null), 1, -2, 0)); - _ = Assert.ThrowsException(() => ReadOnlySpan2D.DangerousCreate(Unsafe.AsRef(null), 1, 0, -5)); + _ = Assert.ThrowsExactly(() => ReadOnlySpan2D.DangerousCreate(Unsafe.AsRef(null), -1, 0, 0)); + _ = Assert.ThrowsExactly(() => ReadOnlySpan2D.DangerousCreate(Unsafe.AsRef(null), 1, -2, 0)); + _ = Assert.ThrowsExactly(() => ReadOnlySpan2D.DangerousCreate(Unsafe.AsRef(null), 1, 0, -5)); } #endif @@ -89,10 +89,10 @@ public unsafe void Test_ReadOnlySpan2DT_PtrConstructor() Assert.AreEqual(span2d[0, 0], 1); Assert.AreEqual(span2d[1, 2], 6); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D((void*)0, -1, 0, 0)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D((void*)0, 1, -2, 0)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D((void*)0, 1, 0, -5)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D((void*)0, 2, 2, 0)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D((void*)0, -1, 0, 0)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D((void*)0, 1, -2, 0)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D((void*)0, 1, 0, -5)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D((void*)0, 2, 2, 0)); } [TestMethod] @@ -115,11 +115,11 @@ public void Test_ReadOnlySpan2DT_Array1DConstructor() // Same for ReadOnlyMemory2D, we need to check that covariant array conversions are allowed _ = new ReadOnlySpan2D(new string[1], 1, 1); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array, -99, 1, 1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array, 0, -10, 1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array, 0, 1, 1, -1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array, 0, 1, -100, 1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array, 0, 10, 1, 120)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array, -99, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array, 0, -10, 1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array, 0, 1, 1, -1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array, 0, 1, -100, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array, 0, 10, 1, 120)); } [TestMethod] @@ -163,7 +163,7 @@ public void Test_ReadOnlySpan2DT_Array2DConstructor_2() _ = new ReadOnlySpan2D(new string[1, 2]); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(new string[1, 2], 0, 0, 2, 2)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(new string[1, 2], 0, 0, 2, 2)); } [TestMethod] @@ -191,8 +191,8 @@ public void Test_ReadOnlySpan2DT_Array3DConstructor_1() Assert.AreEqual(span2d[0, 1], 20); Assert.AreEqual(span2d[1, 2], 60); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array, -1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array, 20)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array, -1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array, 20)); } [TestMethod] @@ -220,11 +220,11 @@ public void Test_ReadOnlySpan2DT_Array3DConstructor_2() Assert.AreEqual(span2d[0, 1], 30); Assert.AreEqual(span2d[1, 1], 60); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array, -1, 1, 1, 1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array, 1, -1, 1, 1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array, 1, 1, -1, 1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array, 1, 1, 1, -1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array, 1, 1, 1, 1, -1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array, -1, 1, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array, 1, -1, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array, 1, 1, -1, 1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array, 1, 1, 1, -1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array, 1, 1, 1, 1, -1)); } [TestMethod] @@ -244,7 +244,7 @@ public void Test_ReadOnlySpan2DT_CopyTo_1() CollectionAssert.AreEqual(array, target); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).CopyTo(Span.Empty)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).CopyTo(Span.Empty)); } [TestMethod] @@ -266,7 +266,7 @@ public void Test_ReadOnlySpan2DT_CopyTo_2() CollectionAssert.AreEqual(target, expected); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).CopyTo(Span.Empty)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).CopyTo(Span.Empty)); } [TestMethod] @@ -286,7 +286,7 @@ public void Test_ReadOnlySpan2DT_CopyTo2D_1() CollectionAssert.AreEqual(array, target); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).CopyTo(Span2D.Empty)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).CopyTo(Span2D.Empty)); } [TestMethod] @@ -312,7 +312,7 @@ public void Test_ReadOnlySpan2DT_CopyTo2D_2() CollectionAssert.AreEqual(target, expected); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).CopyTo(new Span2D(target))); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).CopyTo(new Span2D(target))); } [TestMethod] @@ -417,14 +417,16 @@ public unsafe void Test_ReadOnlySpan2DT_Index_Indexer_2() } [TestMethod] - [ExpectedException(typeof(IndexOutOfRangeException))] public unsafe void Test_ReadOnlySpan2DT_Index_Indexer_Fail() { int[,] array = new int[4, 4]; - ReadOnlySpan2D span2d = new(array); + _ = Assert.ThrowsExactly(() => + { + ReadOnlySpan2D span2d = new(array); - ref readonly int span2dRef = ref span2d[^6, 2]; + ref readonly int span2dRef = ref span2d[^6, 2]; + }); } [TestMethod] @@ -454,15 +456,16 @@ public unsafe void Test_ReadOnlySpan2DT_Range_Indexer_2() } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public unsafe void Test_ReadOnlySpan2DT_Range_Indexer_Fail() { int[,] array = new int[4, 4]; - ReadOnlySpan2D span2d = new(array); - _ = span2d[0..6, 2..^1]; + _ = Assert.ThrowsExactly(() => + { + ReadOnlySpan2D span2d = new(array); - Assert.Fail(); + _ = span2d[0..6, 2..^1]; + }); } #endif @@ -494,13 +497,13 @@ public void Test_ReadOnlySpan2DT_Slice_1() Assert.AreEqual(slice2[1, 0], 5); Assert.AreEqual(slice2[1, 1], 6); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).Slice(-1, 1, 1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).Slice(1, -1, 1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).Slice(1, 1, 1, -1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).Slice(1, 1, -1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).Slice(10, 1, 1, 1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).Slice(1, 12, 1, 12)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).Slice(1, 1, 55, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).Slice(-1, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).Slice(1, -1, 1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).Slice(1, 1, 1, -1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).Slice(1, 1, -1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).Slice(10, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).Slice(1, 12, 1, 12)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).Slice(1, 1, 55, 1)); } [TestMethod] @@ -559,8 +562,8 @@ ref Unsafe.AsRef(in span[0]), ref Unsafe.AsRef(in span[2]), ref array[1, 2])); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).GetRowSpan(-1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).GetRowSpan(5)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).GetRowSpan(-1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).GetRowSpan(5)); } #endif @@ -690,7 +693,6 @@ public void Test_ReadOnlySpan2DT_ToArray_2() } [TestMethod] - [ExpectedException(typeof(NotSupportedException))] public void Test_ReadOnlySpan2DT_Equals() { int[,] array = @@ -699,13 +701,15 @@ public void Test_ReadOnlySpan2DT_Equals() { 4, 5, 6 } }; - ReadOnlySpan2D span2d = new(array); + _ = Assert.ThrowsExactly(() => + { + ReadOnlySpan2D span2d = new(array); - _ = span2d.Equals(null); + _ = span2d.Equals(null); + }); } [TestMethod] - [ExpectedException(typeof(NotSupportedException))] public void Test_ReadOnlySpan2DT_GetHashCode() { int[,] array = @@ -714,9 +718,12 @@ public void Test_ReadOnlySpan2DT_GetHashCode() { 4, 5, 6 } }; - ReadOnlySpan2D span2d = new(array); + _ = Assert.ThrowsExactly(() => + { + ReadOnlySpan2D span2d = new(array); - _ = span2d.GetHashCode(); + _ = span2d.GetHashCode(); + }); } [TestMethod] @@ -797,9 +804,9 @@ public void Test_ReadOnlySpan2DT_GetRow() Assert.AreSame(default(ReadOnlyRefEnumerable).ToArray(), Array.Empty()); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).GetRow(-1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).GetRow(2)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).GetRow(1000)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).GetRow(-1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).GetRow(2)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).GetRow(1000)); } [TestMethod] @@ -827,9 +834,9 @@ public unsafe void Test_ReadOnlySpan2DT_Pointer_GetRow() CollectionAssert.AreEqual(enumerable.ToArray(), expected); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array, 2, 3, 0).GetRow(-1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array, 2, 3, 0).GetRow(2)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array, 2, 3, 0).GetRow(1000)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array, 2, 3, 0).GetRow(-1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array, 2, 3, 0).GetRow(2)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array, 2, 3, 0).GetRow(1000)); } [TestMethod] @@ -853,9 +860,9 @@ public void Test_ReadOnlySpan2DT_GetColumn() CollectionAssert.AreEqual(enumerable.ToArray(), expected); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).GetColumn(-1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).GetColumn(3)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array).GetColumn(1000)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).GetColumn(-1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).GetColumn(3)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array).GetColumn(1000)); } [TestMethod] @@ -883,9 +890,9 @@ public unsafe void Test_ReadOnlySpan2DT_Pointer_GetColumn() CollectionAssert.AreEqual(enumerable.ToArray(), expected); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array, 2, 3, 0).GetColumn(-1)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array, 2, 3, 0).GetColumn(3)); - _ = Assert.ThrowsException(() => new ReadOnlySpan2D(array, 2, 3, 0).GetColumn(1000)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array, 2, 3, 0).GetColumn(-1)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array, 2, 3, 0).GetColumn(3)); + _ = Assert.ThrowsExactly(() => new ReadOnlySpan2D(array, 2, 3, 0).GetColumn(1000)); } [TestMethod] diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_Span2D{T}.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_Span2D{T}.cs index 15be6300c..89f5cc412 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_Span2D{T}.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_Span2D{T}.cs @@ -77,9 +77,9 @@ public unsafe void Test_Span2DT_RefConstructor() Assert.AreEqual(span[5], 101); // A few cases with invalid indices - _ = Assert.ThrowsException(() => Span2D.DangerousCreate(ref Unsafe.AsRef(null), -1, 0, 0)); - _ = Assert.ThrowsException(() => Span2D.DangerousCreate(ref Unsafe.AsRef(null), 1, -2, 0)); - _ = Assert.ThrowsException(() => Span2D.DangerousCreate(ref Unsafe.AsRef(null), 1, 0, -5)); + _ = Assert.ThrowsExactly(() => Span2D.DangerousCreate(ref Unsafe.AsRef(null), -1, 0, 0)); + _ = Assert.ThrowsExactly(() => Span2D.DangerousCreate(ref Unsafe.AsRef(null), 1, -2, 0)); + _ = Assert.ThrowsExactly(() => Span2D.DangerousCreate(ref Unsafe.AsRef(null), 1, 0, -5)); } #endif @@ -110,10 +110,10 @@ public unsafe void Test_Span2DT_PtrConstructor() Assert.AreEqual(ptr[0], 99); Assert.AreEqual(ptr[5], 101); - _ = Assert.ThrowsException(() => new Span2D((void*)0, -1, 0, 0)); - _ = Assert.ThrowsException(() => new Span2D((void*)0, 1, -2, 0)); - _ = Assert.ThrowsException(() => new Span2D((void*)0, 1, 0, -5)); - _ = Assert.ThrowsException(() => new Span2D((void*)0, 2, 2, 0)); + _ = Assert.ThrowsExactly(() => new Span2D((void*)0, -1, 0, 0)); + _ = Assert.ThrowsExactly(() => new Span2D((void*)0, 1, -2, 0)); + _ = Assert.ThrowsExactly(() => new Span2D((void*)0, 1, 0, -5)); + _ = Assert.ThrowsExactly(() => new Span2D((void*)0, 2, 2, 0)); } [TestMethod] @@ -140,12 +140,12 @@ public void Test_Span2DT_Array1DConstructor() // The first check fails due to the array covariance test mentioned in the Memory2D tests. // The others just validate a number of cases with invalid arguments (eg. out of range). - _ = Assert.ThrowsException(() => new Span2D(new string[1], 1, 1)); - _ = Assert.ThrowsException(() => new Span2D(array, -99, 1, 1, 1)); - _ = Assert.ThrowsException(() => new Span2D(array, 0, -10, 1, 1)); - _ = Assert.ThrowsException(() => new Span2D(array, 0, 1, 1, -1)); - _ = Assert.ThrowsException(() => new Span2D(array, 0, 1, -100, 1)); - _ = Assert.ThrowsException(() => new Span2D(array, 0, 10, 1, 120)); + _ = Assert.ThrowsExactly(() => new Span2D(new string[1], 1, 1)); + _ = Assert.ThrowsExactly(() => new Span2D(array, -99, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new Span2D(array, 0, -10, 1, 1)); + _ = Assert.ThrowsExactly(() => new Span2D(array, 0, 1, 1, -1)); + _ = Assert.ThrowsExactly(() => new Span2D(array, 0, 1, -100, 1)); + _ = Assert.ThrowsExactly(() => new Span2D(array, 0, 10, 1, 120)); } [TestMethod] @@ -171,7 +171,7 @@ public void Test_Span2DT_Array2DConstructor_1() Assert.AreEqual(array[0, 1], 99); Assert.AreEqual(array[1, 2], 101); - _ = Assert.ThrowsException(() => new Span2D(new string[1, 2])); + _ = Assert.ThrowsExactly(() => new Span2D(new string[1, 2])); } [TestMethod] @@ -197,7 +197,7 @@ public void Test_Span2DT_Array2DConstructor_2() Assert.AreEqual(array[0, 1], 99); Assert.AreEqual(array[1, 2], 101); - _ = Assert.ThrowsException(() => new Span2D(new string[1, 2], 0, 0, 2, 2)); + _ = Assert.ThrowsExactly(() => new Span2D(new string[1, 2], 0, 0, 2, 2)); } [TestMethod] @@ -230,8 +230,8 @@ public void Test_Span2DT_Array3DConstructor_1() Assert.AreEqual(array[1, 0, 1], 99); Assert.AreEqual(array[1, 1, 2], 101); - _ = Assert.ThrowsException(() => new Span2D(array, -1)); - _ = Assert.ThrowsException(() => new Span2D(array, 20)); + _ = Assert.ThrowsExactly(() => new Span2D(array, -1)); + _ = Assert.ThrowsExactly(() => new Span2D(array, 20)); } [TestMethod] @@ -264,11 +264,11 @@ public void Test_Span2DT_Array3DConstructor_2() Assert.AreEqual(array[1, 0, 2], 99); Assert.AreEqual(array[1, 1, 2], 101); - _ = Assert.ThrowsException(() => new Span2D(array, -1, 1, 1, 1, 1)); - _ = Assert.ThrowsException(() => new Span2D(array, 1, -1, 1, 1, 1)); - _ = Assert.ThrowsException(() => new Span2D(array, 1, 1, -1, 1, 1)); - _ = Assert.ThrowsException(() => new Span2D(array, 1, 1, 1, -1, 1)); - _ = Assert.ThrowsException(() => new Span2D(array, 1, 1, 1, 1, -1)); + _ = Assert.ThrowsExactly(() => new Span2D(array, -1, 1, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new Span2D(array, 1, -1, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new Span2D(array, 1, 1, -1, 1, 1)); + _ = Assert.ThrowsExactly(() => new Span2D(array, 1, 1, 1, -1, 1)); + _ = Assert.ThrowsExactly(() => new Span2D(array, 1, 1, 1, 1, -1)); } [TestMethod] @@ -379,7 +379,7 @@ public void Test_Span2DT_CopyTo_1() CollectionAssert.AreEqual(array, target); // Exception due to the target span being too small for the source Span2D instance - _ = Assert.ThrowsException(() => new Span2D(array).CopyTo(Span.Empty)); + _ = Assert.ThrowsExactly(() => new Span2D(array).CopyTo(Span.Empty)); } [TestMethod] @@ -402,8 +402,8 @@ public void Test_Span2DT_CopyTo_2() CollectionAssert.AreEqual(target, expected); - _ = Assert.ThrowsException(() => new Span2D(array).CopyTo(Span.Empty)); - _ = Assert.ThrowsException(() => new Span2D(array, 0, 1, 2, 2).CopyTo(Span.Empty)); + _ = Assert.ThrowsExactly(() => new Span2D(array).CopyTo(Span.Empty)); + _ = Assert.ThrowsExactly(() => new Span2D(array, 0, 1, 2, 2).CopyTo(Span.Empty)); } [TestMethod] @@ -425,7 +425,7 @@ public void Test_Span2DT_CopyTo2D_1() CollectionAssert.AreEqual(array, target); - _ = Assert.ThrowsException(() => new Span2D(array).CopyTo(Span2D.Empty)); + _ = Assert.ThrowsExactly(() => new Span2D(array).CopyTo(Span2D.Empty)); } [TestMethod] @@ -452,7 +452,7 @@ public void Test_Span2DT_CopyTo2D_2() CollectionAssert.AreEqual(target, expected); - _ = Assert.ThrowsException(() => new Span2D(array).CopyTo(new Span2D(target))); + _ = Assert.ThrowsExactly(() => new Span2D(array).CopyTo(new Span2D(target))); } [TestMethod] @@ -574,14 +574,16 @@ public unsafe void Test_Span2DT_Index_Indexer_2() } [TestMethod] - [ExpectedException(typeof(IndexOutOfRangeException))] public unsafe void Test_Span2DT_Index_Indexer_Fail() { int[,] array = new int[4, 4]; - Span2D span2d = new(array); + _ = Assert.ThrowsExactly(() => + { + Span2D span2d = new(array); - ref int span2dRef = ref span2d[^6, 2]; + ref int span2dRef = ref span2d[^6, 2]; + }); } [TestMethod] @@ -611,15 +613,16 @@ public unsafe void Test_Span2DT_Range_Indexer_2() } [TestMethod] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public unsafe void Test_Span2DT_Range_Indexer_Fail() { int[,] array = new int[4, 4]; - Span2D span2d = new(array); - _ = span2d[0..6, 2..^1]; + _ = Assert.ThrowsExactly(() => + { + Span2D span2d = new(array); - Assert.Fail(); + _ = span2d[0..6, 2..^1]; + }); } #endif @@ -655,13 +658,13 @@ public void Test_Span2DT_Slice_1() Assert.AreEqual(slice2[1, 1], 6); // Some checks for invalid arguments - _ = Assert.ThrowsException(() => new Span2D(array).Slice(-1, 1, 1, 1)); - _ = Assert.ThrowsException(() => new Span2D(array).Slice(1, -1, 1, 1)); - _ = Assert.ThrowsException(() => new Span2D(array).Slice(1, 1, 1, -1)); - _ = Assert.ThrowsException(() => new Span2D(array).Slice(1, 1, -1, 1)); - _ = Assert.ThrowsException(() => new Span2D(array).Slice(10, 1, 1, 1)); - _ = Assert.ThrowsException(() => new Span2D(array).Slice(1, 12, 1, 12)); - _ = Assert.ThrowsException(() => new Span2D(array).Slice(1, 1, 55, 1)); + _ = Assert.ThrowsExactly(() => new Span2D(array).Slice(-1, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new Span2D(array).Slice(1, -1, 1, 1)); + _ = Assert.ThrowsExactly(() => new Span2D(array).Slice(1, 1, 1, -1)); + _ = Assert.ThrowsExactly(() => new Span2D(array).Slice(1, 1, -1, 1)); + _ = Assert.ThrowsExactly(() => new Span2D(array).Slice(10, 1, 1, 1)); + _ = Assert.ThrowsExactly(() => new Span2D(array).Slice(1, 12, 1, 12)); + _ = Assert.ThrowsExactly(() => new Span2D(array).Slice(1, 1, 55, 1)); } [TestMethod] @@ -727,8 +730,8 @@ public void Test_Span2DT_GetRowSpan() ref span[2], ref array[1, 2])); - _ = Assert.ThrowsException(() => new Span2D(array).GetRowSpan(-1)); - _ = Assert.ThrowsException(() => new Span2D(array).GetRowSpan(5)); + _ = Assert.ThrowsExactly(() => new Span2D(array).GetRowSpan(-1)); + _ = Assert.ThrowsExactly(() => new Span2D(array).GetRowSpan(5)); } #endif @@ -882,7 +885,6 @@ public void Test_Span2DT_ToArray_2() } [TestMethod] - [ExpectedException(typeof(NotSupportedException))] public void Test_Span2DT_Equals() { int[,] array = @@ -891,14 +893,16 @@ public void Test_Span2DT_Equals() { 4, 5, 6 } }; - Span2D span2d = new(array); - // Span2D.Equals always throw (this mirrors the behavior of Span.Equals) - _ = span2d.Equals(null); + _ = Assert.ThrowsExactly(() => + { + Span2D span2d = new(array); + + _ = span2d.Equals(null); + }); } [TestMethod] - [ExpectedException(typeof(NotSupportedException))] public void Test_Span2DT_GetHashCode() { int[,] array = @@ -907,10 +911,13 @@ public void Test_Span2DT_GetHashCode() { 4, 5, 6 } }; - Span2D span2d = new(array); - // Same as above, this always throws - _ = span2d.GetHashCode(); + _ = Assert.ThrowsExactly(() => + { + Span2D span2d = new(array); + + _ = span2d.GetHashCode(); + }); } [TestMethod] @@ -990,9 +997,9 @@ public void Test_Span2DT_GetRow() CollectionAssert.AreEqual(enumerable.ToArray(), expected); - _ = Assert.ThrowsException(() => new Span2D(array).GetRow(-1)); - _ = Assert.ThrowsException(() => new Span2D(array).GetRow(2)); - _ = Assert.ThrowsException(() => new Span2D(array).GetRow(1000)); + _ = Assert.ThrowsExactly(() => new Span2D(array).GetRow(-1)); + _ = Assert.ThrowsExactly(() => new Span2D(array).GetRow(2)); + _ = Assert.ThrowsExactly(() => new Span2D(array).GetRow(1000)); } [TestMethod] @@ -1015,9 +1022,9 @@ public unsafe void Test_Span2DT_Pointer_GetRow() CollectionAssert.AreEqual(enumerable.ToArray(), expected); - _ = Assert.ThrowsException(() => new Span2D(array, 2, 3, 0).GetRow(-1)); - _ = Assert.ThrowsException(() => new Span2D(array, 2, 3, 0).GetRow(2)); - _ = Assert.ThrowsException(() => new Span2D(array, 2, 3, 0).GetRow(1000)); + _ = Assert.ThrowsExactly(() => new Span2D(array, 2, 3, 0).GetRow(-1)); + _ = Assert.ThrowsExactly(() => new Span2D(array, 2, 3, 0).GetRow(2)); + _ = Assert.ThrowsExactly(() => new Span2D(array, 2, 3, 0).GetRow(1000)); } [TestMethod] @@ -1036,9 +1043,9 @@ public void Test_Span2DT_GetColumn() CollectionAssert.AreEqual(enumerable.ToArray(), expected); - _ = Assert.ThrowsException(() => new Span2D(array).GetColumn(-1)); - _ = Assert.ThrowsException(() => new Span2D(array).GetColumn(3)); - _ = Assert.ThrowsException(() => new Span2D(array).GetColumn(1000)); + _ = Assert.ThrowsExactly(() => new Span2D(array).GetColumn(-1)); + _ = Assert.ThrowsExactly(() => new Span2D(array).GetColumn(3)); + _ = Assert.ThrowsExactly(() => new Span2D(array).GetColumn(1000)); } [TestMethod] @@ -1061,9 +1068,9 @@ public unsafe void Test_Span2DT_Pointer_GetColumn() CollectionAssert.AreEqual(enumerable.ToArray(), expected); - _ = Assert.ThrowsException(() => new Span2D(array, 2, 3, 0).GetColumn(-1)); - _ = Assert.ThrowsException(() => new Span2D(array, 2, 3, 0).GetColumn(3)); - _ = Assert.ThrowsException(() => new Span2D(array, 2, 3, 0).GetColumn(1000)); + _ = Assert.ThrowsExactly(() => new Span2D(array, 2, 3, 0).GetColumn(-1)); + _ = Assert.ThrowsExactly(() => new Span2D(array, 2, 3, 0).GetColumn(3)); + _ = Assert.ThrowsExactly(() => new Span2D(array, 2, 3, 0).GetColumn(1000)); } [TestMethod] diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Streams/Test_IBufferWriterStream.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Streams/Test_IBufferWriterStream.cs index 4bbaab6bb..978d64f1b 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Streams/Test_IBufferWriterStream.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Streams/Test_IBufferWriterStream.cs @@ -27,8 +27,8 @@ public void Test_IBufferWriterStream_Lifecycle() Assert.IsFalse(stream.CanSeek); Assert.IsTrue(stream.CanWrite); - _ = Assert.ThrowsException(() => stream.Length); - _ = Assert.ThrowsException(() => stream.Position); + _ = Assert.ThrowsExactly(() => stream.Length); + _ = Assert.ThrowsExactly(() => stream.Position); // Dispose the stream and check that no operation is now allowed stream.Dispose(); @@ -36,8 +36,8 @@ public void Test_IBufferWriterStream_Lifecycle() Assert.IsFalse(stream.CanRead); Assert.IsFalse(stream.CanSeek); Assert.IsFalse(stream.CanWrite); - _ = Assert.ThrowsException(() => stream.Length); - _ = Assert.ThrowsException(() => stream.Position); + _ = Assert.ThrowsExactly(() => stream.Length); + _ = Assert.ThrowsExactly(() => stream.Position); } [TestMethod] @@ -56,15 +56,15 @@ public void Test_IBufferWriterStream_Write_Array() Assert.IsTrue(writer.WrittenSpan.SequenceEqual(data)); // A few tests with invalid inputs (null buffers, invalid indices, etc.) - _ = Assert.ThrowsException(() => stream.Write(null!, 0, 10)); - _ = Assert.ThrowsException(() => stream.Write(data, -1, 10)); - _ = Assert.ThrowsException(() => stream.Write(data, 200, 10)); - _ = Assert.ThrowsException(() => stream.Write(data, 0, -24)); - _ = Assert.ThrowsException(() => stream.Write(data, 0, 200)); + _ = Assert.ThrowsExactly(() => stream.Write(null!, 0, 10)); + _ = Assert.ThrowsExactly(() => stream.Write(data, -1, 10)); + _ = Assert.ThrowsExactly(() => stream.Write(data, 200, 10)); + _ = Assert.ThrowsExactly(() => stream.Write(data, 0, -24)); + _ = Assert.ThrowsExactly(() => stream.Write(data, 0, 200)); stream.Dispose(); - _ = Assert.ThrowsException(() => stream.Write(data, 0, data.Length)); + _ = Assert.ThrowsExactly(() => stream.Write(data, 0, data.Length)); } [TestMethod] @@ -81,15 +81,15 @@ public async Task Test_IBufferWriterStream_WriteAsync_Array() Assert.AreEqual(writer.WrittenCount, data.Length); Assert.IsTrue(writer.WrittenSpan.SequenceEqual(data)); - _ = await Assert.ThrowsExceptionAsync(() => stream.WriteAsync(null!, 0, 10)); - _ = await Assert.ThrowsExceptionAsync(() => stream.WriteAsync(data, -1, 10)); - _ = await Assert.ThrowsExceptionAsync(() => stream.WriteAsync(data, 200, 10)); - _ = await Assert.ThrowsExceptionAsync(() => stream.WriteAsync(data, 0, -24)); - _ = await Assert.ThrowsExceptionAsync(() => stream.WriteAsync(data, 0, 200)); + _ = await Assert.ThrowsExactlyAsync(() => stream.WriteAsync(null!, 0, 10)); + _ = await Assert.ThrowsExactlyAsync(() => stream.WriteAsync(data, -1, 10)); + _ = await Assert.ThrowsExactlyAsync(() => stream.WriteAsync(data, 200, 10)); + _ = await Assert.ThrowsExactlyAsync(() => stream.WriteAsync(data, 0, -24)); + _ = await Assert.ThrowsExactlyAsync(() => stream.WriteAsync(data, 0, 200)); stream.Dispose(); - _ = await Assert.ThrowsExceptionAsync(() => stream.WriteAsync(data, 0, data.Length)); + _ = await Assert.ThrowsExactlyAsync(() => stream.WriteAsync(data, 0, data.Length)); } [TestMethod] @@ -114,7 +114,7 @@ public void Test_IBufferWriterStream_WriteByte() Assert.AreEqual(writer.WrittenCount, data.Length); Assert.IsTrue(data.SequenceEqual(writer.WrittenSpan)); - _ = Assert.ThrowsException(() => stream.ReadByte()); + _ = Assert.ThrowsExactly(() => stream.ReadByte()); } [TestMethod] diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Streams/Test_IMemoryOwnerStream.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Streams/Test_IMemoryOwnerStream.cs index c2a9266d8..e8d2674c7 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Streams/Test_IMemoryOwnerStream.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Streams/Test_IMemoryOwnerStream.cs @@ -27,6 +27,6 @@ public void Test_IMemoryOwnerStream_Lifecycle() stream.Dispose(); - _ = Assert.ThrowsException(() => buffer.Memory); + _ = Assert.ThrowsExactly(() => buffer.Memory); } } diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Streams/Test_MemoryStream.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Streams/Test_MemoryStream.cs index 9b057975e..f5fc501b1 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Streams/Test_MemoryStream.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Streams/Test_MemoryStream.cs @@ -31,8 +31,8 @@ public void Test_MemoryStream_Lifecycle() Assert.IsFalse(stream.CanRead); Assert.IsFalse(stream.CanSeek); Assert.IsFalse(stream.CanWrite); - _ = Assert.ThrowsException(() => stream.Length); - _ = Assert.ThrowsException(() => stream.Position); + _ = Assert.ThrowsExactly(() => stream.Length); + _ = Assert.ThrowsExactly(() => stream.Position); } [TestMethod] @@ -46,20 +46,20 @@ public void Test_MemoryStream_Seek() Assert.AreEqual(stream.Position, 42); - _ = Assert.ThrowsException(() => stream.Position = -1); - _ = Assert.ThrowsException(() => stream.Position = 120); + _ = Assert.ThrowsExactly(() => stream.Position = -1); + _ = Assert.ThrowsExactly(() => stream.Position = 120); _ = stream.Seek(0, SeekOrigin.Begin); - _ = Assert.ThrowsException(() => stream.Seek(-1, SeekOrigin.Begin)); - _ = Assert.ThrowsException(() => stream.Seek(120, SeekOrigin.Begin)); + _ = Assert.ThrowsExactly(() => stream.Seek(-1, SeekOrigin.Begin)); + _ = Assert.ThrowsExactly(() => stream.Seek(120, SeekOrigin.Begin)); Assert.AreEqual(stream.Position, 0); _ = stream.Seek(-1, SeekOrigin.End); - _ = Assert.ThrowsException(() => stream.Seek(20, SeekOrigin.End)); - _ = Assert.ThrowsException(() => stream.Seek(-120, SeekOrigin.End)); + _ = Assert.ThrowsExactly(() => stream.Seek(20, SeekOrigin.End)); + _ = Assert.ThrowsExactly(() => stream.Seek(-120, SeekOrigin.End)); Assert.AreEqual(stream.Position, stream.Length - 1); @@ -67,8 +67,8 @@ public void Test_MemoryStream_Seek() _ = stream.Seek(20, SeekOrigin.Current); _ = stream.Seek(-30, SeekOrigin.Current); - _ = Assert.ThrowsException(() => stream.Seek(-64, SeekOrigin.Current)); - _ = Assert.ThrowsException(() => stream.Seek(80, SeekOrigin.Current)); + _ = Assert.ThrowsExactly(() => stream.Seek(-64, SeekOrigin.Current)); + _ = Assert.ThrowsExactly(() => stream.Seek(80, SeekOrigin.Current)); Assert.AreEqual(stream.Position, 32); } @@ -128,15 +128,15 @@ public void Test_MemoryStream_ReadWrite_Array() Assert.AreEqual(stream.Position, data.Length); Assert.IsTrue(data.AsSpan().SequenceEqual(result)); - _ = Assert.ThrowsException(() => stream.Write(null!, 0, 10)); - _ = Assert.ThrowsException(() => stream.Write(data, -1, 10)); - _ = Assert.ThrowsException(() => stream.Write(data, 200, 10)); - _ = Assert.ThrowsException(() => stream.Write(data, 0, -24)); - _ = Assert.ThrowsException(() => stream.Write(data, 0, 200)); + _ = Assert.ThrowsExactly(() => stream.Write(null!, 0, 10)); + _ = Assert.ThrowsExactly(() => stream.Write(data, -1, 10)); + _ = Assert.ThrowsExactly(() => stream.Write(data, 200, 10)); + _ = Assert.ThrowsExactly(() => stream.Write(data, 0, -24)); + _ = Assert.ThrowsExactly(() => stream.Write(data, 0, 200)); stream.Dispose(); - _ = Assert.ThrowsException(() => stream.Write(data, 0, data.Length)); + _ = Assert.ThrowsExactly(() => stream.Write(data, 0, data.Length)); } [TestMethod] @@ -160,15 +160,15 @@ public async Task Test_MemoryStream_ReadWriteAsync_Array() Assert.AreEqual(stream.Position, data.Length); Assert.IsTrue(data.AsSpan().SequenceEqual(result)); - _ = await Assert.ThrowsExceptionAsync(() => stream.WriteAsync(null!, 0, 10)); - _ = await Assert.ThrowsExceptionAsync(() => stream.WriteAsync(data, -1, 10)); - _ = await Assert.ThrowsExceptionAsync(() => stream.WriteAsync(data, 200, 10)); - _ = await Assert.ThrowsExceptionAsync(() => stream.WriteAsync(data, 0, -24)); - _ = await Assert.ThrowsExceptionAsync(() => stream.WriteAsync(data, 0, 200)); + _ = await Assert.ThrowsExactlyAsync(() => stream.WriteAsync(null!, 0, 10)); + _ = await Assert.ThrowsExactlyAsync(() => stream.WriteAsync(data, -1, 10)); + _ = await Assert.ThrowsExactlyAsync(() => stream.WriteAsync(data, 200, 10)); + _ = await Assert.ThrowsExactlyAsync(() => stream.WriteAsync(data, 0, -24)); + _ = await Assert.ThrowsExactlyAsync(() => stream.WriteAsync(data, 0, 200)); stream.Dispose(); - _ = await Assert.ThrowsExceptionAsync(() => stream.WriteAsync(data, 0, data.Length)); + _ = await Assert.ThrowsExactlyAsync(() => stream.WriteAsync(data, 0, data.Length)); } [TestMethod] @@ -199,7 +199,7 @@ public void Test_MemoryStream_ReadWriteByte() Assert.AreEqual(stream.Position, data.Length); Assert.IsTrue(data.SequenceEqual(result)); - _ = Assert.ThrowsException(() => stream.WriteByte(128)); + _ = Assert.ThrowsExactly(() => stream.WriteByte(128)); int exitCode = stream.ReadByte(); diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Streams/Test_ReadOnlySequenceStream.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Streams/Test_ReadOnlySequenceStream.cs index 0cb053f2a..3690b49fb 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Streams/Test_ReadOnlySequenceStream.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Streams/Test_ReadOnlySequenceStream.cs @@ -34,8 +34,8 @@ public void Test_ReadOnlySequenceStream_Lifecycle() Assert.IsFalse(stream.CanSeek); Assert.IsFalse(stream.CanWrite); - _ = Assert.ThrowsException(() => stream.Length); - _ = Assert.ThrowsException(() => stream.Position); + _ = Assert.ThrowsExactly(() => stream.Length); + _ = Assert.ThrowsExactly(() => stream.Position); } [TestMethod] @@ -49,20 +49,20 @@ public void Test_ReadOnlySequenceStream_Seek() Assert.AreEqual(stream.Position, 42); - _ = Assert.ThrowsException(() => stream.Position = -1); - _ = Assert.ThrowsException(() => stream.Position = 120); + _ = Assert.ThrowsExactly(() => stream.Position = -1); + _ = Assert.ThrowsExactly(() => stream.Position = 120); _ = stream.Seek(0, SeekOrigin.Begin); - _ = Assert.ThrowsException(() => stream.Seek(-1, SeekOrigin.Begin)); - _ = Assert.ThrowsException(() => stream.Seek(120, SeekOrigin.Begin)); + _ = Assert.ThrowsExactly(() => stream.Seek(-1, SeekOrigin.Begin)); + _ = Assert.ThrowsExactly(() => stream.Seek(120, SeekOrigin.Begin)); Assert.AreEqual(stream.Position, 0); _ = stream.Seek(-1, SeekOrigin.End); - _ = Assert.ThrowsException(() => stream.Seek(20, SeekOrigin.End)); - _ = Assert.ThrowsException(() => stream.Seek(-120, SeekOrigin.End)); + _ = Assert.ThrowsExactly(() => stream.Seek(20, SeekOrigin.End)); + _ = Assert.ThrowsExactly(() => stream.Seek(-120, SeekOrigin.End)); Assert.AreEqual(stream.Position, stream.Length - 1); @@ -70,8 +70,8 @@ public void Test_ReadOnlySequenceStream_Seek() _ = stream.Seek(20, SeekOrigin.Current); _ = stream.Seek(-30, SeekOrigin.Current); - _ = Assert.ThrowsException(() => stream.Seek(-64, SeekOrigin.Current)); - _ = Assert.ThrowsException(() => stream.Seek(80, SeekOrigin.Current)); + _ = Assert.ThrowsExactly(() => stream.Seek(-64, SeekOrigin.Current)); + _ = Assert.ThrowsExactly(() => stream.Seek(80, SeekOrigin.Current)); Assert.AreEqual(stream.Position, 32); } @@ -95,7 +95,7 @@ public void Test_ReadOnlySequenceStream_Read_Array() stream.Dispose(); - _ = Assert.ThrowsException(() => stream.Read(result, 0, result.Length)); + _ = Assert.ThrowsExactly(() => stream.Read(result, 0, result.Length)); } [TestMethod] @@ -119,7 +119,7 @@ public void Test_ReadOnlySequenceStream_NotFromStart_Read_Array() stream.Dispose(); - _ = Assert.ThrowsException(() => stream.Read(result, 0, result.Length)); + _ = Assert.ThrowsExactly(() => stream.Read(result, 0, result.Length)); } [TestMethod] diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Test_NullableReadOnlyRef{T}.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Test_NullableReadOnlyRef{T}.cs index 5df68f113..d2631a826 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Test_NullableReadOnlyRef{T}.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Test_NullableReadOnlyRef{T}.cs @@ -34,12 +34,14 @@ public void Test_NullableReadOnlyRefOfT_CreateNullableReadOnlyRefOfT_Null() } [TestMethod] - [ExpectedException(typeof(InvalidOperationException))] public void Test_NullableReadOnlyRefOfT_CreateNullableReadOnlyRefOfT_Null_Exception() { - NullableReadOnlyRef reference = default; + _ = Assert.ThrowsExactly(() => + { + NullableReadOnlyRef reference = default; - _ = reference.Value; + _ = reference.Value; + }); } [TestMethod] @@ -85,12 +87,14 @@ public void Test_NullableReadOnlyRefOfT_CreateNullableReadOnlyRefOfT_ExplicitCas } [TestMethod] - [ExpectedException(typeof(InvalidOperationException))] public void Test_NullableReadOnlyRefOfT_CreateNullableReadOnlyRefOfT_ExplicitCastOfT_Exception() { - NullableRef invalid = default; + _ = Assert.ThrowsExactly(() => + { + NullableRef invalid = default; - _ = (int)invalid; + _ = (int)invalid; + }); } } diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Test_NullableRef{T}.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Test_NullableRef{T}.cs index 3cfd54430..222914fba 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Test_NullableRef{T}.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Test_NullableRef{T}.cs @@ -38,12 +38,14 @@ public void Test_NullableRefOfT_CreateNullableRefOfT_Null() } [TestMethod] - [ExpectedException(typeof(InvalidOperationException))] public void Test_NullableRefOfT_CreateNullableRefOfT_Null_Exception() { - NullableRef reference = default; + _ = Assert.ThrowsExactly(() => + { + NullableRef reference = default; - _ = reference.Value; + _ = reference.Value; + }); } [TestMethod] @@ -67,12 +69,14 @@ public void Test_NullableRefOfT_CreateNullableRefOfT_ExplicitCastOfT() } [TestMethod] - [ExpectedException(typeof(InvalidOperationException))] public void Test_NullableRefOfT_CreateNullableRefOfT_ExplicitCastOfT_Exception() { - NullableRef invalid = default; + _ = Assert.ThrowsExactly(() => + { + NullableRef invalid = default; - _ = (int)invalid; + _ = (int)invalid; + }); } } diff --git a/tests/CommunityToolkit.Mvvm.DisableINotifyPropertyChanging.UnitTests/CommunityToolkit.Mvvm.DisableINotifyPropertyChanging.UnitTests.csproj b/tests/CommunityToolkit.Mvvm.DisableINotifyPropertyChanging.UnitTests/CommunityToolkit.Mvvm.DisableINotifyPropertyChanging.UnitTests.csproj index c75186e20..dfd6f49bf 100644 --- a/tests/CommunityToolkit.Mvvm.DisableINotifyPropertyChanging.UnitTests/CommunityToolkit.Mvvm.DisableINotifyPropertyChanging.UnitTests.csproj +++ b/tests/CommunityToolkit.Mvvm.DisableINotifyPropertyChanging.UnitTests/CommunityToolkit.Mvvm.DisableINotifyPropertyChanging.UnitTests.csproj @@ -5,8 +5,8 @@ - - + + diff --git a/tests/CommunityToolkit.Mvvm.Internals.UnitTests/CommunityToolkit.Mvvm.Internals.UnitTests.csproj b/tests/CommunityToolkit.Mvvm.Internals.UnitTests/CommunityToolkit.Mvvm.Internals.UnitTests.csproj index 63a93ed44..cb840a906 100644 --- a/tests/CommunityToolkit.Mvvm.Internals.UnitTests/CommunityToolkit.Mvvm.Internals.UnitTests.csproj +++ b/tests/CommunityToolkit.Mvvm.Internals.UnitTests/CommunityToolkit.Mvvm.Internals.UnitTests.csproj @@ -5,8 +5,8 @@ - - + + diff --git a/tests/CommunityToolkit.Mvvm.Internals.UnitTests/Test_Dictionary2.cs b/tests/CommunityToolkit.Mvvm.Internals.UnitTests/Test_Dictionary2.cs index 06c418c86..1edd57ab3 100644 --- a/tests/CommunityToolkit.Mvvm.Internals.UnitTests/Test_Dictionary2.cs +++ b/tests/CommunityToolkit.Mvvm.Internals.UnitTests/Test_Dictionary2.cs @@ -32,7 +32,7 @@ private static void AddItems(T[] keys) Assert.IsNull(obj); Assert.IsFalse(dictionary.TryRemove(key)); - _ = Assert.ThrowsException(() => _ = dictionary[key]); + _ = Assert.ThrowsExactly(() => _ = dictionary[key]); // This should create a new entry ref object? value = ref dictionary.GetOrAddValueRef(key); diff --git a/tests/CommunityToolkit.Mvvm.Roslyn4001.UnitTests/CommunityToolkit.Mvvm.Roslyn4001.UnitTests.csproj b/tests/CommunityToolkit.Mvvm.Roslyn4001.UnitTests/CommunityToolkit.Mvvm.Roslyn4001.UnitTests.csproj index c02c814d2..0a0010d8c 100644 --- a/tests/CommunityToolkit.Mvvm.Roslyn4001.UnitTests/CommunityToolkit.Mvvm.Roslyn4001.UnitTests.csproj +++ b/tests/CommunityToolkit.Mvvm.Roslyn4001.UnitTests/CommunityToolkit.Mvvm.Roslyn4001.UnitTests.csproj @@ -7,8 +7,8 @@ - - + + diff --git a/tests/CommunityToolkit.Mvvm.Roslyn4031.UnitTests/CommunityToolkit.Mvvm.Roslyn4031.UnitTests.csproj b/tests/CommunityToolkit.Mvvm.Roslyn4031.UnitTests/CommunityToolkit.Mvvm.Roslyn4031.UnitTests.csproj index 808888eee..d5111588e 100644 --- a/tests/CommunityToolkit.Mvvm.Roslyn4031.UnitTests/CommunityToolkit.Mvvm.Roslyn4031.UnitTests.csproj +++ b/tests/CommunityToolkit.Mvvm.Roslyn4031.UnitTests/CommunityToolkit.Mvvm.Roslyn4031.UnitTests.csproj @@ -7,8 +7,8 @@ - - + + diff --git a/tests/CommunityToolkit.Mvvm.Roslyn4120.UnitTests/CommunityToolkit.Mvvm.Roslyn4120.UnitTests.csproj b/tests/CommunityToolkit.Mvvm.Roslyn4120.UnitTests/CommunityToolkit.Mvvm.Roslyn4120.UnitTests.csproj index 6da4240e3..3252a1f5a 100644 --- a/tests/CommunityToolkit.Mvvm.Roslyn4120.UnitTests/CommunityToolkit.Mvvm.Roslyn4120.UnitTests.csproj +++ b/tests/CommunityToolkit.Mvvm.Roslyn4120.UnitTests/CommunityToolkit.Mvvm.Roslyn4120.UnitTests.csproj @@ -12,8 +12,8 @@ - - + + diff --git a/tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4001.UnitTests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4001.UnitTests.csproj b/tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4001.UnitTests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4001.UnitTests.csproj index eb6b29627..00f117384 100644 --- a/tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4001.UnitTests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4001.UnitTests.csproj +++ b/tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4001.UnitTests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4001.UnitTests.csproj @@ -9,8 +9,8 @@ - - + + diff --git a/tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4031.UnitTests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4031.UnitTests.csproj b/tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4031.UnitTests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4031.UnitTests.csproj index bc610e942..f570d3cab 100644 --- a/tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4031.UnitTests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4031.UnitTests.csproj +++ b/tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4031.UnitTests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4031.UnitTests.csproj @@ -14,8 +14,8 @@ - - + + diff --git a/tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4120.UnitTests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4120.UnitTests.csproj b/tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4120.UnitTests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4120.UnitTests.csproj index 62d9db41f..87225d036 100644 --- a/tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4120.UnitTests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4120.UnitTests.csproj +++ b/tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4120.UnitTests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4120.UnitTests.csproj @@ -10,8 +10,8 @@ - - + + diff --git a/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ObservableGroup.cs b/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ObservableGroup.cs index 15c28865f..947f14f14 100644 --- a/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ObservableGroup.cs +++ b/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ObservableGroup.cs @@ -118,39 +118,34 @@ public void Test_ObservableGroup_IReadOnlyObservableGroup_ShouldReturnExpectedVa } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_ObservableGroup_Ctor_NullKey() { - _ = new ObservableGroup((string)null!); + _ = Assert.ThrowsExactly(() => _ = new ObservableGroup((string)null!)); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_ObservableGroup_Ctor_NullGroup() { - _ = new ObservableGroup((IGrouping)null!); + _ = Assert.ThrowsExactly(() => _ = new ObservableGroup((IGrouping)null!)); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_ObservableGroup_Ctor_NullKeyWithNotNullElements() { - _ = new ObservableGroup(null!, new int[0]); + _ = Assert.ThrowsExactly(() => _ = new ObservableGroup(null!, [])); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_ObservableGroup_Ctor_NotNullKeyWithNullElements() { - _ = new ObservableGroup("A", null!); + _ = Assert.ThrowsExactly(() => _ = new ObservableGroup("A", null!)); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_ObservableGroup_Ctor_NullKeySetter() { ObservableGroup group = new("A"); - group.Key = null!; + _ = Assert.ThrowsExactly(() => group.Key = null!); } } diff --git a/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ObservableGroupedCollection.cs b/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ObservableGroupedCollection.cs index 0576c9bd3..fb2ad16aa 100644 --- a/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ObservableGroupedCollection.cs +++ b/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ObservableGroupedCollection.cs @@ -41,9 +41,8 @@ public void Test_ObservableGroupedCollection_Ctor_WithGroups_ShouldHaveExpectedV } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_ObservableGroupedCollection_Ctor_NullCollection() { - _ = new ObservableGroupedCollection(null!); + _ = Assert.ThrowsExactly(() => _ = new ObservableGroupedCollection(null!)); } } diff --git a/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ObservableGroupedCollectionExtensions.cs b/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ObservableGroupedCollectionExtensions.cs index 3086e36f4..c76b91421 100644 --- a/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ObservableGroupedCollectionExtensions.cs +++ b/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ObservableGroupedCollectionExtensions.cs @@ -29,14 +29,13 @@ public void Test_ObservableGroupedCollectionExtensions_FirstGroupByKey_WhenGroup } [TestMethod] - [ExpectedException(typeof(InvalidOperationException))] public void Test_ObservableGroupedCollectionExtensions_FirstGroupByKey_WhenGroupDoesNotExist_ShouldThrow() { ObservableGroupedCollection groupedCollection = new(); _ = groupedCollection.AddGroup("A", new[] { 23 }); - _ = groupedCollection.FirstGroupByKey("I do not exist"); + _ = Assert.ThrowsExactly(() => _ = groupedCollection.FirstGroupByKey("I do not exist")); } [TestMethod] @@ -84,7 +83,6 @@ public void Test_ObservableGroupedCollectionExtensions_FirstGroupByKey_WhenGroup [TestMethod] [DataRow(-1)] [DataRow(3)] - [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_ObservableGroupedCollectionExtensions_FirstGroupByKey_WhenGroupExistsAndIndexOutOfRange_ShouldReturnThrow(int index) { ObservableGroupedCollection groupedCollection = new(); @@ -93,7 +91,7 @@ public void Test_ObservableGroupedCollectionExtensions_FirstGroupByKey_WhenGroup _ = groupedCollection.AddGroup("B", new[] { 10, 11, 12 }); _ = groupedCollection.AddGroup("B", new[] { 42 }); - _ = groupedCollection.FirstGroupByKey("B")[index]; + _ = Assert.ThrowsExactly(() => _ = groupedCollection.FirstGroupByKey("B")[index]); } [TestMethod] diff --git a/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ReadOnlyObservableGroup.cs b/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ReadOnlyObservableGroup.cs index 88da0a1e2..ed4334922 100644 --- a/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ReadOnlyObservableGroup.cs +++ b/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ReadOnlyObservableGroup.cs @@ -131,23 +131,20 @@ public void Test_ReadOnlyObservableGroup_IReadOnlyObservableGroup_ShouldReturnEx } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_ReadOnlyObservableGroup_Ctor_NullKeyWithNotNullElements() { - _ = new ReadOnlyObservableGroup(null!, new ObservableCollection()); + _ = Assert.ThrowsExactly(() => _ = new ReadOnlyObservableGroup(null!, new ObservableCollection())); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_ReadOnlyObservableGroup_Ctor_NotNullKeyWithNullElements() { - _ = new ReadOnlyObservableGroup("A", null!); + _ = Assert.ThrowsExactly(() => _ = new ReadOnlyObservableGroup("A", null!)); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_ReadOnlyObservableGroup_Ctor_NullGroup() { - _ = new ReadOnlyObservableGroup(null!); + _ = Assert.ThrowsExactly(() => _ = new ReadOnlyObservableGroup(null!)); } } diff --git a/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ReadOnlyObservableGroupedCollection.cs b/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ReadOnlyObservableGroupedCollection.cs index 21b52285c..3621c6b19 100644 --- a/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ReadOnlyObservableGroupedCollection.cs +++ b/tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ReadOnlyObservableGroupedCollection.cs @@ -110,12 +110,12 @@ public void Test_ReadOnlyObservableGroupedCollection_IListImplementation_Mutable ReadOnlyObservableGroup? testGroup = new("test", new ObservableCollection()); - _ = Assert.ThrowsException(() => list.Add(testGroup)); - _ = Assert.ThrowsException(() => list.Clear()); - _ = Assert.ThrowsException(() => list.Insert(2, testGroup)); - _ = Assert.ThrowsException(() => list.Remove(testGroup)); - _ = Assert.ThrowsException(() => list.RemoveAt(2)); - _ = Assert.ThrowsException(() => list[2] = testGroup); + _ = Assert.ThrowsExactly(() => list.Add(testGroup)); + _ = Assert.ThrowsExactly(() => list.Clear()); + _ = Assert.ThrowsExactly(() => list.Insert(2, testGroup)); + _ = Assert.ThrowsExactly(() => list.Remove(testGroup)); + _ = Assert.ThrowsExactly(() => list.RemoveAt(2)); + _ = Assert.ThrowsExactly(() => list[2] = testGroup); object[]? array = new object[5]; @@ -470,16 +470,14 @@ private static bool IsReplaceEventValid(NotifyCollectionChangedEventArgs args, I private static bool IsResetEventValid(NotifyCollectionChangedEventArgs args) => args.Action == NotifyCollectionChangedAction.Reset && args.NewItems == null && args.OldItems == null; [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_ReadOnlyObservableGroupedCollection_Ctor_NullCollectionWithObservableGroups() { - _ = new ReadOnlyObservableGroupedCollection((ObservableCollection>)null!); + _ = Assert.ThrowsExactly(() => _ = new ReadOnlyObservableGroupedCollection((ObservableCollection>)null!)); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_ReadOnlyObservableGroupedCollection_Ctor_NullCollectionWithReadOnlyObservableGroups() { - _ = new ReadOnlyObservableGroupedCollection((ObservableCollection>)null!); + _ = Assert.ThrowsExactly(() => _ = new ReadOnlyObservableGroupedCollection((ObservableCollection>)null!)); } } diff --git a/tests/CommunityToolkit.Mvvm.UnitTests/Test_ArgumentNullException.ComponentModel.cs b/tests/CommunityToolkit.Mvvm.UnitTests/Test_ArgumentNullException.ComponentModel.cs index 21187ea57..602a5b0a6 100644 --- a/tests/CommunityToolkit.Mvvm.UnitTests/Test_ArgumentNullException.ComponentModel.cs +++ b/tests/CommunityToolkit.Mvvm.UnitTests/Test_ArgumentNullException.ComponentModel.cs @@ -310,7 +310,7 @@ public void ValidateProperty(string propertyName) /// The parameter name to check. private static void Assert(Action action, string parameterName) { - System.ArgumentNullException exception = Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsException(action); + System.ArgumentNullException exception = Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsExactly(action); Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(exception.ParamName, parameterName); } diff --git a/tests/CommunityToolkit.Mvvm.UnitTests/Test_AsyncRelayCommand.cs b/tests/CommunityToolkit.Mvvm.UnitTests/Test_AsyncRelayCommand.cs index c786e0de0..addc9661b 100644 --- a/tests/CommunityToolkit.Mvvm.UnitTests/Test_AsyncRelayCommand.cs +++ b/tests/CommunityToolkit.Mvvm.UnitTests/Test_AsyncRelayCommand.cs @@ -381,7 +381,7 @@ public async Task Test_AsyncRelayCommand_EnsureExceptionThrown() executeException = e; } - executeAsyncException = await Assert.ThrowsExceptionAsync(() => command.ExecuteAsync(null)); + executeAsyncException = await Assert.ThrowsExactlyAsync(() => command.ExecuteAsync(null)); Assert.AreEqual(nameof(Test_AsyncRelayCommand_EnsureExceptionThrown), executeException?.Message); Assert.AreEqual(nameof(Test_AsyncRelayCommand_EnsureExceptionThrown), executeAsyncException?.Message); diff --git a/tests/CommunityToolkit.Mvvm.UnitTests/Test_AsyncRelayCommand{T}.cs b/tests/CommunityToolkit.Mvvm.UnitTests/Test_AsyncRelayCommand{T}.cs index 481d72113..df24b6aa6 100644 --- a/tests/CommunityToolkit.Mvvm.UnitTests/Test_AsyncRelayCommand{T}.cs +++ b/tests/CommunityToolkit.Mvvm.UnitTests/Test_AsyncRelayCommand{T}.cs @@ -341,7 +341,7 @@ public async Task Test_AsyncRelayCommandOfT_EnsureExceptionThrown() executeException = e; } - executeAsyncException = await Assert.ThrowsExceptionAsync(() => command.ExecuteAsync((object)delay)); + executeAsyncException = await Assert.ThrowsExactlyAsync(() => command.ExecuteAsync((object)delay)); Assert.AreEqual(nameof(Test_AsyncRelayCommandOfT_EnsureExceptionThrown), executeException?.Message); Assert.AreEqual(nameof(Test_AsyncRelayCommandOfT_EnsureExceptionThrown), executeAsyncException?.Message); @@ -376,7 +376,7 @@ public async Task Test_AsyncRelayCommandOfT_EnsureExceptionThrown_GenericExecute executeTException = e; } - executeAsyncException = await Assert.ThrowsExceptionAsync(() => command.ExecuteAsync(delay)); + executeAsyncException = await Assert.ThrowsExactlyAsync(() => command.ExecuteAsync(delay)); Assert.AreEqual(nameof(Test_AsyncRelayCommandOfT_EnsureExceptionThrown_GenericExecute), executeTException?.Message); Assert.AreEqual(nameof(Test_AsyncRelayCommandOfT_EnsureExceptionThrown_GenericExecute), executeAsyncException?.Message); diff --git a/tests/CommunityToolkit.Mvvm.UnitTests/Test_Messenger.Observables.cs b/tests/CommunityToolkit.Mvvm.UnitTests/Test_Messenger.Observables.cs index e631eddf9..161e5ab21 100644 --- a/tests/CommunityToolkit.Mvvm.UnitTests/Test_Messenger.Observables.cs +++ b/tests/CommunityToolkit.Mvvm.UnitTests/Test_Messenger.Observables.cs @@ -80,23 +80,20 @@ public void Test_Messenger_CreateObservable_WithToken(Type type) } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_Messenger_CreateObservable_NullMessenger() { - _ = IMessengerExtensions.CreateObservable(null!); + _ = Assert.ThrowsExactly(() => _ = IMessengerExtensions.CreateObservable(null!)); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_Messenger_CreateObservable_WithToken_NullMessenger() { - _ = IMessengerExtensions.CreateObservable(null!, "Hello"); + _ = Assert.ThrowsExactly(() => _ = IMessengerExtensions.CreateObservable(null!, "Hello")); } [TestMethod] - [ExpectedException(typeof(ArgumentNullException))] public void Test_Messenger_CreateObservable_WithToken_NullToken() { - _ = IMessengerExtensions.CreateObservable(new WeakReferenceMessenger(), null!); + _ = Assert.ThrowsExactly(() => _ = IMessengerExtensions.CreateObservable(new WeakReferenceMessenger(), null!)); } } diff --git a/tests/CommunityToolkit.Mvvm.UnitTests/Test_Messenger.Request.cs b/tests/CommunityToolkit.Mvvm.UnitTests/Test_Messenger.Request.cs index b6c6f3ac1..47a322200 100644 --- a/tests/CommunityToolkit.Mvvm.UnitTests/Test_Messenger.Request.cs +++ b/tests/CommunityToolkit.Mvvm.UnitTests/Test_Messenger.Request.cs @@ -45,7 +45,6 @@ void Receive(object recipient, NumberRequestMessage m) [TestMethod] [DataRow(typeof(StrongReferenceMessenger))] [DataRow(typeof(WeakReferenceMessenger))] - [ExpectedException(typeof(InvalidOperationException))] public void Test_Messenger_RequestMessage_Fail_NoReply(Type type) { IMessenger? messenger = (IMessenger)Activator.CreateInstance(type)!; @@ -54,13 +53,12 @@ public void Test_Messenger_RequestMessage_Fail_NoReply(Type type) Assert.IsFalse(message.HasReceivedResponse); - _ = message.Response; + _ = Assert.ThrowsExactly(() => _ = message.Response); } [TestMethod] [DataRow(typeof(StrongReferenceMessenger))] [DataRow(typeof(WeakReferenceMessenger))] - [ExpectedException(typeof(InvalidOperationException))] public void Test_Messenger_RequestMessage_Fail_MultipleReplies(Type type) { IMessenger? messenger = (IMessenger)Activator.CreateInstance(type)!; @@ -74,7 +72,7 @@ static void Receive(object recipient, NumberRequestMessage m) messenger.Register(recipient, Receive); - int result = messenger.Send(); + _ = Assert.ThrowsExactly(() => _ = messenger.Send()); GC.KeepAlive(recipient); } @@ -145,7 +143,6 @@ void Receive(object recipient, AsyncNumberRequestMessage m) [TestMethod] [DataRow(typeof(StrongReferenceMessenger))] [DataRow(typeof(WeakReferenceMessenger))] - [ExpectedException(typeof(InvalidOperationException))] public async Task Test_Messenger_AsyncRequestMessage_Fail_NoReply(Type type) { IMessenger? messenger = (IMessenger)Activator.CreateInstance(type)!; @@ -154,13 +151,12 @@ public async Task Test_Messenger_AsyncRequestMessage_Fail_NoReply(Type type) Assert.IsFalse(message.HasReceivedResponse); - _ = await message.Response; + _ = await Assert.ThrowsExactlyAsync(async () => _ = await message.Response); } [TestMethod] [DataRow(typeof(StrongReferenceMessenger))] [DataRow(typeof(WeakReferenceMessenger))] - [ExpectedException(typeof(InvalidOperationException))] public async Task Test_Messenger_AsyncRequestMessage_Fail_MultipleReplies(Type type) { IMessenger? messenger = (IMessenger)Activator.CreateInstance(type)!; @@ -174,9 +170,12 @@ static void Receive(object recipient, AsyncNumberRequestMessage m) messenger.Register(recipient, Receive); - int result = await messenger.Send(); + _ = await Assert.ThrowsExactlyAsync(async () => + { + _ = await messenger.Send(); - GC.KeepAlive(recipient); + GC.KeepAlive(recipient); + }); } public class AsyncNumberRequestMessage : AsyncRequestMessage diff --git a/tests/CommunityToolkit.Mvvm.UnitTests/Test_Messenger.cs b/tests/CommunityToolkit.Mvvm.UnitTests/Test_Messenger.cs index 16f5c7afe..f64bf3e94 100644 --- a/tests/CommunityToolkit.Mvvm.UnitTests/Test_Messenger.cs +++ b/tests/CommunityToolkit.Mvvm.UnitTests/Test_Messenger.cs @@ -306,7 +306,7 @@ public void Test_Messenger_DuplicateRegistrationWithMessageType(Type type) messenger.Register(recipient, (r, m) => { }); - _ = Assert.ThrowsException(() => + _ = Assert.ThrowsExactly(() => { messenger.Register(recipient, (r, m) => { }); }); @@ -322,7 +322,7 @@ public void Test_Messenger_DuplicateRegistrationWithMessageTypeAndToken(Type typ messenger.Register(recipient, nameof(MessageA), (r, m) => { }); - _ = Assert.ThrowsException(() => + _ = Assert.ThrowsExactly(() => { messenger.Register(recipient, nameof(MessageA), (r, m) => { }); });