Skip to content

Commit 0f1dc91

Browse files
CopilotSoar360
andcommitted
Upgrade MSTest to v3.11.1 and refactor exception tests to use Assert.ThrowsException
Co-authored-by: Soar360 <15421284+Soar360@users.noreply.github.com>
1 parent e74ede8 commit 0f1dc91

File tree

16 files changed

+122
-158
lines changed

16 files changed

+122
-158
lines changed

tests/LuYao.Common.UnitTests/Collections/EnumerableExtensionsTests.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,23 @@ public void SplitToBatch_ListWithRemainder_ReturnsLastBatchWithFewerItems()
6363
}
6464

6565
[TestMethod]
66-
[ExpectedException(typeof(ArgumentNullException))]
6766
public void SplitToBatch_NullSource_ThrowsArgumentNullException()
6867
{
6968
List<int>? input = null;
70-
_ = input.SplitToBatch(2).ToList();
69+
Assert.ThrowsException<ArgumentNullException>(() => input.SplitToBatch(2).ToList());
7170
}
7271

7372
[TestMethod]
74-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
7573
public void SplitToBatch_BatchSizeZero_ThrowsArgumentOutOfRangeException()
7674
{
7775
var input = Enumerable.Range(1, 3);
78-
_ = input.SplitToBatch(0).ToList();
76+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => input.SplitToBatch(0).ToList());
7977
}
8078

8179
[TestMethod]
82-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
8380
public void SplitToBatch_BatchSizeNegative_ThrowsArgumentOutOfRangeException()
8481
{
8582
var input = Enumerable.Range(1, 3);
86-
_ = input.SplitToBatch(-1).ToList();
83+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => input.SplitToBatch(-1).ToList());
8784
}
8885
}

tests/LuYao.Common.UnitTests/Collections/Generic/KeyedListTests.cs

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,20 @@ public void Constructor_WithValidKeySelector_CreatesInstance()
6363
}
6464

6565
[TestMethod]
66-
[ExpectedException(typeof(ArgumentNullException))]
66+
67+
6768
public void Constructor_WithNullKeySelector_ThrowsArgumentNullException()
69+
70+
6871
{
69-
// Arrange & Act
70-
var keyedList = new KeyedList<int, TestItem>(null!);
7172

72-
// Assert: 期望抛出 ArgumentNullException
73+
74+
// Arrange, Act & Assert
75+
76+
77+
Assert.ThrowsException<ArgumentNullException>(() => new KeyedList<int, TestItem>(null!));
78+
79+
7380
}
7481

7582
#endregion
@@ -87,13 +94,20 @@ public void Indexer_GetWithValidIndex_ReturnsCorrectItem()
8794
}
8895

8996
[TestMethod]
90-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
97+
98+
9199
public void Indexer_GetWithInvalidIndex_ThrowsArgumentOutOfRangeException()
100+
101+
92102
{
93-
// Arrange & Act
94-
var item = _keyedList[-1];
95103

96-
// Assert: 期望抛出 ArgumentOutOfRangeException
104+
105+
// Arrange, Act & Assert
106+
107+
108+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => _keyedList[-1]);
109+
110+
97111
}
98112

99113
[TestMethod]
@@ -110,16 +124,27 @@ public void Indexer_SetWithValidIndex_UpdatesItem()
110124
}
111125

112126
[TestMethod]
113-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
127+
128+
114129
public void Indexer_SetWithInvalidIndex_ThrowsArgumentOutOfRangeException()
130+
131+
115132
{
133+
134+
116135
// Arrange
136+
137+
117138
var newItem = new TestItem { Id = 10, Name = "New Item" };
118139

119-
// Act
120-
_keyedList[10] = newItem;
121140

122-
// Assert: 期望抛出 ArgumentOutOfRangeException
141+
142+
// Act & Assert
143+
144+
145+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => _keyedList[10] = newItem);
146+
147+
123148
}
124149

125150
#endregion
@@ -375,39 +400,30 @@ public void CopyTo_ValidArrayWithOffset_CopiesItemsWithOffset()
375400
}
376401

377402
[TestMethod]
378-
[ExpectedException(typeof(ArgumentNullException))]
379403
public void CopyTo_NullArray_ThrowsArgumentNullException()
380404
{
381-
// Arrange & Act
382-
_keyedList.CopyTo(null!, 0);
383-
384-
// Assert: 期望抛出 ArgumentNullException
405+
// Arrange, Act & Assert
406+
Assert.ThrowsException<ArgumentNullException>(() => _keyedList.CopyTo(null!, 0));
385407
}
386408

387409
[TestMethod]
388-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
389410
public void CopyTo_NegativeArrayIndex_ThrowsArgumentOutOfRangeException()
390411
{
391412
// Arrange
392413
var array = new TestItem[_keyedList.Count];
393414

394-
// Act
395-
_keyedList.CopyTo(array, -1);
396-
397-
// Assert: 期望抛出 ArgumentOutOfRangeException
415+
// Act & Assert
416+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => _keyedList.CopyTo(array, -1));
398417
}
399418

400419
[TestMethod]
401-
[ExpectedException(typeof(ArgumentException))]
402420
public void CopyTo_InsufficientArraySpace_ThrowsArgumentException()
403421
{
404422
// Arrange
405423
var array = new TestItem[_keyedList.Count - 1];
406424

407-
// Act
408-
_keyedList.CopyTo(array, 0);
409-
410-
// Assert: 期望抛出 ArgumentException
425+
// Act & Assert
426+
Assert.ThrowsException<ArgumentException>(() => _keyedList.CopyTo(array, 0));
411427
}
412428

413429
#endregion
@@ -482,29 +498,23 @@ public void Insert_ValidIndex_InsertsItemAtCorrectPosition()
482498
}
483499

484500
[TestMethod]
485-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
486501
public void Insert_NegativeIndex_ThrowsArgumentOutOfRangeException()
487502
{
488503
// Arrange
489504
var newItem = new TestItem { Id = 4, Name = "Item 4" };
490505

491-
// Act
492-
_keyedList.Insert(-1, newItem);
493-
494-
// Assert: 期望抛出 ArgumentOutOfRangeException
506+
// Act & Assert
507+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => _keyedList.Insert(-1, newItem));
495508
}
496509

497510
[TestMethod]
498-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
499511
public void Insert_IndexGreaterThanCount_ThrowsArgumentOutOfRangeException()
500512
{
501513
// Arrange
502514
var newItem = new TestItem { Id = 4, Name = "Item 4" };
503515

504-
// Act
505-
_keyedList.Insert(_keyedList.Count + 1, newItem);
506-
507-
// Assert: 期望抛出 ArgumentOutOfRangeException
516+
// Act & Assert
517+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => _keyedList.Insert(_keyedList.Count + 1, newItem));
508518
}
509519

510520
#endregion
@@ -562,23 +572,17 @@ public void RemoveAt_ValidIndex_RemovesItemAtIndex()
562572
}
563573

564574
[TestMethod]
565-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
566575
public void RemoveAt_NegativeIndex_ThrowsArgumentOutOfRangeException()
567576
{
568-
// Act
569-
_keyedList.RemoveAt(-1);
570-
571-
// Assert: 期望抛出 ArgumentOutOfRangeException
577+
// Act & Assert
578+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => _keyedList.RemoveAt(-1));
572579
}
573580

574581
[TestMethod]
575-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
576582
public void RemoveAt_IndexEqualToCount_ThrowsArgumentOutOfRangeException()
577583
{
578-
// Act
579-
_keyedList.RemoveAt(_keyedList.Count);
580-
581-
// Assert: 期望抛出 ArgumentOutOfRangeException
584+
// Act & Assert
585+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => _keyedList.RemoveAt(_keyedList.Count));
582586
}
583587

584588
#endregion

tests/LuYao.Common.UnitTests/Data/RecordObjectTests.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,13 @@ public void Add_ValidObject_ShouldCreateColumnsAndAddRow()
8484
/// 测试 Add 方法传入 null 对象时抛出异常
8585
/// </summary>
8686
[TestMethod]
87-
[ExpectedException(typeof(ArgumentNullException))]
8887
public void Add_NullObject_ShouldThrowArgumentNullException()
8988
{
9089
// Arrange
9190
var record = new Record();
9291

93-
// Act
94-
record.Add<TestModel>(null!);
92+
// Act & Assert
93+
Assert.ThrowsException<ArgumentNullException>(() => record.Add<TestModel>(null!));
9594
}
9695

9796
/// <summary>
@@ -154,11 +153,10 @@ public void From_SingleObject_ShouldCreateRecordWithData()
154153
/// 测试 From 方法传入 null 对象时抛出异常
155154
/// </summary>
156155
[TestMethod]
157-
[ExpectedException(typeof(ArgumentNullException))]
158156
public void From_SingleObject_Null_ShouldThrowArgumentNullException()
159157
{
160-
// Act
161-
Record.From<TestModel>((TestModel)null!);
158+
// Act & Assert
159+
Assert.ThrowsException<ArgumentNullException>(() => Record.From<TestModel>((TestModel)null!));
162160
}
163161

164162
/// <summary>
@@ -297,11 +295,10 @@ public void From_CollectionWithNulls_ShouldSkipNullItems()
297295
/// 测试 From 方法传入 null 集合时抛出异常
298296
/// </summary>
299297
[TestMethod]
300-
[ExpectedException(typeof(ArgumentNullException))]
301298
public void From_NullCollection_ShouldThrowArgumentNullException()
302299
{
303-
// Act
304-
Record.FromList<TestModel>((TestModel[])null!);
300+
// Act & Assert
301+
Assert.ThrowsException<ArgumentNullException>(() => Record.FromList<TestModel>((TestModel[])null!));
305302
}
306303

307304
#endregion

tests/LuYao.Common.UnitTests/Data/RecordTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,27 +1007,25 @@ public void BoundaryCheck_EmptyTable_IndexGreaterThanZeroThrowsException()
10071007
}
10081008

10091009
[TestMethod]
1010-
[ExpectedException(typeof(DuplicateNameException))]
10111010
public void Columns_AddDuplicateName_ThrowsException()
10121011
{
10131012
// Arrange
10141013
var table = new Record();
10151014
table.Columns.Add<String>("TestColumn");
10161015

10171016
// Act & Assert
1018-
table.Columns.Add<String>("TestColumn"); // 应该抛出异常
1017+
Assert.ThrowsException<DuplicateNameException>(() => table.Columns.Add<String>("TestColumn")); // 应该抛出异常
10191018
}
10201019

10211020
[TestMethod]
1022-
[ExpectedException(typeof(DuplicateNameException))]
10231021
public void Columns_AddDuplicateNameDifferentType_ThrowsException()
10241022
{
10251023
// Arrange
10261024
var table = new Record();
10271025
table.Columns.Add<String>("TestColumn");
10281026

10291027
// Act & Assert
1030-
table.Columns.Add<Int32>("TestColumn"); // 应该抛出异常
1028+
Assert.ThrowsException<DuplicateNameException>(() => table.Columns.Add<Int32>("TestColumn")); // 应该抛出异常
10311029
}
10321030

10331031
public class Student

tests/LuYao.Common.UnitTests/Encoders/Base16Tests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,17 @@ public void FromBase16_EmptyString_ReturnsEmptyArray()
4242
}
4343

4444
[TestMethod]
45-
[ExpectedException(typeof(FormatException))]
4645
public void FromBase16_InvalidHex_ThrowsFormatException()
4746
{
4847
string input = "ZZ";
49-
Base16.FromBase16(input);
48+
Assert.ThrowsException<FormatException>(() => Base16.FromBase16(input));
5049
}
5150

5251
[TestMethod]
53-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
5452
public void FromBase16_OddLength_ThrowsArgumentOutOfRangeException()
5553
{
5654
string input = "ABC";
57-
Base16.FromBase16(input);
55+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => Base16.FromBase16(input));
5856
}
5957
}
6058
}

tests/LuYao.Common.UnitTests/Encoders/Base32Tests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ public void FromBase32_SampleString_ReturnsExpectedBytes()
4343
}
4444

4545
[TestMethod]
46-
[ExpectedException(typeof(ArgumentException))]
4746
public void FromBase32_InvalidCharacter_ThrowsArgumentException()
4847
{
49-
Base32.FromBase32("INVALID*");
48+
Assert.ThrowsException<ArgumentException>(() => Base32.FromBase32("INVALID*"));
5049
}
5150

5251
[TestMethod]

tests/LuYao.Common.UnitTests/Encoders/Base64Tests.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class Base64Tests
1010
public void ToBase64_StandardEncoding_ReturnsCorrectBase64()
1111
{
1212
// Arrange
13-
var data = Encoding.UTF8.GetBytes("Hello, ÊÀ½ç!");
13+
var data = Encoding.UTF8.GetBytes("Hello, ����!");
1414
var expected = Convert.ToBase64String(data);
1515

1616
// Act
@@ -38,7 +38,7 @@ public void ToBase64_TrimPadding_ReturnsTrimmedBase64()
3838
public void FromBase64_StandardBase64_ReturnsOriginalBytes()
3939
{
4040
// Arrange
41-
var original = Encoding.UTF8.GetBytes("Hello, ÊÀ½ç!");
41+
var original = Encoding.UTF8.GetBytes("Hello, ����!");
4242
var base64 = Convert.ToBase64String(original);
4343

4444
// Act
@@ -53,7 +53,7 @@ public void FromBase64_TrimmedBase64_ReturnsOriginalBytes()
5353
{
5454
// Arrange
5555
var original = Encoding.UTF8.GetBytes("test");
56-
var trimmedBase64 = "dGVzdA"; // È¥µôÁË"=="
56+
var trimmedBase64 = "dGVzdA"; // ȥ����"=="
5757

5858
// Act
5959
var result = Base64.FromBase64(trimmedBase64);
@@ -73,11 +73,10 @@ public void FromBase64_EmptyString_ReturnsEmptyArray()
7373
}
7474

7575
[TestMethod]
76-
[ExpectedException(typeof(FormatException))]
7776
public void FromBase64_InvalidString_ThrowsFormatException()
7877
{
79-
// Act
80-
Base64.FromBase64("!@#$");
78+
// Act & Assert
79+
Assert.ThrowsException<FormatException>(() => Base64.FromBase64("!@#$"));
8180
}
8281
}
8382
}

tests/LuYao.Common.UnitTests/EnumTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@ public void Parse_ValidString_ReturnsEnumValue()
8080
}
8181

8282
[TestMethod]
83-
[ExpectedException(typeof(ArgumentException))]
8483
public void Parse_InvalidString_ThrowsException()
8584
{
86-
Enum<TestEnum>.Parse("NonExistent");
85+
Assert.ThrowsException<ArgumentException>(() => Enum<TestEnum>.Parse("NonExistent"));
8786
}
8887

8988
[TestMethod]

0 commit comments

Comments
 (0)