Path: blob/master/tests/core/variant/test_callable.h
10278 views
/**************************************************************************/1/* test_callable.h */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#pragma once3132#include "core/object/class_db.h"33#include "core/object/object.h"3435#include "tests/test_macros.h"3637namespace TestCallable {3839class TestClass : public Object {40GDCLASS(TestClass, Object);4142protected:43static void _bind_methods() {44ClassDB::bind_method(D_METHOD("test_func_1", "foo", "bar"), &TestClass::test_func_1);45ClassDB::bind_method(D_METHOD("test_func_2", "foo", "bar", "baz"), &TestClass::test_func_2);46ClassDB::bind_static_method("TestClass", D_METHOD("test_func_5", "foo", "bar"), &TestClass::test_func_5);47ClassDB::bind_static_method("TestClass", D_METHOD("test_func_6", "foo", "bar", "baz"), &TestClass::test_func_6);4849{50MethodInfo mi;51mi.name = "test_func_7";52mi.arguments.push_back(PropertyInfo(Variant::INT, "foo"));53mi.arguments.push_back(PropertyInfo(Variant::INT, "bar"));5455ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func_7", &TestClass::test_func_7, mi, varray(), false);56}5758{59MethodInfo mi;60mi.name = "test_func_8";61mi.arguments.push_back(PropertyInfo(Variant::INT, "foo"));62mi.arguments.push_back(PropertyInfo(Variant::INT, "bar"));63mi.arguments.push_back(PropertyInfo(Variant::INT, "baz"));6465ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func_8", &TestClass::test_func_8, mi, varray(), false);66}67}6869public:70void test_func_1(int p_foo, int p_bar) {}71void test_func_2(int p_foo, int p_bar, int p_baz) {}7273int test_func_3(int p_foo, int p_bar) const { return 0; }74int test_func_4(int p_foo, int p_bar, int p_baz) const { return 0; }7576static void test_func_5(int p_foo, int p_bar) {}77static void test_func_6(int p_foo, int p_bar, int p_baz) {}7879void test_func_7(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {}80void test_func_8(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {}81};8283TEST_CASE("[Callable] Argument count") {84TestClass *my_test = memnew(TestClass);8586// Bound methods tests.8788// Test simple methods.89Callable callable_1 = Callable(my_test, "test_func_1");90CHECK_EQ(callable_1.get_argument_count(), 2);91Callable callable_2 = Callable(my_test, "test_func_2");92CHECK_EQ(callable_2.get_argument_count(), 3);93Callable callable_3 = Callable(my_test, "test_func_5");94CHECK_EQ(callable_3.get_argument_count(), 2);95Callable callable_4 = Callable(my_test, "test_func_6");96CHECK_EQ(callable_4.get_argument_count(), 3);9798// Test vararg methods.99Callable callable_vararg_1 = Callable(my_test, "test_func_7");100CHECK_MESSAGE(callable_vararg_1.get_argument_count() == 2, "vararg Callable should return the number of declared arguments");101Callable callable_vararg_2 = Callable(my_test, "test_func_8");102CHECK_MESSAGE(callable_vararg_2.get_argument_count() == 3, "vararg Callable should return the number of declared arguments");103104// Callable MP tests.105106// Test simple methods.107Callable callable_mp_1 = callable_mp(my_test, &TestClass::test_func_1);108CHECK_EQ(callable_mp_1.get_argument_count(), 2);109Callable callable_mp_2 = callable_mp(my_test, &TestClass::test_func_2);110CHECK_EQ(callable_mp_2.get_argument_count(), 3);111Callable callable_mp_3 = callable_mp(my_test, &TestClass::test_func_3);112CHECK_EQ(callable_mp_3.get_argument_count(), 2);113Callable callable_mp_4 = callable_mp(my_test, &TestClass::test_func_4);114CHECK_EQ(callable_mp_4.get_argument_count(), 3);115116// Test static methods.117Callable callable_mp_static_1 = callable_mp_static(&TestClass::test_func_5);118CHECK_EQ(callable_mp_static_1.get_argument_count(), 2);119Callable callable_mp_static_2 = callable_mp_static(&TestClass::test_func_6);120CHECK_EQ(callable_mp_static_2.get_argument_count(), 3);121122// Test bind.123Callable callable_mp_bind_1 = callable_mp_2.bind(1);124CHECK_MESSAGE(callable_mp_bind_1.get_argument_count() == 2, "bind should subtract from the argument count");125Callable callable_mp_bind_2 = callable_mp_2.bind(1, 2);126CHECK_MESSAGE(callable_mp_bind_2.get_argument_count() == 1, "bind should subtract from the argument count");127128// Test unbind.129Callable callable_mp_unbind_1 = callable_mp_2.unbind(1);130CHECK_MESSAGE(callable_mp_unbind_1.get_argument_count() == 4, "unbind should add to the argument count");131Callable callable_mp_unbind_2 = callable_mp_2.unbind(2);132CHECK_MESSAGE(callable_mp_unbind_2.get_argument_count() == 5, "unbind should add to the argument count");133134memdelete(my_test);135}136137class TestBoundUnboundArgumentCount : public Object {138GDCLASS(TestBoundUnboundArgumentCount, Object);139140protected:141static void _bind_methods() {142ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func", &TestBoundUnboundArgumentCount::test_func, MethodInfo("test_func"));143}144145public:146Variant test_func(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {147Array result;148result.resize(p_argcount);149for (int i = 0; i < p_argcount; i++) {150result[i] = *p_args[i];151}152return result;153}154155static String get_output(const Callable &p_callable) {156Array effective_args = { 7, 8, 9 };157effective_args.resize(3 - p_callable.get_unbound_arguments_count());158effective_args.append_array(p_callable.get_bound_arguments());159160return vformat(161"%d %d %s %s %s",162p_callable.get_unbound_arguments_count(),163p_callable.get_bound_arguments_count(),164p_callable.get_bound_arguments(),165p_callable.call(7, 8, 9),166effective_args);167}168};169170TEST_CASE("[Callable] Bound and unbound argument count") {171String (*get_output)(const Callable &) = TestBoundUnboundArgumentCount::get_output;172173TestBoundUnboundArgumentCount *test_instance = memnew(TestBoundUnboundArgumentCount);174175Callable test_func = Callable(test_instance, "test_func");176177CHECK(get_output(test_func) == "0 0 [] [7, 8, 9] [7, 8, 9]");178CHECK(get_output(test_func.bind(1, 2)) == "0 2 [1, 2] [7, 8, 9, 1, 2] [7, 8, 9, 1, 2]");179CHECK(get_output(test_func.bind(1, 2).unbind(1)) == "1 2 [1, 2] [7, 8, 1, 2] [7, 8, 1, 2]");180CHECK(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]");181CHECK(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]");182183CHECK(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]");184CHECK(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]");185CHECK(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]");186CHECK(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]");187188CHECK(get_output(test_func.unbind(1).unbind(1).unbind(1).bind(1, 2, 3)) == "0 0 [] [7, 8, 9] [7, 8, 9]");189CHECK(get_output(test_func.unbind(1).unbind(1).bind(1, 2, 3).unbind(1)) == "1 1 [1] [7, 8, 1] [7, 8, 1]");190CHECK(get_output(test_func.unbind(1).bind(1, 2, 3).unbind(1).unbind(1)) == "2 2 [1, 2] [7, 1, 2] [7, 1, 2]");191CHECK(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]");192193memdelete(test_instance);194}195196} // namespace TestCallable197198199