Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/object/test_class_db.h
10278 views
1
/**************************************************************************/
2
/* test_class_db.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/core_bind.h"
34
#include "core/core_constants.h"
35
#include "core/object/class_db.h"
36
37
#include "tests/test_macros.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((p_exposed_class.is_singleton && p_exposed_class.base != p_context.names_cache.object_class),
500
"Singleton base class '", String(p_exposed_class.base), "' is not Object, for class '", p_exposed_class.name, "'.");
501
502
TEST_FAIL_COND((is_derived_type && !p_context.exposed_classes.has(p_exposed_class.base)),
503
"Base type '", p_exposed_class.base.operator String(), "' does not exist, for class '", p_exposed_class.name, "'.");
504
505
for (const PropertyData &F : p_exposed_class.properties) {
506
validate_property(p_context, p_exposed_class, F);
507
}
508
509
for (const MethodData &F : p_exposed_class.methods) {
510
validate_method(p_context, p_exposed_class, F);
511
}
512
513
for (const SignalData &F : p_exposed_class.signals_) {
514
validate_signal(p_context, p_exposed_class, F);
515
}
516
}
517
518
void add_exposed_classes(Context &r_context) {
519
List<StringName> class_list;
520
ClassDB::get_class_list(&class_list);
521
class_list.sort_custom<StringName::AlphCompare>();
522
523
while (class_list.size()) {
524
StringName class_name = class_list.front()->get();
525
526
ClassDB::APIType api_type = ClassDB::get_api_type(class_name);
527
528
if (api_type == ClassDB::API_NONE) {
529
class_list.pop_front();
530
continue;
531
}
532
533
if (!ClassDB::is_class_exposed(class_name)) {
534
INFO(vformat("Ignoring class '%s' because it's not exposed.", class_name));
535
class_list.pop_front();
536
continue;
537
}
538
539
if (!ClassDB::is_class_enabled(class_name)) {
540
INFO(vformat("Ignoring class '%s' because it's not enabled.", class_name));
541
class_list.pop_front();
542
continue;
543
}
544
545
ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(class_name);
546
547
ExposedClass exposed_class;
548
exposed_class.name = class_name;
549
exposed_class.api_type = api_type;
550
exposed_class.is_singleton = Engine::get_singleton()->has_singleton(class_name);
551
exposed_class.is_instantiable = class_info->creation_func && !exposed_class.is_singleton;
552
exposed_class.is_ref_counted = ClassDB::is_parent_class(class_name, "RefCounted");
553
exposed_class.base = ClassDB::get_parent_class(class_name);
554
555
// Add properties
556
557
List<PropertyInfo> property_list;
558
ClassDB::get_property_list(class_name, &property_list, true);
559
560
HashMap<StringName, StringName> accessor_methods;
561
562
for (const PropertyInfo &property : property_list) {
563
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)) {
564
continue;
565
}
566
567
PropertyData prop;
568
prop.name = property.name;
569
prop.setter = ClassDB::get_property_setter(class_name, prop.name);
570
prop.getter = ClassDB::get_property_getter(class_name, prop.name);
571
572
if (prop.setter != StringName()) {
573
accessor_methods[prop.setter] = prop.name;
574
}
575
if (prop.getter != StringName()) {
576
accessor_methods[prop.getter] = prop.name;
577
}
578
579
bool valid = false;
580
prop.index = ClassDB::get_property_index(class_name, prop.name, &valid);
581
TEST_FAIL_COND(!valid, "Invalid property: '", exposed_class.name, ".", String(prop.name), "'.");
582
583
exposed_class.properties.push_back(prop);
584
}
585
586
// Add methods
587
588
List<MethodInfo> virtual_method_list;
589
ClassDB::get_virtual_methods(class_name, &virtual_method_list, true);
590
591
List<MethodInfo> method_list;
592
ClassDB::get_method_list(class_name, &method_list, true);
593
method_list.sort();
594
595
for (const MethodInfo &E : method_list) {
596
const MethodInfo &method_info = E;
597
598
if (method_info.name.is_empty()) {
599
continue;
600
}
601
602
MethodData method;
603
method.name = method_info.name;
604
TEST_FAIL_COND(!String(method.name).is_valid_ascii_identifier(),
605
"Method name is not a valid identifier: '", exposed_class.name, ".", method.name, "'.");
606
607
if (method_info.flags & METHOD_FLAG_VIRTUAL) {
608
method.is_virtual = true;
609
}
610
611
PropertyInfo return_info = method_info.return_val;
612
613
MethodBind *m = method.is_virtual ? nullptr : ClassDB::get_method(class_name, method_info.name);
614
615
method.is_vararg = m && m->is_vararg();
616
617
if (!m && !method.is_virtual) {
618
TEST_FAIL_COND(!virtual_method_list.find(method_info),
619
"Missing MethodBind for non-virtual method: '", exposed_class.name, ".", method.name, "'.");
620
621
// A virtual method without the virtual flag. This is a special case.
622
623
// The method Object.free is registered as a virtual method, but without the virtual flag.
624
// This is because this method is not supposed to be overridden, but called.
625
// We assume the return type is void.
626
method.return_type.name = r_context.names_cache.void_type;
627
628
// Actually, more methods like this may be added in the future, which could return
629
// something different. Let's put this check to notify us if that ever happens.
630
String warn_msg = vformat(
631
"Notification: New unexpected virtual non-overridable method found. "
632
"We only expected Object.free, but found '%s.%s'.",
633
exposed_class.name, method.name);
634
TEST_FAIL_COND_WARN(
635
(exposed_class.name != r_context.names_cache.object_class || String(method.name) != "free"),
636
warn_msg);
637
638
} else if (return_info.type == Variant::INT && return_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
639
method.return_type.name = return_info.class_name;
640
method.return_type.is_enum = true;
641
} else if (return_info.class_name != StringName()) {
642
method.return_type.name = return_info.class_name;
643
644
bool bad_reference_hint = !method.is_virtual && return_info.hint != PROPERTY_HINT_RESOURCE_TYPE &&
645
ClassDB::is_parent_class(return_info.class_name, r_context.names_cache.ref_counted_class);
646
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: '",
647
exposed_class.name, ".", method.name, "'.");
648
} else if (return_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
649
method.return_type.name = return_info.hint_string;
650
} else if (return_info.type == Variant::NIL && return_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
651
method.return_type.name = r_context.names_cache.variant_type;
652
} else if (return_info.type == Variant::NIL) {
653
method.return_type.name = r_context.names_cache.void_type;
654
} else {
655
// NOTE: We don't care about the size and sign of int and float in these tests
656
method.return_type.name = Variant::get_type_name(return_info.type);
657
}
658
659
for (int64_t i = 0; i < method_info.arguments.size(); ++i) {
660
const PropertyInfo &arg_info = method_info.arguments[i];
661
662
String orig_arg_name = arg_info.name;
663
664
ArgumentData arg;
665
arg.name = orig_arg_name;
666
arg.position = i;
667
668
if (arg_info.type == Variant::INT && arg_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
669
arg.type.name = arg_info.class_name;
670
arg.type.is_enum = true;
671
} else if (arg_info.class_name != StringName()) {
672
arg.type.name = arg_info.class_name;
673
} else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
674
arg.type.name = arg_info.hint_string;
675
} else if (arg_info.type == Variant::NIL) {
676
arg.type.name = r_context.names_cache.variant_type;
677
} else {
678
// NOTE: We don't care about the size and sign of int and float in these tests
679
arg.type.name = Variant::get_type_name(arg_info.type);
680
}
681
682
if (m && m->has_default_argument(i)) {
683
arg.has_defval = true;
684
arg.defval = m->get_default_argument(i);
685
}
686
687
method.arguments.push_back(arg);
688
}
689
690
if (method.is_vararg) {
691
ArgumentData vararg;
692
vararg.type.name = r_context.names_cache.vararg_stub_type;
693
vararg.name = "@varargs@";
694
method.arguments.push_back(vararg);
695
}
696
697
TEST_COND(exposed_class.find_property_by_name(method.name),
698
"Method name conflicts with property: '", String(class_name), ".", String(method.name), "'.");
699
700
// Methods starting with an underscore are ignored unless they're virtual or used as a property setter or getter.
701
if (!method.is_virtual && String(method.name)[0] == '_') {
702
for (const PropertyData &F : exposed_class.properties) {
703
const PropertyData &prop = F;
704
705
if (prop.setter == method.name || prop.getter == method.name) {
706
exposed_class.methods.push_back(method);
707
break;
708
}
709
}
710
} else {
711
exposed_class.methods.push_back(method);
712
}
713
714
if (method.is_virtual) {
715
TEST_COND(String(method.name)[0] != '_', "Virtual method ", String(method.name), " does not start with underscore.");
716
}
717
}
718
719
// Add signals
720
721
const HashMap<StringName, MethodInfo> &signal_map = class_info->signal_map;
722
723
for (const KeyValue<StringName, MethodInfo> &K : signal_map) {
724
SignalData signal;
725
726
const MethodInfo &method_info = signal_map.get(K.key);
727
728
signal.name = method_info.name;
729
TEST_FAIL_COND(!String(signal.name).is_valid_ascii_identifier(),
730
"Signal name is not a valid identifier: '", exposed_class.name, ".", signal.name, "'.");
731
732
for (int64_t i = 0; i < method_info.arguments.size(); ++i) {
733
const PropertyInfo &arg_info = method_info.arguments[i];
734
735
String orig_arg_name = arg_info.name;
736
737
ArgumentData arg;
738
arg.name = orig_arg_name;
739
arg.position = i;
740
741
if (arg_info.type == Variant::INT && arg_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
742
arg.type.name = arg_info.class_name;
743
arg.type.is_enum = true;
744
} else if (arg_info.class_name != StringName()) {
745
arg.type.name = arg_info.class_name;
746
} else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
747
arg.type.name = arg_info.hint_string;
748
} else if (arg_info.type == Variant::NIL) {
749
arg.type.name = r_context.names_cache.variant_type;
750
} else {
751
// NOTE: We don't care about the size and sign of int and float in these tests
752
arg.type.name = Variant::get_type_name(arg_info.type);
753
}
754
755
signal.arguments.push_back(arg);
756
}
757
758
bool method_conflict = exposed_class.find_property_by_name(signal.name);
759
760
String warn_msg = vformat(
761
"Signal name conflicts with %s: '%s.%s.",
762
method_conflict ? "method" : "property", class_name, signal.name);
763
TEST_FAIL_COND((method_conflict || exposed_class.find_method_by_name(signal.name)),
764
warn_msg);
765
766
exposed_class.signals_.push_back(signal);
767
}
768
769
// Add enums and constants
770
771
List<String> constants;
772
ClassDB::get_integer_constant_list(class_name, &constants, true);
773
774
const HashMap<StringName, ClassDB::ClassInfo::EnumInfo> &enum_map = class_info->enum_map;
775
776
for (const KeyValue<StringName, ClassDB::ClassInfo::EnumInfo> &K : enum_map) {
777
EnumData enum_;
778
enum_.name = K.key;
779
780
for (const StringName &E : K.value.constants) {
781
const StringName &constant_name = E;
782
TEST_FAIL_COND(String(constant_name).contains("::"),
783
"Enum constant contains '::', check bindings to remove the scope: '",
784
String(class_name), ".", String(enum_.name), ".", String(constant_name), "'.");
785
int64_t *value = class_info->constant_map.getptr(constant_name);
786
TEST_FAIL_COND(!value, "Missing enum constant value: '",
787
String(class_name), ".", String(enum_.name), ".", String(constant_name), "'.");
788
constants.erase(constant_name);
789
790
ConstantData constant;
791
constant.name = constant_name;
792
constant.value = *value;
793
794
enum_.constants.push_back(constant);
795
}
796
797
exposed_class.enums.push_back(enum_);
798
799
r_context.enum_types.push_back(String(class_name) + "." + String(K.key));
800
}
801
802
for (const String &E : constants) {
803
const String &constant_name = E;
804
TEST_FAIL_COND(constant_name.contains("::"),
805
"Constant contains '::', check bindings to remove the scope: '",
806
String(class_name), ".", constant_name, "'.");
807
int64_t *value = class_info->constant_map.getptr(StringName(E));
808
TEST_FAIL_COND(!value, "Missing constant value: '", String(class_name), ".", String(constant_name), "'.");
809
810
ConstantData constant;
811
constant.name = constant_name;
812
constant.value = *value;
813
814
exposed_class.constants.push_back(constant);
815
}
816
817
r_context.exposed_classes.insert(class_name, exposed_class);
818
class_list.pop_front();
819
}
820
}
821
822
void add_builtin_types(Context &r_context) {
823
// NOTE: We don't care about the size and sign of int and float in these tests
824
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
825
r_context.builtin_types.push_back(Variant::get_type_name(Variant::Type(i)));
826
}
827
828
r_context.builtin_types.push_back(_STR(Variant));
829
r_context.builtin_types.push_back(r_context.names_cache.vararg_stub_type);
830
r_context.builtin_types.push_back("void");
831
}
832
833
void add_global_enums(Context &r_context) {
834
int global_constants_count = CoreConstants::get_global_constant_count();
835
836
if (global_constants_count > 0) {
837
for (int i = 0; i < global_constants_count; i++) {
838
StringName enum_name = CoreConstants::get_global_constant_enum(i);
839
840
if (enum_name != StringName()) {
841
ConstantData constant;
842
constant.name = CoreConstants::get_global_constant_name(i);
843
constant.value = CoreConstants::get_global_constant_value(i);
844
845
EnumData enum_;
846
enum_.name = enum_name;
847
List<EnumData>::Element *enum_match = r_context.global_enums.find(enum_);
848
if (enum_match) {
849
enum_match->get().constants.push_back(constant);
850
} else {
851
enum_.constants.push_back(constant);
852
r_context.global_enums.push_back(enum_);
853
}
854
}
855
}
856
857
for (const EnumData &E : r_context.global_enums) {
858
r_context.enum_types.push_back(E.name);
859
}
860
}
861
862
for (int i = 0; i < Variant::VARIANT_MAX; i++) {
863
if (i == Variant::OBJECT) {
864
continue;
865
}
866
867
const Variant::Type type = Variant::Type(i);
868
869
List<StringName> enum_names;
870
Variant::get_enums_for_type(type, &enum_names);
871
872
for (const StringName &enum_name : enum_names) {
873
r_context.enum_types.push_back(Variant::get_type_name(type) + "." + enum_name);
874
}
875
}
876
}
877
878
TEST_SUITE("[ClassDB]") {
879
TEST_CASE("[ClassDB] Add exposed classes, builtin types, and global enums") {
880
Context context;
881
882
add_exposed_classes(context);
883
add_builtin_types(context);
884
add_global_enums(context);
885
886
SUBCASE("[ClassDB] Validate exposed classes") {
887
const ExposedClass *object_class = context.find_exposed_class(context.names_cache.object_class);
888
TEST_FAIL_COND(!object_class, "Object class not found.");
889
TEST_FAIL_COND(object_class->base != StringName(),
890
"Object class derives from another class: '", object_class->base, "'.");
891
892
for (const KeyValue<StringName, ExposedClass> &E : context.exposed_classes) {
893
validate_class(context, E.value);
894
}
895
}
896
}
897
}
898
} // namespace TestClassDB
899
900