Path: blob/master/tests/core/object/test_class_db.cpp
46006 views
/**************************************************************************/1/* test_class_db.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "tests/test_macros.h"3132TEST_FORCE_LINK(test_class_db)3334#include "core/config/engine.h"35#include "core/core_constants.h"36#include "core/object/class_db.h"3738namespace TestClassDB {3940struct TypeReference {41StringName name;42bool is_enum = false;43};4445struct ConstantData {46String name;47int64_t value = 0;48};4950struct EnumData {51StringName name;52List<ConstantData> constants;5354_FORCE_INLINE_ bool operator==(const EnumData &p_enum) const {55return p_enum.name == name;56}57};5859struct PropertyData {60StringName name;61int index = 0;6263StringName getter;64StringName setter;65};6667struct ArgumentData {68TypeReference type;69String name;70bool has_defval = false;71Variant defval;72int position;73};7475struct MethodData {76StringName name;77TypeReference return_type;78List<ArgumentData> arguments;79bool is_virtual = false;80bool is_vararg = false;81};8283struct SignalData {84StringName name;85List<ArgumentData> arguments;86};8788struct ExposedClass {89StringName name;90StringName base;9192bool is_singleton = false;93bool is_instantiable = false;94bool is_ref_counted = false;9596ClassDB::APIType api_type;9798List<ConstantData> constants;99List<EnumData> enums;100List<PropertyData> properties;101List<MethodData> methods;102List<SignalData> signals_;103104const PropertyData *find_property_by_name(const StringName &p_name) const {105for (const PropertyData &E : properties) {106if (E.name == p_name) {107return &E;108}109}110111return nullptr;112}113114const MethodData *find_method_by_name(const StringName &p_name) const {115for (const MethodData &E : methods) {116if (E.name == p_name) {117return &E;118}119}120121return nullptr;122}123};124125struct NamesCache {126StringName variant_type = StringName("Variant");127StringName object_class = StringName("Object");128StringName ref_counted_class = StringName("RefCounted");129StringName string_type = StringName("String");130StringName string_name_type = StringName("StringName");131StringName node_path_type = StringName("NodePath");132StringName bool_type = StringName("bool");133StringName int_type = StringName("int");134StringName float_type = StringName("float");135StringName void_type = StringName("void");136StringName vararg_stub_type = StringName("@VarArg@");137StringName vector2_type = StringName("Vector2");138StringName rect2_type = StringName("Rect2");139StringName vector3_type = StringName("Vector3");140StringName vector4_type = StringName("Vector4");141142// Object not included as it must be checked for all derived classes143static constexpr int nullable_types_count = 18;144StringName nullable_types[nullable_types_count] = {145string_type,146string_name_type,147node_path_type,148149StringName(_STR(Array)),150StringName(_STR(Dictionary)),151StringName(_STR(Callable)),152StringName(_STR(Signal)),153154StringName(_STR(PackedByteArray)),155StringName(_STR(PackedInt32Array)),156StringName(_STR(PackedInt64rray)),157StringName(_STR(PackedFloat32Array)),158StringName(_STR(PackedFloat64Array)),159StringName(_STR(PackedStringArray)),160StringName(_STR(PackedVector2Array)),161StringName(_STR(PackedVector3Array)),162StringName(_STR(PackedColorArray)),163StringName(_STR(PackedVector4Array)),164};165166bool is_nullable_type(const StringName &p_type) const {167for (int i = 0; i < nullable_types_count; i++) {168if (p_type == nullable_types[i]) {169return true;170}171}172173return false;174}175};176177typedef HashMap<StringName, ExposedClass> ExposedClasses;178179struct Context {180Vector<StringName> enum_types;181Vector<StringName> builtin_types;182ExposedClasses exposed_classes;183List<EnumData> global_enums;184NamesCache names_cache;185186const ExposedClass *find_exposed_class(const StringName &p_name) const {187ExposedClasses::ConstIterator elem = exposed_classes.find(p_name);188return elem ? &elem->value : nullptr;189}190191const ExposedClass *find_exposed_class(const TypeReference &p_type_ref) const {192ExposedClasses::ConstIterator elem = exposed_classes.find(p_type_ref.name);193return elem ? &elem->value : nullptr;194}195196bool has_type(const TypeReference &p_type_ref) const {197if (builtin_types.has(p_type_ref.name)) {198return true;199}200201if (p_type_ref.is_enum) {202if (enum_types.has(p_type_ref.name)) {203return true;204}205206// Enum not found. Most likely because none of its constants were bound, so it's empty. That's fine. Use int instead.207return builtin_types.find(names_cache.int_type);208}209210return false;211}212};213214bool 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) {215if (p_arg_type.name == p_context.names_cache.variant_type) {216// Variant can take anything217return true;218}219220switch (p_val.get_type()) {221case Variant::NIL:222return p_context.find_exposed_class(p_arg_type) ||223p_context.names_cache.is_nullable_type(p_arg_type.name);224case Variant::BOOL:225return p_arg_type.name == p_context.names_cache.bool_type;226case Variant::INT:227return p_arg_type.name == p_context.names_cache.int_type ||228p_arg_type.name == p_context.names_cache.float_type ||229p_arg_type.is_enum;230case Variant::FLOAT:231return p_arg_type.name == p_context.names_cache.float_type;232case Variant::STRING:233case Variant::STRING_NAME:234return p_arg_type.name == p_context.names_cache.string_type ||235p_arg_type.name == p_context.names_cache.string_name_type ||236p_arg_type.name == p_context.names_cache.node_path_type;237case Variant::NODE_PATH:238return p_arg_type.name == p_context.names_cache.node_path_type;239case Variant::TRANSFORM3D:240case Variant::TRANSFORM2D:241case Variant::BASIS:242case Variant::QUATERNION:243case Variant::PLANE:244case Variant::AABB:245case Variant::COLOR:246case Variant::VECTOR2:247case Variant::RECT2:248case Variant::VECTOR3:249case Variant::VECTOR4:250case Variant::PROJECTION:251case Variant::RID:252case Variant::ARRAY:253case Variant::DICTIONARY:254case Variant::PACKED_BYTE_ARRAY:255case Variant::PACKED_INT32_ARRAY:256case Variant::PACKED_INT64_ARRAY:257case Variant::PACKED_FLOAT32_ARRAY:258case Variant::PACKED_FLOAT64_ARRAY:259case Variant::PACKED_STRING_ARRAY:260case Variant::PACKED_VECTOR2_ARRAY:261case Variant::PACKED_VECTOR3_ARRAY:262case Variant::PACKED_COLOR_ARRAY:263case Variant::PACKED_VECTOR4_ARRAY:264case Variant::CALLABLE:265case Variant::SIGNAL:266return p_arg_type.name == Variant::get_type_name(p_val.get_type());267case Variant::OBJECT:268return p_context.find_exposed_class(p_arg_type);269case Variant::VECTOR2I:270return p_arg_type.name == p_context.names_cache.vector2_type ||271p_arg_type.name == Variant::get_type_name(p_val.get_type());272case Variant::RECT2I:273return p_arg_type.name == p_context.names_cache.rect2_type ||274p_arg_type.name == Variant::get_type_name(p_val.get_type());275case Variant::VECTOR3I:276return p_arg_type.name == p_context.names_cache.vector3_type ||277p_arg_type.name == Variant::get_type_name(p_val.get_type());278case Variant::VECTOR4I:279return p_arg_type.name == p_context.names_cache.vector4_type ||280p_arg_type.name == Variant::get_type_name(p_val.get_type());281case Variant::VARIANT_MAX:282break;283}284if (r_err_msg) {285*r_err_msg = "Unexpected Variant type: " + itos(p_val.get_type());286}287return false;288}289290bool arg_default_value_is_valid_data(const Variant &p_val, String *r_err_msg = nullptr) {291switch (p_val.get_type()) {292case Variant::RID:293case Variant::ARRAY:294case Variant::DICTIONARY:295case Variant::PACKED_BYTE_ARRAY:296case Variant::PACKED_INT32_ARRAY:297case Variant::PACKED_INT64_ARRAY:298case Variant::PACKED_FLOAT32_ARRAY:299case Variant::PACKED_FLOAT64_ARRAY:300case Variant::PACKED_STRING_ARRAY:301case Variant::PACKED_VECTOR2_ARRAY:302case Variant::PACKED_VECTOR3_ARRAY:303case Variant::PACKED_COLOR_ARRAY:304case Variant::PACKED_VECTOR4_ARRAY:305case Variant::CALLABLE:306case Variant::SIGNAL:307case Variant::OBJECT:308if (p_val.is_zero()) {309return true;310}311if (r_err_msg) {312*r_err_msg = "Must be zero.";313}314break;315default:316return true;317}318319return false;320}321322void validate_property(const Context &p_context, const ExposedClass &p_class, const PropertyData &p_prop) {323const MethodData *setter = p_class.find_method_by_name(p_prop.setter);324325// Search it in base classes too326const ExposedClass *top = &p_class;327while (!setter && top->base != StringName()) {328top = p_context.find_exposed_class(top->base);329TEST_FAIL_COND(!top, "Class not found '", top->base, "'. Inherited by '", top->name, "'.");330setter = top->find_method_by_name(p_prop.setter);331}332333const MethodData *getter = p_class.find_method_by_name(p_prop.getter);334335// Search it in base classes too336top = &p_class;337while (!getter && top->base != StringName()) {338top = p_context.find_exposed_class(top->base);339TEST_FAIL_COND(!top, "Class not found '", top->base, "'. Inherited by '", top->name, "'.");340getter = top->find_method_by_name(p_prop.getter);341}342343TEST_FAIL_COND((!setter && !getter),344"Couldn't find neither the setter nor the getter for property: '", p_class.name, ".", String(p_prop.name), "'.");345346if (setter) {347int setter_argc = p_prop.index != -1 ? 2 : 1;348TEST_FAIL_COND(setter->arguments.size() != setter_argc,349"Invalid property setter argument count: '", p_class.name, ".", String(p_prop.name), "'.");350}351352if (getter) {353int getter_argc = p_prop.index != -1 ? 1 : 0;354TEST_FAIL_COND(getter->arguments.size() != getter_argc,355"Invalid property setter argument count: '", p_class.name, ".", String(p_prop.name), "'.");356}357358if (getter && setter) {359const ArgumentData &setter_first_arg = setter->arguments.back()->get();360if (getter->return_type.name != setter_first_arg.type.name) {361TEST_FAIL(362"Return type from getter doesn't match first argument of setter, for property: '", p_class.name, ".", String(p_prop.name), "'.");363}364}365366const TypeReference &prop_type_ref = getter ? getter->return_type : setter->arguments.back()->get().type;367368const ExposedClass *prop_class = p_context.find_exposed_class(prop_type_ref);369if (prop_class) {370TEST_COND(prop_class->is_singleton,371"Property type is a singleton: '", p_class.name, ".", String(p_prop.name), "'.");372373if (p_class.api_type == ClassDB::API_CORE) {374TEST_COND(prop_class->api_type == ClassDB::API_EDITOR,375"Property '", p_class.name, ".", p_prop.name, "' has type '", prop_class->name,376"' from the editor API. Core API cannot have dependencies on the editor API.");377}378} else {379// Look for types that don't inherit Object380TEST_FAIL_COND(!p_context.has_type(prop_type_ref),381"Property type '", prop_type_ref.name, "' not found: '", p_class.name, ".", String(p_prop.name), "'.");382}383384if (getter) {385if (p_prop.index != -1) {386const ArgumentData &idx_arg = getter->arguments.front()->get();387if (idx_arg.type.name != p_context.names_cache.int_type) {388// If not an int, it can be an enum389TEST_COND(!p_context.enum_types.has(idx_arg.type.name),390"Invalid type '", idx_arg.type.name, "' for index argument of property getter: '", p_class.name, ".", String(p_prop.name), "'.");391}392}393}394395if (setter) {396if (p_prop.index != -1) {397const ArgumentData &idx_arg = setter->arguments.front()->get();398if (idx_arg.type.name != p_context.names_cache.int_type) {399// Assume the index parameter is an enum400// If not an int, it can be an enum401TEST_COND(!p_context.enum_types.has(idx_arg.type.name),402"Invalid type '", idx_arg.type.name, "' for index argument of property setter: '", p_class.name, ".", String(p_prop.name), "'.");403}404}405}406}407408void validate_argument(const Context &p_context, const ExposedClass &p_class, const String &p_owner_name, const String &p_owner_type, const ArgumentData &p_arg) {409#ifdef DEBUG_ENABLED410TEST_COND((p_arg.name.is_empty() || p_arg.name.begins_with("_unnamed_arg")),411vformat("Unnamed argument in position %d of %s '%s.%s'.", p_arg.position, p_owner_type, p_class.name, p_owner_name));412413TEST_FAIL_COND((p_arg.name != "@varargs@" && !p_arg.name.is_valid_ascii_identifier()),414vformat("Invalid argument name '%s' of %s '%s.%s'.", p_arg.name, p_owner_type, p_class.name, p_owner_name));415#endif // DEBUG_ENABLED416417const ExposedClass *arg_class = p_context.find_exposed_class(p_arg.type);418if (arg_class) {419TEST_COND(arg_class->is_singleton,420vformat("Argument type is a singleton: '%s' of %s '%s.%s'.", p_arg.name, p_owner_type, p_class.name, p_owner_name));421422if (p_class.api_type == ClassDB::API_CORE) {423TEST_COND(arg_class->api_type == ClassDB::API_EDITOR,424vformat("Argument '%s' of %s '%s.%s' has type '%s' from the editor API. Core API cannot have dependencies on the editor API.",425p_arg.name, p_owner_type, p_class.name, p_owner_name, arg_class->name));426}427} else {428// Look for types that don't inherit Object.429TEST_FAIL_COND(!p_context.has_type(p_arg.type),430vformat("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));431}432433if (p_arg.has_defval) {434String type_error_msg;435bool arg_defval_assignable_to_type = arg_default_value_is_assignable_to_type(p_context, p_arg.defval, p_arg.type, &type_error_msg);436437String 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);438if (!type_error_msg.is_empty()) {439err_msg += " " + type_error_msg;440}441442TEST_COND(!arg_defval_assignable_to_type, err_msg);443444bool arg_defval_valid_data = arg_default_value_is_valid_data(p_arg.defval, &type_error_msg);445446if (!type_error_msg.is_empty()) {447err_msg += " " + type_error_msg;448}449450TEST_COND(!arg_defval_valid_data, err_msg);451}452}453454void validate_method(const Context &p_context, const ExposedClass &p_class, const MethodData &p_method) {455if (p_method.return_type.name != StringName()) {456const ExposedClass *return_class = p_context.find_exposed_class(p_method.return_type);457if (return_class) {458if (p_class.api_type == ClassDB::API_CORE) {459TEST_COND(return_class->api_type == ClassDB::API_EDITOR,460"Method '", p_class.name, ".", p_method.name, "' has return type '", return_class->name,461"' from the editor API. Core API cannot have dependencies on the editor API.");462}463} else {464// Look for types that don't inherit Object465TEST_FAIL_COND(!p_context.has_type(p_method.return_type),466"Method return type '", p_method.return_type.name, "' not found: '", p_class.name, ".", p_method.name, "'.");467}468}469470for (const ArgumentData &F : p_method.arguments) {471const ArgumentData &arg = F;472validate_argument(p_context, p_class, p_method.name, "method", arg);473}474}475476void validate_signal(const Context &p_context, const ExposedClass &p_class, const SignalData &p_signal) {477for (const ArgumentData &F : p_signal.arguments) {478const ArgumentData &arg = F;479validate_argument(p_context, p_class, p_signal.name, "signal", arg);480}481}482483void validate_class(const Context &p_context, const ExposedClass &p_exposed_class) {484bool is_derived_type = p_exposed_class.base != StringName();485486if (!is_derived_type) {487// Asserts about the base Object class488TEST_FAIL_COND(p_exposed_class.name != p_context.names_cache.object_class,489"Class '", p_exposed_class.name, "' has no base class.");490TEST_FAIL_COND(!p_exposed_class.is_instantiable,491"Object class is not instantiable.");492TEST_FAIL_COND(p_exposed_class.api_type != ClassDB::API_CORE,493"Object class is API is not API_CORE.");494TEST_FAIL_COND(p_exposed_class.is_singleton,495"Object class is registered as a singleton.");496}497498TEST_FAIL_COND((is_derived_type && !p_context.exposed_classes.has(p_exposed_class.base)),499"Base type '", p_exposed_class.base.operator String(), "' does not exist, for class '", p_exposed_class.name, "'.");500501for (const PropertyData &F : p_exposed_class.properties) {502validate_property(p_context, p_exposed_class, F);503}504505for (const MethodData &F : p_exposed_class.methods) {506validate_method(p_context, p_exposed_class, F);507}508509for (const SignalData &F : p_exposed_class.signals_) {510validate_signal(p_context, p_exposed_class, F);511}512}513514void add_exposed_classes(Context &r_context) {515LocalVector<StringName> class_list;516ClassDB::get_class_list(class_list);517518for (uint32_t class_list_idx = 0; class_list_idx < class_list.size(); class_list_idx++) {519StringName class_name = class_list[class_list_idx];520521ClassDB::APIType api_type = ClassDB::get_api_type(class_name);522523if (api_type == ClassDB::API_NONE) {524continue;525}526527if (!ClassDB::is_class_exposed(class_name)) {528INFO(vformat("Ignoring class '%s' because it's not exposed.", class_name));529continue;530}531532if (!ClassDB::is_class_enabled(class_name)) {533INFO(vformat("Ignoring class '%s' because it's not enabled.", class_name));534continue;535}536537ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(class_name);538539ExposedClass exposed_class;540exposed_class.name = class_name;541exposed_class.api_type = api_type;542exposed_class.is_singleton = Engine::get_singleton()->has_singleton(class_name);543exposed_class.is_instantiable = class_info->creation_func && !exposed_class.is_singleton;544exposed_class.is_ref_counted = ClassDB::is_parent_class(class_name, "RefCounted");545exposed_class.base = ClassDB::get_parent_class(class_name);546547// Add properties548549List<PropertyInfo> property_list;550ClassDB::get_property_list(class_name, &property_list, true);551552HashMap<StringName, StringName> accessor_methods;553554for (const PropertyInfo &property : property_list) {555if (property.usage & PROPERTY_USAGE_GROUP || property.usage & PROPERTY_USAGE_SUBGROUP || property.usage & PROPERTY_USAGE_CATEGORY || (property.type == Variant::NIL && property.usage & PROPERTY_USAGE_ARRAY)) {556continue;557}558559PropertyData prop;560prop.name = property.name;561prop.setter = ClassDB::get_property_setter(class_name, prop.name);562prop.getter = ClassDB::get_property_getter(class_name, prop.name);563564if (prop.setter != StringName()) {565accessor_methods[prop.setter] = prop.name;566}567if (prop.getter != StringName()) {568accessor_methods[prop.getter] = prop.name;569}570571bool valid = false;572prop.index = ClassDB::get_property_index(class_name, prop.name, &valid);573TEST_FAIL_COND(!valid, "Invalid property: '", exposed_class.name, ".", String(prop.name), "'.");574575exposed_class.properties.push_back(prop);576}577578// Add methods579580List<MethodInfo> virtual_method_list;581ClassDB::get_virtual_methods(class_name, &virtual_method_list, true);582583List<MethodInfo> method_list;584ClassDB::get_method_list(class_name, &method_list, true);585method_list.sort();586587for (const MethodInfo &E : method_list) {588const MethodInfo &method_info = E;589590if (method_info.name.is_empty()) {591continue;592}593594MethodData method;595method.name = method_info.name;596TEST_FAIL_COND(!String(method.name).is_valid_ascii_identifier(),597"Method name is not a valid identifier: '", exposed_class.name, ".", method.name, "'.");598599if (method_info.flags & METHOD_FLAG_VIRTUAL) {600method.is_virtual = true;601}602603PropertyInfo return_info = method_info.return_val;604605MethodBind *m = method.is_virtual ? nullptr : ClassDB::get_method(class_name, method_info.name);606607method.is_vararg = m && m->is_vararg();608609if (!m && !method.is_virtual) {610TEST_FAIL_COND(!virtual_method_list.find(method_info),611"Missing MethodBind for non-virtual method: '", exposed_class.name, ".", method.name, "'.");612613// A virtual method without the virtual flag. This is a special case.614615// The method Object.free is registered as a virtual method, but without the virtual flag.616// This is because this method is not supposed to be overridden, but called.617// We assume the return type is void.618method.return_type.name = r_context.names_cache.void_type;619620// Actually, more methods like this may be added in the future, which could return621// something different. Let's put this check to notify us if that ever happens.622String warn_msg = vformat(623"Notification: New unexpected virtual non-overridable method found. "624"We only expected Object.free, but found '%s.%s'.",625exposed_class.name, method.name);626TEST_FAIL_COND_WARN(627(exposed_class.name != r_context.names_cache.object_class || String(method.name) != "free"),628warn_msg);629630} else if (return_info.type == Variant::INT && return_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {631method.return_type.name = return_info.class_name;632method.return_type.is_enum = true;633} else if (return_info.class_name != StringName()) {634method.return_type.name = return_info.class_name;635636bool bad_reference_hint = !method.is_virtual && return_info.hint != PROPERTY_HINT_RESOURCE_TYPE &&637ClassDB::is_parent_class(return_info.class_name, r_context.names_cache.ref_counted_class);638TEST_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: '",639exposed_class.name, ".", method.name, "'.");640} else if (return_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {641method.return_type.name = return_info.hint_string;642} else if (return_info.type == Variant::NIL && return_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {643method.return_type.name = r_context.names_cache.variant_type;644} else if (return_info.type == Variant::NIL) {645method.return_type.name = r_context.names_cache.void_type;646} else {647// NOTE: We don't care about the size and sign of int and float in these tests648method.return_type.name = Variant::get_type_name(return_info.type);649}650651for (int64_t i = 0; i < method_info.arguments.size(); ++i) {652const PropertyInfo &arg_info = method_info.arguments[i];653654String orig_arg_name = arg_info.name;655656ArgumentData arg;657arg.name = orig_arg_name;658arg.position = i;659660if (arg_info.type == Variant::INT && arg_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {661arg.type.name = arg_info.class_name;662arg.type.is_enum = true;663} else if (arg_info.class_name != StringName()) {664arg.type.name = arg_info.class_name;665} else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {666arg.type.name = arg_info.hint_string;667} else if (arg_info.type == Variant::NIL) {668arg.type.name = r_context.names_cache.variant_type;669} else {670// NOTE: We don't care about the size and sign of int and float in these tests671arg.type.name = Variant::get_type_name(arg_info.type);672}673674if (m && m->has_default_argument(i)) {675arg.has_defval = true;676arg.defval = m->get_default_argument(i);677}678679method.arguments.push_back(arg);680}681682if (method.is_vararg) {683ArgumentData vararg;684vararg.type.name = r_context.names_cache.vararg_stub_type;685vararg.name = "@varargs@";686method.arguments.push_back(vararg);687}688689TEST_COND(exposed_class.find_property_by_name(method.name),690"Method name conflicts with property: '", String(class_name), ".", String(method.name), "'.");691692// Methods starting with an underscore are ignored unless they're virtual or used as a property setter or getter.693if (!method.is_virtual && String(method.name)[0] == '_') {694for (const PropertyData &F : exposed_class.properties) {695const PropertyData &prop = F;696697if (prop.setter == method.name || prop.getter == method.name) {698exposed_class.methods.push_back(method);699break;700}701}702} else {703exposed_class.methods.push_back(method);704}705706if (method.is_virtual) {707TEST_COND(String(method.name)[0] != '_', "Virtual method ", String(method.name), " does not start with underscore.");708}709}710711// Add signals712713const AHashMap<StringName, const MethodInfo *> &signal_map = class_info->gdtype->get_signal_map(true);714715for (const KeyValue<StringName, const MethodInfo *> &K : signal_map) {716SignalData signal;717718const MethodInfo &method_info = *signal_map.get(K.key);719720signal.name = method_info.name;721TEST_FAIL_COND(!String(signal.name).is_valid_ascii_identifier(),722"Signal name is not a valid identifier: '", exposed_class.name, ".", signal.name, "'.");723724for (int64_t i = 0; i < method_info.arguments.size(); ++i) {725const PropertyInfo &arg_info = method_info.arguments[i];726727String orig_arg_name = arg_info.name;728729ArgumentData arg;730arg.name = orig_arg_name;731arg.position = i;732733if (arg_info.type == Variant::INT && arg_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {734arg.type.name = arg_info.class_name;735arg.type.is_enum = true;736} else if (arg_info.class_name != StringName()) {737arg.type.name = arg_info.class_name;738} else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {739arg.type.name = arg_info.hint_string;740} else if (arg_info.type == Variant::NIL) {741arg.type.name = r_context.names_cache.variant_type;742} else {743// NOTE: We don't care about the size and sign of int and float in these tests744arg.type.name = Variant::get_type_name(arg_info.type);745}746747signal.arguments.push_back(arg);748}749750bool method_conflict = exposed_class.find_property_by_name(signal.name);751752String warn_msg = vformat(753"Signal name conflicts with %s: '%s.%s.",754method_conflict ? "method" : "property", class_name, signal.name);755TEST_FAIL_COND((method_conflict || exposed_class.find_method_by_name(signal.name)),756warn_msg);757758exposed_class.signals_.push_back(signal);759}760761// Add enums and constants762763List<String> constants;764ClassDB::get_integer_constant_list(class_name, &constants, true);765766const AHashMap<StringName, const GDType::EnumInfo *> &enum_map = class_info->gdtype->get_enum_map(true);767768for (const KeyValue<StringName, const GDType::EnumInfo *> &kv_enum : enum_map) {769EnumData enum_;770enum_.name = kv_enum.key;771772for (const KeyValue<StringName, int64_t> &kv_case : kv_enum.value->values) {773const StringName &constant_name = kv_case.key;774TEST_FAIL_COND(String(constant_name).contains("::"),775"Enum constant contains '::', check bindings to remove the scope: '",776String(class_name), ".", String(enum_.name), ".", String(constant_name), "'.");777const int64_t *value = class_info->gdtype->get_integer_constant_map(false).getptr(constant_name);778TEST_FAIL_COND(!value, "Missing enum constant value: '",779String(class_name), ".", String(enum_.name), ".", String(constant_name), "'.");780constants.erase(constant_name);781782ConstantData constant;783constant.name = constant_name;784constant.value = *value;785786enum_.constants.push_back(constant);787}788789exposed_class.enums.push_back(enum_);790791r_context.enum_types.push_back(String(class_name) + "." + String(kv_enum.key));792}793794for (const String &E : constants) {795const String &constant_name = E;796TEST_FAIL_COND(constant_name.contains("::"),797"Constant contains '::', check bindings to remove the scope: '",798String(class_name), ".", constant_name, "'.");799const int64_t *value = class_info->gdtype->get_integer_constant_map(false).getptr(StringName(E));800TEST_FAIL_COND(!value, "Missing constant value: '", String(class_name), ".", String(constant_name), "'.");801802ConstantData constant;803constant.name = constant_name;804constant.value = *value;805806exposed_class.constants.push_back(constant);807}808809r_context.exposed_classes.insert(class_name, exposed_class);810}811}812813void add_builtin_types(Context &r_context) {814// NOTE: We don't care about the size and sign of int and float in these tests815for (int i = 0; i < Variant::VARIANT_MAX; i++) {816r_context.builtin_types.push_back(Variant::get_type_name(Variant::Type(i)));817}818819r_context.builtin_types.push_back(_STR(Variant));820r_context.builtin_types.push_back(r_context.names_cache.vararg_stub_type);821r_context.builtin_types.push_back("void");822}823824void add_global_enums(Context &r_context) {825int global_constants_count = CoreConstants::get_global_constant_count();826827if (global_constants_count > 0) {828for (int i = 0; i < global_constants_count; i++) {829StringName enum_name = CoreConstants::get_global_constant_enum(i);830831if (enum_name != StringName()) {832ConstantData constant;833constant.name = CoreConstants::get_global_constant_name(i);834constant.value = CoreConstants::get_global_constant_value(i);835836EnumData enum_;837enum_.name = enum_name;838List<EnumData>::Element *enum_match = r_context.global_enums.find(enum_);839if (enum_match) {840enum_match->get().constants.push_back(constant);841} else {842enum_.constants.push_back(constant);843r_context.global_enums.push_back(enum_);844}845}846}847848for (const EnumData &E : r_context.global_enums) {849r_context.enum_types.push_back(E.name);850}851}852853for (int i = 0; i < Variant::VARIANT_MAX; i++) {854if (i == Variant::OBJECT) {855continue;856}857858const Variant::Type type = Variant::Type(i);859860List<StringName> enum_names;861Variant::get_enums_for_type(type, &enum_names);862863for (const StringName &enum_name : enum_names) {864r_context.enum_types.push_back(Variant::get_type_name(type) + "." + enum_name);865}866}867}868869TEST_SUITE("[ClassDB]") {870TEST_CASE("[ClassDB] Add exposed classes, builtin types, and global enums") {871Context context;872873add_exposed_classes(context);874add_builtin_types(context);875add_global_enums(context);876877SUBCASE("[ClassDB] Validate exposed classes") {878const ExposedClass *object_class = context.find_exposed_class(context.names_cache.object_class);879TEST_FAIL_COND(!object_class, "Object class not found.");880TEST_FAIL_COND(object_class->base != StringName(),881"Object class derives from another class: '", object_class->base, "'.");882883for (const KeyValue<StringName, ExposedClass> &E : context.exposed_classes) {884validate_class(context, E.value);885}886}887}888}889890} // namespace TestClassDB891892893