refactor(utils): improve TasStorage implementation and tests

- Change exception type from ArgumentOutOfRangeException to ArgumentException
- Fix node seeking logic by correcting candidate order
- Update Visit, Insert, and Remove methods with proper range checks
- Enhance cursor management after removal operations
- Add comprehensive test cases for edge scenarios
- Introduce AssertExtension for better exception testing
- Handle empty collection states more robustly
This commit is contained in:
2025-11-15 12:20:46 +08:00
parent 630365a6a6
commit df4a7252c1
4 changed files with 98 additions and 21 deletions

View File

@ -0,0 +1,28 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BallanceTasEditorTests {
public static class AssertExtension {
public static T ThrowsDerivedException<T>(Action action) where T : Exception {
try {
action();
} catch (T ex) {
return ex;
} catch (Exception ex) {
if (ex is T derivedEx)
return derivedEx;
throw new AssertFailedException(
$"Expected exception of type {typeof(T)} or derived type, but got {ex.GetType()}. " +
$"Message: {ex.Message}");
}
throw new AssertFailedException(
$"Expected exception of type {typeof(T)} or derived type, but no exception was thrown.");
}
}
}

View File

@ -49,6 +49,7 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="AssertExtension.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utils\TasStorageTests.cs" />
</ItemGroup>

View File

@ -10,6 +10,7 @@ namespace BallanceTasEditorTests.Utils {
[TestClass]
public class TasStorageTests {
private static readonly int[] BLANK = { };
private static readonly int[] PROBE = { 10, 20, 30, 40, 50 };
private static IEnumerable<object[]> TasStorageInstanceProvider {
@ -28,18 +29,18 @@ namespace BallanceTasEditorTests.Utils {
[DynamicData(nameof(TasStorageInstanceProvider))]
public void VisitTest(ITasStorage<int> storage) {
// 空时访问
Assert.ThrowsException<ArgumentOutOfRangeException>(() => storage.Visit(-1));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => storage.Visit(0));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => storage.Visit(1));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(-1));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(0));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(1));
// 设置数据
storage.Insert(0, PROBE);
// 访问数据
Assert.ThrowsException<ArgumentOutOfRangeException>(() => storage.Visit(-1));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(-1));
for (int i = 0; i < PROBE.Length; i++) {
Assert.AreEqual(storage.Visit(i), PROBE[i]);
}
Assert.ThrowsException<ArgumentOutOfRangeException>(() => storage.Visit(PROBE.Length));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(PROBE.Length));
}
/// <summary>
@ -52,8 +53,8 @@ namespace BallanceTasEditorTests.Utils {
// 和在非空时的头,中,尾分别插入的结果。
// 先检测空插入
Assert.ThrowsException<ArgumentOutOfRangeException>(() => storage.Insert(-1, PROBE));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => storage.Insert(1, PROBE));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Insert(-1, PROBE));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Insert(1, PROBE));
storage.Insert(0, PROBE);
for (int i = 0; i < PROBE.Length; i++) {
Assert.AreEqual(storage.Visit(i), PROBE[i]);
@ -78,6 +79,7 @@ namespace BallanceTasEditorTests.Utils {
Assert.AreEqual(storage.Visit(i), expected[i]);
}
}
}
/// <summary>
@ -86,7 +88,28 @@ namespace BallanceTasEditorTests.Utils {
[TestMethod]
[DynamicData(nameof(TasStorageInstanceProvider))]
public void RemoveTest(ITasStorage<int> storage) {
// 在空的时候删除0项
storage.Remove(0, 0);
// 插入项目后尝试在头中尾分别删除
var indices = new int[] { 0, PROBE.Length / 2, PROBE.Length - 1 };
foreach (var index in indices) {
// 清空,插入,删除
storage.Clear();
storage.Insert(0, PROBE);
storage.Remove(index, 1);
// 用List做正确模拟
var expected = new List<int>();
expected.AddRange(PROBE);
expected.RemoveRange(index, 1);
// 检查结果
Assert.AreEqual(storage.GetCount(), expected.Count);
for (int i = 0; i < expected.Count; i++) {
Assert.AreEqual(storage.Visit(i), expected[i]);
}
}
}
/// <summary>
@ -159,6 +182,13 @@ namespace BallanceTasEditorTests.Utils {
// 再次检查数据
Assert.IsTrue(storage.IsEmpty());
Assert.AreEqual(storage.GetCount(), 0);
// 清空后插入0项然后确认
storage.Clear();
storage.Insert(0, BLANK);
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(-1));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(0));
AssertExtension.ThrowsDerivedException<ArgumentException>(() => storage.Visit(1));
}
}
}