diff --git a/GFramework.Core.Tests/coroutine/DelayTests.cs b/GFramework.Core.Tests/coroutine/DelayTests.cs index e3ff5dd..ab55229 100644 --- a/GFramework.Core.Tests/coroutine/DelayTests.cs +++ b/GFramework.Core.Tests/coroutine/DelayTests.cs @@ -3,9 +3,15 @@ using NUnit.Framework; namespace GFramework.Core.Tests.coroutine { + /// + /// Delay类的单元测试,用于验证延迟指令的功能 + /// [TestFixture] public class DelayTests { + /// + /// 测试构造函数设置初始剩余时间 + /// [Test] public void Constructor_SetsInitialRemainingTime() { @@ -16,6 +22,9 @@ namespace GFramework.Core.Tests.coroutine Assert.That(delay.IsDone, Is.False); } + /// + /// 测试Update方法减少剩余时间 + /// [Test] public void Update_ReducesRemainingTime() { @@ -29,6 +38,9 @@ namespace GFramework.Core.Tests.coroutine Assert.That(delay.IsDone, Is.False); } + /// + /// 测试多次Update后最终完成 + /// [Test] public void Update_MultipleTimes_EventuallyCompletes() { @@ -43,6 +55,9 @@ namespace GFramework.Core.Tests.coroutine Assert.That(delay.IsDone, Is.True); } + /// + /// 测试负数时间被视为零 + /// [Test] public void NegativeTime_TreatedAsZero() { @@ -53,6 +68,9 @@ namespace GFramework.Core.Tests.coroutine Assert.That(delay.IsDone, Is.True); } + /// + /// 测试零时间立即完成 + /// [Test] public void ZeroTime_CompletesImmediately() { diff --git a/GFramework.Core.Tests/extensions/AsyncExtensionsTests.cs b/GFramework.Core.Tests/extensions/AsyncExtensionsTests.cs index 5a7ef23..1d91184 100644 --- a/GFramework.Core.Tests/extensions/AsyncExtensionsTests.cs +++ b/GFramework.Core.Tests/extensions/AsyncExtensionsTests.cs @@ -11,6 +11,9 @@ namespace GFramework.Core.Tests.extensions; [TestFixture] public class AsyncExtensionsTests { + /// + /// 测试WithTimeout方法在任务超时前完成时返回结果 + /// [Test] public async Task WithTimeout_Should_Return_Result_When_Task_Completes_Before_Timeout() { @@ -23,6 +26,9 @@ public class AsyncExtensionsTests Assert.That(result, Is.EqualTo(42)); } + /// + /// 测试WithTimeout方法在任务超过超时时抛出TimeoutException + /// [Test] public void WithTimeout_Should_Throw_TimeoutException_When_Task_Exceeds_Timeout() { @@ -37,6 +43,9 @@ public class AsyncExtensionsTests TimeSpan.FromMilliseconds(100))); } + /// + /// 测试WithTimeout方法在取消请求时抛出OperationCanceledException + /// [Test] public void WithTimeout_Should_Throw_OperationCanceledException_When_Cancellation_Requested() { @@ -55,6 +64,9 @@ public class AsyncExtensionsTests cts.Token)); } + /// + /// 测试WithTimeout方法在超时时取消内部任务 + /// [Test] public void WithTimeout_Should_Cancel_Inner_Task_When_Timeout_Elapses() { @@ -82,6 +94,9 @@ public class AsyncExtensionsTests Assert.That(innerTaskCanceled, Is.True, "内部任务应在超时时收到取消信号"); } + /// + /// 测试无返回值的WithTimeout在任务超时前完成 + /// [Test] public async Task WithTimeout_NoResult_Should_Complete_When_Task_Completes_Before_Timeout() { @@ -99,6 +114,9 @@ public class AsyncExtensionsTests Assert.Pass("Task completed successfully within timeout period"); } + /// + /// 测试无返回值的WithTimeout在任务超时时抛出TimeoutException + /// [Test] public void WithTimeout_NoResult_Should_Throw_TimeoutException_When_Task_Exceeds_Timeout() { @@ -109,6 +127,9 @@ public class AsyncExtensionsTests TimeSpan.FromMilliseconds(100))); } + /// + /// 测试无返回值的WithTimeout在超时时取消内部任务 + /// [Test] public void WithTimeout_NoResult_Should_Cancel_Inner_Task_When_Timeout_Elapses() { @@ -135,6 +156,9 @@ public class AsyncExtensionsTests Assert.That(innerTaskCanceled, Is.True, "内部任务应在超时时收到取消信号"); } + /// + /// 测试WithRetry方法在任务成功时返回结果 + /// [Test] public async Task WithRetry_Should_Return_Result_When_Task_Succeeds() { @@ -154,6 +178,9 @@ public class AsyncExtensionsTests Assert.That(attemptCount, Is.EqualTo(1)); } + /// + /// 测试WithRetry方法在失败时重试 + /// [Test] public async Task WithRetry_Should_Retry_On_Failure() { @@ -175,6 +202,9 @@ public class AsyncExtensionsTests Assert.That(attemptCount, Is.EqualTo(3)); } + /// + /// 测试WithRetry方法在所有重试都失败时抛出AggregateException + /// [Test] public void WithRetry_Should_Throw_AggregateException_When_All_Retries_Fail() { @@ -191,6 +221,9 @@ public class AsyncExtensionsTests await taskFactory.WithRetry(2, TimeSpan.FromMilliseconds(10))); } + /// + /// 测试WithRetry方法遵守ShouldRetry谓词 + /// [Test] public async Task WithRetry_Should_Respect_ShouldRetry_Predicate() { @@ -211,6 +244,9 @@ public class AsyncExtensionsTests Assert.That(attemptCount, Is.EqualTo(1)); // 不应该重试 } + /// + /// 测试TryAsync方法在任务成功时返回成功结果 + /// [Test] public async Task TryAsync_Should_Return_Success_When_Task_Succeeds() { @@ -225,6 +261,9 @@ public class AsyncExtensionsTests Assert.That(result.IfFail(0), Is.EqualTo(42)); } + /// + /// 测试TryAsync方法在任务抛出异常时返回失败结果 + /// [Test] public async Task TryAsync_Should_Return_Failure_When_Task_Throws() { @@ -238,6 +277,9 @@ public class AsyncExtensionsTests Assert.That(result.IsFaulted, Is.True); } + /// + /// 测试WithFallback方法在任务成功时返回结果 + /// [Test] public async Task WithFallback_Should_Return_Result_When_Task_Succeeds() { @@ -251,6 +293,9 @@ public class AsyncExtensionsTests Assert.That(result, Is.EqualTo(42)); } + /// + /// 测试WithFallback方法在任务失败时返回备用值 + /// [Test] public async Task WithFallback_Should_Return_Fallback_Value_When_Task_Fails() { @@ -264,6 +309,9 @@ public class AsyncExtensionsTests Assert.That(result, Is.EqualTo(-1)); } + /// + /// 测试WithFallback方法将异常传递给备用函数 + /// [Test] public async Task WithFallback_Should_Pass_Exception_To_Fallback() { diff --git a/GFramework.Core.Tests/extensions/CollectionExtensionsTests.cs b/GFramework.Core.Tests/extensions/CollectionExtensionsTests.cs index 683b88b..9822aed 100644 --- a/GFramework.Core.Tests/extensions/CollectionExtensionsTests.cs +++ b/GFramework.Core.Tests/extensions/CollectionExtensionsTests.cs @@ -9,6 +9,9 @@ namespace GFramework.Core.Tests.extensions; [TestFixture] public class CollectionExtensionsTests { + /// + /// 测试ForEach方法对每个元素执行指定操作 + /// [Test] public void ForEach_Should_Execute_Action_For_Each_Element() { @@ -23,6 +26,9 @@ public class CollectionExtensionsTests Assert.That(sum, Is.EqualTo(15)); } + /// + /// 测试ForEach方法在源为null时抛出ArgumentNullException + /// [Test] public void ForEach_Should_Throw_ArgumentNullException_When_Source_Is_Null() { @@ -33,6 +39,9 @@ public class CollectionExtensionsTests Assert.Throws(() => numbers!.ForEach(n => { })); } + /// + /// 测试ForEach方法在操作为null时抛出ArgumentNullException + /// [Test] public void ForEach_Should_Throw_ArgumentNullException_When_Action_Is_Null() { @@ -43,6 +52,9 @@ public class CollectionExtensionsTests Assert.Throws(() => numbers.ForEach(null!)); } + /// + /// 测试IsNullOrEmpty方法在源为null时返回true + /// [Test] public void IsNullOrEmpty_Should_Return_True_When_Source_Is_Null() { @@ -56,6 +68,9 @@ public class CollectionExtensionsTests Assert.That(result, Is.True); } + /// + /// 测试IsNullOrEmpty方法在源为空时返回true + /// [Test] public void IsNullOrEmpty_Should_Return_True_When_Source_Is_Empty() { @@ -69,6 +84,9 @@ public class CollectionExtensionsTests Assert.That(result, Is.True); } + /// + /// 测试IsNullOrEmpty方法在源有元素时返回false + /// [Test] public void IsNullOrEmpty_Should_Return_False_When_Source_Has_Elements() { @@ -82,6 +100,9 @@ public class CollectionExtensionsTests Assert.That(result, Is.False); } + /// + /// 测试WhereNotNull方法过滤掉null元素 + /// [Test] public void WhereNotNull_Should_Filter_Out_Null_Elements() { @@ -96,6 +117,9 @@ public class CollectionExtensionsTests Assert.That(result, Is.EqualTo(new[] { "a", "b", "c" })); } + /// + /// 测试WhereNotNull方法在所有元素都为null时返回空集合 + /// [Test] public void WhereNotNull_Should_Return_Empty_Collection_When_All_Elements_Are_Null() { @@ -109,6 +133,9 @@ public class CollectionExtensionsTests Assert.That(result, Is.Empty); } + /// + /// 测试WhereNotNull方法在源为null时抛出ArgumentNullException + /// [Test] public void WhereNotNull_Should_Throw_ArgumentNullException_When_Source_Is_Null() { @@ -119,6 +146,9 @@ public class CollectionExtensionsTests Assert.Throws(() => items!.WhereNotNull().ToArray()); } + /// + /// 测试ToDictionarySafe方法创建字典 + /// [Test] public void ToDictionarySafe_Should_Create_Dictionary() { @@ -135,6 +165,9 @@ public class CollectionExtensionsTests Assert.That(result["c"], Is.EqualTo(3)); } + /// + /// 测试ToDictionarySafe方法在存在重复键时覆盖前面的值 + /// [Test] public void ToDictionarySafe_Should_Overwrite_Duplicate_Keys() { @@ -150,6 +183,9 @@ public class CollectionExtensionsTests Assert.That(result["b"], Is.EqualTo(2)); } + /// + /// 测试ToDictionarySafe方法在源为null时抛出ArgumentNullException + /// [Test] public void ToDictionarySafe_Should_Throw_ArgumentNullException_When_Source_Is_Null() { @@ -161,6 +197,9 @@ public class CollectionExtensionsTests items!.ToDictionarySafe(x => x.Item1, x => x.Item2)); } + /// + /// 测试ToDictionarySafe方法在键选择器为null时抛出ArgumentNullException + /// [Test] public void ToDictionarySafe_Should_Throw_ArgumentNullException_When_KeySelector_Is_Null() { @@ -172,6 +211,9 @@ public class CollectionExtensionsTests items.ToDictionarySafe<(string, int), string, int>(null!, x => x.Item2)); } + /// + /// 测试ToDictionarySafe方法在值选择器为null时抛出ArgumentNullException + /// [Test] public void ToDictionarySafe_Should_Throw_ArgumentNullException_When_ValueSelector_Is_Null() { @@ -182,4 +224,4 @@ public class CollectionExtensionsTests Assert.Throws(() => items.ToDictionarySafe<(string, int), string, int>(x => x.Item1, null!)); } -} +} \ No newline at end of file diff --git a/GFramework.Core.Tests/extensions/GuardExtensionsTests.cs b/GFramework.Core.Tests/extensions/GuardExtensionsTests.cs index 30aa325..01ed483 100644 --- a/GFramework.Core.Tests/extensions/GuardExtensionsTests.cs +++ b/GFramework.Core.Tests/extensions/GuardExtensionsTests.cs @@ -9,6 +9,11 @@ namespace GFramework.Core.Tests.extensions; [TestFixture] public class GuardExtensionsTests { + const string TestParamName = "testParam"; + + /// + /// 测试ThrowIfNull方法在值不为null时返回值本身 + /// [Test] public void ThrowIfNull_Should_Return_Value_When_Value_Is_Not_Null() { @@ -22,6 +27,9 @@ public class GuardExtensionsTests Assert.That(result, Is.EqualTo("test")); } + /// + /// 测试ThrowIfNull方法在值为null时抛出ArgumentNullException + /// [Test] public void ThrowIfNull_Should_Throw_ArgumentNullException_When_Value_Is_Null() { @@ -32,6 +40,9 @@ public class GuardExtensionsTests Assert.Throws(() => value.ThrowIfNull()); } + /// + /// 测试ThrowIfNull方法在抛出异常时包含参数名称 + /// [Test] public void ThrowIfNull_Should_Include_ParamName_In_Exception() { @@ -39,10 +50,13 @@ public class GuardExtensionsTests string? value = null; // Act & Assert - var ex = Assert.Throws(() => value.ThrowIfNull("testParam")); - Assert.That(ex.ParamName, Is.EqualTo("testParam")); + var ex = Assert.Throws(() => value.ThrowIfNull(TestParamName)); + Assert.That(ex?.ParamName, Is.EqualTo(TestParamName)); } + /// + /// 测试ThrowIfNullOrEmpty方法在值不为空时返回值本身 + /// [Test] public void ThrowIfNullOrEmpty_Should_Return_Value_When_Value_Is_Not_Empty() { @@ -56,6 +70,9 @@ public class GuardExtensionsTests Assert.That(result, Is.EqualTo("test")); } + /// + /// 测试ThrowIfNullOrEmpty方法在值为null时抛出ArgumentNullException + /// [Test] public void ThrowIfNullOrEmpty_Should_Throw_ArgumentNullException_When_Value_Is_Null() { @@ -66,6 +83,9 @@ public class GuardExtensionsTests Assert.Throws(() => value.ThrowIfNullOrEmpty()); } + /// + /// 测试ThrowIfNullOrEmpty方法在值为空时抛出ArgumentException + /// [Test] public void ThrowIfNullOrEmpty_Should_Throw_ArgumentException_When_Value_Is_Empty() { @@ -76,6 +96,9 @@ public class GuardExtensionsTests Assert.Throws(() => value.ThrowIfNullOrEmpty()); } + /// + /// 测试ThrowIfNullOrEmpty方法在抛出异常时包含参数名称 + /// [Test] public void ThrowIfNullOrEmpty_Should_Include_ParamName_In_Exception() { @@ -83,10 +106,13 @@ public class GuardExtensionsTests var value = string.Empty; // Act & Assert - var ex = Assert.Throws(() => value.ThrowIfNullOrEmpty("testParam")); - Assert.That(ex.ParamName, Is.EqualTo("testParam")); + var ex = Assert.Throws(() => value.ThrowIfNullOrEmpty(TestParamName)); + Assert.That(ex?.ParamName, Is.EqualTo(TestParamName)); } + /// + /// 测试ThrowIfEmpty方法在集合有元素时返回集合本身 + /// [Test] public void ThrowIfEmpty_Should_Return_Collection_When_Collection_Has_Elements() { @@ -100,6 +126,9 @@ public class GuardExtensionsTests Assert.That(result, Is.EqualTo(collection)); } + /// + /// 测试ThrowIfEmpty方法在集合为null时抛出ArgumentNullException + /// [Test] public void ThrowIfEmpty_Should_Throw_ArgumentNullException_When_Collection_Is_Null() { @@ -110,6 +139,9 @@ public class GuardExtensionsTests Assert.Throws(() => collection.ThrowIfEmpty()); } + /// + /// 测试ThrowIfEmpty方法在集合为空时抛出ArgumentException + /// [Test] public void ThrowIfEmpty_Should_Throw_ArgumentException_When_Collection_Is_Empty() { @@ -120,6 +152,9 @@ public class GuardExtensionsTests Assert.Throws(() => collection.ThrowIfEmpty()); } + /// + /// 测试ThrowIfEmpty方法在抛出异常时包含参数名称 + /// [Test] public void ThrowIfEmpty_Should_Include_ParamName_In_Exception() { @@ -127,7 +162,7 @@ public class GuardExtensionsTests var collection = Array.Empty(); // Act & Assert - var ex = Assert.Throws(() => collection.ThrowIfEmpty("testParam")); - Assert.That(ex.ParamName, Is.EqualTo("testParam")); + var ex = Assert.Throws(() => collection.ThrowIfEmpty(TestParamName)); + Assert.That(ex?.ParamName, Is.EqualTo(TestParamName)); } -} +} \ No newline at end of file diff --git a/GFramework.Core.Tests/extensions/NumericExtensionsTests.cs b/GFramework.Core.Tests/extensions/NumericExtensionsTests.cs index 32465d6..341db7a 100644 --- a/GFramework.Core.Tests/extensions/NumericExtensionsTests.cs +++ b/GFramework.Core.Tests/extensions/NumericExtensionsTests.cs @@ -9,6 +9,9 @@ namespace GFramework.Core.Tests.extensions; [TestFixture] public class NumericExtensionsTests { + /// + /// 测试Between方法在值在范围内时返回true + /// [Test] public void Between_Should_Return_True_When_Value_Is_Within_Range() { @@ -22,6 +25,9 @@ public class NumericExtensionsTests Assert.That(result, Is.True); } + /// + /// 测试Between方法在值等于最小值时返回true + /// [Test] public void Between_Should_Return_True_When_Value_Equals_Min() { @@ -35,6 +41,9 @@ public class NumericExtensionsTests Assert.That(result, Is.True); } + /// + /// 测试Between方法在值等于最大值时返回true + /// [Test] public void Between_Should_Return_True_When_Value_Equals_Max() { @@ -48,6 +57,9 @@ public class NumericExtensionsTests Assert.That(result, Is.True); } + /// + /// 测试Between方法在值小于最小值时返回false + /// [Test] public void Between_Should_Return_False_When_Value_Is_Less_Than_Min() { @@ -61,6 +73,9 @@ public class NumericExtensionsTests Assert.That(result, Is.False); } + /// + /// 测试Between方法在值大于最大值时返回false + /// [Test] public void Between_Should_Return_False_When_Value_Is_Greater_Than_Max() { @@ -74,6 +89,9 @@ public class NumericExtensionsTests Assert.That(result, Is.False); } + /// + /// 测试Between方法在边界不包含时返回false + /// [Test] public void Between_Should_Return_False_When_Value_Equals_Boundary_And_Not_Inclusive() { @@ -87,6 +105,9 @@ public class NumericExtensionsTests Assert.That(result, Is.False); } + /// + /// 测试Between方法在最小值大于最大值时抛出ArgumentException + /// [Test] public void Between_Should_Throw_ArgumentException_When_Min_Is_Greater_Than_Max() { @@ -97,6 +118,9 @@ public class NumericExtensionsTests Assert.Throws(() => value.Between(100, 0)); } + /// + /// 测试Lerp方法在t为0时返回起始值 + /// [Test] public void Lerp_Should_Return_From_When_T_Is_Zero() { @@ -107,6 +131,9 @@ public class NumericExtensionsTests Assert.That(result, Is.EqualTo(0f)); } + /// + /// 测试Lerp方法在t为1时返回结束值 + /// [Test] public void Lerp_Should_Return_To_When_T_Is_One() { @@ -117,6 +144,9 @@ public class NumericExtensionsTests Assert.That(result, Is.EqualTo(100f)); } + /// + /// 测试Lerp方法在t为0.5时返回中间值 + /// [Test] public void Lerp_Should_Return_Midpoint_When_T_Is_Half() { @@ -127,6 +157,9 @@ public class NumericExtensionsTests Assert.That(result, Is.EqualTo(50f)); } + /// + /// 测试InverseLerp方法在值等于起始值时返回0 + /// [Test] public void InverseLerp_Should_Return_Zero_When_Value_Equals_From() { @@ -137,6 +170,9 @@ public class NumericExtensionsTests Assert.That(result, Is.EqualTo(0f)); } + /// + /// 测试InverseLerp方法在值等于结束值时返回1 + /// [Test] public void InverseLerp_Should_Return_One_When_Value_Equals_To() { @@ -147,6 +183,9 @@ public class NumericExtensionsTests Assert.That(result, Is.EqualTo(1f)); } + /// + /// 测试InverseLerp方法在值为中间值时返回0.5 + /// [Test] public void InverseLerp_Should_Return_Half_When_Value_Is_Midpoint() { @@ -157,6 +196,9 @@ public class NumericExtensionsTests Assert.That(result, Is.EqualTo(0.5f)); } + /// + /// 测试InverseLerp方法在起始值等于结束值时抛出DivideByZeroException + /// [Test] public void InverseLerp_Should_Throw_DivideByZeroException_When_From_Equals_To() { diff --git a/GFramework.Core.Tests/extensions/ResultExtensionsTests.cs b/GFramework.Core.Tests/extensions/ResultExtensionsTests.cs index 22e2195..c639fb1 100644 --- a/GFramework.Core.Tests/extensions/ResultExtensionsTests.cs +++ b/GFramework.Core.Tests/extensions/ResultExtensionsTests.cs @@ -23,6 +23,9 @@ namespace GFramework.Core.Tests.Extensions; [TestFixture] public class ResultExtensionsTests { + /// + /// 测试Combine方法在所有结果成功时返回成功列表 + /// [Test] public void Combine_Should_Return_Success_List_When_All_Results_Succeed() { @@ -54,6 +57,9 @@ public class ResultExtensionsTests Assert.That(combined.Exception, Is.SameAs(exception)); } + /// + /// 测试Combine方法处理空集合 + /// [Test] public void Combine_Should_Handle_Empty_Collection() { @@ -64,12 +70,18 @@ public class ResultExtensionsTests Assert.That(list, Is.Empty); } + /// + /// 测试Combine方法在集合为null时抛出ArgumentNullException + /// [Test] public void Combine_Should_Throw_ArgumentNullException_When_Collection_Is_Null() { Assert.Throws(() => ((IEnumerable>)null!).Combine()); } + /// + /// 测试Combine方法保留值的顺序 + /// [Test] public void Combine_Should_Preserve_Order_Of_Values() { @@ -84,6 +96,9 @@ public class ResultExtensionsTests Assert.That(list, Is.EqualTo(new[] { 3, 1, 2 })); } + /// + /// 测试Combine方法在第一个失败时短路 + /// [Test] public void Combine_Should_Short_Circuit_On_First_Failure() { @@ -102,6 +117,9 @@ public class ResultExtensionsTests Assert.That(combined.IsFaulted, Is.True); } + /// + /// 测试Map方法转换成功值 + /// [Test] public void Map_Should_Transform_Success_Value() { @@ -111,6 +129,9 @@ public class ResultExtensionsTests Assert.That(mapped.Match(succ: v => v, fail: _ => ""), Is.EqualTo("42")); } + /// + /// 测试Map方法传播失败 + /// [Test] public void Map_Should_Propagate_Failure() { @@ -121,6 +142,9 @@ public class ResultExtensionsTests Assert.That(mapped.Exception, Is.SameAs(exception)); } + /// + /// 测试Map方法在映射器为null时抛出ArgumentNullException + /// [Test] public void Map_Should_Throw_ArgumentNullException_When_Mapper_Is_Null() { @@ -128,6 +152,9 @@ public class ResultExtensionsTests Assert.Throws(() => result.Map(null!)); } + /// + /// 测试Bind方法链接成功结果 + /// [Test] public void Bind_Should_Chain_Success_Results() { @@ -137,6 +164,9 @@ public class ResultExtensionsTests Assert.That(bound.Match(succ: v => v, fail: _ => ""), Is.EqualTo("42")); } + /// + /// 测试Bind方法传播失败 + /// [Test] public void Bind_Should_Propagate_Failure() { @@ -147,6 +177,9 @@ public class ResultExtensionsTests Assert.That(bound.Exception, Is.SameAs(exception)); } + /// + /// 测试Bind方法在绑定器为null时抛出ArgumentNullException + /// [Test] public void Bind_Should_Throw_ArgumentNullException_When_Binder_Is_Null() { @@ -154,6 +187,9 @@ public class ResultExtensionsTests Assert.Throws(() => result.Bind(null!)); } + /// + /// 测试OnSuccess方法在成功时执行操作 + /// [Test] public void OnSuccess_Should_Execute_Action_When_Success() { @@ -163,6 +199,9 @@ public class ResultExtensionsTests Assert.That(executed, Is.True); } + /// + /// 测试OnSuccess方法在失败时不执行操作 + /// [Test] public void OnSuccess_Should_Not_Execute_Action_When_Faulted() { @@ -172,6 +211,9 @@ public class ResultExtensionsTests Assert.That(executed, Is.False); } + /// + /// 测试OnSuccess方法返回原始结果用于链式调用 + /// [Test] public void OnSuccess_Should_Return_Original_Result_For_Chaining() { @@ -180,6 +222,9 @@ public class ResultExtensionsTests Assert.That(returned, Is.EqualTo(result)); } + /// + /// 测试OnSuccess方法在操作为null时抛出ArgumentNullException + /// [Test] public void OnSuccess_Should_Throw_ArgumentNullException_When_Action_Is_Null() { @@ -187,6 +232,9 @@ public class ResultExtensionsTests Assert.Throws(() => result.OnSuccess(null!)); } + /// + /// 测试OnFailure方法在失败时执行操作 + /// [Test] public void OnFailure_Should_Execute_Action_When_Faulted() { @@ -196,6 +244,9 @@ public class ResultExtensionsTests Assert.That(executed, Is.True); } + /// + /// 测试OnFailure方法在成功时不执行操作 + /// [Test] public void OnFailure_Should_Not_Execute_Action_When_Success() { @@ -205,6 +256,9 @@ public class ResultExtensionsTests Assert.That(executed, Is.False); } + /// + /// 测试OnFailure方法返回原始结果用于链式调用 + /// [Test] public void OnFailure_Should_Return_Original_Result_For_Chaining() { @@ -213,6 +267,9 @@ public class ResultExtensionsTests Assert.That(returned, Is.EqualTo(result)); } + /// + /// 测试OnFailure方法在操作为null时抛出ArgumentNullException + /// [Test] public void OnFailure_Should_Throw_ArgumentNullException_When_Action_Is_Null() { @@ -220,6 +277,9 @@ public class ResultExtensionsTests Assert.Throws(() => result.OnFailure(null!)); } + /// + /// 测试Ensure方法在谓词为true时返回成功 + /// [Test] public void Ensure_Should_Return_Success_When_Predicate_Is_True() { @@ -228,6 +288,9 @@ public class ResultExtensionsTests Assert.That(ensured.IsSuccess, Is.True); } + /// + /// 测试Ensure方法在谓词为false时返回失败 + /// [Test] public void Ensure_Should_Return_Failure_When_Predicate_Is_False() { @@ -237,6 +300,9 @@ public class ResultExtensionsTests Assert.That(ensured.Exception, Is.TypeOf()); } + /// + /// 测试Ensure方法传播现有失败 + /// [Test] public void Ensure_Should_Propagate_Existing_Failure() { @@ -247,6 +313,9 @@ public class ResultExtensionsTests Assert.That(ensured.Exception, Is.SameAs(exception)); } + /// + /// 测试Ensure方法创建带消息的ArgumentException + /// [Test] public void Ensure_Should_Create_ArgumentException_With_Message() { @@ -255,6 +324,9 @@ public class ResultExtensionsTests Assert.That(ensured.Exception.Message, Is.EqualTo("Value must be positive")); } + /// + /// 测试Ensure方法在谓词为null时抛出ArgumentNullException + /// [Test] public void Ensure_Should_Throw_ArgumentNullException_When_Predicate_Is_Null() { @@ -262,6 +334,9 @@ public class ResultExtensionsTests Assert.Throws(() => result.Ensure(null!, "Error")); } + /// + /// 测试Ensure方法在消息为null或空白时抛出ArgumentException + /// [Test] public void Ensure_Should_Throw_ArgumentException_When_Message_Is_NullOrWhiteSpace() { @@ -270,6 +345,9 @@ public class ResultExtensionsTests Assert.Throws(() => result.Ensure(x => true, " ")); } + /// + /// 测试带工厂的Ensure方法在谓词为true时返回成功 + /// [Test] public void Ensure_WithFactory_Should_Return_Success_When_Predicate_Is_True() { @@ -278,6 +356,9 @@ public class ResultExtensionsTests Assert.That(ensured.IsSuccess, Is.True); } + /// + /// 测试带工厂的Ensure方法返回带自定义异常的成功 + /// [Test] public void Ensure_WithFactory_Should_Return_Failure_With_Custom_Exception() { @@ -287,6 +368,9 @@ public class ResultExtensionsTests Assert.That(ensured.Exception, Is.TypeOf()); } + /// + /// 测试带工厂的Ensure方法传递值给异常工厂 + /// [Test] public void Ensure_WithFactory_Should_Pass_Value_To_Exception_Factory() { @@ -300,6 +384,9 @@ public class ResultExtensionsTests Assert.That(capturedValue, Is.EqualTo(-1)); } + /// + /// 测试带工厂的Ensure方法传播现有失败 + /// [Test] public void Ensure_WithFactory_Should_Propagate_Existing_Failure() { @@ -309,6 +396,9 @@ public class ResultExtensionsTests Assert.That(ensured.Exception, Is.SameAs(exception)); } + /// + /// 测试带工厂的Ensure方法在参数为null时抛出ArgumentNullException + /// [Test] public void Ensure_WithFactory_Should_Throw_ArgumentNullException_When_Parameters_Are_Null() { @@ -317,6 +407,9 @@ public class ResultExtensionsTests Assert.Throws(() => result.Ensure(x => true, (Func)null!)); } + /// + /// 测试Try方法在函数成功时返回成功 + /// [Test] public void Try_Should_Return_Success_When_Function_Succeeds() { @@ -325,6 +418,9 @@ public class ResultExtensionsTests Assert.That(result.Match(succ: v => v, fail: _ => 0), Is.EqualTo(42)); } + /// + /// 测试Try方法在函数抛出异常时返回失败 + /// [Test] public void Try_Should_Return_Failure_When_Function_Throws() { @@ -333,12 +429,18 @@ public class ResultExtensionsTests Assert.That(result.Exception, Is.TypeOf()); } + /// + /// 测试Try方法在函数为null时抛出ArgumentNullException + /// [Test] public void Try_Should_Throw_ArgumentNullException_When_Function_Is_Null() { Assert.Throws(() => ResultExtensions.Try(null!)); } + /// + /// 测试TryAsync方法在异步函数成功时返回成功 + /// [Test] public async Task TryAsync_Should_Return_Success_When_Async_Function_Succeeds() { @@ -351,6 +453,9 @@ public class ResultExtensionsTests Assert.That(result.Match(succ: v => v, fail: _ => 0), Is.EqualTo(42)); } + /// + /// 测试TryAsync方法在异步函数抛出异常时返回失败 + /// [Test] public async Task TryAsync_Should_Return_Failure_When_Async_Function_Throws() { @@ -363,6 +468,9 @@ public class ResultExtensionsTests Assert.That(result.Exception, Is.TypeOf()); } + /// + /// 测试TryAsync方法处理同步异常 + /// [Test] public async Task TryAsync_Should_Handle_Synchronous_Exceptions() { @@ -370,12 +478,18 @@ public class ResultExtensionsTests Assert.That(result.IsFaulted, Is.True); } + /// + /// 测试TryAsync方法在函数为null时抛出ArgumentNullException + /// [Test] public void TryAsync_Should_Throw_ArgumentNullException_When_Function_Is_Null() { Assert.ThrowsAsync(async () => await ResultExtensions.TryAsync(null!)); } + /// + /// 测试ToNullable方法在成功时返回值 + /// [Test] public void ToNullable_Should_Return_Value_When_Success() { @@ -384,6 +498,9 @@ public class ResultExtensionsTests Assert.That(nullable, Is.EqualTo(42)); } + /// + /// 测试ToNullable方法在失败时返回null + /// [Test] public void ToNullable_Should_Return_Null_When_Faulted() { @@ -392,6 +509,9 @@ public class ResultExtensionsTests Assert.That(nullable, Is.Null); } + /// + /// 测试ToNullable方法与值类型一起工作 + /// [Test] public void ToNullable_Should_Work_With_Value_Types() { @@ -399,10 +519,13 @@ public class ResultExtensionsTests var nullable = result.ToNullable(); Assert.That(nullable, Is.Not.Null); Assert.That(nullable!.Value.Year, Is.EqualTo(2025)); - Assert.That(nullable!.Value.Kind, Is.EqualTo(DateTimeKind.Utc)); // 验证 DateTimeKind + Assert.That(nullable!.Value.Kind, Is.EqualTo(DateTimeKind.Utc)); } + /// + /// 测试ToResult方法在值不为null时返回成功 + /// [Test] public void ToResult_Should_Return_Success_When_Value_Is_Not_Null() { @@ -412,6 +535,9 @@ public class ResultExtensionsTests Assert.That(result.Match(succ: v => v, fail: _ => ""), Is.EqualTo("test")); } + /// + /// 测试ToResult方法在值为null时返回失败 + /// [Test] public void ToResult_Should_Return_Failure_When_Value_Is_Null() { @@ -421,6 +547,9 @@ public class ResultExtensionsTests Assert.That(result.Exception, Is.TypeOf()); } + /// + /// 测试ToResult方法使用自定义错误消息 + /// [Test] public void ToResult_Should_Use_Custom_Error_Message() { @@ -429,6 +558,9 @@ public class ResultExtensionsTests Assert.That(result.Exception.Message, Does.Contain("Custom error")); } + /// + /// 测试ToResult方法在可空类型有值时返回成功 + /// [Test] public void ToResult_Should_Return_Success_When_Nullable_Has_Value() { @@ -438,6 +570,9 @@ public class ResultExtensionsTests Assert.That(result.Match(succ: v => v, fail: _ => 0), Is.EqualTo(42)); } + /// + /// 测试ToResult方法在可空类型为null时返回失败 + /// [Test] public void ToResult_Should_Return_Failure_When_Nullable_Is_Null() { @@ -447,6 +582,9 @@ public class ResultExtensionsTests Assert.That(result.Exception, Is.TypeOf()); } + /// + /// 测试ToResult方法对可空类型使用自定义错误消息 + /// [Test] public void ToResult_Should_Use_Custom_Error_Message_For_Nullable() { @@ -455,6 +593,9 @@ public class ResultExtensionsTests Assert.That(result.Exception.Message, Does.Contain("Custom nullable error")); } + /// + /// 测试支持多个操作的复杂链式调用 + /// [Test] public void Should_Support_Complex_Chaining_With_Multiple_Operations() { @@ -469,6 +610,9 @@ public class ResultExtensionsTests Assert.That(result.Match(succ: v => v, fail: _ => 0), Is.EqualTo(25)); } + /// + /// 测试支持OnSuccess和OnFailure链式调用 + /// [Test] public void Should_Support_OnSuccess_And_OnFailure_Chaining() { diff --git a/GFramework.Core.Tests/extensions/StringExtensionsTests.cs b/GFramework.Core.Tests/extensions/StringExtensionsTests.cs index 789820c..d8b53bd 100644 --- a/GFramework.Core.Tests/extensions/StringExtensionsTests.cs +++ b/GFramework.Core.Tests/extensions/StringExtensionsTests.cs @@ -9,6 +9,9 @@ namespace GFramework.Core.Tests.extensions; [TestFixture] public class StringExtensionsTests { + /// + /// 测试IsNullOrEmpty方法在字符串为null时返回true + /// [Test] public void IsNullOrEmpty_Should_Return_True_When_String_Is_Null() { @@ -22,6 +25,9 @@ public class StringExtensionsTests Assert.That(result, Is.True); } + /// + /// 测试IsNullOrEmpty方法在字符串为空时返回true + /// [Test] public void IsNullOrEmpty_Should_Return_True_When_String_Is_Empty() { @@ -35,6 +41,9 @@ public class StringExtensionsTests Assert.That(result, Is.True); } + /// + /// 测试IsNullOrEmpty方法在字符串有内容时返回false + /// [Test] public void IsNullOrEmpty_Should_Return_False_When_String_Has_Content() { @@ -48,6 +57,9 @@ public class StringExtensionsTests Assert.That(result, Is.False); } + /// + /// 测试IsNullOrWhiteSpace方法在字符串为null时返回true + /// [Test] public void IsNullOrWhiteSpace_Should_Return_True_When_String_Is_Null() { @@ -61,6 +73,9 @@ public class StringExtensionsTests Assert.That(result, Is.True); } + /// + /// 测试IsNullOrWhiteSpace方法在字符串为空白时返回true + /// [Test] public void IsNullOrWhiteSpace_Should_Return_True_When_String_Is_WhiteSpace() { @@ -74,6 +89,9 @@ public class StringExtensionsTests Assert.That(result, Is.True); } + /// + /// 测试IsNullOrWhiteSpace方法在字符串有内容时返回false + /// [Test] public void IsNullOrWhiteSpace_Should_Return_False_When_String_Has_Content() { @@ -87,6 +105,9 @@ public class StringExtensionsTests Assert.That(result, Is.False); } + /// + /// 测试NullIfEmpty方法在字符串为空时返回null + /// [Test] public void NullIfEmpty_Should_Return_Null_When_String_Is_Empty() { @@ -100,6 +121,9 @@ public class StringExtensionsTests Assert.That(result, Is.Null); } + /// + /// 测试NullIfEmpty方法在字符串为null时返回null + /// [Test] public void NullIfEmpty_Should_Return_Null_When_String_Is_Null() { @@ -113,6 +137,9 @@ public class StringExtensionsTests Assert.That(result, Is.Null); } + /// + /// 测试NullIfEmpty方法在字符串有内容时返回原字符串 + /// [Test] public void NullIfEmpty_Should_Return_String_When_String_Has_Content() { @@ -126,6 +153,9 @@ public class StringExtensionsTests Assert.That(result, Is.EqualTo("Hello")); } + /// + /// 测试Truncate方法在字符串长度小于最大长度时返回原字符串 + /// [Test] public void Truncate_Should_Return_Original_String_When_Length_Is_Less_Than_MaxLength() { @@ -139,6 +169,9 @@ public class StringExtensionsTests Assert.That(result, Is.EqualTo("Hello")); } + /// + /// 测试Truncate方法在字符串长度超过最大长度时截断并添加后缀 + /// [Test] public void Truncate_Should_Truncate_String_And_Add_Suffix() { @@ -152,6 +185,9 @@ public class StringExtensionsTests Assert.That(result, Is.EqualTo("Hello...")); } + /// + /// 测试Truncate方法使用自定义后缀 + /// [Test] public void Truncate_Should_Use_Custom_Suffix() { @@ -165,6 +201,9 @@ public class StringExtensionsTests Assert.That(result, Is.EqualTo("Hello W~")); } + /// + /// 测试Truncate方法在字符串为null时抛出ArgumentNullException + /// [Test] public void Truncate_Should_Throw_ArgumentNullException_When_String_Is_Null() { @@ -175,6 +214,9 @@ public class StringExtensionsTests Assert.Throws(() => text!.Truncate(10)); } + /// + /// 测试Truncate方法在最大长度小于后缀长度时抛出ArgumentOutOfRangeException + /// [Test] public void Truncate_Should_Throw_ArgumentOutOfRangeException_When_MaxLength_Is_Less_Than_Suffix_Length() { @@ -185,6 +227,9 @@ public class StringExtensionsTests Assert.Throws(() => text.Truncate(2, "...")); } + /// + /// 测试Join方法使用分隔符连接字符串数组 + /// [Test] public void Join_Should_Join_Strings_With_Separator() { @@ -198,6 +243,9 @@ public class StringExtensionsTests Assert.That(result, Is.EqualTo("Hello, World")); } + /// + /// 测试Join方法在集合为空时返回空字符串 + /// [Test] public void Join_Should_Return_Empty_String_When_Collection_Is_Empty() { @@ -211,6 +259,9 @@ public class StringExtensionsTests Assert.That(result, Is.EqualTo(string.Empty)); } + /// + /// 测试Join方法在集合为null时抛出ArgumentNullException + /// [Test] public void Join_Should_Throw_ArgumentNullException_When_Collection_Is_Null() { @@ -221,6 +272,9 @@ public class StringExtensionsTests Assert.Throws(() => words!.Join(", ")); } + /// + /// 测试Join方法在分隔符为null时抛出ArgumentNullException + /// [Test] public void Join_Should_Throw_ArgumentNullException_When_Separator_Is_Null() { diff --git a/GFramework.Core.Tests/functional/OptionTests.cs b/GFramework.Core.Tests/functional/OptionTests.cs index be86809..ab5a10f 100644 --- a/GFramework.Core.Tests/functional/OptionTests.cs +++ b/GFramework.Core.Tests/functional/OptionTests.cs @@ -22,6 +22,9 @@ namespace GFramework.Core.Tests.Functional; [TestFixture] public class OptionTests { + /// + /// 测试Some方法使用值创建Some选项 + /// [Test] public void Some_WithValue_Should_Create_Some_Option() { @@ -30,12 +33,18 @@ public class OptionTests Assert.That(option.IsNone, Is.False); } + /// + /// 测试Some方法使用null值时抛出ArgumentNullException + /// [Test] public void Some_WithNull_Should_Throw_ArgumentNullException() { Assert.Throws(() => Option.Some(null!)); } + /// + /// 测试None方法创建None选项 + /// [Test] public void None_Should_Create_None_Option() { @@ -44,6 +53,9 @@ public class OptionTests Assert.That(option.IsNone, Is.True); } + /// + /// 测试GetOrElse方法在Some时返回值 + /// [Test] public void GetOrElse_WithSome_Should_Return_Value() { @@ -52,6 +64,9 @@ public class OptionTests Assert.That(result, Is.EqualTo(42)); } + /// + /// 测试GetOrElse方法在None时返回默认值 + /// [Test] public void GetOrElse_WithNone_Should_Return_Default_Value() { @@ -60,6 +75,9 @@ public class OptionTests Assert.That(result, Is.EqualTo(99)); } + /// + /// 测试带工厂的GetOrElse方法在Some时不调用工厂 + /// [Test] public void GetOrElse_WithFactory_WithSome_Should_Return_Value_Without_Calling_Factory() { @@ -76,6 +94,9 @@ public class OptionTests Assert.That(factoryCalled, Is.False); } + /// + /// 测试带工厂的GetOrElse方法在None时调用工厂 + /// [Test] public void GetOrElse_WithFactory_WithNone_Should_Call_Factory() { @@ -84,6 +105,9 @@ public class OptionTests Assert.That(result, Is.EqualTo(99)); } + /// + /// 测试GetOrElse方法在工厂为null时抛出ArgumentNullException + /// [Test] public void GetOrElse_WithNullFactory_Should_Throw_ArgumentNullException() { @@ -91,6 +115,9 @@ public class OptionTests Assert.Throws(() => option.GetOrElse(null!)); } + /// + /// 测试Map方法在Some时转换值 + /// [Test] public void Map_WithSome_Should_Map_Value() { @@ -100,6 +127,9 @@ public class OptionTests Assert.That(mapped.GetOrElse(""), Is.EqualTo("42")); } + /// + /// 测试Map方法在None时返回None + /// [Test] public void Map_WithNone_Should_Return_None() { @@ -108,6 +138,9 @@ public class OptionTests Assert.That(mapped.IsNone, Is.True); } + /// + /// 测试Map方法在映射器为null时抛出ArgumentNullException + /// [Test] public void Map_WithNullMapper_Should_Throw_ArgumentNullException() { @@ -115,6 +148,9 @@ public class OptionTests Assert.Throws(() => option.Map(null!)); } + /// + /// 测试Bind方法在Some时绑定值 + /// [Test] public void Bind_WithSome_Should_Bind_Value() { @@ -127,6 +163,9 @@ public class OptionTests Assert.That(bound.GetOrElse(0), Is.EqualTo(42)); } + /// + /// 测试Bind方法在Some时可以返回None + /// [Test] public void Bind_WithSome_Can_Return_None() { @@ -138,6 +177,9 @@ public class OptionTests Assert.That(bound.IsNone, Is.True); } + /// + /// 测试Bind方法在None时返回None + /// [Test] public void Bind_WithNone_Should_Return_None() { @@ -146,6 +188,9 @@ public class OptionTests Assert.That(bound.IsNone, Is.True); } + /// + /// 测试Bind方法在绑定器为null时抛出ArgumentNullException + /// [Test] public void Bind_WithNullBinder_Should_Throw_ArgumentNullException() { @@ -153,6 +198,9 @@ public class OptionTests Assert.Throws(() => option.Bind(null!)); } + /// + /// 测试Filter方法在Some且谓词为true时返回Some + /// [Test] public void Filter_WithSome_PredicateTrue_Should_Return_Some() { @@ -162,6 +210,9 @@ public class OptionTests Assert.That(filtered.GetOrElse(0), Is.EqualTo(42)); } + /// + /// 测试Filter方法在Some且谓词为false时返回None + /// [Test] public void Filter_WithSome_PredicateFalse_Should_Return_None() { @@ -170,6 +221,9 @@ public class OptionTests Assert.That(filtered.IsNone, Is.True); } + /// + /// 测试Filter方法在None时返回None + /// [Test] public void Filter_WithNone_Should_Return_None() { @@ -178,6 +232,9 @@ public class OptionTests Assert.That(filtered.IsNone, Is.True); } + /// + /// 测试Filter方法在谓词为null时抛出ArgumentNullException + /// [Test] public void Filter_WithNullPredicate_Should_Throw_ArgumentNullException() { @@ -185,6 +242,9 @@ public class OptionTests Assert.Throws(() => option.Filter(null!)); } + /// + /// 测试Match方法在Some时调用some函数 + /// [Test] public void Match_WithSome_Should_Call_Some_Function() { @@ -196,6 +256,9 @@ public class OptionTests Assert.That(result, Is.EqualTo("Value: 42")); } + /// + /// 测试Match方法在None时调用none函数 + /// [Test] public void Match_WithNone_Should_Call_None_Function() { @@ -207,6 +270,9 @@ public class OptionTests Assert.That(result, Is.EqualTo("No value")); } + /// + /// 测试Match方法在some函数为null时抛出ArgumentNullException + /// [Test] public void Match_WithNullSomeFunction_Should_Throw_ArgumentNullException() { @@ -215,6 +281,9 @@ public class OptionTests option.Match(null!, () => "")); } + /// + /// 测试Match方法在none函数为null时抛出ArgumentNullException + /// [Test] public void Match_WithNullNoneFunction_Should_Throw_ArgumentNullException() { @@ -223,6 +292,9 @@ public class OptionTests option.Match(value => "", null!)); } + /// + /// 测试Match方法(Action形式)在Some时调用some操作 + /// [Test] public void Match_Action_WithSome_Should_Call_Some_Action() { @@ -239,6 +311,9 @@ public class OptionTests Assert.That(noneCalled, Is.False); } + /// + /// 测试Match方法(Action形式)在None时调用none操作 + /// [Test] public void Match_Action_WithNone_Should_Call_None_Action() { @@ -255,6 +330,9 @@ public class OptionTests Assert.That(noneCalled, Is.True); } + /// + /// 测试ToResult方法在Some时返回成功结果 + /// [Test] public void ToResult_WithSome_Should_Return_Success_Result() { @@ -264,6 +342,9 @@ public class OptionTests Assert.That(result.Match(succ: v => v, fail: _ => 0), Is.EqualTo(42)); } + /// + /// 测试ToResult方法在None时返回失败结果 + /// [Test] public void ToResult_WithNone_Should_Return_Failure_Result() { @@ -274,6 +355,9 @@ public class OptionTests Assert.That(result.Exception.Message, Is.EqualTo("Value not found")); } + /// + /// 测试ToResult方法在消息为空或空白时抛出ArgumentException + /// [Test] public void ToResult_WithNullOrWhiteSpaceMessage_Should_Throw_ArgumentException() { @@ -282,6 +366,9 @@ public class OptionTests Assert.Throws(() => option.ToResult(" ")); } + /// + /// 测试ToEnumerable方法在Some时返回包含一个元素的序列 + /// [Test] public void ToEnumerable_WithSome_Should_Return_Sequence_With_One_Element() { @@ -291,6 +378,9 @@ public class OptionTests Assert.That(enumerable[0], Is.EqualTo(42)); } + /// + /// 测试ToEnumerable方法在None时返回空序列 + /// [Test] public void ToEnumerable_WithNone_Should_Return_Empty_Sequence() { @@ -299,6 +389,9 @@ public class OptionTests Assert.That(enumerable, Is.Empty); } + /// + /// 测试隐式转换从值创建Some选项 + /// [Test] public void ImplicitConversion_FromValue_Should_Create_Some_Option() { @@ -307,6 +400,9 @@ public class OptionTests Assert.That(option.GetOrElse(0), Is.EqualTo(42)); } + /// + /// 测试隐式转换从null创建None选项 + /// [Test] public void ImplicitConversion_FromNull_Should_Create_None_Option() { @@ -314,6 +410,9 @@ public class OptionTests Assert.That(option.IsNone, Is.True); } + /// + /// 测试Equals方法在两个Some值相同时返回true + /// [Test] public void Equals_TwoSomeWithSameValue_Should_Return_True() { @@ -324,6 +423,9 @@ public class OptionTests Assert.That(option1 != option2, Is.False); } + /// + /// 测试Equals方法在两个Some值不同时返回false + /// [Test] public void Equals_TwoSomeWithDifferentValue_Should_Return_False() { @@ -334,6 +436,9 @@ public class OptionTests Assert.That(option1 != option2, Is.True); } + /// + /// 测试Equals方法在两个None时返回true + /// [Test] public void Equals_TwoNone_Should_Return_True() { @@ -344,6 +449,9 @@ public class OptionTests Assert.That(option1 != option2, Is.False); } + /// + /// 测试Equals方法在Some和None比较时返回false + /// [Test] public void Equals_SomeAndNone_Should_Return_False() { @@ -354,6 +462,9 @@ public class OptionTests Assert.That(option1 != option2, Is.True); } + /// + /// 测试GetHashCode方法在两个Some值相同时返回相同哈希码 + /// [Test] public void GetHashCode_TwoSomeWithSameValue_Should_Return_Same_HashCode() { @@ -362,6 +473,9 @@ public class OptionTests Assert.That(option1.GetHashCode(), Is.EqualTo(option2.GetHashCode())); } + /// + /// 测试GetHashCode方法在两个None时返回相同哈希码 + /// [Test] public void GetHashCode_TwoNone_Should_Return_Same_HashCode() { @@ -370,6 +484,9 @@ public class OptionTests Assert.That(option1.GetHashCode(), Is.EqualTo(option2.GetHashCode())); } + /// + /// 测试ToString方法在Some时返回格式化字符串 + /// [Test] public void ToString_WithSome_Should_Return_Formatted_String() { @@ -378,6 +495,9 @@ public class OptionTests Assert.That(result, Is.EqualTo("Some(42)")); } + /// + /// 测试ToString方法在None时返回None + /// [Test] public void ToString_WithNone_Should_Return_None() { diff --git a/GFramework.Core.Tests/functional/ResultTTests.cs b/GFramework.Core.Tests/functional/ResultTTests.cs index 7ccbcf1..ec2d86c 100644 --- a/GFramework.Core.Tests/functional/ResultTTests.cs +++ b/GFramework.Core.Tests/functional/ResultTTests.cs @@ -22,6 +22,9 @@ namespace GFramework.Core.Tests.Functional; [TestFixture] public class ResultTTests { + /// + /// 测试构造函数使用值创建成功结果 + /// [Test] public void Constructor_WithValue_Should_Create_Success_Result() { @@ -31,6 +34,9 @@ public class ResultTTests Assert.That(result.IsBottom, Is.False); } + /// + /// 测试构造函数使用异常创建失败结果 + /// [Test] public void Constructor_WithException_Should_Create_Faulted_Result() { @@ -41,12 +47,18 @@ public class ResultTTests Assert.That(result.Exception, Is.SameAs(exception)); } + /// + /// 测试构造函数使用null异常时抛出ArgumentNullException + /// [Test] public void Constructor_WithNullException_Should_Throw_ArgumentNullException() { Assert.Throws(() => new Result(null!)); } + /// + /// 测试默认构造函数创建Bottom状态 + /// [Test] public void DefaultConstructor_Should_Create_Bottom_State() { @@ -56,6 +68,9 @@ public class ResultTTests Assert.That(result.IsFaulted, Is.False); } + /// + /// 测试隐式转换创建成功结果 + /// [Test] public void ImplicitConversion_Should_Create_Success_Result() { @@ -64,6 +79,9 @@ public class ResultTTests Assert.That(result.Match(succ: v => v, fail: _ => 0), Is.EqualTo(42)); } + /// + /// 测试IsSuccess属性在成功时返回true + /// [Test] public void IsSuccess_Should_Return_True_When_Result_Is_Successful() { @@ -71,6 +89,9 @@ public class ResultTTests Assert.That(result.IsSuccess, Is.True); } + /// + /// 测试IsFaulted属性在失败时返回true + /// [Test] public void IsFaulted_Should_Return_True_When_Result_Is_Faulted() { @@ -78,6 +99,9 @@ public class ResultTTests Assert.That(result.IsFaulted, Is.True); } + /// + /// 测试IsBottom属性在Bottom状态时返回true + /// [Test] public void IsBottom_Should_Return_True_When_Result_Is_Bottom() { @@ -85,6 +109,9 @@ public class ResultTTests Assert.That(result.IsBottom, Is.True); } + /// + /// 测试Exception属性在失败时返回异常 + /// [Test] public void Exception_Should_Return_Exception_When_Faulted() { @@ -93,6 +120,9 @@ public class ResultTTests Assert.That(result.Exception, Is.SameAs(exception)); } + /// + /// 测试Exception属性在Bottom状态时返回InvalidOperationException + /// [Test] public void Exception_Should_Return_InvalidOperationException_When_Bottom() { @@ -100,6 +130,9 @@ public class ResultTTests Assert.That(result.Exception, Is.TypeOf()); } + /// + /// 测试Exception属性在成功时返回InvalidOperationException + /// [Test] public void Exception_Should_Return_InvalidOperationException_When_Success() { @@ -107,6 +140,9 @@ public class ResultTTests Assert.That(result.Exception, Is.TypeOf()); } + /// + /// 测试Succeed方法创建成功结果 + /// [Test] public void Succeed_Should_Create_Successful_Result() { @@ -115,6 +151,9 @@ public class ResultTTests Assert.That(result.Match(succ: v => v, fail: _ => 0), Is.EqualTo(42)); } + /// + /// 测试Success方法创建成功结果 + /// [Test] public void Success_Should_Create_Successful_Result() { @@ -123,6 +162,9 @@ public class ResultTTests Assert.That(result.Match(succ: v => v, fail: _ => 0), Is.EqualTo(42)); } + /// + /// 测试Fail方法创建失败结果 + /// [Test] public void Fail_Should_Create_Faulted_Result() { @@ -132,6 +174,9 @@ public class ResultTTests Assert.That(result.Exception, Is.SameAs(exception)); } + /// + /// 测试Failure方法使用异常创建失败结果 + /// [Test] public void Failure_WithException_Should_Create_Faulted_Result() { @@ -141,6 +186,9 @@ public class ResultTTests Assert.That(result.Exception, Is.SameAs(exception)); } + /// + /// 测试Failure方法使用消息创建失败结果 + /// [Test] public void Failure_WithMessage_Should_Create_Faulted_Result() { @@ -150,6 +198,9 @@ public class ResultTTests Assert.That(result.Exception.Message, Is.EqualTo(message)); } + /// + /// 测试Try方法在函数成功时返回成功结果 + /// [Test] public void Try_Should_Return_Success_When_Function_Succeeds() { @@ -158,6 +209,9 @@ public class ResultTTests Assert.That(result.Match(succ: v => v, fail: _ => 0), Is.EqualTo(42)); } + /// + /// 测试Try方法在函数抛出异常时返回失败结果 + /// [Test] public void Try_Should_Return_Failure_When_Function_Throws() { @@ -166,6 +220,9 @@ public class ResultTTests Assert.That(result.Exception, Is.TypeOf()); } + /// + /// 测试IfFail方法使用默认值在成功时返回值 + /// [Test] public void IfFail_WithDefaultValue_Should_Return_Value_When_Success() { @@ -174,6 +231,9 @@ public class ResultTTests Assert.That(value, Is.EqualTo(42)); } + /// + /// 测试IfFail方法使用默认值在失败时返回默认值 + /// [Test] public void IfFail_WithDefaultValue_Should_Return_Default_When_Faulted() { @@ -182,6 +242,9 @@ public class ResultTTests Assert.That(value, Is.EqualTo(99)); } + /// + /// 测试IfFail方法使用函数在成功时返回值 + /// [Test] public void IfFail_WithFunction_Should_Return_Value_When_Success() { @@ -190,6 +253,9 @@ public class ResultTTests Assert.That(value, Is.EqualTo(42)); } + /// + /// 测试IfFail方法使用函数在失败时执行函数 + /// [Test] public void IfFail_WithFunction_Should_Execute_Function_When_Faulted() { @@ -205,6 +271,9 @@ public class ResultTTests Assert.That(value, Is.EqualTo(99)); } + /// + /// 测试IfFail方法使用Action在失败时执行操作 + /// [Test] public void IfFail_WithAction_Should_Execute_Action_When_Faulted() { @@ -214,6 +283,9 @@ public class ResultTTests Assert.That(executed, Is.True); } + /// + /// 测试IfSucc方法在成功时执行操作 + /// [Test] public void IfSucc_Should_Execute_Action_When_Success() { @@ -223,6 +295,9 @@ public class ResultTTests Assert.That(executed, Is.True); } + /// + /// 测试IfSucc方法在失败时不执行操作 + /// [Test] public void IfSucc_Should_Not_Execute_Action_When_Faulted() { @@ -232,6 +307,9 @@ public class ResultTTests Assert.That(executed, Is.False); } + /// + /// 测试Map方法在成功时转换值 + /// [Test] public void Map_Should_Transform_Value_When_Success() { @@ -241,6 +319,9 @@ public class ResultTTests Assert.That(mapped.Match(succ: v => v, fail: _ => ""), Is.EqualTo("42")); } + /// + /// 测试Map方法在失败时传播异常 + /// [Test] public void Map_Should_Propagate_Exception_When_Faulted() { @@ -251,6 +332,9 @@ public class ResultTTests Assert.That(mapped.Exception, Is.SameAs(exception)); } + /// + /// 测试Map方法处理null结果 + /// [Test] public void Map_Should_Handle_Null_Result_From_Mapper() { @@ -259,6 +343,9 @@ public class ResultTTests Assert.That(mapped.IsSuccess, Is.True); } + /// + /// 测试Map方法在映射器返回null时不抛出异常 + /// [Test] public void Map_Should_Not_Throw_When_Mapper_Returns_Null() { @@ -266,6 +353,9 @@ public class ResultTTests Assert.DoesNotThrow(() => result.Map(_ => null)); } + /// + /// 测试Bind方法链接成功结果 + /// [Test] public void Bind_Should_Chain_Success_Results() { @@ -275,6 +365,9 @@ public class ResultTTests Assert.That(bound.Match(succ: v => v, fail: _ => ""), Is.EqualTo("42")); } + /// + /// 测试Bind方法传播第一个失败 + /// [Test] public void Bind_Should_Propagate_First_Failure() { @@ -285,6 +378,9 @@ public class ResultTTests Assert.That(bound.Exception, Is.SameAs(exception)); } + /// + /// 测试Bind方法传播第二个失败 + /// [Test] public void Bind_Should_Propagate_Second_Failure() { @@ -295,6 +391,9 @@ public class ResultTTests Assert.That(bound.Exception, Is.SameAs(exception)); } + /// + /// 测试Bind方法处理复杂链接 + /// [Test] public void Bind_Should_Handle_Complex_Chaining() { @@ -305,6 +404,9 @@ public class ResultTTests Assert.That(result.Match(succ: v => v, fail: _ => 0), Is.EqualTo(25)); } + /// + /// 测试MapAsync方法在成功时异步转换值 + /// [Test] public async Task MapAsync_Should_Transform_Value_When_Success() { @@ -318,6 +420,9 @@ public class ResultTTests Assert.That(mapped.Match(succ: v => v, fail: _ => ""), Is.EqualTo("42")); } + /// + /// 测试MapAsync方法在失败时传播异常 + /// [Test] public async Task MapAsync_Should_Propagate_Exception_When_Faulted() { @@ -332,6 +437,9 @@ public class ResultTTests Assert.That(mapped.Exception, Is.SameAs(exception)); } + /// + /// 测试MapAsync方法处理异步异常 + /// [Test] public async Task MapAsync_Should_Handle_Async_Exceptions() { @@ -345,6 +453,9 @@ public class ResultTTests Assert.That(mapped.Exception, Is.TypeOf()); } + /// + /// 测试Match方法使用函数在成功时执行succ函数 + /// [Test] public void Match_WithFunctions_Should_Execute_Succ_When_Success() { @@ -353,6 +464,9 @@ public class ResultTTests Assert.That(value, Is.EqualTo(84)); } + /// + /// 测试Match方法使用函数在失败时执行fail函数 + /// [Test] public void Match_WithFunctions_Should_Execute_Fail_When_Faulted() { @@ -361,6 +475,9 @@ public class ResultTTests Assert.That(value, Is.EqualTo(99)); } + /// + /// 测试Match方法使用Action在成功时执行succ操作 + /// [Test] public void Match_WithActions_Should_Execute_Succ_When_Success() { @@ -370,6 +487,9 @@ public class ResultTTests Assert.That(executed, Is.True); } + /// + /// 测试Match方法使用Action在失败时执行fail操作 + /// [Test] public void Match_WithActions_Should_Execute_Fail_When_Faulted() { @@ -379,6 +499,9 @@ public class ResultTTests Assert.That(executed, Is.True); } + /// + /// 测试Match方法正确处理Bottom状态 + /// [Test] public void Match_Should_Handle_Bottom_State_Correctly() { @@ -387,6 +510,9 @@ public class ResultTTests Assert.That(value, Is.EqualTo(99)); } + /// + /// 测试Equals方法在两个成功值相同时返回true + /// [Test] public void Equals_Should_Return_True_When_Both_Success_With_Same_Value() { @@ -395,6 +521,9 @@ public class ResultTTests Assert.That(result1.Equals(result2), Is.True); } + /// + /// 测试Equals方法在成功值不同时返回false + /// [Test] public void Equals_Should_Return_False_When_Success_Values_Differ() { @@ -403,6 +532,9 @@ public class ResultTTests Assert.That(result1.Equals(result2), Is.False); } + /// + /// 测试Equals方法在两个失败且异常相同时返回true + /// [Test] public void Equals_Should_Return_True_When_Both_Faulted_With_Same_Exception() { @@ -411,6 +543,9 @@ public class ResultTTests Assert.That(result1.Equals(result2), Is.True); } + /// + /// 测试Equals方法在异常类型不同时返回false + /// [Test] public void Equals_Should_Return_False_When_Exception_Types_Differ() { @@ -419,6 +554,9 @@ public class ResultTTests Assert.That(result1.Equals(result2), Is.False); } + /// + /// 测试Equals方法在两个都是Bottom时返回true + /// [Test] public void Equals_Should_Return_True_When_Both_Bottom() { @@ -427,6 +565,9 @@ public class ResultTTests Assert.That(result1.Equals(result2), Is.True); } + /// + /// 测试Equals方法在状态不同时返回false + /// [Test] public void Equals_Should_Return_False_When_States_Differ() { @@ -435,6 +576,9 @@ public class ResultTTests Assert.That(result1.Equals(result2), Is.False); } + /// + /// 测试GetHashCode方法在相等结果时返回一致的哈希码 + /// [Test] public void GetHashCode_Should_Be_Consistent_For_Equal_Results() { @@ -443,6 +587,9 @@ public class ResultTTests Assert.That(result1.GetHashCode(), Is.EqualTo(result2.GetHashCode())); } + /// + /// 测试==操作符正确工作 + /// [Test] public void OperatorEquals_Should_Work_Correctly() { @@ -453,6 +600,9 @@ public class ResultTTests Assert.That(result1 == result3, Is.False); } + /// + /// 测试!=操作符正确工作 + /// [Test] public void OperatorNotEquals_Should_Work_Correctly() { @@ -461,6 +611,9 @@ public class ResultTTests Assert.That(result1 != result2, Is.True); } + /// + /// 测试CompareTo方法排序Bottom在失败之前 + /// [Test] public void CompareTo_Should_Order_Bottom_Before_Faulted() { @@ -469,6 +622,9 @@ public class ResultTTests Assert.That(bottom.CompareTo(faulted), Is.LessThan(0)); } + /// + /// 测试CompareTo方法排序失败在成功之前 + /// [Test] public void CompareTo_Should_Order_Faulted_Before_Success() { @@ -477,6 +633,9 @@ public class ResultTTests Assert.That(faulted.CompareTo(success), Is.LessThan(0)); } + /// + /// 测试CompareTo方法在两个成功时比较值 + /// [Test] public void CompareTo_Should_Compare_Success_Values_When_Both_Success() { @@ -485,6 +644,9 @@ public class ResultTTests Assert.That(result1.CompareTo(result2), Is.LessThan(0)); } + /// + /// 测试CompareTo方法在两个都失败时返回0 + /// [Test] public void CompareTo_Should_Return_Zero_When_Both_Faulted() { @@ -493,6 +655,9 @@ public class ResultTTests Assert.That(result1.CompareTo(result2), Is.EqualTo(0)); } + /// + /// 测试CompareTo方法在两个都是Bottom时返回0 + /// [Test] public void CompareTo_Should_Return_Zero_When_Both_Bottom() { @@ -501,6 +666,9 @@ public class ResultTTests Assert.That(result1.CompareTo(result2), Is.EqualTo(0)); } + /// + /// 测试<操作符正确工作 + /// [Test] public void OperatorLessThan_Should_Work_Correctly() { @@ -509,6 +677,9 @@ public class ResultTTests Assert.That(result1 < result2, Is.True); } + /// + /// 测试<=操作符正确工作 + /// [Test] public void OperatorLessThanOrEqual_Should_Work_Correctly() { @@ -517,6 +688,9 @@ public class ResultTTests Assert.That(result1 <= result2, Is.True); } + /// + /// 测试>操作符正确工作 + /// [Test] public void OperatorGreaterThan_Should_Work_Correctly() { @@ -525,6 +699,9 @@ public class ResultTTests Assert.That(result1 > result2, Is.True); } + /// + /// 测试>=操作符正确工作 + /// [Test] public void OperatorGreaterThanOrEqual_Should_Work_Correctly() { @@ -533,6 +710,9 @@ public class ResultTTests Assert.That(result1 >= result2, Is.True); } + /// + /// 测试CompareTo方法优雅处理不可比较类型 + /// [Test] public void CompareTo_Should_Handle_NonComparable_Types_Gracefully() { @@ -541,6 +721,9 @@ public class ResultTTests Assert.DoesNotThrow(() => result1.CompareTo(result2)); } + /// + /// 测试ToString方法在成功时返回值字符串 + /// [Test] public void ToString_Should_Return_Value_String_When_Success() { @@ -548,6 +731,9 @@ public class ResultTTests Assert.That(result.ToString(), Is.EqualTo("42")); } + /// + /// 测试ToString方法在失败时返回失败消息 + /// [Test] public void ToString_Should_Return_Fail_Message_When_Faulted() { @@ -555,6 +741,9 @@ public class ResultTTests Assert.That(result.ToString(), Is.EqualTo("Fail(Test error)")); } + /// + /// 测试ToString方法在Bottom时返回Bottom + /// [Test] public void ToString_Should_Return_Bottom_When_Bottom() { @@ -562,6 +751,9 @@ public class ResultTTests Assert.That(result.ToString(), Is.EqualTo("(Bottom)")); } + /// + /// 测试Success方法使用null值创建有效结果 + /// [Test] public void Success_WithNullValue_Should_Create_Valid_Result() { @@ -569,6 +761,9 @@ public class ResultTTests Assert.That(result.IsSuccess, Is.True); } + /// + /// 测试Map方法正确处理null值 + /// [Test] public void Map_WithNullValue_Should_Handle_Correctly() { @@ -578,6 +773,9 @@ public class ResultTTests Assert.That(mapped.Match(succ: v => v, fail: _ => -1), Is.EqualTo(0)); } + /// + /// 测试Equals方法null值正确工作 + /// [Test] public void Equals_WithNullValues_Should_Work_Correctly() { @@ -586,6 +784,9 @@ public class ResultTTests Assert.That(result1.Equals(result2), Is.True); } + /// + /// 测试隐式转换null创建成功的null值 + /// [Test] public void ImplicitConversion_WithNull_Should_Create_Success_With_Null() { @@ -593,6 +794,9 @@ public class ResultTTests Assert.That(result.IsSuccess, Is.True); } + /// + /// 测试Bottom是只读和不可变的 + /// [Test] public void Bottom_Should_Be_Readonly_And_Immutable() { diff --git a/GFramework.Core.Tests/functional/pipe/PipeExtensionsTests.cs b/GFramework.Core.Tests/functional/pipe/PipeExtensionsTests.cs index dc9d820..762ea1a 100644 --- a/GFramework.Core.Tests/functional/pipe/PipeExtensionsTests.cs +++ b/GFramework.Core.Tests/functional/pipe/PipeExtensionsTests.cs @@ -28,6 +28,9 @@ public class PipeExtensionsTests Assert.That(capturedValue, Is.EqualTo(42)); } + /// + /// 测试Tap方法执行操作并返回原值 + /// [Test] public void Tap_Should_Execute_Action_And_Return_Original_Value() { @@ -43,6 +46,9 @@ public class PipeExtensionsTests Assert.That(capturedValue, Is.EqualTo(42)); } + /// + /// 测试Tap方法在操作为null时抛出ArgumentNullException + /// [Test] public void Tap_WithNullAction_Should_Throw_ArgumentNullException() { @@ -53,6 +59,9 @@ public class PipeExtensionsTests Assert.Throws(() => value.Tap(null!)); } + /// + /// 测试Tap方法支持链式调用 + /// [Test] public void Tap_Should_Allow_Chaining() { @@ -72,6 +81,9 @@ public class PipeExtensionsTests Assert.That(log[1], Is.EqualTo("Step 2: 10")); } + /// + /// 测试Pipe方法转换值 + /// [Test] public void Pipe_Should_Transform_Value() { @@ -85,6 +97,9 @@ public class PipeExtensionsTests Assert.That(result, Is.EqualTo(84)); } + /// + /// 测试Pipe方法在函数为null时抛出ArgumentNullException + /// [Test] public void Pipe_WithNullFunction_Should_Throw_ArgumentNullException() { @@ -95,6 +110,9 @@ public class PipeExtensionsTests Assert.Throws(() => value.Pipe(null!)); } + /// + /// 测试Pipe方法支持链式调用 + /// [Test] public void Pipe_Should_Allow_Chaining() { @@ -111,6 +129,9 @@ public class PipeExtensionsTests Assert.That(result, Is.EqualTo("20")); } + /// + /// 测试Let方法转换值 + /// [Test] public void Let_Should_Transform_Value() { @@ -124,6 +145,9 @@ public class PipeExtensionsTests Assert.That(result, Is.EqualTo("42")); } + /// + /// 测试Let方法在转换函数为null时抛出ArgumentNullException + /// [Test] public void Let_WithNullTransform_Should_Throw_ArgumentNullException() { @@ -134,6 +158,9 @@ public class PipeExtensionsTests Assert.Throws(() => value.Let(null!)); } + /// + /// 测试Let方法支持复杂转换 + /// [Test] public void Let_Should_Allow_Complex_Transformations() { @@ -154,6 +181,9 @@ public class PipeExtensionsTests Assert.That(result.Length, Is.EqualTo(5)); } + /// + /// 测试PipeIf方法在谓词为true时应用IfTrue函数 + /// [Test] public void PipeIf_WithTruePredicate_Should_Apply_IfTrue_Function() { @@ -171,6 +201,9 @@ public class PipeExtensionsTests Assert.That(result, Is.EqualTo("Positive: 42")); } + /// + /// 测试PipeIf方法在谓词为false时应用IfFalse函数 + /// [Test] public void PipeIf_WithFalsePredicate_Should_Apply_IfFalse_Function() { @@ -188,6 +221,9 @@ public class PipeExtensionsTests Assert.That(result, Is.EqualTo("Non-positive: -5")); } + /// + /// 测试PipeIf方法在谓词为null时抛出ArgumentNullException + /// [Test] public void PipeIf_WithNullPredicate_Should_Throw_ArgumentNullException() { @@ -199,6 +235,9 @@ public class PipeExtensionsTests value.PipeIf(null!, x => "", x => "")); } + /// + /// 测试PipeIf方法在IfTrue函数为null时抛出ArgumentNullException + /// [Test] public void PipeIf_WithNullIfTrue_Should_Throw_ArgumentNullException() { @@ -210,6 +249,9 @@ public class PipeExtensionsTests value.PipeIf(x => true, null!, x => "")); } + /// + /// 测试PipeIf方法在IfFalse函数为null时抛出ArgumentNullException + /// [Test] public void PipeIf_WithNullIfFalse_Should_Throw_ArgumentNullException() { @@ -221,6 +263,9 @@ public class PipeExtensionsTests value.PipeIf(x => true, x => "", null!)); } + /// + /// 测试PipeIf方法支持链式调用 + /// [Test] public void PipeIf_Should_Allow_Chaining() { diff --git a/GFramework.Core.Tests/mediator/MediatorComprehensiveTests.cs b/GFramework.Core.Tests/mediator/MediatorComprehensiveTests.cs index 9362987..bbc961d 100644 --- a/GFramework.Core.Tests/mediator/MediatorComprehensiveTests.cs +++ b/GFramework.Core.Tests/mediator/MediatorComprehensiveTests.cs @@ -24,6 +24,10 @@ namespace GFramework.Core.Tests.mediator; [TestFixture] public class MediatorComprehensiveTests { + /// + /// 测试初始化方法,在每个测试方法执行前运行。 + /// 负责初始化日志工厂、依赖注入容器、Mediator以及各种总线服务。 + /// [SetUp] public void SetUp() { @@ -60,6 +64,10 @@ public class MediatorComprehensiveTests _context = new ArchitectureContext(_container); } + /// + /// 测试清理方法,在每个测试方法执行后运行。 + /// 负责释放容器和上下文资源。 + /// [TearDown] public void TearDown() { @@ -80,6 +88,9 @@ public class MediatorComprehensiveTests private AsyncQueryExecutor? _asyncQueryBus; private DefaultEnvironment? _environment; + /// + /// 测试SendRequestAsync方法在请求有效时返回结果 + /// [Test] public async Task SendRequestAsync_Should_ReturnResult_When_Request_IsValid() { @@ -89,6 +100,9 @@ public class MediatorComprehensiveTests Assert.That(result, Is.EqualTo(42)); } + /// + /// 测试SendRequestAsync方法在请求为null时抛出ArgumentNullException + /// [Test] public void SendRequestAsync_Should_ThrowArgumentNullException_When_Request_IsNull() { @@ -96,6 +110,9 @@ public class MediatorComprehensiveTests await _context!.SendRequestAsync(null!)); } + /// + /// 测试SendRequest方法在请求有效时返回结果 + /// [Test] public void SendRequest_Should_ReturnResult_When_Request_IsValid() { @@ -105,6 +122,9 @@ public class MediatorComprehensiveTests Assert.That(result, Is.EqualTo(123)); } + /// + /// 测试PublishAsync方法在通知有效时发布通知 + /// [Test] public async Task PublishAsync_Should_PublishNotification_When_Notification_IsValid() { @@ -117,6 +137,9 @@ public class MediatorComprehensiveTests Assert.That(TestNotificationHandler.LastReceivedMessage, Is.EqualTo("test")); } + /// + /// 测试CreateStream方法在流请求有效时返回流 + /// [Test] public async Task CreateStream_Should_ReturnStream_When_StreamRequest_IsValid() { @@ -132,6 +155,9 @@ public class MediatorComprehensiveTests Assert.That(results, Is.EqualTo(new[] { 1, 2, 3, 4, 5 })); } + /// + /// 测试SendAsync方法(无返回值命令)在命令有效时执行 + /// [Test] public async Task SendAsync_CommandWithoutResult_Should_Execute_When_Command_IsValid() { @@ -141,6 +167,9 @@ public class MediatorComprehensiveTests Assert.That(testCommand.Executed, Is.True); } + /// + /// 测试SendAsync方法(带返回值命令)在命令有效时返回结果 + /// [Test] public async Task SendAsync_CommandWithResult_Should_ReturnResult_When_Command_IsValid() { @@ -150,6 +179,9 @@ public class MediatorComprehensiveTests Assert.That(result, Is.EqualTo(42)); } + /// + /// 测试GetService方法使用缓存 + /// [Test] public void GetService_Should_Use_Cache() { @@ -162,6 +194,9 @@ public class MediatorComprehensiveTests } + /// + /// 测试未注册的Mediator抛出InvalidOperationException + /// [Test] public void Unregistered_Mediator_Should_Throw_InvalidOperationException() { @@ -175,6 +210,9 @@ public class MediatorComprehensiveTests await contextWithoutMediator.SendRequestAsync(testRequest)); } + /// + /// 测试多个通知处理器都被调用 + /// [Test] public async Task Multiple_Notification_Handlers_Should_All_Be_Invoked() { @@ -193,6 +231,9 @@ public class MediatorComprehensiveTests Assert.That(TestNotificationHandler3.LastReceivedMessage, Is.EqualTo("multi-handler test")); } + /// + /// 测试CancellationToken取消长时间运行的请求 + /// [Test] public async Task CancellationToken_Should_Cancel_Long_Running_Request() { @@ -204,6 +245,9 @@ public class MediatorComprehensiveTests await _context!.SendRequestAsync(longRequest, cts.Token)); } + /// + /// 测试CancellationToken取消流请求 + /// [Test] public async Task CancellationToken_Should_Cancel_Stream_Request() { @@ -226,6 +270,9 @@ public class MediatorComprehensiveTests Assert.That(results.Count, Is.LessThan(1000)); } + /// + /// 测试并发Mediator请求不会相互干扰 + /// [Test] public async Task Concurrent_Mediator_Requests_Should_Not_Interfere() { @@ -246,6 +293,9 @@ public class MediatorComprehensiveTests Assert.That(results.OrderBy(x => x), Is.EqualTo(Enumerable.Range(0, requestCount))); } + /// + /// 测试处理器异常被正确传播 + /// [Test] public async Task Handler_Exception_Should_Be_Propagated() { @@ -255,6 +305,9 @@ public class MediatorComprehensiveTests await _context!.SendRequestAsync(faultyRequest)); } + /// + /// 测试多个命令处理器可以修改同一对象 + /// [Test] public async Task Multiple_Command_Handlers_Can_Modify_Same_Object() { @@ -269,6 +322,9 @@ public class MediatorComprehensiveTests Assert.That(sharedData.Value, Is.EqualTo(30)); // 10 + 20 } + /// + /// 测试通知顺序被保留 + /// [Test] public async Task Notification_Ordering_Should_Be_Preserved() { @@ -296,6 +352,9 @@ public class MediatorComprehensiveTests Assert.That(receivedOrder[2], Is.EqualTo("Third")); } + /// + /// 测试流请求带过滤功能 + /// [Test] public async Task Stream_Request_With_Filtering() { @@ -318,6 +377,9 @@ public class MediatorComprehensiveTests Assert.That(results, Is.EqualTo(new[] { 2, 4, 6, 8, 10 })); } + /// + /// 测试请求验证使用Behaviors + /// [Test] public async Task Request_Validation_With_Behaviors() { @@ -327,6 +389,9 @@ public class MediatorComprehensiveTests await _context!.SendAsync(invalidCommand)); } + /// + /// 测试Mediator性能基准 + /// [Test] public async Task Performance_Benchmark_For_Mediator() { @@ -348,6 +413,9 @@ public class MediatorComprehensiveTests Console.WriteLine($"Average time per request: {avgTime:F2}ms"); } + /// + /// 测试Mediator和传统CQRS可以共存 + /// [Test] public async Task Mediator_And_Legacy_CQRS_Can_Coexist() {