Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/variant/test_callable.cpp
45997 views
1
/**************************************************************************/
2
/* test_callable.cpp */
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
#include "tests/test_macros.h"
32
33
TEST_FORCE_LINK(test_callable)
34
35
#include "core/object/callable_mp.h"
36
#include "core/object/class_db.h"
37
#include "core/object/object.h"
38
39
namespace TestCallable {
40
41
class TestClass : public Object {
42
GDCLASS(TestClass, Object);
43
44
protected:
45
static void _bind_methods() {
46
ClassDB::bind_method(D_METHOD("test_func_1", "foo", "bar"), &TestClass::test_func_1);
47
ClassDB::bind_method(D_METHOD("test_func_2", "foo", "bar", "baz"), &TestClass::test_func_2);
48
ClassDB::bind_static_method("TestClass", D_METHOD("test_func_5", "foo", "bar"), &TestClass::test_func_5);
49
ClassDB::bind_static_method("TestClass", D_METHOD("test_func_6", "foo", "bar", "baz"), &TestClass::test_func_6);
50
51
{
52
MethodInfo mi;
53
mi.name = "test_func_7";
54
mi.arguments.push_back(PropertyInfo(Variant::INT, "foo"));
55
mi.arguments.push_back(PropertyInfo(Variant::INT, "bar"));
56
57
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func_7", &TestClass::test_func_7, mi, varray(), false);
58
}
59
60
{
61
MethodInfo mi;
62
mi.name = "test_func_8";
63
mi.arguments.push_back(PropertyInfo(Variant::INT, "foo"));
64
mi.arguments.push_back(PropertyInfo(Variant::INT, "bar"));
65
mi.arguments.push_back(PropertyInfo(Variant::INT, "baz"));
66
67
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func_8", &TestClass::test_func_8, mi, varray(), false);
68
}
69
}
70
71
public:
72
void test_func_1(int p_foo, int p_bar) {}
73
void test_func_2(int p_foo, int p_bar, int p_baz) {}
74
75
int test_func_3(int p_foo, int p_bar) const { return 0; }
76
int test_func_4(int p_foo, int p_bar, int p_baz) const { return 0; }
77
78
static void test_func_5(int p_foo, int p_bar) {}
79
static void test_func_6(int p_foo, int p_bar, int p_baz) {}
80
81
void test_func_7(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {}
82
void test_func_8(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {}
83
};
84
85
TEST_CASE("[Callable] Argument count") {
86
TestClass *my_test = memnew(TestClass);
87
88
// Bound methods tests.
89
90
// Test simple methods.
91
Callable callable_1 = Callable(my_test, "test_func_1");
92
CHECK_EQ(callable_1.get_argument_count(), 2);
93
Callable callable_2 = Callable(my_test, "test_func_2");
94
CHECK_EQ(callable_2.get_argument_count(), 3);
95
Callable callable_3 = Callable(my_test, "test_func_5");
96
CHECK_EQ(callable_3.get_argument_count(), 2);
97
Callable callable_4 = Callable(my_test, "test_func_6");
98
CHECK_EQ(callable_4.get_argument_count(), 3);
99
100
// Test vararg methods.
101
Callable callable_vararg_1 = Callable(my_test, "test_func_7");
102
CHECK_MESSAGE(callable_vararg_1.get_argument_count() == 2, "vararg Callable should return the number of declared arguments");
103
Callable callable_vararg_2 = Callable(my_test, "test_func_8");
104
CHECK_MESSAGE(callable_vararg_2.get_argument_count() == 3, "vararg Callable should return the number of declared arguments");
105
106
// Callable MP tests.
107
108
// Test simple methods.
109
Callable callable_mp_1 = callable_mp(my_test, &TestClass::test_func_1);
110
CHECK_EQ(callable_mp_1.get_argument_count(), 2);
111
Callable callable_mp_2 = callable_mp(my_test, &TestClass::test_func_2);
112
CHECK_EQ(callable_mp_2.get_argument_count(), 3);
113
Callable callable_mp_3 = callable_mp(my_test, &TestClass::test_func_3);
114
CHECK_EQ(callable_mp_3.get_argument_count(), 2);
115
Callable callable_mp_4 = callable_mp(my_test, &TestClass::test_func_4);
116
CHECK_EQ(callable_mp_4.get_argument_count(), 3);
117
118
// Test static methods.
119
Callable callable_mp_static_1 = callable_mp_static(&TestClass::test_func_5);
120
CHECK_EQ(callable_mp_static_1.get_argument_count(), 2);
121
Callable callable_mp_static_2 = callable_mp_static(&TestClass::test_func_6);
122
CHECK_EQ(callable_mp_static_2.get_argument_count(), 3);
123
124
// Test bind.
125
Callable callable_mp_bind_1 = callable_mp_2.bind(1);
126
CHECK_MESSAGE(callable_mp_bind_1.get_argument_count() == 2, "bind should subtract from the argument count");
127
Callable callable_mp_bind_2 = callable_mp_2.bind(1, 2);
128
CHECK_MESSAGE(callable_mp_bind_2.get_argument_count() == 1, "bind should subtract from the argument count");
129
130
// Test unbind.
131
Callable callable_mp_unbind_1 = callable_mp_2.unbind(1);
132
CHECK_MESSAGE(callable_mp_unbind_1.get_argument_count() == 4, "unbind should add to the argument count");
133
Callable callable_mp_unbind_2 = callable_mp_2.unbind(2);
134
CHECK_MESSAGE(callable_mp_unbind_2.get_argument_count() == 5, "unbind should add to the argument count");
135
136
memdelete(my_test);
137
}
138
139
class TestBoundUnboundArgumentCount : public Object {
140
GDCLASS(TestBoundUnboundArgumentCount, Object);
141
142
protected:
143
static void _bind_methods() {
144
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "test_func", &TestBoundUnboundArgumentCount::test_func, MethodInfo("test_func"));
145
}
146
147
public:
148
Variant test_func(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
149
Array result;
150
result.resize(p_argcount);
151
for (int i = 0; i < p_argcount; i++) {
152
result[i] = *p_args[i];
153
}
154
return result;
155
}
156
157
static String get_output(const Callable &p_callable) {
158
Array effective_args = { 7, 8, 9 };
159
effective_args.resize(3 - p_callable.get_unbound_arguments_count());
160
effective_args.append_array(p_callable.get_bound_arguments());
161
162
return vformat(
163
"%d %d %s %s %s",
164
p_callable.get_unbound_arguments_count(),
165
p_callable.get_bound_arguments_count(),
166
p_callable.get_bound_arguments(),
167
p_callable.call(7, 8, 9),
168
effective_args);
169
}
170
};
171
172
TEST_CASE("[Callable] Bound and unbound argument count") {
173
String (*get_output)(const Callable &) = TestBoundUnboundArgumentCount::get_output;
174
175
TestBoundUnboundArgumentCount *test_instance = memnew(TestBoundUnboundArgumentCount);
176
177
Callable test_func = Callable(test_instance, "test_func");
178
179
CHECK(get_output(test_func) == "0 0 [] [7, 8, 9] [7, 8, 9]");
180
CHECK(get_output(test_func.bind(1, 2)) == "0 2 [1, 2] [7, 8, 9, 1, 2] [7, 8, 9, 1, 2]");
181
CHECK(get_output(test_func.bind(1, 2).unbind(1)) == "1 2 [1, 2] [7, 8, 1, 2] [7, 8, 1, 2]");
182
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]");
183
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]");
184
185
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]");
186
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]");
187
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]");
188
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]");
189
190
CHECK(get_output(test_func.unbind(1).unbind(1).unbind(1).bind(1, 2, 3)) == "0 0 [] [7, 8, 9] [7, 8, 9]");
191
CHECK(get_output(test_func.unbind(1).unbind(1).bind(1, 2, 3).unbind(1)) == "1 1 [1] [7, 8, 1] [7, 8, 1]");
192
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]");
193
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]");
194
195
memdelete(test_instance);
196
}
197
198
} // namespace TestCallable
199
200