Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace LuYao.Collections.Concurrent;
namespace LuYao.Collections.Concurrent;

[TestClass]
public class AsyncQueueTests
Expand Down Expand Up @@ -53,7 +53,7 @@ public async Task DequeueAsync_NoItem_CancellationTokenTriggersException()
cts.Cancel();

// Act & Assert
await Assert.ThrowsExceptionAsync<TaskCanceledException>(async () =>
await Assert.ThrowsAsync<TaskCanceledException>(async () =>
{
await queue.DequeueAsync(cts.Token);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,23 @@ public void SplitToBatch_ListWithRemainder_ReturnsLastBatchWithFewerItems()
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void SplitToBatch_NullSource_ThrowsArgumentNullException()
{
List<int>? input = null;
_ = input.SplitToBatch(2).ToList();
Assert.Throws<ArgumentNullException>(() => _ = input.SplitToBatch(2).ToList());
}

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void SplitToBatch_BatchSizeZero_ThrowsArgumentOutOfRangeException()
{
var input = Enumerable.Range(1, 3);
_ = input.SplitToBatch(0).ToList();
Assert.Throws<ArgumentOutOfRangeException>(() => _ = input.SplitToBatch(0).ToList());
}

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void SplitToBatch_BatchSizeNegative_ThrowsArgumentOutOfRangeException()
{
var input = Enumerable.Range(1, 3);
_ = input.SplitToBatch(-1).ToList();
Assert.Throws<ArgumentOutOfRangeException>(() => _ = input.SplitToBatch(-1).ToList());
}
}
70 changes: 20 additions & 50 deletions tests/LuYao.Common.UnitTests/Collections/Generic/KeyedListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,10 @@ public void Constructor_WithValidKeySelector_CreatesInstance()
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Constructor_WithNullKeySelector_ThrowsArgumentNullException()
{
// Arrange & Act
var keyedList = new KeyedList<int, TestItem>(null!);

// Assert: 期望抛出 ArgumentNullException
// Arrange & Act & Assert
Assert.Throws<ArgumentNullException>(() => new KeyedList<int, TestItem>(null!));
}

#endregion
Expand All @@ -87,13 +84,10 @@ public void Indexer_GetWithValidIndex_ReturnsCorrectItem()
}

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Indexer_GetWithInvalidIndex_ThrowsArgumentOutOfRangeException()
{
// Arrange & Act
var item = _keyedList[-1];

// Assert: 期望抛出 ArgumentOutOfRangeException
// Arrange & Act & Assert
Assert.Throws<ArgumentOutOfRangeException>(() => { var item = _keyedList[-1]; });
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assignment to item is useless, since its value is never read.

Suggested change
Assert.Throws<ArgumentOutOfRangeException>(() => { var item = _keyedList[-1]; });
Assert.Throws<ArgumentOutOfRangeException>(() => _keyedList[-1]);

Copilot uses AI. Check for mistakes.
}

[TestMethod]
Expand All @@ -110,16 +104,13 @@ public void Indexer_SetWithValidIndex_UpdatesItem()
}

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Indexer_SetWithInvalidIndex_ThrowsArgumentOutOfRangeException()
{
// Arrange
var newItem = new TestItem { Id = 10, Name = "New Item" };

// Act
_keyedList[10] = newItem;

// Assert: 期望抛出 ArgumentOutOfRangeException
// Act & Assert
Assert.Throws<ArgumentOutOfRangeException>(() => _keyedList[10] = newItem);
}

#endregion
Expand Down Expand Up @@ -375,39 +366,30 @@ public void CopyTo_ValidArrayWithOffset_CopiesItemsWithOffset()
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void CopyTo_NullArray_ThrowsArgumentNullException()
{
// Arrange & Act
_keyedList.CopyTo(null!, 0);

// Assert: 期望抛出 ArgumentNullException
// Arrange & Act & Assert
Assert.Throws<ArgumentNullException>(() => _keyedList.CopyTo(null!, 0));
}

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void CopyTo_NegativeArrayIndex_ThrowsArgumentOutOfRangeException()
{
// Arrange
var array = new TestItem[_keyedList.Count];

// Act
_keyedList.CopyTo(array, -1);

// Assert: 期望抛出 ArgumentOutOfRangeException
// Act & Assert
Assert.Throws<ArgumentOutOfRangeException>(() => _keyedList.CopyTo(array, -1));
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void CopyTo_InsufficientArraySpace_ThrowsArgumentException()
{
// Arrange
var array = new TestItem[_keyedList.Count - 1];

// Act
_keyedList.CopyTo(array, 0);

// Assert: 期望抛出 ArgumentException
// Act & Assert
Assert.Throws<ArgumentException>(() => _keyedList.CopyTo(array, 0));
}

#endregion
Expand Down Expand Up @@ -482,29 +464,23 @@ public void Insert_ValidIndex_InsertsItemAtCorrectPosition()
}

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Insert_NegativeIndex_ThrowsArgumentOutOfRangeException()
{
// Arrange
var newItem = new TestItem { Id = 4, Name = "Item 4" };

// Act
_keyedList.Insert(-1, newItem);

// Assert: 期望抛出 ArgumentOutOfRangeException
// Act & Assert
Assert.Throws<ArgumentOutOfRangeException>(() => _keyedList.Insert(-1, newItem));
}

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Insert_IndexGreaterThanCount_ThrowsArgumentOutOfRangeException()
{
// Arrange
var newItem = new TestItem { Id = 4, Name = "Item 4" };

// Act
_keyedList.Insert(_keyedList.Count + 1, newItem);

// Assert: 期望抛出 ArgumentOutOfRangeException
// Act & Assert
Assert.Throws<ArgumentOutOfRangeException>(() => _keyedList.Insert(_keyedList.Count + 1, newItem));
}

#endregion
Expand Down Expand Up @@ -562,23 +538,17 @@ public void RemoveAt_ValidIndex_RemovesItemAtIndex()
}

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void RemoveAt_NegativeIndex_ThrowsArgumentOutOfRangeException()
{
// Act
_keyedList.RemoveAt(-1);

// Assert: 期望抛出 ArgumentOutOfRangeException
// Act & Assert
Assert.Throws<ArgumentOutOfRangeException>(() => _keyedList.RemoveAt(-1));
}

[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void RemoveAt_IndexEqualToCount_ThrowsArgumentOutOfRangeException()
{
// Act
_keyedList.RemoveAt(_keyedList.Count);

// Assert: 期望抛出 ArgumentOutOfRangeException
// Act & Assert
Assert.Throws<ArgumentOutOfRangeException>(() => _keyedList.RemoveAt(_keyedList.Count));
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -16,7 +16,7 @@ public void Add_NullItem_ThrowsArgumentNullException()
var collection = new WeakCollection<object>();

// Act & Assert
Assert.ThrowsException<ArgumentNullException>(() => collection.Add(null));
Assert.Throws<ArgumentNullException>(() => collection.Add(null));
}

[TestMethod]
Expand Down
Loading
Loading