Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/object/test_method_bind.h
10278 views
1
/**************************************************************************/
2
/* test_method_bind.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
35
#include "tests/test_macros.h"
36
37
namespace TestMethodBind {
38
39
class MethodBindTester : public Object {
40
GDCLASS(MethodBindTester, Object);
41
42
public:
43
enum Test {
44
TEST_METHOD,
45
TEST_METHOD_ARGS,
46
TEST_METHODC,
47
TEST_METHODC_ARGS,
48
TEST_METHODR,
49
TEST_METHODR_ARGS,
50
TEST_METHODRC,
51
TEST_METHODRC_ARGS,
52
TEST_METHOD_DEFARGS,
53
TEST_METHOD_OBJECT_CAST,
54
TEST_MAX
55
};
56
57
class ObjectSubclass : public Object {
58
GDSOFTCLASS(ObjectSubclass, Object);
59
60
public:
61
int value = 1;
62
};
63
64
int test_num = 0;
65
66
bool test_valid[TEST_MAX];
67
68
void test_method() {
69
test_valid[TEST_METHOD] = true;
70
}
71
72
void test_method_args(int p_arg) {
73
test_valid[TEST_METHOD_ARGS] = p_arg == test_num;
74
}
75
76
void test_methodc() {
77
test_valid[TEST_METHODC] = true;
78
}
79
80
void test_methodc_args(int p_arg) {
81
test_valid[TEST_METHODC_ARGS] = p_arg == test_num;
82
}
83
84
int test_methodr() {
85
test_valid[TEST_METHODR] = true; //temporary
86
return test_num;
87
}
88
89
int test_methodr_args(int p_arg) {
90
test_valid[TEST_METHODR_ARGS] = true; //temporary
91
return p_arg;
92
}
93
94
int test_methodrc() {
95
test_valid[TEST_METHODRC] = true; //temporary
96
return test_num;
97
}
98
99
int test_methodrc_args(int p_arg) {
100
test_valid[TEST_METHODRC_ARGS] = true; //temporary
101
return p_arg;
102
}
103
104
void test_method_default_args(int p_arg1, int p_arg2, int p_arg3, int p_arg4, int p_arg5) {
105
test_valid[TEST_METHOD_DEFARGS] = p_arg1 == 1 && p_arg2 == 2 && p_arg3 == 3 && p_arg4 == 4 && p_arg5 == 5; //temporary
106
}
107
108
void test_method_object_cast(ObjectSubclass *p_object) {
109
test_valid[TEST_METHOD_OBJECT_CAST] = p_object->value == 1;
110
}
111
112
static void _bind_methods() {
113
ClassDB::bind_method(D_METHOD("test_method"), &MethodBindTester::test_method);
114
ClassDB::bind_method(D_METHOD("test_method_args"), &MethodBindTester::test_method_args);
115
ClassDB::bind_method(D_METHOD("test_methodc"), &MethodBindTester::test_methodc);
116
ClassDB::bind_method(D_METHOD("test_methodc_args"), &MethodBindTester::test_methodc_args);
117
ClassDB::bind_method(D_METHOD("test_methodr"), &MethodBindTester::test_methodr);
118
ClassDB::bind_method(D_METHOD("test_methodr_args"), &MethodBindTester::test_methodr_args);
119
ClassDB::bind_method(D_METHOD("test_methodrc"), &MethodBindTester::test_methodrc);
120
ClassDB::bind_method(D_METHOD("test_methodrc_args"), &MethodBindTester::test_methodrc_args);
121
ClassDB::bind_method(D_METHOD("test_method_default_args"), &MethodBindTester::test_method_default_args, DEFVAL(9) /* wrong on purpose */, DEFVAL(4), DEFVAL(5));
122
ClassDB::bind_method(D_METHOD("test_method_object_cast", "object"), &MethodBindTester::test_method_object_cast);
123
}
124
125
virtual void run_tests() {
126
for (int i = 0; i < TEST_MAX; i++) {
127
test_valid[i] = false;
128
}
129
//regular
130
test_num = Math::rand();
131
call("test_method");
132
test_num = Math::rand();
133
call("test_method_args", test_num);
134
test_num = Math::rand();
135
call("test_methodc");
136
test_num = Math::rand();
137
call("test_methodc_args", test_num);
138
//return
139
test_num = Math::rand();
140
test_valid[TEST_METHODR] = int(call("test_methodr")) == test_num && test_valid[TEST_METHODR];
141
test_num = Math::rand();
142
test_valid[TEST_METHODR_ARGS] = int(call("test_methodr_args", test_num)) == test_num && test_valid[TEST_METHODR_ARGS];
143
test_num = Math::rand();
144
test_valid[TEST_METHODRC] = int(call("test_methodrc")) == test_num && test_valid[TEST_METHODRC];
145
test_num = Math::rand();
146
test_valid[TEST_METHODRC_ARGS] = int(call("test_methodrc_args", test_num)) == test_num && test_valid[TEST_METHODRC_ARGS];
147
148
call("test_method_default_args", 1, 2, 3, 4);
149
150
ObjectSubclass *obj = memnew(ObjectSubclass);
151
call("test_method_object_cast", obj);
152
memdelete(obj);
153
}
154
};
155
156
TEST_CASE("[MethodBind] check all method binds") {
157
MethodBindTester *mbt = memnew(MethodBindTester);
158
159
mbt->run_tests();
160
161
CHECK(mbt->test_valid[MethodBindTester::TEST_METHOD]);
162
CHECK(mbt->test_valid[MethodBindTester::TEST_METHOD_ARGS]);
163
CHECK(mbt->test_valid[MethodBindTester::TEST_METHODC]);
164
CHECK(mbt->test_valid[MethodBindTester::TEST_METHODC_ARGS]);
165
CHECK(mbt->test_valid[MethodBindTester::TEST_METHODR]);
166
CHECK(mbt->test_valid[MethodBindTester::TEST_METHODR_ARGS]);
167
CHECK(mbt->test_valid[MethodBindTester::TEST_METHODRC]);
168
CHECK(mbt->test_valid[MethodBindTester::TEST_METHODRC_ARGS]);
169
CHECK(mbt->test_valid[MethodBindTester::TEST_METHOD_DEFARGS]);
170
CHECK(mbt->test_valid[MethodBindTester::TEST_METHOD_OBJECT_CAST]);
171
172
memdelete(mbt);
173
}
174
} // namespace TestMethodBind
175
176