Skip to content

Commit 2b42d13

Browse files
committed
Use Assert.ThrowsExactly in unit tests
Replaces usage of Assert.ThrowsException with Assert.ThrowsExactly across multiple unit test files to ensure that tests only pass when the exact exception type is thrown, improving test precision and reliability.
1 parent 5215c84 commit 2b42d13

22 files changed

+297
-297
lines changed

tests/CommunityToolkit.Common.UnitTests/Extensions/Test_ArrayExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public void Test_ArrayExtensions_Jagged_GetColumn_Exception()
3737
new int[] { 7 }
3838
};
3939

40-
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
40+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() =>
4141
{
4242
_ = array.GetColumn(-1).ToArray();
4343
});
4444

45-
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
45+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() =>
4646
{
4747
_ = array.GetColumn(3).ToArray();
4848
});

tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_ArrayPoolBufferWriter{T}.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,18 @@ public void Test_ArrayPoolBufferWriterOfT_AllocateAndGetMemoryAndSpan()
7878
Assert.AreEqual(writer.WrittenMemory.Length, 43);
7979
Assert.AreEqual(writer.WrittenSpan.Length, 43);
8080

81-
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => writer.Advance(-1));
82-
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => writer.GetMemory(-1));
83-
_ = Assert.ThrowsException<ArgumentException>(() => writer.Advance(1024));
81+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => writer.Advance(-1));
82+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => writer.GetMemory(-1));
83+
_ = Assert.ThrowsExactly<ArgumentException>(() => writer.Advance(1024));
8484

8585
writer.Dispose();
8686

87-
_ = Assert.ThrowsException<ObjectDisposedException>(() => writer.WrittenMemory);
88-
_ = Assert.ThrowsException<ObjectDisposedException>(() => writer.WrittenSpan.Length);
89-
_ = Assert.ThrowsException<ObjectDisposedException>(() => writer.Capacity);
90-
_ = Assert.ThrowsException<ObjectDisposedException>(() => writer.FreeCapacity);
91-
_ = Assert.ThrowsException<ObjectDisposedException>(() => writer.Clear());
92-
_ = Assert.ThrowsException<ObjectDisposedException>(() => writer.Advance(1));
87+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => writer.WrittenMemory);
88+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => writer.WrittenSpan.Length);
89+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => writer.Capacity);
90+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => writer.FreeCapacity);
91+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => writer.Clear());
92+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => writer.Advance(1));
9393
}
9494

9595
[TestMethod]
@@ -119,18 +119,18 @@ public void Test_ArrayPoolBufferWriterOfT_AllocateFromCustomPoolAndGetMemoryAndS
119119
Assert.AreEqual(writer.WrittenMemory.Length, 43);
120120
Assert.AreEqual(writer.WrittenSpan.Length, 43);
121121

122-
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => writer.Advance(-1));
123-
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => writer.GetMemory(-1));
124-
_ = Assert.ThrowsException<ArgumentException>(() => writer.Advance(1024));
122+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => writer.Advance(-1));
123+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => writer.GetMemory(-1));
124+
_ = Assert.ThrowsExactly<ArgumentException>(() => writer.Advance(1024));
125125

126126
writer.Dispose();
127127

128-
_ = Assert.ThrowsException<ObjectDisposedException>(() => writer.WrittenMemory);
129-
_ = Assert.ThrowsException<ObjectDisposedException>(() => writer.WrittenSpan.Length);
130-
_ = Assert.ThrowsException<ObjectDisposedException>(() => writer.Capacity);
131-
_ = Assert.ThrowsException<ObjectDisposedException>(() => writer.FreeCapacity);
132-
_ = Assert.ThrowsException<ObjectDisposedException>(() => writer.Clear());
133-
_ = Assert.ThrowsException<ObjectDisposedException>(() => writer.Advance(1));
128+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => writer.WrittenMemory);
129+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => writer.WrittenSpan.Length);
130+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => writer.Capacity);
131+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => writer.FreeCapacity);
132+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => writer.Clear());
133+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => writer.Advance(1));
134134
}
135135

136136
Assert.AreEqual(pool.RentedArrays.Count, 0);
@@ -218,7 +218,7 @@ public void Test_ArrayPoolBufferWriterOfT_AsStream()
218218
writer.Dispose();
219219

220220
// Now check that the writer is actually disposed instead
221-
_ = Assert.ThrowsException<ObjectDisposedException>(() => writer.Capacity);
221+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => writer.Capacity);
222222
}
223223

224224
[TestMethod]
@@ -244,6 +244,6 @@ public void Test_ArrayPoolBufferWriterOfT_AllocateAndGetArray()
244244

245245
bufferWriter.Dispose();
246246

247-
_ = Assert.ThrowsException<ObjectDisposedException>(() => bufferWriter.DangerousGetArray());
247+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => bufferWriter.DangerousGetArray());
248248
}
249249
}

tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_MemoryBufferWriter{T}.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ public void Test_MemoryBufferWriterOfT_AllocateAndGetMemoryAndSpan()
4242
Assert.AreEqual(memory.Length - 43, writer.GetSpan(22).Length);
4343
Assert.AreEqual(memory.Length - 43, writer.GetMemory(22).Length);
4444

45-
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => writer.Advance(-1));
46-
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => writer.GetMemory(-1));
47-
_ = Assert.ThrowsException<ArgumentException>(() => writer.GetSpan(1024));
48-
_ = Assert.ThrowsException<ArgumentException>(() => writer.GetMemory(1024));
49-
_ = Assert.ThrowsException<ArgumentException>(() => writer.Advance(1024));
45+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => writer.Advance(-1));
46+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => writer.GetMemory(-1));
47+
_ = Assert.ThrowsExactly<ArgumentException>(() => writer.GetSpan(1024));
48+
_ = Assert.ThrowsExactly<ArgumentException>(() => writer.GetMemory(1024));
49+
_ = Assert.ThrowsExactly<ArgumentException>(() => writer.Advance(1024));
5050
}
5151

5252
[TestMethod]

tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_MemoryOwner{T}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void Test_MemoryOwnerOfT_AllocateAndGetArray()
138138
// The original buffer instance is disposed here, because calling Slice transfers
139139
// the ownership of the internal buffer to the new instance (this is documented in
140140
// XML docs for the MemoryOwner<T>.Slice method).
141-
_ = Assert.ThrowsException<ObjectDisposedException>(() => buffer.DangerousGetArray());
141+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => buffer.DangerousGetArray());
142142

143143
segment = second.DangerousGetArray();
144144

@@ -150,6 +150,6 @@ public void Test_MemoryOwnerOfT_AllocateAndGetArray()
150150

151151
second.Dispose();
152152

153-
_ = Assert.ThrowsException<ObjectDisposedException>(() => second.DangerousGetArray());
153+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => second.DangerousGetArray());
154154
}
155155
}

tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_ReadOnlyRefEnumerable{T}.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public void Test_ReadOnlyRefEnumerable_Indexer_ThrowsIndexOutOfRange()
7676
0, 0, 0, 0
7777
};
7878

79-
_ = Assert.ThrowsException<IndexOutOfRangeException>(() => ReadOnlyRefEnumerable<int>.DangerousCreate(in array[0], array.Length, 1)[-1]);
80-
_ = Assert.ThrowsException<IndexOutOfRangeException>(() => ReadOnlyRefEnumerable<int>.DangerousCreate(in array[0], array.Length, 1)[array.Length]);
79+
_ = Assert.ThrowsExactly<IndexOutOfRangeException>(() => ReadOnlyRefEnumerable<int>.DangerousCreate(in array[0], array.Length, 1)[-1]);
80+
_ = Assert.ThrowsExactly<IndexOutOfRangeException>(() => ReadOnlyRefEnumerable<int>.DangerousCreate(in array[0], array.Length, 1)[array.Length]);
8181
}
8282

8383
#if NET6_0_OR_GREATER
@@ -112,8 +112,8 @@ public void Test_ReadOnlyRefEnumerable_Index_Indexer_ThrowsIndexOutOfRange()
112112
0, 0, 0, 0
113113
};
114114

115-
_ = Assert.ThrowsException<IndexOutOfRangeException>(() => ReadOnlyRefEnumerable<int>.DangerousCreate(in array[0], array.Length, 1)[new Index(array.Length)]);
116-
_ = Assert.ThrowsException<IndexOutOfRangeException>(() => ReadOnlyRefEnumerable<int>.DangerousCreate(in array[0], array.Length, 1)[^0]);
115+
_ = Assert.ThrowsExactly<IndexOutOfRangeException>(() => ReadOnlyRefEnumerable<int>.DangerousCreate(in array[0], array.Length, 1)[new Index(array.Length)]);
116+
_ = Assert.ThrowsExactly<IndexOutOfRangeException>(() => ReadOnlyRefEnumerable<int>.DangerousCreate(in array[0], array.Length, 1)[^0]);
117117
}
118118
#endif
119119
}

tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_RefEnumerable{T}.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public void Test_RefEnumerable_Indexer_ThrowsIndexOutOfRange()
7676
0, 0, 0, 0
7777
};
7878

79-
_ = Assert.ThrowsException<IndexOutOfRangeException>(() => RefEnumerable<int>.DangerousCreate(ref array[0], array.Length, 1)[-1]);
80-
_ = Assert.ThrowsException<IndexOutOfRangeException>(() => RefEnumerable<int>.DangerousCreate(ref array[0], array.Length, 1)[array.Length]);
79+
_ = Assert.ThrowsExactly<IndexOutOfRangeException>(() => RefEnumerable<int>.DangerousCreate(ref array[0], array.Length, 1)[-1]);
80+
_ = Assert.ThrowsExactly<IndexOutOfRangeException>(() => RefEnumerable<int>.DangerousCreate(ref array[0], array.Length, 1)[array.Length]);
8181
}
8282

8383
#if NET6_0_OR_GREATER
@@ -112,8 +112,8 @@ public void Test_RefEnumerable_Index_Indexer_ThrowsIndexOutOfRange()
112112
0, 0, 0, 0
113113
};
114114

115-
_ = Assert.ThrowsException<IndexOutOfRangeException>(() => RefEnumerable<int>.DangerousCreate(ref array[0], array.Length, 1)[new Index(array.Length)]);
116-
_ = Assert.ThrowsException<IndexOutOfRangeException>(() => RefEnumerable<int>.DangerousCreate(ref array[0], array.Length, 1)[^0]);
115+
_ = Assert.ThrowsExactly<IndexOutOfRangeException>(() => RefEnumerable<int>.DangerousCreate(ref array[0], array.Length, 1)[new Index(array.Length)]);
116+
_ = Assert.ThrowsExactly<IndexOutOfRangeException>(() => RefEnumerable<int>.DangerousCreate(ref array[0], array.Length, 1)[^0]);
117117
}
118118
#endif
119119
}

tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_ArrayExtensions.2D.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,10 @@ public void Test_ArrayExtensions_2D_GetRow_Rectangle()
179179
// Test an empty array
180180
Assert.AreSame(new int[1, 0].GetRow(0).ToArray(), Array.Empty<int>());
181181

182-
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => array.GetRow(-1));
183-
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => array.GetRow(3));
182+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => array.GetRow(-1));
183+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => array.GetRow(3));
184184

185-
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => array.GetRow(20));
185+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => array.GetRow(20));
186186
}
187187

188188
[TestMethod]
@@ -204,10 +204,10 @@ public void Test_ArrayExtensions_2D_GetColumn_Rectangle()
204204

205205
CollectionAssert.AreEqual(array.GetColumn(1).ToArray(), new[] { 2, 6, 10 });
206206

207-
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => array.GetColumn(-1));
208-
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => array.GetColumn(4));
207+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => array.GetColumn(-1));
208+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => array.GetColumn(4));
209209

210-
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => array.GetColumn(20));
210+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => array.GetColumn(20));
211211
}
212212

213213
[TestMethod]
@@ -216,7 +216,7 @@ public void Test_ArrayExtensions_2D_GetRow_Empty()
216216
int[,] array = new int[0, 0];
217217

218218
// Try to get a row from an empty array (the row index isn't in range)
219-
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => array.GetRow(0).ToArray());
219+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => array.GetRow(0).ToArray());
220220
}
221221

222222
[TestMethod]
@@ -274,11 +274,11 @@ public void Test_ArrayExtensions_2D_GetRowOrColumn_Helpers()
274274
CollectionAssert.AreEqual(copy, result);
275275

276276
// Some invalid attempts to copy to an empty span or sequence
277-
_ = Assert.ThrowsException<ArgumentException>(() => array.GetRow(0).CopyTo(default(RefEnumerable<int>)));
278-
_ = Assert.ThrowsException<ArgumentException>(() => array.GetRow(0).CopyTo(default(Span<int>)));
277+
_ = Assert.ThrowsExactly<ArgumentException>(() => array.GetRow(0).CopyTo(default(RefEnumerable<int>)));
278+
_ = Assert.ThrowsExactly<ArgumentException>(() => array.GetRow(0).CopyTo(default(Span<int>)));
279279

280-
_ = Assert.ThrowsException<ArgumentException>(() => array.GetColumn(0).CopyTo(default(RefEnumerable<int>)));
281-
_ = Assert.ThrowsException<ArgumentException>(() => array.GetColumn(0).CopyTo(default(Span<int>)));
280+
_ = Assert.ThrowsExactly<ArgumentException>(() => array.GetColumn(0).CopyTo(default(RefEnumerable<int>)));
281+
_ = Assert.ThrowsExactly<ArgumentException>(() => array.GetColumn(0).CopyTo(default(Span<int>)));
282282

283283
// Same as CopyTo, but this will fail gracefully with an invalid target
284284
Assert.IsTrue(array.GetRow(2).TryCopyTo(copy));
@@ -344,11 +344,11 @@ public void Test_ArrayExtensions_2D_ReadOnlyGetRowOrColumn_Helpers()
344344

345345
CollectionAssert.AreEqual(copy, result);
346346

347-
_ = Assert.ThrowsException<ArgumentException>(() => ((ReadOnlySpan2D<int>)array).GetRow(0).CopyTo(default(RefEnumerable<int>)));
348-
_ = Assert.ThrowsException<ArgumentException>(() => ((ReadOnlySpan2D<int>)array).GetRow(0).CopyTo(default(Span<int>)));
347+
_ = Assert.ThrowsExactly<ArgumentException>(() => ((ReadOnlySpan2D<int>)array).GetRow(0).CopyTo(default(RefEnumerable<int>)));
348+
_ = Assert.ThrowsExactly<ArgumentException>(() => ((ReadOnlySpan2D<int>)array).GetRow(0).CopyTo(default(Span<int>)));
349349

350-
_ = Assert.ThrowsException<ArgumentException>(() => ((ReadOnlySpan2D<int>)array).GetColumn(0).CopyTo(default(RefEnumerable<int>)));
351-
_ = Assert.ThrowsException<ArgumentException>(() => ((ReadOnlySpan2D<int>)array).GetColumn(0).CopyTo(default(Span<int>)));
350+
_ = Assert.ThrowsExactly<ArgumentException>(() => ((ReadOnlySpan2D<int>)array).GetColumn(0).CopyTo(default(RefEnumerable<int>)));
351+
_ = Assert.ThrowsExactly<ArgumentException>(() => ((ReadOnlySpan2D<int>)array).GetColumn(0).CopyTo(default(Span<int>)));
352352

353353
Assert.IsTrue(span2D.GetRow(2).TryCopyTo(copy));
354354
Assert.IsFalse(span2D.GetRow(2).TryCopyTo(default(Span<int>)));
@@ -416,7 +416,7 @@ public void Test_ArrayExtensions_2D_GetColumn_Empty()
416416
{
417417
int[,] array = new int[0, 0];
418418

419-
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => array.GetColumn(0).ToArray());
419+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => array.GetColumn(0).ToArray());
420420
}
421421

422422
#if NET6_0_OR_GREATER

tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_StreamExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void Test_StreamExtensions_ReadWrite()
2525

2626
Assert.AreEqual(stream.Position, 17);
2727

28-
_ = Assert.ThrowsException<ArgumentException>(() => stream.Write(long.MaxValue));
28+
_ = Assert.ThrowsExactly<ArgumentException>(() => stream.Write(long.MaxValue));
2929

3030
stream.Position = 0;
3131

@@ -34,7 +34,7 @@ public void Test_StreamExtensions_ReadWrite()
3434
Assert.AreEqual(3.14f, stream.Read<float>());
3535
Assert.AreEqual(unchecked(uint.MaxValue * 324823489204ul), stream.Read<ulong>());
3636

37-
_ = Assert.ThrowsException<EndOfStreamException>(() => stream.Read<long>());
37+
_ = Assert.ThrowsExactly<EndOfStreamException>(() => stream.Read<long>());
3838
}
3939

4040
// See https://github.com/CommunityToolkit/dotnet/issues/513
@@ -55,7 +55,7 @@ public void Test_StreamExtensions_ReadWrite_WithBufferedStream()
5555
Assert.AreEqual(3.14f, stream.Read<float>());
5656
Assert.AreEqual(unchecked(uint.MaxValue * 324823489204ul), stream.Read<ulong>());
5757

58-
_ = Assert.ThrowsException<EndOfStreamException>(() => stream.Read<long>());
58+
_ = Assert.ThrowsExactly<EndOfStreamException>(() => stream.Read<long>());
5959
}
6060

6161
private sealed class BufferedStream : MemoryStream

0 commit comments

Comments
 (0)