limboai/tests/test_call_method.h

121 lines
3.5 KiB
C
Raw Permalink Normal View History

2023-09-05 09:01:20 +00:00
/**
* test_call_method.h
* =============================================================================
2024-03-21 20:38:57 +00:00
* Copyright 2021-2024 Serhii Snitsaruk
2023-09-05 09:01:20 +00:00
*
* 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"
2023-09-20 08:40:16 +00:00
#include "modules/limboai/bt/tasks/utility/bt_call_method.h"
2023-09-05 09:01:20 +00:00
#include "core/os/memory.h"
#include "core/variant/array.h"
2023-09-05 09:01:20 +00:00
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, dummy);
2023-09-05 09:01:20 +00:00
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);
}
SUBCASE("With arguments") {
cm->set_method("callback_delta");
SUBCASE("Should fail with 0 arguments") {
cm->set_include_delta(false);
2024-01-16 09:46:42 +00:00
cm->set_args(TypedArray<BBVariant>());
ERR_PRINT_OFF;
CHECK(cm->execute(0.01666) == BTTask::FAILURE);
ERR_PRINT_ON;
CHECK(callback_counter->num_callbacks == 0);
}
SUBCASE("Should fail with too many arguments") {
cm->set_include_delta(true);
2024-01-16 09:46:42 +00:00
TypedArray<BBVariant> args;
args.push_back(memnew(BBVariant(0.2)));
cm->set_args(args);
ERR_PRINT_OFF;
CHECK(cm->execute(0.01666) == BTTask::FAILURE);
ERR_PRINT_ON;
CHECK(callback_counter->num_callbacks == 0);
}
SUBCASE("Should fail with a wrong type arg") {
cm->set_include_delta(false);
2024-01-16 09:46:42 +00:00
TypedArray<BBVariant> args;
args.push_back(memnew(BBVariant("wrong data type")));
cm->set_args(args);
ERR_PRINT_OFF;
CHECK(cm->execute(0.01666) == BTTask::FAILURE);
ERR_PRINT_ON;
CHECK(callback_counter->num_callbacks == 1);
}
SUBCASE("Should succeed with delta included") {
cm->set_include_delta(true);
CHECK(cm->execute(0.01666) == BTTask::SUCCESS);
CHECK(callback_counter->num_callbacks == 1);
}
SUBCASE("Should succeed with one float arg") {
cm->set_include_delta(false);
2024-01-16 09:46:42 +00:00
TypedArray<BBVariant> args;
args.push_back(memnew(BBVariant(0.2)));
cm->set_args(args);
CHECK(cm->execute(0.01666) == BTTask::SUCCESS);
CHECK(callback_counter->num_callbacks == 1);
}
}
2023-09-05 09:01:20 +00:00
memdelete(dummy);
}
}
} //namespace TestCallMethod
#endif // TEST_CALL_METHOD_H