mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-25 13:33:28 +08:00
test(coroutine): 为 WaitWhile 添加断言以验证谓词函数行为
- 修改测试逻辑以跟踪谓词函数的调用次数 - 添加对 IsDone 属性访问时谓词评估的验证 - 增加对条件变化时谓词重新评估的检查 - 添加相应的断言确保谓词按预期工作
This commit is contained in:
parent
bb95f738a8
commit
684d4601d0
@ -221,13 +221,28 @@ public class YieldInstructionTests
|
|||||||
[Test]
|
[Test]
|
||||||
public void WaitWhile_Should_Use_Predicate_Function()
|
public void WaitWhile_Should_Use_Predicate_Function()
|
||||||
{
|
{
|
||||||
var continueWaiting = true;
|
var callCount = 0;
|
||||||
var wait = new WaitWhile(() => continueWaiting);
|
var shouldContinue = true;
|
||||||
|
|
||||||
|
var wait = new WaitWhile(() =>
|
||||||
|
{
|
||||||
|
callCount++;
|
||||||
|
return shouldContinue;
|
||||||
|
});
|
||||||
|
|
||||||
|
// 访问 IsDone,会触发 predicate
|
||||||
Assert.That(wait.IsDone, Is.False);
|
Assert.That(wait.IsDone, Is.False);
|
||||||
|
Assert.That(callCount, Is.GreaterThan(0),
|
||||||
|
"Predicate should be evaluated when checking IsDone");
|
||||||
|
|
||||||
continueWaiting = false;
|
var previousCount = callCount;
|
||||||
|
|
||||||
|
shouldContinue = false;
|
||||||
|
|
||||||
|
// 再次访问 IsDone,应再次调用 predicate,并改变结果
|
||||||
Assert.That(wait.IsDone, Is.True);
|
Assert.That(wait.IsDone, Is.True);
|
||||||
|
Assert.That(callCount, Is.GreaterThan(previousCount),
|
||||||
|
"Predicate should be re-evaluated when condition changes");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -387,12 +402,21 @@ public class YieldInstructionTests
|
|||||||
[Test]
|
[Test]
|
||||||
public void WaitWhile_Should_Evaluate_Condition_Immediately()
|
public void WaitWhile_Should_Evaluate_Condition_Immediately()
|
||||||
{
|
{
|
||||||
|
var callCount = 0;
|
||||||
var continueWaiting = true;
|
var continueWaiting = true;
|
||||||
var wait = new WaitWhile(() => continueWaiting);
|
var wait = new WaitWhile(() =>
|
||||||
|
{
|
||||||
|
callCount++;
|
||||||
|
return continueWaiting;
|
||||||
|
});
|
||||||
|
|
||||||
|
// 初始检查,确保谓词被调用
|
||||||
Assert.That(wait.IsDone, Is.False);
|
Assert.That(wait.IsDone, Is.False);
|
||||||
|
Assert.That(callCount, Is.EqualTo(1), "Predicate should be called once initially");
|
||||||
|
|
||||||
|
// 改变条件并验证谓词再次被调用
|
||||||
continueWaiting = false;
|
continueWaiting = false;
|
||||||
Assert.That(wait.IsDone, Is.True);
|
Assert.That(wait.IsDone, Is.True);
|
||||||
|
Assert.That(callCount, Is.EqualTo(2), "Predicate should be called again after condition change");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user