test_class_db.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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. #ifndef TEST_CLASS_DB_H
  31. #define TEST_CLASS_DB_H
  32. #include "core/core_bind.h"
  33. #include "core/core_constants.h"
  34. #include "core/object/class_db.h"
  35. #include "tests/test_macros.h"
  36. namespace TestClassDB {
  37. struct TypeReference {
  38. StringName name;
  39. bool is_enum = false;
  40. };
  41. struct ConstantData {
  42. String name;
  43. int64_t value = 0;
  44. };
  45. struct EnumData {
  46. StringName name;
  47. List<ConstantData> constants;
  48. _FORCE_INLINE_ bool operator==(const EnumData &p_enum) const {
  49. return p_enum.name == name;
  50. }
  51. };
  52. struct PropertyData {
  53. StringName name;
  54. int index = 0;
  55. StringName getter;
  56. StringName setter;
  57. };
  58. struct ArgumentData {
  59. TypeReference type;
  60. String name;
  61. bool has_defval = false;
  62. Variant defval;
  63. int position;
  64. };
  65. struct MethodData {
  66. StringName name;
  67. TypeReference return_type;
  68. List<ArgumentData> arguments;
  69. bool is_virtual = false;
  70. bool is_vararg = false;
  71. };
  72. struct SignalData {
  73. StringName name;
  74. List<ArgumentData> arguments;
  75. };
  76. struct ExposedClass {
  77. StringName name;
  78. StringName base;
  79. bool is_singleton = false;
  80. bool is_instantiable = false;
  81. bool is_ref_counted = false;
  82. ClassDB::APIType api_type;
  83. List<ConstantData> constants;
  84. List<EnumData> enums;
  85. List<PropertyData> properties;
  86. List<MethodData> methods;
  87. List<SignalData> signals_;
  88. const PropertyData *find_property_by_name(const StringName &p_name) const {
  89. for (const PropertyData &E : properties) {
  90. if (E.name == p_name) {
  91. return &E;
  92. }
  93. }
  94. return nullptr;
  95. }
  96. const MethodData *find_method_by_name(const StringName &p_name) const {
  97. for (const MethodData &E : methods) {
  98. if (E.name == p_name) {
  99. return &E;
  100. }
  101. }
  102. return nullptr;
  103. }
  104. };
  105. struct NamesCache {
  106. StringName variant_type = StaticCString::create("Variant");
  107. StringName object_class = StaticCString::create("Object");
  108. StringName ref_counted_class = StaticCString::create("RefCounted");
  109. StringName string_type = StaticCString::create("String");
  110. StringName string_name_type = StaticCString::create("StringName");
  111. StringName node_path_type = StaticCString::create("NodePath");
  112. StringName bool_type = StaticCString::create("bool");
  113. StringName int_type = StaticCString::create("int");
  114. StringName float_type = StaticCString::create("float");
  115. StringName void_type = StaticCString::create("void");
  116. StringName vararg_stub_type = StaticCString::create("@VarArg@");
  117. StringName vector2_type = StaticCString::create("Vector2");
  118. StringName rect2_type = StaticCString::create("Rect2");
  119. StringName vector3_type = StaticCString::create("Vector3");
  120. // Object not included as it must be checked for all derived classes
  121. static constexpr int nullable_types_count = 17;
  122. StringName nullable_types[nullable_types_count] = {
  123. string_type,
  124. string_name_type,
  125. node_path_type,
  126. StaticCString::create(_STR(Array)),
  127. StaticCString::create(_STR(Dictionary)),
  128. StaticCString::create(_STR(Callable)),
  129. StaticCString::create(_STR(Signal)),
  130. StaticCString::create(_STR(PackedByteArray)),
  131. StaticCString::create(_STR(PackedInt32Array)),
  132. StaticCString::create(_STR(PackedInt64rray)),
  133. StaticCString::create(_STR(PackedFloat32Array)),
  134. StaticCString::create(_STR(PackedFloat64Array)),
  135. StaticCString::create(_STR(PackedStringArray)),
  136. StaticCString::create(_STR(PackedVector2Array)),
  137. StaticCString::create(_STR(PackedVector3Array)),
  138. StaticCString::create(_STR(PackedColorArray)),
  139. };
  140. bool is_nullable_type(const StringName &p_type) const {
  141. for (int i = 0; i < nullable_types_count; i++) {
  142. if (p_type == nullable_types[i]) {
  143. return true;
  144. }
  145. }
  146. return false;
  147. }
  148. };
  149. typedef HashMap<StringName, ExposedClass> ExposedClasses;
  150. struct Context {
  151. Vector<StringName> enum_types;
  152. Vector<StringName> builtin_types;
  153. ExposedClasses exposed_classes;
  154. List<EnumData> global_enums;
  155. NamesCache names_cache;
  156. const ExposedClass *find_exposed_class(const StringName &p_name) const {
  157. ExposedClasses::ConstIterator elem = exposed_classes.find(p_name);
  158. return elem ? &elem->value : nullptr;
  159. }
  160. const ExposedClass *find_exposed_class(const TypeReference &p_type_ref) const {
  161. ExposedClasses::ConstIterator elem = exposed_classes.find(p_type_ref.name);
  162. return elem ? &elem->value : nullptr;
  163. }
  164. bool has_type(const TypeReference &p_type_ref) const {
  165. if (builtin_types.find(p_type_ref.name) >= 0) {
  166. return true;
  167. }
  168. if (p_type_ref.is_enum) {
  169. if (enum_types.find(p_type_ref.name) >= 0) {
  170. return true;
  171. }
  172. // Enum not found. Most likely because none of its constants were bound, so it's empty. That's fine. Use int instead.
  173. return builtin_types.find(names_cache.int_type);
  174. }
  175. return false;
  176. }
  177. };
  178. 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) {
  179. if (p_arg_type.name == p_context.names_cache.variant_type) {
  180. // Variant can take anything
  181. return true;
  182. }
  183. switch (p_val.get_type()) {
  184. case Variant::NIL:
  185. return p_context.find_exposed_class(p_arg_type) ||
  186. p_context.names_cache.is_nullable_type(p_arg_type.name);
  187. case Variant::BOOL:
  188. return p_arg_type.name == p_context.names_cache.bool_type;
  189. case Variant::INT:
  190. return p_arg_type.name == p_context.names_cache.int_type ||
  191. p_arg_type.name == p_context.names_cache.float_type ||
  192. p_arg_type.is_enum;
  193. case Variant::FLOAT:
  194. return p_arg_type.name == p_context.names_cache.float_type;
  195. case Variant::STRING:
  196. case Variant::STRING_NAME:
  197. return p_arg_type.name == p_context.names_cache.string_type ||
  198. p_arg_type.name == p_context.names_cache.string_name_type ||
  199. p_arg_type.name == p_context.names_cache.node_path_type;
  200. case Variant::NODE_PATH:
  201. return p_arg_type.name == p_context.names_cache.node_path_type;
  202. case Variant::TRANSFORM3D:
  203. case Variant::TRANSFORM2D:
  204. case Variant::BASIS:
  205. case Variant::QUATERNION:
  206. case Variant::PLANE:
  207. case Variant::AABB:
  208. case Variant::COLOR:
  209. case Variant::VECTOR2:
  210. case Variant::RECT2:
  211. case Variant::VECTOR3:
  212. case Variant::RID:
  213. case Variant::ARRAY:
  214. case Variant::DICTIONARY:
  215. case Variant::PACKED_BYTE_ARRAY:
  216. case Variant::PACKED_INT32_ARRAY:
  217. case Variant::PACKED_INT64_ARRAY:
  218. case Variant::PACKED_FLOAT32_ARRAY:
  219. case Variant::PACKED_FLOAT64_ARRAY:
  220. case Variant::PACKED_STRING_ARRAY:
  221. case Variant::PACKED_VECTOR2_ARRAY:
  222. case Variant::PACKED_VECTOR3_ARRAY:
  223. case Variant::PACKED_COLOR_ARRAY:
  224. case Variant::CALLABLE:
  225. case Variant::SIGNAL:
  226. return p_arg_type.name == Variant::get_type_name(p_val.get_type());
  227. case Variant::OBJECT:
  228. return p_context.find_exposed_class(p_arg_type);
  229. case Variant::VECTOR2I:
  230. return p_arg_type.name == p_context.names_cache.vector2_type ||
  231. p_arg_type.name == Variant::get_type_name(p_val.get_type());
  232. case Variant::RECT2I:
  233. return p_arg_type.name == p_context.names_cache.rect2_type ||
  234. p_arg_type.name == Variant::get_type_name(p_val.get_type());
  235. case Variant::VECTOR3I:
  236. return p_arg_type.name == p_context.names_cache.vector3_type ||
  237. p_arg_type.name == Variant::get_type_name(p_val.get_type());
  238. default:
  239. if (r_err_msg) {
  240. *r_err_msg = "Unexpected Variant type: " + itos(p_val.get_type());
  241. }
  242. break;
  243. }
  244. return false;
  245. }
  246. void validate_property(const Context &p_context, const ExposedClass &p_class, const PropertyData &p_prop) {
  247. const MethodData *setter = p_class.find_method_by_name(p_prop.setter);
  248. // Search it in base classes too
  249. const ExposedClass *top = &p_class;
  250. while (!setter && top->base != StringName()) {
  251. top = p_context.find_exposed_class(top->base);
  252. TEST_FAIL_COND(!top, "Class not found '", top->base, "'. Inherited by '", top->name, "'.");
  253. setter = top->find_method_by_name(p_prop.setter);
  254. }
  255. const MethodData *getter = p_class.find_method_by_name(p_prop.getter);
  256. // Search it in base classes too
  257. top = &p_class;
  258. while (!getter && top->base != StringName()) {
  259. top = p_context.find_exposed_class(top->base);
  260. TEST_FAIL_COND(!top, "Class not found '", top->base, "'. Inherited by '", top->name, "'.");
  261. getter = top->find_method_by_name(p_prop.getter);
  262. }
  263. TEST_FAIL_COND((!setter && !getter),
  264. "Couldn't find neither the setter nor the getter for property: '", p_class.name, ".", String(p_prop.name), "'.");
  265. if (setter) {
  266. int setter_argc = p_prop.index != -1 ? 2 : 1;
  267. TEST_FAIL_COND(setter->arguments.size() != setter_argc,
  268. "Invalid property setter argument count: '", p_class.name, ".", String(p_prop.name), "'.");
  269. }
  270. if (getter) {
  271. int getter_argc = p_prop.index != -1 ? 1 : 0;
  272. TEST_FAIL_COND(getter->arguments.size() != getter_argc,
  273. "Invalid property setter argument count: '", p_class.name, ".", String(p_prop.name), "'.");
  274. }
  275. if (getter && setter) {
  276. const ArgumentData &setter_first_arg = setter->arguments.back()->get();
  277. if (getter->return_type.name != setter_first_arg.type.name) {
  278. // Special case for Node::set_name
  279. bool whitelisted = getter->return_type.name == p_context.names_cache.string_name_type &&
  280. setter_first_arg.type.name == p_context.names_cache.string_type;
  281. TEST_FAIL_COND(!whitelisted,
  282. "Return type from getter doesn't match first argument of setter, for property: '", p_class.name, ".", String(p_prop.name), "'.");
  283. }
  284. }
  285. const TypeReference &prop_type_ref = getter ? getter->return_type : setter->arguments.back()->get().type;
  286. const ExposedClass *prop_class = p_context.find_exposed_class(prop_type_ref);
  287. if (prop_class) {
  288. TEST_COND(prop_class->is_singleton,
  289. "Property type is a singleton: '", p_class.name, ".", String(p_prop.name), "'.");
  290. if (p_class.api_type == ClassDB::API_CORE) {
  291. TEST_COND(prop_class->api_type == ClassDB::API_EDITOR,
  292. "Property '", p_class.name, ".", p_prop.name, "' has type '", prop_class->name,
  293. "' from the editor API. Core API cannot have dependencies on the editor API.");
  294. }
  295. } else {
  296. // Look for types that don't inherit Object
  297. TEST_FAIL_COND(!p_context.has_type(prop_type_ref),
  298. "Property type '", prop_type_ref.name, "' not found: '", p_class.name, ".", String(p_prop.name), "'.");
  299. }
  300. if (getter) {
  301. if (p_prop.index != -1) {
  302. const ArgumentData &idx_arg = getter->arguments.front()->get();
  303. if (idx_arg.type.name != p_context.names_cache.int_type) {
  304. // If not an int, it can be an enum
  305. TEST_COND(p_context.enum_types.find(idx_arg.type.name) < 0,
  306. "Invalid type '", idx_arg.type.name, "' for index argument of property getter: '", p_class.name, ".", String(p_prop.name), "'.");
  307. }
  308. }
  309. }
  310. if (setter) {
  311. if (p_prop.index != -1) {
  312. const ArgumentData &idx_arg = setter->arguments.front()->get();
  313. if (idx_arg.type.name != p_context.names_cache.int_type) {
  314. // Assume the index parameter is an enum
  315. // If not an int, it can be an enum
  316. TEST_COND(p_context.enum_types.find(idx_arg.type.name) < 0,
  317. "Invalid type '", idx_arg.type.name, "' for index argument of property setter: '", p_class.name, ".", String(p_prop.name), "'.");
  318. }
  319. }
  320. }
  321. }
  322. 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) {
  323. TEST_COND((p_arg.name.is_empty() || p_arg.name.begins_with("_unnamed_arg")),
  324. vformat("Unnamed argument in position %d of %s '%s.%s'.", p_arg.position, p_owner_type, p_class.name, p_owner_name));
  325. const ExposedClass *arg_class = p_context.find_exposed_class(p_arg.type);
  326. if (arg_class) {
  327. TEST_COND(arg_class->is_singleton,
  328. vformat("Argument type is a singleton: '%s' of %s '%s.%s'.", p_arg.name, p_owner_type, p_class.name, p_owner_name));
  329. if (p_class.api_type == ClassDB::API_CORE) {
  330. TEST_COND(arg_class->api_type == ClassDB::API_EDITOR,
  331. vformat("Argument '%s' of %s '%s.%s' has type '%s' from the editor API. Core API cannot have dependencies on the editor API.",
  332. p_arg.name, p_owner_type, p_class.name, p_owner_name, arg_class->name));
  333. }
  334. } else {
  335. // Look for types that don't inherit Object.
  336. TEST_FAIL_COND(!p_context.has_type(p_arg.type),
  337. 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));
  338. }
  339. if (p_arg.has_defval) {
  340. String type_error_msg;
  341. bool arg_defval_assignable_to_type = arg_default_value_is_assignable_to_type(p_context, p_arg.defval, p_arg.type, &type_error_msg);
  342. 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);
  343. if (!type_error_msg.is_empty()) {
  344. err_msg += " " + type_error_msg;
  345. }
  346. TEST_COND(!arg_defval_assignable_to_type, err_msg.utf8().get_data());
  347. }
  348. }
  349. void validate_method(const Context &p_context, const ExposedClass &p_class, const MethodData &p_method) {
  350. if (p_method.return_type.name != StringName()) {
  351. const ExposedClass *return_class = p_context.find_exposed_class(p_method.return_type);
  352. if (return_class) {
  353. TEST_COND(return_class->is_singleton,
  354. "Method return type is a singleton: '", p_class.name, ".", p_method.name, "'.");
  355. if (p_class.api_type == ClassDB::API_CORE) {
  356. TEST_COND(return_class->api_type == ClassDB::API_EDITOR,
  357. "Method '", p_class.name, ".", p_method.name, "' has return type '", return_class->name,
  358. "' from the editor API. Core API cannot have dependencies on the editor API.");
  359. }
  360. } else {
  361. // Look for types that don't inherit Object
  362. TEST_FAIL_COND(!p_context.has_type(p_method.return_type),
  363. "Method return type '", p_method.return_type.name, "' not found: '", p_class.name, ".", p_method.name, "'.");
  364. }
  365. }
  366. for (const ArgumentData &F : p_method.arguments) {
  367. const ArgumentData &arg = F;
  368. validate_argument(p_context, p_class, p_method.name, "method", arg);
  369. }
  370. }
  371. void validate_signal(const Context &p_context, const ExposedClass &p_class, const SignalData &p_signal) {
  372. for (const ArgumentData &F : p_signal.arguments) {
  373. const ArgumentData &arg = F;
  374. validate_argument(p_context, p_class, p_signal.name, "signal", arg);
  375. }
  376. }
  377. void validate_class(const Context &p_context, const ExposedClass &p_exposed_class) {
  378. bool is_derived_type = p_exposed_class.base != StringName();
  379. if (!is_derived_type) {
  380. // Asserts about the base Object class
  381. TEST_FAIL_COND(p_exposed_class.name != p_context.names_cache.object_class,
  382. "Class '", p_exposed_class.name, "' has no base class.");
  383. TEST_FAIL_COND(!p_exposed_class.is_instantiable,
  384. "Object class is not instantiable.");
  385. TEST_FAIL_COND(p_exposed_class.api_type != ClassDB::API_CORE,
  386. "Object class is API is not API_CORE.");
  387. TEST_FAIL_COND(p_exposed_class.is_singleton,
  388. "Object class is registered as a singleton.");
  389. }
  390. TEST_FAIL_COND((p_exposed_class.is_singleton && p_exposed_class.base != p_context.names_cache.object_class),
  391. "Singleton base class '", String(p_exposed_class.base), "' is not Object, for class '", p_exposed_class.name, "'.");
  392. TEST_FAIL_COND((is_derived_type && !p_context.exposed_classes.has(p_exposed_class.base)),
  393. "Base type '", p_exposed_class.base.operator String(), "' does not exist, for class '", p_exposed_class.name, "'.");
  394. for (const PropertyData &F : p_exposed_class.properties) {
  395. validate_property(p_context, p_exposed_class, F);
  396. }
  397. for (const MethodData &F : p_exposed_class.methods) {
  398. validate_method(p_context, p_exposed_class, F);
  399. }
  400. for (const SignalData &F : p_exposed_class.signals_) {
  401. validate_signal(p_context, p_exposed_class, F);
  402. }
  403. }
  404. void add_exposed_classes(Context &r_context) {
  405. List<StringName> class_list;
  406. ClassDB::get_class_list(&class_list);
  407. class_list.sort_custom<StringName::AlphCompare>();
  408. while (class_list.size()) {
  409. StringName class_name = class_list.front()->get();
  410. ClassDB::APIType api_type = ClassDB::get_api_type(class_name);
  411. if (api_type == ClassDB::API_NONE) {
  412. class_list.pop_front();
  413. continue;
  414. }
  415. if (!ClassDB::is_class_exposed(class_name)) {
  416. MESSAGE(vformat("Ignoring class '%s' because it's not exposed.", class_name));
  417. class_list.pop_front();
  418. continue;
  419. }
  420. if (!ClassDB::is_class_enabled(class_name)) {
  421. MESSAGE(vformat("Ignoring class '%s' because it's not enabled.", class_name));
  422. class_list.pop_front();
  423. continue;
  424. }
  425. ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(class_name);
  426. ExposedClass exposed_class;
  427. exposed_class.name = class_name;
  428. exposed_class.api_type = api_type;
  429. exposed_class.is_singleton = Engine::get_singleton()->has_singleton(class_name);
  430. exposed_class.is_instantiable = class_info->creation_func && !exposed_class.is_singleton;
  431. exposed_class.is_ref_counted = ClassDB::is_parent_class(class_name, "RefCounted");
  432. exposed_class.base = ClassDB::get_parent_class(class_name);
  433. // Add properties
  434. List<PropertyInfo> property_list;
  435. ClassDB::get_property_list(class_name, &property_list, true);
  436. HashMap<StringName, StringName> accessor_methods;
  437. for (const PropertyInfo &property : property_list) {
  438. 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)) {
  439. continue;
  440. }
  441. PropertyData prop;
  442. prop.name = property.name;
  443. prop.setter = ClassDB::get_property_setter(class_name, prop.name);
  444. prop.getter = ClassDB::get_property_getter(class_name, prop.name);
  445. if (prop.setter != StringName()) {
  446. accessor_methods[prop.setter] = prop.name;
  447. }
  448. if (prop.getter != StringName()) {
  449. accessor_methods[prop.getter] = prop.name;
  450. }
  451. bool valid = false;
  452. prop.index = ClassDB::get_property_index(class_name, prop.name, &valid);
  453. TEST_FAIL_COND(!valid, "Invalid property: '", exposed_class.name, ".", String(prop.name), "'.");
  454. exposed_class.properties.push_back(prop);
  455. }
  456. // Add methods
  457. List<MethodInfo> virtual_method_list;
  458. ClassDB::get_virtual_methods(class_name, &virtual_method_list, true);
  459. List<MethodInfo> method_list;
  460. ClassDB::get_method_list(class_name, &method_list, true);
  461. method_list.sort();
  462. for (const MethodInfo &E : method_list) {
  463. const MethodInfo &method_info = E;
  464. int argc = method_info.arguments.size();
  465. if (method_info.name.is_empty()) {
  466. continue;
  467. }
  468. MethodData method;
  469. method.name = method_info.name;
  470. TEST_FAIL_COND(!String(method.name).is_valid_identifier(),
  471. "Method name is not a valid identifier: '", exposed_class.name, ".", method.name, "'.");
  472. if (method_info.flags & METHOD_FLAG_VIRTUAL) {
  473. method.is_virtual = true;
  474. }
  475. PropertyInfo return_info = method_info.return_val;
  476. MethodBind *m = method.is_virtual ? nullptr : ClassDB::get_method(class_name, method_info.name);
  477. method.is_vararg = m && m->is_vararg();
  478. if (!m && !method.is_virtual) {
  479. TEST_FAIL_COND(!virtual_method_list.find(method_info),
  480. "Missing MethodBind for non-virtual method: '", exposed_class.name, ".", method.name, "'.");
  481. // A virtual method without the virtual flag. This is a special case.
  482. // The method Object.free is registered as a virtual method, but without the virtual flag.
  483. // This is because this method is not supposed to be overridden, but called.
  484. // We assume the return type is void.
  485. method.return_type.name = r_context.names_cache.void_type;
  486. // Actually, more methods like this may be added in the future, which could return
  487. // something different. Let's put this check to notify us if that ever happens.
  488. String warn_msg = vformat(
  489. "Notification: New unexpected virtual non-overridable method found. "
  490. "We only expected Object.free, but found '%s.%s'.",
  491. exposed_class.name, method.name);
  492. TEST_FAIL_COND_WARN(
  493. (exposed_class.name != r_context.names_cache.object_class || String(method.name) != "free"),
  494. warn_msg.utf8().get_data());
  495. } else if (return_info.type == Variant::INT && return_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
  496. method.return_type.name = return_info.class_name;
  497. method.return_type.is_enum = true;
  498. } else if (return_info.class_name != StringName()) {
  499. method.return_type.name = return_info.class_name;
  500. bool bad_reference_hint = !method.is_virtual && return_info.hint != PROPERTY_HINT_RESOURCE_TYPE &&
  501. ClassDB::is_parent_class(return_info.class_name, r_context.names_cache.ref_counted_class);
  502. 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: '",
  503. exposed_class.name, ".", method.name, "'.");
  504. } else if (return_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  505. method.return_type.name = return_info.hint_string;
  506. } else if (return_info.type == Variant::NIL && return_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
  507. method.return_type.name = r_context.names_cache.variant_type;
  508. } else if (return_info.type == Variant::NIL) {
  509. method.return_type.name = r_context.names_cache.void_type;
  510. } else {
  511. // NOTE: We don't care about the size and sign of int and float in these tests
  512. method.return_type.name = Variant::get_type_name(return_info.type);
  513. }
  514. for (int i = 0; i < argc; i++) {
  515. PropertyInfo arg_info = method_info.arguments[i];
  516. String orig_arg_name = arg_info.name;
  517. ArgumentData arg;
  518. arg.name = orig_arg_name;
  519. arg.position = i;
  520. if (arg_info.type == Variant::INT && arg_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
  521. arg.type.name = arg_info.class_name;
  522. arg.type.is_enum = true;
  523. } else if (arg_info.class_name != StringName()) {
  524. arg.type.name = arg_info.class_name;
  525. } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  526. arg.type.name = arg_info.hint_string;
  527. } else if (arg_info.type == Variant::NIL) {
  528. arg.type.name = r_context.names_cache.variant_type;
  529. } else {
  530. // NOTE: We don't care about the size and sign of int and float in these tests
  531. arg.type.name = Variant::get_type_name(arg_info.type);
  532. }
  533. if (m && m->has_default_argument(i)) {
  534. arg.has_defval = true;
  535. arg.defval = m->get_default_argument(i);
  536. }
  537. method.arguments.push_back(arg);
  538. }
  539. if (method.is_vararg) {
  540. ArgumentData vararg;
  541. vararg.type.name = r_context.names_cache.vararg_stub_type;
  542. vararg.name = "@varargs@";
  543. method.arguments.push_back(vararg);
  544. }
  545. TEST_COND(exposed_class.find_property_by_name(method.name),
  546. "Method name conflicts with property: '", String(class_name), ".", String(method.name), "'.");
  547. // Methods starting with an underscore are ignored unless they're virtual or used as a property setter or getter.
  548. if (!method.is_virtual && String(method.name)[0] == '_') {
  549. for (const PropertyData &F : exposed_class.properties) {
  550. const PropertyData &prop = F;
  551. if (prop.setter == method.name || prop.getter == method.name) {
  552. exposed_class.methods.push_back(method);
  553. break;
  554. }
  555. }
  556. } else {
  557. exposed_class.methods.push_back(method);
  558. }
  559. if (method.is_virtual) {
  560. TEST_COND(String(method.name)[0] != '_', "Virtual method ", String(method.name), " does not start with underscore.");
  561. }
  562. }
  563. // Add signals
  564. const HashMap<StringName, MethodInfo> &signal_map = class_info->signal_map;
  565. for (const KeyValue<StringName, MethodInfo> &K : signal_map) {
  566. SignalData signal;
  567. const MethodInfo &method_info = signal_map.get(K.key);
  568. signal.name = method_info.name;
  569. TEST_FAIL_COND(!String(signal.name).is_valid_identifier(),
  570. "Signal name is not a valid identifier: '", exposed_class.name, ".", signal.name, "'.");
  571. int argc = method_info.arguments.size();
  572. for (int i = 0; i < argc; i++) {
  573. PropertyInfo arg_info = method_info.arguments[i];
  574. String orig_arg_name = arg_info.name;
  575. ArgumentData arg;
  576. arg.name = orig_arg_name;
  577. arg.position = i;
  578. if (arg_info.type == Variant::INT && arg_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
  579. arg.type.name = arg_info.class_name;
  580. arg.type.is_enum = true;
  581. } else if (arg_info.class_name != StringName()) {
  582. arg.type.name = arg_info.class_name;
  583. } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  584. arg.type.name = arg_info.hint_string;
  585. } else if (arg_info.type == Variant::NIL) {
  586. arg.type.name = r_context.names_cache.variant_type;
  587. } else {
  588. // NOTE: We don't care about the size and sign of int and float in these tests
  589. arg.type.name = Variant::get_type_name(arg_info.type);
  590. }
  591. signal.arguments.push_back(arg);
  592. }
  593. bool method_conflict = exposed_class.find_property_by_name(signal.name);
  594. // TODO:
  595. // ClassDB allows signal names that conflict with method or property names.
  596. // However registering a signal with a conflicting name is still considered wrong.
  597. // Unfortunately there are some existing cases that are yet to be fixed.
  598. // Until those are fixed we will print a warning instead of failing the test.
  599. String warn_msg = vformat(
  600. "Signal name conflicts with %s: '%s.%s.",
  601. method_conflict ? "method" : "property", class_name, signal.name);
  602. TEST_FAIL_COND_WARN((method_conflict || exposed_class.find_method_by_name(signal.name)),
  603. warn_msg.utf8().get_data());
  604. exposed_class.signals_.push_back(signal);
  605. }
  606. // Add enums and constants
  607. List<String> constants;
  608. ClassDB::get_integer_constant_list(class_name, &constants, true);
  609. const HashMap<StringName, ClassDB::ClassInfo::EnumInfo> &enum_map = class_info->enum_map;
  610. for (const KeyValue<StringName, ClassDB::ClassInfo::EnumInfo> &K : enum_map) {
  611. EnumData enum_;
  612. enum_.name = K.key;
  613. for (const StringName &E : K.value.constants) {
  614. const StringName &constant_name = E;
  615. TEST_FAIL_COND(String(constant_name).find("::") != -1,
  616. "Enum constant contains '::', check bindings to remove the scope: '",
  617. String(class_name), ".", String(enum_.name), ".", String(constant_name), "'.");
  618. int64_t *value = class_info->constant_map.getptr(constant_name);
  619. TEST_FAIL_COND(!value, "Missing enum constant value: '",
  620. String(class_name), ".", String(enum_.name), ".", String(constant_name), "'.");
  621. constants.erase(constant_name);
  622. ConstantData constant;
  623. constant.name = constant_name;
  624. constant.value = *value;
  625. enum_.constants.push_back(constant);
  626. }
  627. exposed_class.enums.push_back(enum_);
  628. r_context.enum_types.push_back(String(class_name) + "." + String(K.key));
  629. }
  630. for (const String &E : constants) {
  631. const String &constant_name = E;
  632. TEST_FAIL_COND(constant_name.find("::") != -1,
  633. "Constant contains '::', check bindings to remove the scope: '",
  634. String(class_name), ".", constant_name, "'.");
  635. int64_t *value = class_info->constant_map.getptr(StringName(E));
  636. TEST_FAIL_COND(!value, "Missing constant value: '", String(class_name), ".", String(constant_name), "'.");
  637. ConstantData constant;
  638. constant.name = constant_name;
  639. constant.value = *value;
  640. exposed_class.constants.push_back(constant);
  641. }
  642. r_context.exposed_classes.insert(class_name, exposed_class);
  643. class_list.pop_front();
  644. }
  645. }
  646. void add_builtin_types(Context &r_context) {
  647. // NOTE: We don't care about the size and sign of int and float in these tests
  648. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  649. r_context.builtin_types.push_back(Variant::get_type_name(Variant::Type(i)));
  650. }
  651. r_context.builtin_types.push_back(_STR(Variant));
  652. r_context.builtin_types.push_back(r_context.names_cache.vararg_stub_type);
  653. r_context.builtin_types.push_back("void");
  654. }
  655. void add_global_enums(Context &r_context) {
  656. int global_constants_count = CoreConstants::get_global_constant_count();
  657. if (global_constants_count > 0) {
  658. for (int i = 0; i < global_constants_count; i++) {
  659. StringName enum_name = CoreConstants::get_global_constant_enum(i);
  660. if (enum_name != StringName()) {
  661. ConstantData constant;
  662. constant.name = CoreConstants::get_global_constant_name(i);
  663. constant.value = CoreConstants::get_global_constant_value(i);
  664. EnumData enum_;
  665. enum_.name = enum_name;
  666. List<EnumData>::Element *enum_match = r_context.global_enums.find(enum_);
  667. if (enum_match) {
  668. enum_match->get().constants.push_back(constant);
  669. } else {
  670. enum_.constants.push_back(constant);
  671. r_context.global_enums.push_back(enum_);
  672. }
  673. }
  674. }
  675. for (const EnumData &E : r_context.global_enums) {
  676. r_context.enum_types.push_back(E.name);
  677. }
  678. }
  679. // HARDCODED
  680. List<StringName> hardcoded_enums;
  681. hardcoded_enums.push_back("Vector2.Axis");
  682. hardcoded_enums.push_back("Vector2i.Axis");
  683. hardcoded_enums.push_back("Vector3.Axis");
  684. hardcoded_enums.push_back("Vector3i.Axis");
  685. for (const StringName &E : hardcoded_enums) {
  686. // These enums are not generated and must be written manually (e.g.: Vector3.Axis)
  687. // Here, we assume core types do not begin with underscore
  688. r_context.enum_types.push_back(E);
  689. }
  690. }
  691. TEST_SUITE("[ClassDB]") {
  692. TEST_CASE("[ClassDB] Add exposed classes, builtin types, and global enums") {
  693. Context context;
  694. add_exposed_classes(context);
  695. add_builtin_types(context);
  696. add_global_enums(context);
  697. SUBCASE("[ClassDB] Validate exposed classes") {
  698. const ExposedClass *object_class = context.find_exposed_class(context.names_cache.object_class);
  699. TEST_FAIL_COND(!object_class, "Object class not found.");
  700. TEST_FAIL_COND(object_class->base != StringName(),
  701. "Object class derives from another class: '", object_class->base, "'.");
  702. for (const KeyValue<StringName, ExposedClass> &E : context.exposed_classes) {
  703. validate_class(context, E.value);
  704. }
  705. }
  706. }
  707. }
  708. } // namespace TestClassDB
  709. #endif // TEST_CLASS_DB_H