Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/object/test_class_db.cpp
46006 views
1
/**************************************************************************/
2
/* test_class_db.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_class_db)
34
35
#include "core/config/engine.h"
36
#include "core/core_constants.h"
37
#include "core/object/class_db.h"
38
39
namespace TestClassDB {
40
41
struct TypeReference {
42
StringName name;
43
bool is_enum = false;
44
};
45
46
struct ConstantData {
47
String name;
48
int64_t value = 0;
49
};
50
51
struct EnumData {
52
StringName name;
53
List<ConstantData> constants;
54
55
_FORCE_INLINE_ bool operator==(const EnumData &p_enum) const {
56
return p_enum.name == name;
57
}
58
};
59
60
struct PropertyData {
61
StringName name;
62
int index = 0;
63
64
StringName getter;
65
StringName setter;
66
};
67
68
struct ArgumentData {
69
TypeReference type;
70
String name;
71
bool has_defval = false;
72
Variant defval;
73
int position;
74
};
75
76
struct MethodData {
77
StringName name;
78
TypeReference return_type;
79
List<ArgumentData> arguments;
80
bool is_virtual = false;
81
bool is_vararg = false;
82
};
83
84
struct SignalData {
85
StringName name;
86
List<ArgumentData> arguments;
87
};
88
89
struct ExposedClass {
90
StringName name;
91
StringName base;
92
93
bool is_singleton = false;
94
bool is_instantiable = false;
95
bool is_ref_counted = false;
96
97
ClassDB::APIType api_type;
98
99
List<ConstantData> constants;
100
List<EnumData> enums;
101
List<PropertyData> properties;
102
List<MethodData> methods;
103
List<SignalData> signals_;
104
105
const PropertyData *find_property_by_name(const StringName &p_name) const {
106
for (const PropertyData &E : properties) {
107
if (E.name == p_name) {
108
return &E;
109
}
110
}
111
112
return nullptr;
113
}
114
115
const MethodData *find_method_by_name(const StringName &p_name) const {
116
for (const MethodData &E : methods) {
117
if (E.name == p_name) {
118
return &E;
119
}
120
}
121
122
return nullptr;
123
}
124
};
125
126
struct NamesCache {
127
StringName variant_type = StringName("Variant");
128
StringName object_class = StringName("Object");
129
StringName ref_counted_class = StringName("RefCounted");
130
StringName string_type = StringName("String");
131
StringName string_name_type = StringName("StringName");
132
StringName node_path_type = StringName("NodePath");
133
StringName bool_type = StringName("bool");
134
StringName int_type = StringName("int");
135
StringName float_type = StringName("float");
136
StringName void_type = StringName("void");
137
StringName vararg_stub_type = StringName("@VarArg@");
138
StringName vector2_type = StringName("Vector2");
139
StringName rect2_type = StringName("Rect2");
140
StringName vector3_type = StringName("Vector3");
141
StringName vector4_type = StringName("Vector4");
142
143
// Object not included as it must be checked for all derived classes
144
static constexpr int nullable_types_count = 18;
145
StringName nullable_types[nullable_types_count] = {
146
string_type,
147
string_name_type,
148
node_path_type,
149
150
StringName(_STR(Array)),
151
StringName(_STR(Dictionary)),
152
StringName(_STR(Callable)),
153
StringName(_STR(Signal)),
154
155
StringName(_STR(PackedByteArray)),
156
StringName(_STR(PackedInt32Array)),
157
StringName(_STR(PackedInt64rray)),
158
StringName(_STR(PackedFloat32Array)),
159
StringName(_STR(PackedFloat64Array)),
160
StringName(_STR(PackedStringArray)),
161
StringName(_STR(PackedVector2Array)),
162
StringName(_STR(PackedVector3Array)),
163
StringName(_STR(PackedColorArray)),
164
StringName(_STR(PackedVector4Array)),
165
};
166
167
bool is_nullable_type(const StringName &p_type) const {
168
for (int i = 0; i < nullable_types_count; i++) {
169
if (p_type == nullable_types[i]) {
170
return true;
171
}
172
}
173
174
return false;
175
}
176
};
177
178
typedef HashMap<StringName, ExposedClass> ExposedClasses;
179
180
struct Context {
181
Vector<StringName> enum_types;
182
Vector<StringName> builtin_types;
183
ExposedClasses exposed_classes;
184
List<EnumData> global_enums;
185
NamesCache names_cache;
186
187
const ExposedClass *find_exposed_class(const StringName &p_name) const {
188
ExposedClasses::ConstIterator elem = exposed_classes.find(p_name);
189
return elem ? &elem->value : nullptr;
190
}
191
192
const ExposedClass *find_exposed_class(const TypeReference &p_type_ref) const {
193
ExposedClasses::ConstIterator elem = exposed_classes.find(p_type_ref.name);
194
return elem ? &elem->value : nullptr;
195
}
196
197
bool has_type(const TypeReference &p_type_ref) const {
198
if (builtin_types.has(p_type_ref.name)) {
199
return true;
200
}
201
202
if (p_type_ref.is_enum) {
203
if (enum_types.has(p_type_ref.name)) {
204
return true;
205
}
206
207
// Enum not found. Most likely because none of its constants were bound, so it's empty. That's fine. Use int instead.
208
return builtin_types.find(names_cache.int_type);
209
}
210
211
return false;
212
}
213
};
214
215
bool arg_default_value_is_assignable_to_type(const Context &p_context, const Variant &p_val, const TypeReference &p_arg_type, String *r_err_msg = nullptr) {
216
if (p_arg_type.name == p_context.names_cache.variant_type) {
217
// Variant can take anything
218
return true;
219
}
220
221
switch (p_val.get_type()) {
222
case Variant::NIL:
223
return p_context.find_exposed_class(p_arg_type) ||
224
p_context.names_cache.is_nullable_type(p_arg_type.name);
225
case Variant::BOOL:
226
return p_arg_type.name == p_context.names_cache.bool_type;
227
case Variant::INT:
228
return p_arg_type.name == p_context.names_cache.int_type ||
229
p_arg_type.name == p_context.names_cache.float_type ||
230
p_arg_type.is_enum;
231
case Variant::FLOAT:
232
return p_arg_type.name == p_context.names_cache.float_type;
233
case Variant::STRING:
234
case Variant::STRING_NAME:
235
return p_arg_type.name == p_context.names_cache.string_type ||
236
p_arg_type.name == p_context.names_cache.string_name_type ||
237
p_arg_type.name == p_context.names_cache.node_path_type;
238
case Variant::NODE_PATH:
239
return p_arg_type.name == p_context.names_cache.node_path_type;
240
case Variant::TRANSFORM3D:
241
case Variant::TRANSFORM2D:
242
case Variant::BASIS:
243
case Variant::QUATERNION:
244
case Variant::PLANE:
245
case Variant::AABB:
246
case Variant::COLOR:
247
case Variant::VECTOR2:
248
case Variant::RECT2:
249
case Variant::VECTOR3:
250
case Variant::VECTOR4:
251
case Variant::PROJECTION:
252
case Variant::RID:
253
case Variant::ARRAY:
254
case Variant::DICTIONARY:
255
case Variant::PACKED_BYTE_ARRAY:
256
case Variant::PACKED_INT32_ARRAY:
257
case Variant::PACKED_INT64_ARRAY:
258
case Variant::PACKED_FLOAT32_ARRAY:
259
case Variant::PACKED_FLOAT64_ARRAY:
260
case Variant::PACKED_STRING_ARRAY:
261
case Variant::PACKED_VECTOR2_ARRAY:
262
case Variant::PACKED_VECTOR3_ARRAY:
263
case Variant::PACKED_COLOR_ARRAY:
264
case Variant::PACKED_VECTOR4_ARRAY:
265
case Variant::CALLABLE:
266
case Variant::SIGNAL:
267
return p_arg_type.name == Variant::get_type_name(p_val.get_type());
268
case Variant::OBJECT:
269
return p_context.find_exposed_class(p_arg_type);
270
case Variant::VECTOR2I:
271
return p_arg_type.name == p_context.names_cache.vector2_type ||
272
p_arg_type.name == Variant::get_type_name(p_val.get_type());
273
case Variant::RECT2I:
274
return p_arg_type.name == p_context.names_cache.rect2_type ||
275
p_arg_type.name == Variant::get_type_name(p_val.get_type());
276
case Variant::VECTOR3I:
277
return p_arg_type.name == p_context.names_cache.vector3_type ||
278
p_arg_type.name == Variant::get_type_name(p_val.get_type());
279
case Variant::VECTOR4I:
280
return p_arg_type.name == p_context.names_cache.vector4_type ||
281
p_arg_type.name == Variant::get_type_name(p_val.get_type());
282
case Variant::VARIANT_MAX:
283
break;
284
}
285
if (r_err_msg) {
286
*r_err_msg = "Unexpected Variant type: " + itos(p_val.get_type());
287
}
288
return false;
289
}
290
291
bool arg_default_value_is_valid_data(const Variant &p_val, String *r_err_msg = nullptr) {
292
switch (p_val.get_type()) {
293
case Variant::RID:
294
case Variant::ARRAY:
295
case Variant::DICTIONARY:
296
case Variant::PACKED_BYTE_ARRAY:
297
case Variant::PACKED_INT32_ARRAY:
298
case Variant::PACKED_INT64_ARRAY:
299
case Variant::PACKED_FLOAT32_ARRAY:
300
case Variant::PACKED_FLOAT64_ARRAY:
301
case Variant::PACKED_STRING_ARRAY:
302
case Variant::PACKED_VECTOR2_ARRAY:
303
case Variant::PACKED_VECTOR3_ARRAY:
304
case Variant::PACKED_COLOR_ARRAY:
305
case Variant::PACKED_VECTOR4_ARRAY:
306
case Variant::CALLABLE:
307
case Variant::SIGNAL:
308
case Variant::OBJECT:
309
if (p_val.is_zero()) {
310
return true;
311
}
312
if (r_err_msg) {
313
*r_err_msg = "Must be zero.";
314
}
315
break;
316
default:
317
return true;
318
}
319
320
return false;
321
}
322
323
void validate_property(const Context &p_context, const ExposedClass &p_class, const PropertyData &p_prop) {
324
const MethodData *setter = p_class.find_method_by_name(p_prop.setter);
325
326
// Search it in base classes too
327
const ExposedClass *top = &p_class;
328
while (!setter && top->base != StringName()) {
329
top = p_context.find_exposed_class(top->base);
330
TEST_FAIL_COND(!top, "Class not found '", top->base, "'. Inherited by '", top->name, "'.");
331
setter = top->find_method_by_name(p_prop.setter);
332
}
333
334
const MethodData *getter = p_class.find_method_by_name(p_prop.getter);
335
336
// Search it in base classes too
337
top = &p_class;
338
while (!getter && top->base != StringName()) {
339
top = p_context.find_exposed_class(top->base);
340
TEST_FAIL_COND(!top, "Class not found '", top->base, "'. Inherited by '", top->name, "'.");
341
getter = top->find_method_by_name(p_prop.getter);
342
}
343
344
TEST_FAIL_COND((!setter && !getter),
345
"Couldn't find neither the setter nor the getter for property: '", p_class.name, ".", String(p_prop.name), "'.");
346
347
if (setter) {
348
int setter_argc = p_prop.index != -1 ? 2 : 1;
349
TEST_FAIL_COND(setter->arguments.size() != setter_argc,
350
"Invalid property setter argument count: '", p_class.name, ".", String(p_prop.name), "'.");
351
}
352
353
if (getter) {
354
int getter_argc = p_prop.index != -1 ? 1 : 0;
355
TEST_FAIL_COND(getter->arguments.size() != getter_argc,
356
"Invalid property setter argument count: '", p_class.name, ".", String(p_prop.name), "'.");
357
}
358
359
if (getter && setter) {
360
const ArgumentData &setter_first_arg = setter->arguments.back()->get();
361
if (getter->return_type.name != setter_first_arg.type.name) {
362
TEST_FAIL(
363
"Return type from getter doesn't match first argument of setter, for property: '", p_class.name, ".", String(p_prop.name), "'.");
364
}
365
}
366
367
const TypeReference &prop_type_ref = getter ? getter->return_type : setter->arguments.back()->get().type;
368
369
const ExposedClass *prop_class = p_context.find_exposed_class(prop_type_ref);
370
if (prop_class) {
371
TEST_COND(prop_class->is_singleton,
372
"Property type is a singleton: '", p_class.name, ".", String(p_prop.name), "'.");
373
374
if (p_class.api_type == ClassDB::API_CORE) {
375
TEST_COND(prop_class->api_type == ClassDB::API_EDITOR,
376
"Property '", p_class.name, ".", p_prop.name, "' has type '", prop_class->name,
377
"' from the editor API. Core API cannot have dependencies on the editor API.");
378
}
379
} else {
380
// Look for types that don't inherit Object
381
TEST_FAIL_COND(!p_context.has_type(prop_type_ref),
382
"Property type '", prop_type_ref.name, "' not found: '", p_class.name, ".", String(p_prop.name), "'.");
383
}
384
385
if (getter) {
386
if (p_prop.index != -1) {
387
const ArgumentData &idx_arg = getter->arguments.front()->get();
388
if (idx_arg.type.name != p_context.names_cache.int_type) {
389
// If not an int, it can be an enum
390
TEST_COND(!p_context.enum_types.has(idx_arg.type.name),
391
"Invalid type '", idx_arg.type.name, "' for index argument of property getter: '", p_class.name, ".", String(p_prop.name), "'.");
392
}
393
}
394
}
395
396
if (setter) {
397
if (p_prop.index != -1) {
398
const ArgumentData &idx_arg = setter->arguments.front()->get();
399
if (idx_arg.type.name != p_context.names_cache.int_type) {
400
// Assume the index parameter is an enum
401
// If not an int, it can be an enum
402
TEST_COND(!p_context.enum_types.has(idx_arg.type.name),
403
"Invalid type '", idx_arg.type.name, "' for index argument of property setter: '", p_class.name, ".", String(p_prop.name), "'.");
404
}
405
}
406
}
407
}
408
409
void validate_argument(const Context &p_context, const ExposedClass &p_class, const String &p_owner_name, const String &p_owner_type, const ArgumentData &p_arg) {
410
#ifdef DEBUG_ENABLED
411
TEST_COND((p_arg.name.is_empty() || p_arg.name.begins_with("_unnamed_arg")),
412
vformat("Unnamed argument in position %d of %s '%s.%s'.", p_arg.position, p_owner_type, p_class.name, p_owner_name));
413
414
TEST_FAIL_COND((p_arg.name != "@varargs@" && !p_arg.name.is_valid_ascii_identifier()),
415
vformat("Invalid argument name '%s' of %s '%s.%s'.", p_arg.name, p_owner_type, p_class.name, p_owner_name));
416
#endif // DEBUG_ENABLED
417
418
const ExposedClass *arg_class = p_context.find_exposed_class(p_arg.type);
419
if (arg_class) {
420
TEST_COND(arg_class->is_singleton,
421
vformat("Argument type is a singleton: '%s' of %s '%s.%s'.", p_arg.name, p_owner_type, p_class.name, p_owner_name));
422
423
if (p_class.api_type == ClassDB::API_CORE) {
424
TEST_COND(arg_class->api_type == ClassDB::API_EDITOR,
425
vformat("Argument '%s' of %s '%s.%s' has type '%s' from the editor API. Core API cannot have dependencies on the editor API.",
426
p_arg.name, p_owner_type, p_class.name, p_owner_name, arg_class->name));
427
}
428
} else {
429
// Look for types that don't inherit Object.
430
TEST_FAIL_COND(!p_context.has_type(p_arg.type),
431
vformat("Argument type '%s' not found: '%s' of %s '%s.%s'.", p_arg.type.name, p_arg.name, p_owner_type, p_class.name, p_owner_name));
432
}
433
434
if (p_arg.has_defval) {
435
String type_error_msg;
436
bool arg_defval_assignable_to_type = arg_default_value_is_assignable_to_type(p_context, p_arg.defval, p_arg.type, &type_error_msg);
437
438
String err_msg = vformat("Invalid default value for parameter '%s' of %s '%s.%s'.", p_arg.name, p_owner_type, p_class.name, p_owner_name);
439
if (!type_error_msg.is_empty()) {
440
err_msg += " " + type_error_msg;
441
}
442
443
TEST_COND(!arg_defval_assignable_to_type, err_msg);
444
445
bool arg_defval_valid_data = arg_default_value_is_valid_data(p_arg.defval, &type_error_msg);
446
447
if (!type_error_msg.is_empty()) {
448
err_msg += " " + type_error_msg;
449
}
450
451
TEST_COND(!arg_defval_valid_data, err_msg);
452
}
453
}
454
455
void validate_method(const Context &p_context, const ExposedClass &p_class, const MethodData &p_method) {
456
if (p_method.return_type.name != StringName()) {
457
const ExposedClass *return_class = p_context.find_exposed_class(p_method.return_type);
458
if (return_class) {
459
if (p_class.api_type == ClassDB::API_CORE) {
460
TEST_COND(return_class->api_type == ClassDB::API_EDITOR,
461
"Method '", p_class.name, ".", p_method.name, "' has return type '", return_class->name,
462
"' from the editor API. Core API cannot have dependencies on the editor API.");
463
}
464
} else {
465
// Look for types that don't inherit Object
466
TEST_FAIL_COND(!p_context.has_type(p_method.return_type),
467
"Method return type '", p_method.return_type.name, "' not found: '", p_class.name, ".", p_method.name, "'.");
468
}
469
}
470
471
for (const ArgumentData &F : p_method.arguments) {
472
const ArgumentData &arg = F;
473
validate_argument(p_context, p_class, p_method.name, "method", arg);
474
}
475
}
476
477
void validate_signal(const Context &p_context, const ExposedClass &p_class, const SignalData &p_signal) {
478
for (const ArgumentData &F : p_signal.arguments) {
479
const ArgumentData &arg = F;
480
validate_argument(p_context, p_class, p_signal.name, "signal", arg);
481
}
482
}
483
484
void validate_class(const Context &p_context, const ExposedClass &p_exposed_class) {
485
bool is_derived_type = p_exposed_class.base != StringName();
486
487
if (!is_derived_type) {
488
// Asserts about the base Object class
489
TEST_FAIL_COND(p_exposed_class.name != p_context.names_cache.object_class,
490
"Class '", p_exposed_class.name, "' has no base class.");
491
TEST_FAIL_COND(!p_exposed_class.is_instantiable,
492
"Object class is not instantiable.");
493
TEST_FAIL_COND(p_exposed_class.api_type != ClassDB::API_CORE,
494
"Object class is API is not API_CORE.");
495
TEST_FAIL_COND(p_exposed_class.is_singleton,
496
"Object class is registered as a singleton.");
497
}
498
499
TEST_FAIL_COND((is_derived_type && !p_context.exposed_classes.has(p_exposed_class.base)),
500
"Base type '", p_exposed_class.base.operator String(), "' does not exist, for class '", p_exposed_class.name, "'.");
501
502
for (const PropertyData &F : p_exposed_class.properties) {
503
validate_property(p_context, p_exposed_class, F);
504
}
505
506
for (const MethodData &F : p_exposed_class.methods) {
507
validate_method(p_context, p_exposed_class, F);
508
}
509
510
for (const SignalData &F : p_exposed_class.signals_) {
511
validate_signal(p_context, p_exposed_class, F);
512
}
513
}
514
515
void add_exposed_classes(Context &r_context) {
516
LocalVector<StringName> class_list;
517
ClassDB::get_class_list(class_list);
518
519
for (uint32_t class_list_idx = 0; class_list_idx < class_list.size(); class_list_idx++) {
520
StringName class_name = class_list[class_list_idx];
521
522
ClassDB::APIType api_type = ClassDB::get_api_type(class_name);
523
524
if (api_type == ClassDB::API_NONE) {
525
continue;
526
}
527
528
if (!ClassDB::is_class_exposed(class_name)) {
529
INFO(vformat("Ignoring class '%s' because it's not exposed.", class_name));
530
continue;
531
}
532
533
if (!ClassDB::is_class_enabled(class_name)) {
534
INFO(vformat("Ignoring class '%s' because it's not enabled.", class_name));
535
continue;
536
}
537
538
ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(class_name);
539
540
ExposedClass exposed_class;
541
exposed_class.name = class_name;
542
exposed_class.api_type = api_type;
543
exposed_class.is_singleton = Engine::get_singleton()->has_singleton(class_name);
544
exposed_class.is_instantiable = class_info->creation_func && !exposed_class.is_singleton;
545
exposed_class.is_ref_counted = ClassDB::is_parent_class(class_name, "RefCounted");
546
exposed_class.base = ClassDB::get_parent_class(class_name);
547
548
// Add properties
549
550
List<PropertyInfo> property_list;
551
ClassDB::get_property_list(class_name, &property_list, true);
552
553
HashMap<StringName, StringName> accessor_methods;
554
555
for (const PropertyInfo &property : property_list) {
556
if (property.usage & PROPERTY_USAGE_GROUP || property.usage & PROPERTY_USAGE_SUBGROUP || property.usage & PROPERTY_USAGE_CATEGORY || (property.type == Variant::NIL && property.usage & PROPERTY_USAGE_ARRAY)) {
557
continue;
558
}
559
560
PropertyData prop;
561
prop.name = property.name;
562
prop.setter = ClassDB::get_property_setter(class_name, prop.name);
563
prop.getter = ClassDB::get_property_getter(class_name, prop.name);
564
565
if (prop.setter != StringName()) {
566
accessor_methods[prop.setter] = prop.name;
567
}
568
if (prop.getter != StringName()) {
569
accessor_methods[prop.getter] = prop.name;
570
}
571
572
bool valid = false;
573
prop.index = ClassDB::get_property_index(class_name, prop.name, &valid);
574
TEST_FAIL_COND(!valid, "Invalid property: '", exposed_class.name, ".", String(prop.name), "'.");
575
576
exposed_class.properties.push_back(prop);
577
}
578
579
// Add methods
580
581
List<MethodInfo> virtual_method_list;
582
ClassDB::get_virtual_methods(class_name, &virtual_method_list, true);
583
584
List<MethodInfo> method_list;
585
ClassDB::get_method_list(class_name, &method_list, true);
586
method_list.sort();
587
588
for (const MethodInfo &E : method_list) {
589
const MethodInfo &method_info = E;
590
591
if (method_info.name.is_empty()) {
592
continue;
593
}
594
595
MethodData method;
596
method.name = method_info.name;
597
TEST_FAIL_COND(!String(method.name).is_valid_ascii_identifier(),
598
"Method name is not a valid identifier: '", exposed_class.name, ".", method.name, "'.");
599
600
if (method_info.flags & METHOD_FLAG_VIRTUAL) {
601
method.is_virtual = true;
602
}
603
604
PropertyInfo return_info = method_info.return_val;
605
606
MethodBind *m = method.is_virtual ? nullptr : ClassDB::get_method(class_name, method_info.name);
607
608
method.is_vararg = m && m->is_vararg();
609
610
if (!m && !method.is_virtual) {
611
TEST_FAIL_COND(!virtual_method_list.find(method_info),
612
"Missing MethodBind for non-virtual method: '", exposed_class.name, ".", method.name, "'.");
613
614
// A virtual method without the virtual flag. This is a special case.
615
616
// The method Object.free is registered as a virtual method, but without the virtual flag.
617
// This is because this method is not supposed to be overridden, but called.
618
// We assume the return type is void.
619
method.return_type.name = r_context.names_cache.void_type;
620
621
// Actually, more methods like this may be added in the future, which could return
622
// something different. Let's put this check to notify us if that ever happens.
623
String warn_msg = vformat(
624
"Notification: New unexpected virtual non-overridable method found. "
625
"We only expected Object.free, but found '%s.%s'.",
626
exposed_class.name, method.name);
627
TEST_FAIL_COND_WARN(
628
(exposed_class.name != r_context.names_cache.object_class || String(method.name) != "free"),
629
warn_msg);
630
631
} else if (return_info.type == Variant::INT && return_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
632
method.return_type.name = return_info.class_name;
633
method.return_type.is_enum = true;
634
} else if (return_info.class_name != StringName()) {
635
method.return_type.name = return_info.class_name;
636
637
bool bad_reference_hint = !method.is_virtual && return_info.hint != PROPERTY_HINT_RESOURCE_TYPE &&
638
ClassDB::is_parent_class(return_info.class_name, r_context.names_cache.ref_counted_class);
639
TEST_COND(bad_reference_hint, "Return type is reference but hint is not '" _STR(PROPERTY_HINT_RESOURCE_TYPE) "'.", " Are you returning a reference type by pointer? Method: '",
640
exposed_class.name, ".", method.name, "'.");
641
} else if (return_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
642
method.return_type.name = return_info.hint_string;
643
} else if (return_info.type == Variant::NIL && return_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
644
method.return_type.name = r_context.names_cache.variant_type;
645
} else if (return_info.type == Variant::NIL) {
646
method.return_type.name = r_context.names_cache.void_type;
647
} else {
648
// NOTE: We don't care about the size and sign of int and float in these tests
649
method.return_type.name = Variant::get_type_name(return_info.type);
650
}
651
652
for (int64_t i = 0; i < method_info.arguments.size(); ++i) {
653
const PropertyInfo &arg_info = method_info.arguments[i];
654
655
String orig_arg_name = arg_info.name;
656
657
ArgumentData arg;
658
arg.name = orig_arg_name;
659
arg.position = i;
660
661
if (arg_info.type == Variant::INT && arg_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
662
arg.type.name = arg_info.class_name;
663
arg.type.is_enum = true;
664
} else if (arg_info.class_name != StringName()) {
665
arg.type.name = arg_info.class_name;
666
} else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
667
arg.type.name = arg_info.hint_string;
668
} else if (arg_info.type == Variant::NIL) {
669
arg.type.name = r_context.names_cache.variant_type;
670
} else {
671
// NOTE: We don't care about the size and sign of int and float in these tests
672
arg.type.name = Variant::get_type_name(arg_info.type);
673
}
674
675
if (m && m->has_default_argument(i)) {
676
arg.has_defval = true;
677
arg.defval = m->get_default_argument(i);
678
}
679
680
method.arguments.push_back(arg);
681
}
682
683
if (method.is_vararg) {
684
ArgumentData vararg;
685
vararg.type.name = r_context.names_cache.vararg_stub_type;
686
vararg.name = "@varargs@";
687
method.arguments.push_back(vararg);
688
}
689
690
TEST_COND(exposed_class.find_property_by_name(method.name),
691
"Method name conflicts with property: '", String(class_name), ".", String(method.name), "'.");
692
693
// Methods starting with an underscore are ignored unless they're virtual or used as a property setter or getter.
694
if (!method.is_virtual && String(method.name)[0] == '_') {
695
for (const PropertyData &F : exposed_class.properties) {
696
const PropertyData &prop = F;
697
698
if (prop.setter == method.name || prop.getter == method.name) {
699
exposed_class.methods.push_back(method);
700
break;
701
}
702
}
703
} else {
704
exposed_class.methods.push_back(method);
705
}
706
707
if (method.is_virtual) {
708
TEST_COND(String(method.name)[0] != '_', "Virtual method ", String(method.name), " does not start with underscore.");
709
}
710
}
711
712
// Add signals
713
714
const AHashMap<StringName, const MethodInfo *> &signal_map = class_info->gdtype->get_signal_map(true);
715
716
for (const KeyValue<StringName, const MethodInfo *> &K : signal_map) {
717
SignalData signal;
718
719
const MethodInfo &method_info = *signal_map.get(K.key);
720
721
signal.name = method_info.name;
722
TEST_FAIL_COND(!String(signal.name).is_valid_ascii_identifier(),
723
"Signal name is not a valid identifier: '", exposed_class.name, ".", signal.name, "'.");
724
725
for (int64_t i = 0; i < method_info.arguments.size(); ++i) {
726
const PropertyInfo &arg_info = method_info.arguments[i];
727
728
String orig_arg_name = arg_info.name;
729
730
ArgumentData arg;
731
arg.name = orig_arg_name;
732
arg.position = i;
733
734
if (arg_info.type == Variant::INT && arg_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
735
arg.type.name = arg_info.class_name;
736
arg.type.is_enum = true;
737
} else if (arg_info.class_name != StringName()) {
738
arg.type.name = arg_info.class_name;
739
} else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
740
arg.type.name = arg_info.hint_string;
741
} else if (arg_info.type == Variant::NIL) {
742
arg.type.name = r_context.names_cache.variant_type;
743
} else {
744
// NOTE: We don't care about the size and sign of int and float in these tests
745
arg.type.name = Variant::get_type_name(arg_info.type);
746
}
747
748
signal.arguments.push_back(arg);
749
}
750
751
bool method_conflict = exposed_class.find_property_by_name(signal.name);
752
753
String warn_msg = vformat(
754
"Signal name conflicts with %s: '%s.%s.",
755
method_conflict ? "method" : "property", class_name, signal.name);
756
TEST_FAIL_COND((method_conflict || exposed_class.find_method_by_name(signal.name)),
757
warn_msg);
758
759
exposed_class.signals_.push_back(signal);
760
}
761
762
// Add enums and constants
763
764
List<String> constants;
765
ClassDB::get_integer_constant_list(class_name, &constants, true);
766
767
const AHashMap<StringName, const GDType::EnumInfo *> &enum_map = class_info->gdtype->get_enum_map(true);
768
769
for (const KeyValue<StringName, const GDType::EnumInfo *> &kv_enum : enum_map) {
770
EnumData enum_;
771
enum_.name = kv_enum.key;
772
773
for (const KeyValue<StringName, int64_t> &kv_case : kv_enum.value->values) {
774
const StringName &constant_name = kv_case.key;
775
TEST_FAIL_COND(String(constant_name).contains("::"),
776
"Enum constant contains '::', check bindings to remove the scope: '",
777
String(class_name), ".", String(enum_.name), ".", String(constant_name), "'.");
778
const int64_t *value = class_info->gdtype->get_integer_constant_map(false).getptr(constant_name);
779
TEST_FAIL_COND(!value, "Missing enum constant value: '",
780
String(class_name), ".", String(enum_.name), ".", String(constant_name), "'.");
781
constants.erase(constant_name);
782
783
ConstantData constant;
784
constant.name = constant_name;
785
constant.value = *value;
786
787
enum_.constants.push_back(constant);
788
}
789
790
exposed_class.enums.push_back(enum_);
791
792
r_context.enum_types.push_back(String(class_name) + "." + String(kv_enum.key));
793
}
794
795
for (const String &E : constants) {
796
const String &constant_name = E;
797
TEST_FAIL_COND(constant_name.contains("::"),
798
"Constant contains '::', check bindings to remove the scope: '",
799
String(class_name), ".", constant_name, "'.");
800
const int64_t *value = class_info->gdtype->get_integer_constant_map(false).getptr(StringName(E));
801
TEST_FAIL_COND(!value, "Missing constant value: '", String(class_name), ".", String(constant_name), "'.");
802
803
ConstantData constant;
804
constant.name = constant_name;
805
constant.value = *value;
806
807
exposed_class.constants.push_back(constant);
808
}
809
810
r_context.exposed_classes.insert(class_name, exposed_class);
811
}
812
}
813
814
void add_builtin_types(Context &r_context) {
815
// NOTE: We don't care about the size and sign of int and float in these tests
816
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
817
r_context.builtin_types.push_back(Variant::get_type_name(Variant::Type(i)));
818
}
819
820
r_context.builtin_types.push_back(_STR(Variant));
821
r_context.builtin_types.push_back(r_context.names_cache.vararg_stub_type);
822
r_context.builtin_types.push_back("void");
823
}
824
825
void add_global_enums(Context &r_context) {
826
int global_constants_count = CoreConstants::get_global_constant_count();
827
828
if (global_constants_count > 0) {
829
for (int i = 0; i < global_constants_count; i++) {
830
StringName enum_name = CoreConstants::get_global_constant_enum(i);
831
832
if (enum_name != StringName()) {
833
ConstantData constant;
834
constant.name = CoreConstants::get_global_constant_name(i);
835
constant.value = CoreConstants::get_global_constant_value(i);
836
837
EnumData enum_;
838
enum_.name = enum_name;
839
List<EnumData>::Element *enum_match = r_context.global_enums.find(enum_);
840
if (enum_match) {
841
enum_match->get().constants.push_back(constant);
842
} else {
843
enum_.constants.push_back(constant);
844
r_context.global_enums.push_back(enum_);
845
}
846
}
847
}
848
849
for (const EnumData &E : r_context.global_enums) {
850
r_context.enum_types.push_back(E.name);
851
}
852
}
853
854
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
855
if (i == Variant::OBJECT) {
856
continue;
857
}
858
859
const Variant::Type type = Variant::Type(i);
860
861
List<StringName> enum_names;
862
Variant::get_enums_for_type(type, &enum_names);
863
864
for (const StringName &enum_name : enum_names) {
865
r_context.enum_types.push_back(Variant::get_type_name(type) + "." + enum_name);
866
}
867
}
868
}
869
870
TEST_SUITE("[ClassDB]") {
871
TEST_CASE("[ClassDB] Add exposed classes, builtin types, and global enums") {
872
Context context;
873
874
add_exposed_classes(context);
875
add_builtin_types(context);
876
add_global_enums(context);
877
878
SUBCASE("[ClassDB] Validate exposed classes") {
879
const ExposedClass *object_class = context.find_exposed_class(context.names_cache.object_class);
880
TEST_FAIL_COND(!object_class, "Object class not found.");
881
TEST_FAIL_COND(object_class->base != StringName(),
882
"Object class derives from another class: '", object_class->base, "'.");
883
884
for (const KeyValue<StringName, ExposedClass> &E : context.exposed_classes) {
885
validate_class(context, E.value);
886
}
887
}
888
}
889
}
890
891
} // namespace TestClassDB
892
893