Add tests for BTCallMethod

This commit is contained in:
Serhii Snitsaruk 2023-09-05 11:01:20 +02:00
parent fe81b90dc1
commit 3952ba46cb
2 changed files with 89 additions and 0 deletions

View File

@ -12,10 +12,25 @@
#ifndef LIMBO_TEST_H #ifndef LIMBO_TEST_H
#define LIMBO_TEST_H #define LIMBO_TEST_H
#include "core/object/ref_counted.h"
#include "tests/test_macros.h" #include "tests/test_macros.h"
#include "modules/limboai/bt/tasks/bt_action.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 { class BTTestAction : public BTAction {
GDCLASS(BTTestAction, BTAction); GDCLASS(BTTestAction, BTAction);

74
tests/test_call_method.h Normal file
View File

@ -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<BTCallMethod> 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<Blackboard> bb = memnew(Blackboard);
Ref<BBNode> node_param = memnew(BBNode);
cm->set_node_param(node_param);
Ref<CallbackCounter> 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