test_class_db.h 31 KB

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