From 85eda3c804ff04528e45b667f84a4ddf2a9b1b2e Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Fri, 1 Nov 2024 14:59:10 +0100 Subject: [PATCH] Tests for per-transition guards --- tests/test_hsm.h | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/test_hsm.h b/tests/test_hsm.h index 83388d2..741c6a2 100644 --- a/tests/test_hsm.h +++ b/tests/test_hsm.h @@ -215,7 +215,7 @@ TEST_CASE("[Modules][LimboAI] HSM") { CHECK(beta_updates->num_callbacks == 0); CHECK(beta_exits->num_callbacks == 1); // * exited } - SUBCASE("Test transition with guard") { + SUBCASE("Test transition with state-wide guard") { Ref guard = memnew(TestGuard); state_beta->set_guard(callable_mp(guard.ptr(), &TestGuard::can_enter)); @@ -234,6 +234,25 @@ TEST_CASE("[Modules][LimboAI] HSM") { CHECK(beta_entries->num_callbacks == 0); } } + SUBCASE("Test transition with transition-scoped guard") { + Ref guard = memnew(TestGuard); + hsm->add_transition(state_alpha, state_beta, "guarded_transition", callable_mp(guard.ptr(), &TestGuard::can_enter)); + + SUBCASE("When entry is permitted") { + guard->permitted_to_enter = true; + hsm->dispatch("guarded_transition"); + CHECK(hsm->get_active_state() == state_beta); + CHECK(alpha_exits->num_callbacks == 1); + CHECK(beta_entries->num_callbacks == 1); + } + SUBCASE("When entry is not permitted") { + guard->permitted_to_enter = false; + hsm->dispatch("guarded_transition"); + CHECK(hsm->get_active_state() == state_alpha); + CHECK(alpha_exits->num_callbacks == 0); + CHECK(beta_entries->num_callbacks == 0); + } + } SUBCASE("When there is no transition for given event") { hsm->dispatch("not_found"); CHECK(alpha_exits->num_callbacks == 0);