Path: blob/master/tests/core/variant/test_callable.cpp
45997 views
/**************************************************************************/1/* test_callable.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "tests/test_macros.h"3132TEST_FORCE_LINK(test_callable)3334#include "core/object/callable_mp.h"35#include "core/object/class_db.h"36#include "core/object/object.h"3738namespace TestCallable {3940class TestClass : public Object {41GDCLASS(TestClass, Object);4243protected:44static void _bind_methods() {45ClassDB::bind_method(D_METHOD("test_func_1", "foo", "bar"), &TestClass::test_func_1);46ClassDB::bind_method(D_METHOD("test_func_2", "foo", "bar", "baz"), &TestClass::test_func_2);47ClassDB::bind_static_method("TestClass", D_METHOD("test_func_5", "foo", "bar"), &TestClass::test_func_5);48ClassDB::bind_static_method("TestClass", D_METHOD("test_func_6", "foo", "bar", "baz"), &TestClass::test_func_6);4950{51MethodInfo mi;52mi.name = "test_func_7";53mi.arguments.push_back(PropertyInfo(Variant::INT, "foo"));54mi.arguments.push_back(PropertyInfo(Variant::INT, "bar"));5556ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func_7", &TestClass::test_func_7, mi, varray(), false);57}5859{60MethodInfo mi;61mi.name = "test_func_8";62mi.arguments.push_back(PropertyInfo(Variant::INT, "foo"));63mi.arguments.push_back(PropertyInfo(Variant::INT, "bar"));64mi.arguments.push_back(PropertyInfo(Variant::INT, "baz"));6566ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func_8", &TestClass::test_func_8, mi, varray(), false);67}68}6970public:71void test_func_1(int p_foo, int p_bar) {}72void test_func_2(int p_foo, int p_bar, int p_baz) {}7374int test_func_3(int p_foo, int p_bar) const { return 0; }75int test_func_4(int p_foo, int p_bar, int p_baz) const { return 0; }7677static void test_func_5(int p_foo, int p_bar) {}78static void test_func_6(int p_foo, int p_bar, int p_baz) {}7980void test_func_7(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {}81void test_func_8(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {}82};8384TEST_CASE("[Callable] Argument count") {85TestClass *my_test = memnew(TestClass);8687// Bound methods tests.8889// Test simple methods.90Callable callable_1 = Callable(my_test, "test_func_1");91CHECK_EQ(callable_1.get_argument_count(), 2);92Callable callable_2 = Callable(my_test, "test_func_2");93CHECK_EQ(callable_2.get_argument_count(), 3);94Callable callable_3 = Callable(my_test, "test_func_5");95CHECK_EQ(callable_3.get_argument_count(), 2);96Callable callable_4 = Callable(my_test, "test_func_6");97CHECK_EQ(callable_4.get_argument_count(), 3);9899// Test vararg methods.100Callable callable_vararg_1 = Callable(my_test, "test_func_7");101CHECK_MESSAGE(callable_vararg_1.get_argument_count() == 2, "vararg Callable should return the number of declared arguments");102Callable callable_vararg_2 = Callable(my_test, "test_func_8");103CHECK_MESSAGE(callable_vararg_2.get_argument_count() == 3, "vararg Callable should return the number of declared arguments");104105// Callable MP tests.106107// Test simple methods.108Callable callable_mp_1 = callable_mp(my_test, &TestClass::test_func_1);109CHECK_EQ(callable_mp_1.get_argument_count(), 2);110Callable callable_mp_2 = callable_mp(my_test, &TestClass::test_func_2);111CHECK_EQ(callable_mp_2.get_argument_count(), 3);112Callable callable_mp_3 = callable_mp(my_test, &TestClass::test_func_3);113CHECK_EQ(callable_mp_3.get_argument_count(), 2);114Callable callable_mp_4 = callable_mp(my_test, &TestClass::test_func_4);115CHECK_EQ(callable_mp_4.get_argument_count(), 3);116117// Test static methods.118Callable callable_mp_static_1 = callable_mp_static(&TestClass::test_func_5);119CHECK_EQ(callable_mp_static_1.get_argument_count(), 2);120Callable callable_mp_static_2 = callable_mp_static(&TestClass::test_func_6);121CHECK_EQ(callable_mp_static_2.get_argument_count(), 3);122123// Test bind.124Callable callable_mp_bind_1 = callable_mp_2.bind(1);125CHECK_MESSAGE(callable_mp_bind_1.get_argument_count() == 2, "bind should subtract from the argument count");126Callable callable_mp_bind_2 = callable_mp_2.bind(1, 2);127CHECK_MESSAGE(callable_mp_bind_2.get_argument_count() == 1, "bind should subtract from the argument count");128129// Test unbind.130Callable callable_mp_unbind_1 = callable_mp_2.unbind(1);131CHECK_MESSAGE(callable_mp_unbind_1.get_argument_count() == 4, "unbind should add to the argument count");132Callable callable_mp_unbind_2 = callable_mp_2.unbind(2);133CHECK_MESSAGE(callable_mp_unbind_2.get_argument_count() == 5, "unbind should add to the argument count");134135memdelete(my_test);136}137138class TestBoundUnboundArgumentCount : public Object {139GDCLASS(TestBoundUnboundArgumentCount, Object);140141protected:142static void _bind_methods() {143ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func", &TestBoundUnboundArgumentCount::test_func, MethodInfo("test_func"));144}145146public:147Variant test_func(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {148Array result;149result.resize(p_argcount);150for (int i = 0; i < p_argcount; i++) {151result[i] = *p_args[i];152}153return result;154}155156static String get_output(const Callable &p_callable) {157Array effective_args = { 7, 8, 9 };158effective_args.resize(3 - p_callable.get_unbound_arguments_count());159effective_args.append_array(p_callable.get_bound_arguments());160161return vformat(162"%d %d %s %s %s",163p_callable.get_unbound_arguments_count(),164p_callable.get_bound_arguments_count(),165p_callable.get_bound_arguments(),166p_callable.call(7, 8, 9),167effective_args);168}169};170171TEST_CASE("[Callable] Bound and unbound argument count") {172String (*get_output)(const Callable &) = TestBoundUnboundArgumentCount::get_output;173174TestBoundUnboundArgumentCount *test_instance = memnew(TestBoundUnboundArgumentCount);175176Callable test_func = Callable(test_instance, "test_func");177178CHECK(get_output(test_func) == "0 0 [] [7, 8, 9] [7, 8, 9]");179CHECK(get_output(test_func.bind(1, 2)) == "0 2 [1, 2] [7, 8, 9, 1, 2] [7, 8, 9, 1, 2]");180CHECK(get_output(test_func.bind(1, 2).unbind(1)) == "1 2 [1, 2] [7, 8, 1, 2] [7, 8, 1, 2]");181CHECK(get_output(test_func.bind(1, 2).unbind(1).bind(3, 4)) == "0 3 [3, 1, 2] [7, 8, 9, 3, 1, 2] [7, 8, 9, 3, 1, 2]");182CHECK(get_output(test_func.bind(1, 2).unbind(1).bind(3, 4).unbind(1)) == "1 3 [3, 1, 2] [7, 8, 3, 1, 2] [7, 8, 3, 1, 2]");183184CHECK(get_output(test_func.bind(1).bind(2).bind(3).unbind(1)) == "1 3 [3, 2, 1] [7, 8, 3, 2, 1] [7, 8, 3, 2, 1]");185CHECK(get_output(test_func.bind(1).bind(2).unbind(1).bind(3)) == "0 2 [2, 1] [7, 8, 9, 2, 1] [7, 8, 9, 2, 1]");186CHECK(get_output(test_func.bind(1).unbind(1).bind(2).bind(3)) == "0 2 [3, 1] [7, 8, 9, 3, 1] [7, 8, 9, 3, 1]");187CHECK(get_output(test_func.unbind(1).bind(1).bind(2).bind(3)) == "0 2 [3, 2] [7, 8, 9, 3, 2] [7, 8, 9, 3, 2]");188189CHECK(get_output(test_func.unbind(1).unbind(1).unbind(1).bind(1, 2, 3)) == "0 0 [] [7, 8, 9] [7, 8, 9]");190CHECK(get_output(test_func.unbind(1).unbind(1).bind(1, 2, 3).unbind(1)) == "1 1 [1] [7, 8, 1] [7, 8, 1]");191CHECK(get_output(test_func.unbind(1).bind(1, 2, 3).unbind(1).unbind(1)) == "2 2 [1, 2] [7, 1, 2] [7, 1, 2]");192CHECK(get_output(test_func.bind(1, 2, 3).unbind(1).unbind(1).unbind(1)) == "3 3 [1, 2, 3] [1, 2, 3] [1, 2, 3]");193194memdelete(test_instance);195}196197} // namespace TestCallable198199200