Debian Codegraph @

tests/core/variant/test_callable.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_callable.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/object/class_db.h"
34 #include "core/object/object.h"
35
36 #include "tests/test_macros.h"
37
38 namespace TestCallable {
39
40 class TestClass : public Object {
41 GDCLASS(TestClass, Object);
42
43 protected:
44 static void _bind_methods() {
45 ClassDB::bind_method(D_METHOD("test_func_1", "foo", "bar"), &TestClass::test_func_1);
46 ClassDB::bind_method(D_METHOD("test_func_2", "foo", "bar", "baz"), &TestClass::test_func_2);
47 ClassDB::bind_static_method("TestClass", D_METHOD("test_func_5", "foo", "bar"), &TestClass::test_func_5);
48 ClassDB::bind_static_method("TestClass", D_METHOD("test_func_6", "foo", "bar", "baz"), &TestClass::test_func_6);
49
50 {
51 MethodInfo mi;
52 mi.name = "test_func_7";
53 mi.arguments.push_back(PropertyInfo(Variant::INT, "foo"));
54 mi.arguments.push_back(PropertyInfo(Variant::INT, "bar"));
55
56 ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func_7", &TestClass::test_func_7, mi, varray(), false);
57 }
58
59 {
60 MethodInfo mi;
61 mi.name = "test_func_8";
62 mi.arguments.push_back(PropertyInfo(Variant::INT, "foo"));
63 mi.arguments.push_back(PropertyInfo(Variant::INT, "bar"));
64 mi.arguments.push_back(PropertyInfo(Variant::INT, "baz"));
65
66 ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func_8", &TestClass::test_func_8, mi, varray(), false);
67 }
68 }
69
70 public:
71 void test_func_1(int p_foo, int p_bar) {}
72 void test_func_2(int p_foo, int p_bar, int p_baz) {}
73
74 int test_func_3(int p_foo, int p_bar) const { return 0; }
75 int test_func_4(int p_foo, int p_bar, int p_baz) const { return 0; }
76
77 static void test_func_5(int p_foo, int p_bar) {}
78 static void test_func_6(int p_foo, int p_bar, int p_baz) {}
79
80 void test_func_7(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {}
81 void test_func_8(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {}
82 };
83
84 TEST_CASE("[Callable] Argument count") {
85 TestClass *my_test = memnew(TestClass);
86
87 // Bound methods tests.
88
89 // Test simple methods.
90 Callable callable_1 = Callable(my_test, "test_func_1");
91 CHECK_EQ(callable_1.get_argument_count(), 2);
92 Callable callable_2 = Callable(my_test, "test_func_2");
93 CHECK_EQ(callable_2.get_argument_count(), 3);
94 Callable callable_3 = Callable(my_test, "test_func_5");
95 CHECK_EQ(callable_3.get_argument_count(), 2);
96 Callable callable_4 = Callable(my_test, "test_func_6");
97 CHECK_EQ(callable_4.get_argument_count(), 3);
98
99 // Test vararg methods.
100 Callable callable_vararg_1 = Callable(my_test, "test_func_7");
101 CHECK_MESSAGE(callable_vararg_1.get_argument_count() == 2, "vararg Callable should return the number of declared arguments");
102 Callable callable_vararg_2 = Callable(my_test, "test_func_8");
103 CHECK_MESSAGE(callable_vararg_2.get_argument_count() == 3, "vararg Callable should return the number of declared arguments");
104
105 // Callable MP tests.
106
107 // Test simple methods.
108 Callable callable_mp_1 = callable_mp(my_test, &TestClass::test_func_1);
109 CHECK_EQ(callable_mp_1.get_argument_count(), 2);
110 Callable callable_mp_2 = callable_mp(my_test, &TestClass::test_func_2);
111 CHECK_EQ(callable_mp_2.get_argument_count(), 3);
112 Callable callable_mp_3 = callable_mp(my_test, &TestClass::test_func_3);
113 CHECK_EQ(callable_mp_3.get_argument_count(), 2);
114 Callable callable_mp_4 = callable_mp(my_test, &TestClass::test_func_4);
115 CHECK_EQ(callable_mp_4.get_argument_count(), 3);
116
117 // Test static methods.
118 Callable callable_mp_static_1 = callable_mp_static(&TestClass::test_func_5);
119 CHECK_EQ(callable_mp_static_1.get_argument_count(), 2);
120 Callable callable_mp_static_2 = callable_mp_static(&TestClass::test_func_6);
121 CHECK_EQ(callable_mp_static_2.get_argument_count(), 3);
122
123 // Test bind.
124 Callable callable_mp_bind_1 = callable_mp_2.bind(1);
125 CHECK_MESSAGE(callable_mp_bind_1.get_argument_count() == 2, "bind should subtract from the argument count");
126 Callable callable_mp_bind_2 = callable_mp_2.bind(1, 2);
127 CHECK_MESSAGE(callable_mp_bind_2.get_argument_count() == 1, "bind should subtract from the argument count");
128
129 // Test unbind.
130 Callable callable_mp_unbind_1 = callable_mp_2.unbind(1);
131 CHECK_MESSAGE(callable_mp_unbind_1.get_argument_count() == 4, "unbind should add to the argument count");
132 Callable callable_mp_unbind_2 = callable_mp_2.unbind(2);
133 CHECK_MESSAGE(callable_mp_unbind_2.get_argument_count() == 5, "unbind should add to the argument count");
134
135 memdelete(my_test);
136 }
137
138 class TestBoundUnboundArgumentCount : public Object {
139 GDCLASS(TestBoundUnboundArgumentCount, Object);
140
141 protected:
142 static void _bind_methods() {
143 ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func", &TestBoundUnboundArgumentCount::test_func, MethodInfo("test_func"));
144 }
145
146 public:
147 Variant test_func(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
148 Array result;
149 result.resize(p_argcount);
150 for (int i = 0; i < p_argcount; i++) {
151 result[i] = *p_args[i];
152 }
153 return result;
154 }
155
156 static String get_output(const Callable &p_callable) {
157 Array effective_args = { 7, 8, 9 };
158 effective_args.resize(3 - p_callable.get_unbound_arguments_count());
159 effective_args.append_array(p_callable.get_bound_arguments());
160
161 return vformat(
162 "%d %d %s %s %s",
163 p_callable.get_unbound_arguments_count(),
164 p_callable.get_bound_arguments_count(),
165 p_callable.get_bound_arguments(),
166 p_callable.call(7, 8, 9),
167 effective_args);
168 }
169 };
170
171 TEST_CASE("[Callable] Bound and unbound argument count") {
172 String (*get_output)(const Callable &) = TestBoundUnboundArgumentCount::get_output;
173
174 TestBoundUnboundArgumentCount *test_instance = memnew(TestBoundUnboundArgumentCount);
175
176 Callable test_func = Callable(test_instance, "test_func");
177
178 CHECK(get_output(test_func) == "0 0 [] [7, 8, 9] [7, 8, 9]");
179 CHECK(get_output(test_func.bind(1, 2)) == "0 2 [1, 2] [7, 8, 9, 1, 2] [7, 8, 9, 1, 2]");
180 CHECK(get_output(test_func.bind(1, 2).unbind(1)) == "1 2 [1, 2] [7, 8, 1, 2] [7, 8, 1, 2]");
181 CHECK(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]");
182 CHECK(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]");
183
184 CHECK(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]");
185 CHECK(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]");
186 CHECK(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]");
187 CHECK(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]");
188
189 CHECK(get_output(test_func.unbind(1).unbind(1).unbind(1).bind(1, 2, 3)) == "0 0 [] [7, 8, 9] [7, 8, 9]");
190 CHECK(get_output(test_func.unbind(1).unbind(1).bind(1, 2, 3).unbind(1)) == "1 1 [1] [7, 8, 1] [7, 8, 1]");
191 CHECK(get_output(test_func.unbind(1).bind(1, 2, 3).unbind(1).unbind(1)) == "2 2 [1, 2] [7, 1, 2] [7, 1, 2]");
192 CHECK(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]");
193
194 memdelete(test_instance);
195 }
196
197 } // namespace TestCallable