From 3952ba46cb60182e9f70620220e37399bc0bcde0 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Tue, 5 Sep 2023 11:01:20 +0200 Subject: [PATCH] Add tests for BTCallMethod --- tests/limbo_test.h | 15 ++++++++ tests/test_call_method.h | 74 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 tests/test_call_method.h diff --git a/tests/limbo_test.h b/tests/limbo_test.h index 20b354c..cbca952 100644 --- a/tests/limbo_test.h +++ b/tests/limbo_test.h @@ -12,10 +12,25 @@ #ifndef LIMBO_TEST_H #define LIMBO_TEST_H +#include "core/object/ref_counted.h" #include "tests/test_macros.h" #include "modules/limboai/bt/tasks/bt_action.h" +class CallbackCounter : public RefCounted { + GDCLASS(CallbackCounter, RefCounted); + +public: + int num_callbacks = 0; + + void callback() { num_callbacks += 1; } + +protected: + static void _bind_methods() { + ClassDB::bind_method(D_METHOD("callback"), &CallbackCounter::callback); + } +}; + class BTTestAction : public BTAction { GDCLASS(BTTestAction, BTAction); diff --git a/tests/test_call_method.h b/tests/test_call_method.h new file mode 100644 index 0000000..ea191cd --- /dev/null +++ b/tests/test_call_method.h @@ -0,0 +1,74 @@ +/** + * test_call_method.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_CALL_METHOD_H +#define TEST_CALL_METHOD_H + +#include "limbo_test.h" + +#include "modules/limboai/blackboard/bb_param/bb_node.h" +#include "modules/limboai/blackboard/blackboard.h" +#include "modules/limboai/bt/tasks/bt_task.h" +#include "modules/limboai/bt/tasks/scene/bt_call_method.h" + +#include "core/os/memory.h" + +namespace TestCallMethod { + +TEST_CASE("[Modules][LimboAI] BTCallMethod") { + Ref cm = memnew(BTCallMethod); + + SUBCASE("When node parameter is null") { + cm->set_node_param(nullptr); + cm->set_method("test"); + ERR_PRINT_OFF; + CHECK(cm->execute(0.01666) == BTTask::FAILURE); + ERR_PRINT_ON; + } + + SUBCASE("With object on the blackboard") { + Node *dummy = memnew(Node); + Ref bb = memnew(Blackboard); + + Ref node_param = memnew(BBNode); + cm->set_node_param(node_param); + Ref callback_counter = memnew(CallbackCounter); + bb->set_var("object", callback_counter); + node_param->set_value_source(BBParam::BLACKBOARD_VAR); + node_param->set_variable("object"); + cm->set_method("callback"); + + cm->initialize(dummy, bb); + + SUBCASE("When method is empty") { + cm->set_method(""); + ERR_PRINT_OFF; + CHECK(cm->execute(0.01666) == BTTask::FAILURE); + ERR_PRINT_ON; + } + SUBCASE("When method doesn't exist") { + cm->set_method("not_found"); + ERR_PRINT_OFF; + CHECK(cm->execute(0.01666) == BTTask::FAILURE); + ERR_PRINT_ON; + } + SUBCASE("When method exists") { + CHECK(cm->execute(0.01666) == BTTask::SUCCESS); + CHECK(callback_counter->num_callbacks == 1); + } + + memdelete(dummy); + } +} + +} //namespace TestCallMethod + +#endif // TEST_CALL_METHOD_H