From 3e8a15883563d1ea85017942056615e2f45de0a4 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Sun, 10 Sep 2023 10:54:59 +0200 Subject: [PATCH] Add tests for BBNode param --- tests/test_bb_param.h | 54 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/test_bb_param.h b/tests/test_bb_param.h index 17c38f6..853203a 100644 --- a/tests/test_bb_param.h +++ b/tests/test_bb_param.h @@ -12,7 +12,9 @@ #ifndef TEST_BB_PARAM_H #define TEST_BB_PARAM_H +#include "core/object/ref_counted.h" #include "core/string/node_path.h" +#include "core/variant/variant.h" #include "limbo_test.h" #include "modules/limboai/blackboard/bb_param/bb_node.h" @@ -66,6 +68,58 @@ TEST_CASE("[Modules][LimboAI] BBParam") { memdelete(dummy); } +TEST_CASE("[Modules][LimboAI] BBNode") { + Ref param = memnew(BBNode); + Node *dummy = memnew(Node); + Ref bb = memnew(Blackboard); + + Node *other = memnew(Node); + other->set_name("Other"); + dummy->add_child(other); + + SUBCASE("With a valid path") { + param->set_value_source(BBParam::SAVED_VALUE); + param->set_saved_value(NodePath("./Other")); + CHECK(param->get_value(dummy, bb).get_type() == Variant::Type::OBJECT); + CHECK(param->get_value(dummy, bb) == Variant(other)); + } + SUBCASE("With an invalid path") { + param->set_value_source(BBParam::SAVED_VALUE); + param->set_saved_value(NodePath("./SomeOther")); + ERR_PRINT_OFF; + CHECK(param->get_value(dummy, bb, Variant()).is_null()); + ERR_PRINT_ON; + } + SUBCASE("With an object on the blackboard") { + param->set_value_source(BBParam::BLACKBOARD_VAR); + param->set_variable("test_var"); + + SUBCASE("When variable exists") { + bb->set_var("test_var", other); + CHECK(param->get_value(dummy, bb).get_type() == Variant::Type::OBJECT); + CHECK(param->get_value(dummy, bb) == Variant(other)); + } + SUBCASE("When variable doesn't exist") { + CHECK(param->get_value(dummy, bb, Variant()).is_null()); + } + SUBCASE("When variable has wrong type") { + bb->set_var("test_var", 123); + ERR_PRINT_OFF; + CHECK(param->get_value(dummy, bb, Variant()).is_null()); + ERR_PRINT_ON; + } + SUBCASE("When variable is an object") { + // * Note: We allow also fetching objects on the blackboard. + Ref some_other = memnew(RefCounted); + bb->set_var("test_var", some_other); + CHECK(param->get_value(dummy, bb) == some_other); + } + } + + memdelete(other); + memdelete(dummy); +} + } //namespace TestBBParam #endif // TEST_BB_PARAM_H