From 029abbfc08ff00a9c46608ca4f0abadc94e13f8b Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Wed, 6 Sep 2023 12:14:20 +0200 Subject: [PATCH] Add tests for BTCheckAgentProperty --- tests/test_check_agent_property.h | 89 +++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tests/test_check_agent_property.h diff --git a/tests/test_check_agent_property.h b/tests/test_check_agent_property.h new file mode 100644 index 0000000..e35730e --- /dev/null +++ b/tests/test_check_agent_property.h @@ -0,0 +1,89 @@ +/** + * test_check_agent_property.h + * ============================================================================= + * Copyright 2021-2023 Serhii Snitsaruk + * + * Use of this source code is governed by an MIT-style + * license that can be found in the LICENSE file or at + * https://opensource.org/licenses/MIT. + * ============================================================================= + */ + +#ifndef TEST_CHECK_AGENT_PROPERTY_H +#define TEST_CHECK_AGENT_PROPERTY_H + +#include "core/variant/variant.h" +#include "limbo_test.h" + +#include "modules/limboai/blackboard/bb_param/bb_variant.h" +#include "modules/limboai/blackboard/blackboard.h" +#include "modules/limboai/bt/tasks/bt_task.h" +#include "modules/limboai/bt/tasks/scene/bt_check_agent_property.h" +#include "modules/limboai/util/limbo_utility.h" + +namespace TestCheckAgentProperty { + +// Check with m_correct, m_incorrect and m_invalid values using m_check_type. +#define TC_CHECK_AGENT_PROP(m_task, m_check_type, m_correct, m_incorrect, m_invalid) \ + m_task->set_check_type(m_check_type); \ + m_task->get_value()->set_saved_value(m_correct); \ + CHECK(m_task->execute(0.01666) == BTTask::SUCCESS); \ + m_task->get_value()->set_saved_value(m_incorrect); \ + CHECK(m_task->execute(0.01666) == BTTask::FAILURE); \ + m_task->get_value()->set_saved_value(m_invalid); \ + CHECK(m_task->execute(0.01666) == BTTask::FAILURE); + +TEST_CASE("[Modules][LimboAI] BTCheckAgentProperty") { + Ref cap = memnew(BTCheckAgentProperty); + Node *agent = memnew(Node); + Ref bb = memnew(Blackboard); + cap->initialize(agent, bb); + StringName agent_name = "SimpleNode"; + agent->set_name(agent_name); + + // * Defaults that should produce successful check: + cap->set_property("name"); + cap->set_check_type(LimboUtility::CHECK_EQUAL); + Ref value = memnew(BBVariant); + cap->set_value(value); + value->set_saved_value(agent_name); + REQUIRE(cap->execute(0.01666) == BTTask::SUCCESS); + + SUBCASE("When property is not set") { + cap->set_property(""); + ERR_PRINT_OFF; + CHECK(cap->execute(0.01666) == BTTask::FAILURE); + ERR_PRINT_ON; + } + SUBCASE("When property is not found") { + cap->set_property("not_found"); + ERR_PRINT_OFF; + CHECK(cap->execute(0.01666) == BTTask::FAILURE); + ERR_PRINT_ON; + } + SUBCASE("When value is not set") { + cap->set_value(nullptr); + + ERR_PRINT_OFF; + CHECK(cap->execute(0.01666) == BTTask::FAILURE); + ERR_PRINT_ON; + } + SUBCASE("With StringName") { + StringName other_name = "OtherName"; + TC_CHECK_AGENT_PROP(cap, LimboUtility::CHECK_EQUAL, agent_name, other_name, 123); + TC_CHECK_AGENT_PROP(cap, LimboUtility::CHECK_NOT_EQUAL, other_name, agent_name, 123); + } + SUBCASE("With integer") { + cap->set_property("process_priority"); + TC_CHECK_AGENT_PROP(cap, LimboUtility::CHECK_EQUAL, 0, -1, "invalid"); + TC_CHECK_AGENT_PROP(cap, LimboUtility::CHECK_GREATER_THAN_OR_EQUAL, 0, 1, "invalid"); + TC_CHECK_AGENT_PROP(cap, LimboUtility::CHECK_GREATER_THAN, -1, 1, "invalid"); + TC_CHECK_AGENT_PROP(cap, LimboUtility::CHECK_LESS_THAN_OR_EQUAL, 0, -1, "invalid"); + TC_CHECK_AGENT_PROP(cap, LimboUtility::CHECK_LESS_THAN, 1, 0, "invalid"); + TC_CHECK_AGENT_PROP(cap, LimboUtility::CHECK_NOT_EQUAL, 1, 0, "invalid"); + } +} + +} //namespace TestCheckAgentProperty + +#endif // TEST_CHECK_AGENT_PROPERTY_H