test_class_db.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /*************************************************************************/
  2. /* test_class_db.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #include "test_class_db.h"
  31. #include "core/global_constants.h"
  32. #include "core/ordered_hash_map.h"
  33. #include "core/os/os.h"
  34. #include "core/string_name.h"
  35. #include "core/ustring.h"
  36. #include "core/variant.h"
  37. namespace TestClassDB {
  38. enum class [[nodiscard]] TestResult{
  39. FAILED,
  40. PASS
  41. };
  42. #define TEST_FAIL_COND_FATAL(m_cond, m_msg) \
  43. if (unlikely(m_cond)) { \
  44. ERR_PRINT(m_msg); \
  45. return TestResult::FAILED; \
  46. } else \
  47. ((void)0)
  48. #define TEST_FAIL_COND(m_cond, m_msg) \
  49. if (unlikely(m_cond)) { \
  50. ERR_PRINT(m_msg); \
  51. __test_result__ = TestResult::FAILED; \
  52. } else \
  53. ((void)0)
  54. #define TEST_CHECK_FATAL(m_test_expr) \
  55. if (unlikely((m_test_expr) == TestResult::FAILED)) { \
  56. return TestResult::FAILED; \
  57. } else \
  58. ((void)0)
  59. #define TEST_CHECK(m_test_expr) \
  60. if (unlikely((m_test_expr) == TestResult::FAILED)) { \
  61. __test_result__ = TestResult::FAILED; \
  62. } else \
  63. ((void)0)
  64. #define TEST_START() \
  65. TestResult __test_result__ = TestResult::PASS; \
  66. ((void)0)
  67. #define TEST_END() return __test_result__;
  68. struct TypeReference {
  69. StringName name;
  70. bool is_enum = false;
  71. };
  72. struct ConstantData {
  73. String name;
  74. int value = 0;
  75. };
  76. struct EnumData {
  77. StringName name;
  78. List<ConstantData> constants;
  79. _FORCE_INLINE_ bool operator==(const EnumData &p_enum) const {
  80. return p_enum.name == name;
  81. }
  82. };
  83. struct PropertyData {
  84. StringName name;
  85. int index = 0;
  86. StringName getter;
  87. StringName setter;
  88. };
  89. struct ArgumentData {
  90. TypeReference type;
  91. String name;
  92. bool has_defval = false;
  93. Variant defval;
  94. };
  95. struct MethodData {
  96. StringName name;
  97. TypeReference return_type;
  98. List<ArgumentData> arguments;
  99. bool is_virtual = false;
  100. bool is_vararg = false;
  101. };
  102. struct SignalData {
  103. StringName name;
  104. List<ArgumentData> arguments;
  105. };
  106. struct ExposedClass {
  107. StringName name;
  108. StringName base;
  109. bool is_singleton = false;
  110. bool is_instantiable = false;
  111. bool is_reference = false;
  112. ClassDB::APIType api_type;
  113. List<ConstantData> constants;
  114. List<EnumData> enums;
  115. List<PropertyData> properties;
  116. List<MethodData> methods;
  117. List<SignalData> signals_;
  118. const PropertyData *find_property_by_name(const StringName &p_name) const {
  119. for (const List<PropertyData>::Element *E = properties.front(); E; E = E->next()) {
  120. if (E->get().name == p_name) {
  121. return &E->get();
  122. }
  123. }
  124. return nullptr;
  125. }
  126. const MethodData *find_method_by_name(const StringName &p_name) const {
  127. for (const List<MethodData>::Element *E = methods.front(); E; E = E->next()) {
  128. if (E->get().name == p_name) {
  129. return &E->get();
  130. }
  131. }
  132. return nullptr;
  133. }
  134. };
  135. struct NamesCache {
  136. StringName variant_type = StaticCString::create("Variant");
  137. StringName object_class = StaticCString::create("Object");
  138. StringName reference_class = StaticCString::create("Reference");
  139. StringName string_type = StaticCString::create("String");
  140. StringName string_name_type = StaticCString::create("StringName");
  141. StringName node_path_type = StaticCString::create("NodePath");
  142. StringName bool_type = StaticCString::create("bool");
  143. StringName int_type = StaticCString::create("int");
  144. StringName float_type = StaticCString::create("float");
  145. StringName void_type = StaticCString::create("void");
  146. StringName vararg_stub_type = StaticCString::create("@VarArg@");
  147. StringName vector2_type = StaticCString::create("Vector2");
  148. StringName rect2_type = StaticCString::create("Rect2");
  149. StringName vector3_type = StaticCString::create("Vector3");
  150. // Object not included as it must be checked for all derived classes
  151. static constexpr int nullable_types_count = 17;
  152. StringName nullable_types[nullable_types_count] = {
  153. string_type,
  154. string_name_type,
  155. node_path_type,
  156. StaticCString::create(_STR(Array)),
  157. StaticCString::create(_STR(Dictionary)),
  158. StaticCString::create(_STR(Callable)),
  159. StaticCString::create(_STR(Signal)),
  160. StaticCString::create(_STR(PackedByteArray)),
  161. StaticCString::create(_STR(PackedInt32Array)),
  162. StaticCString::create(_STR(PackedInt64rray)),
  163. StaticCString::create(_STR(PackedFloat32Array)),
  164. StaticCString::create(_STR(PackedFloat64Array)),
  165. StaticCString::create(_STR(PackedStringArray)),
  166. StaticCString::create(_STR(PackedVector2Array)),
  167. StaticCString::create(_STR(PackedVector3Array)),
  168. StaticCString::create(_STR(PackedColorArray)),
  169. };
  170. bool is_nullable_type(const StringName &p_type) const {
  171. for (int i = 0; i < nullable_types_count; i++) {
  172. if (p_type == nullable_types[i]) {
  173. return true;
  174. }
  175. }
  176. return false;
  177. }
  178. };
  179. typedef OrderedHashMap<StringName, ExposedClass> ExposedClasses;
  180. struct Context {
  181. Vector<StringName> enum_types;
  182. Vector<StringName> builtin_types;
  183. ExposedClasses exposed_classes;
  184. List<EnumData> global_enums;
  185. NamesCache names_cache;
  186. const ExposedClass *find_exposed_class(const StringName &p_name) const {
  187. ExposedClasses::ConstElement elem = exposed_classes.find(p_name);
  188. return elem ? &elem.value() : nullptr;
  189. }
  190. const ExposedClass *find_exposed_class(const TypeReference &p_type_ref) const {
  191. ExposedClasses::ConstElement elem = exposed_classes.find(p_type_ref.name);
  192. return elem ? &elem.value() : nullptr;
  193. }
  194. bool has_type(const Context &p_context, const TypeReference &p_type_ref) const {
  195. if (p_context.builtin_types.find(p_type_ref.name) >= 0) {
  196. return true;
  197. }
  198. if (p_type_ref.is_enum) {
  199. if (p_context.enum_types.find(p_type_ref.name) >= 0) {
  200. return true;
  201. }
  202. // Enum not found. Most likely because none of its constants were bound, so it's empty. That's fine. Use int instead.
  203. return p_context.builtin_types.find(p_context.names_cache.int_type);
  204. }
  205. return false;
  206. }
  207. };
  208. bool arg_default_value_is_assignable_to_type(const Context &p_context, const Variant &p_val, const TypeReference &p_arg_type) {
  209. if (p_arg_type.name == p_context.names_cache.variant_type) {
  210. // Variant can take anything
  211. return true;
  212. }
  213. switch (p_val.get_type()) {
  214. case Variant::NIL:
  215. return p_context.find_exposed_class(p_arg_type) ||
  216. p_context.names_cache.is_nullable_type(p_arg_type.name);
  217. case Variant::BOOL:
  218. return p_arg_type.name == p_context.names_cache.bool_type;
  219. case Variant::INT:
  220. return p_arg_type.name == p_context.names_cache.int_type ||
  221. p_arg_type.name == p_context.names_cache.float_type ||
  222. p_arg_type.is_enum;
  223. case Variant::FLOAT:
  224. return p_arg_type.name == p_context.names_cache.float_type;
  225. case Variant::STRING:
  226. case Variant::STRING_NAME:
  227. return p_arg_type.name == p_context.names_cache.string_type ||
  228. p_arg_type.name == p_context.names_cache.string_name_type ||
  229. p_arg_type.name == p_context.names_cache.node_path_type;
  230. case Variant::NODE_PATH:
  231. return p_arg_type.name == p_context.names_cache.node_path_type;
  232. case Variant::TRANSFORM:
  233. case Variant::TRANSFORM2D:
  234. case Variant::BASIS:
  235. case Variant::QUAT:
  236. case Variant::PLANE:
  237. case Variant::AABB:
  238. case Variant::COLOR:
  239. case Variant::VECTOR2:
  240. case Variant::RECT2:
  241. case Variant::VECTOR3:
  242. case Variant::_RID:
  243. case Variant::ARRAY:
  244. case Variant::DICTIONARY:
  245. case Variant::PACKED_BYTE_ARRAY:
  246. case Variant::PACKED_INT32_ARRAY:
  247. case Variant::PACKED_INT64_ARRAY:
  248. case Variant::PACKED_FLOAT32_ARRAY:
  249. case Variant::PACKED_FLOAT64_ARRAY:
  250. case Variant::PACKED_STRING_ARRAY:
  251. case Variant::PACKED_VECTOR2_ARRAY:
  252. case Variant::PACKED_VECTOR3_ARRAY:
  253. case Variant::PACKED_COLOR_ARRAY:
  254. case Variant::CALLABLE:
  255. case Variant::SIGNAL:
  256. return p_arg_type.name == Variant::get_type_name(p_val.get_type());
  257. case Variant::OBJECT:
  258. return p_context.find_exposed_class(p_arg_type);
  259. case Variant::VECTOR2I:
  260. return p_arg_type.name == p_context.names_cache.vector2_type ||
  261. p_arg_type.name == Variant::get_type_name(p_val.get_type());
  262. case Variant::RECT2I:
  263. return p_arg_type.name == p_context.names_cache.rect2_type ||
  264. p_arg_type.name == Variant::get_type_name(p_val.get_type());
  265. case Variant::VECTOR3I:
  266. return p_arg_type.name == p_context.names_cache.vector3_type ||
  267. p_arg_type.name == Variant::get_type_name(p_val.get_type());
  268. default:
  269. ERR_PRINT("Unexpected Variant type: " + itos(p_val.get_type()));
  270. break;
  271. }
  272. return false;
  273. }
  274. TestResult validate_property(const Context &p_context, const ExposedClass &p_class, const PropertyData &p_prop) {
  275. TEST_START();
  276. const MethodData *setter = p_class.find_method_by_name(p_prop.setter);
  277. // Search it in base classes too
  278. const ExposedClass *top = &p_class;
  279. while (!setter && top->base != StringName()) {
  280. top = p_context.find_exposed_class(top->base);
  281. TEST_FAIL_COND(!top, "Class not found '" + top->base + "'. Inherited by '" + top->name + "'.");
  282. setter = top->find_method_by_name(p_prop.setter);
  283. }
  284. const MethodData *getter = p_class.find_method_by_name(p_prop.getter);
  285. // Search it in base classes too
  286. top = &p_class;
  287. while (!getter && top->base != StringName()) {
  288. top = p_context.find_exposed_class(top->base);
  289. TEST_FAIL_COND(!top, "Class not found '" + top->base + "'. Inherited by '" + top->name + "'.");
  290. getter = top->find_method_by_name(p_prop.getter);
  291. }
  292. TEST_FAIL_COND(!setter && !getter,
  293. "Couldn't find neither the setter nor the getter for property: '" + p_class.name + "." + String(p_prop.name) + "'.");
  294. if (setter) {
  295. int setter_argc = p_prop.index != -1 ? 2 : 1;
  296. TEST_FAIL_COND(setter->arguments.size() != setter_argc,
  297. "Invalid property setter argument count: '" + p_class.name + "." + String(p_prop.name) + "'.");
  298. }
  299. if (getter) {
  300. int getter_argc = p_prop.index != -1 ? 1 : 0;
  301. TEST_FAIL_COND(getter->arguments.size() != getter_argc,
  302. "Invalid property setter argument count: '" + p_class.name + "." + String(p_prop.name) + "'.");
  303. }
  304. if (getter && setter) {
  305. const ArgumentData &setter_first_arg = setter->arguments.back()->get();
  306. if (getter->return_type.name != setter_first_arg.type.name) {
  307. // Special case for Node::set_name
  308. bool whitelisted = getter->return_type.name == p_context.names_cache.string_name_type &&
  309. setter_first_arg.type.name == p_context.names_cache.string_type;
  310. TEST_FAIL_COND(!whitelisted,
  311. "Return type from getter doesn't match first argument of setter, for property: '" + p_class.name + "." + String(p_prop.name) + "'.");
  312. }
  313. }
  314. const TypeReference &prop_type_ref = getter ? getter->return_type : setter->arguments.back()->get().type;
  315. const ExposedClass *prop_class = p_context.find_exposed_class(prop_type_ref);
  316. if (prop_class) {
  317. TEST_FAIL_COND(prop_class->is_singleton,
  318. "Property type is a singleton: '" + p_class.name + "." + String(p_prop.name) + "'.");
  319. } else {
  320. TEST_FAIL_COND(!p_context.has_type(p_context, prop_type_ref),
  321. "Property type '" + prop_type_ref.name + "' not found: '" + p_class.name + "." + String(p_prop.name) + "'.");
  322. }
  323. if (getter) {
  324. if (p_prop.index != -1) {
  325. const ArgumentData &idx_arg = getter->arguments.front()->get();
  326. if (idx_arg.type.name != p_context.names_cache.int_type) {
  327. // If not an int, it can be an enum
  328. TEST_FAIL_COND(p_context.enum_types.find(idx_arg.type.name) < 0,
  329. "Invalid type '" + idx_arg.type.name + "' for index argument of property getter: '" + p_class.name + "." + String(p_prop.name) + "'.");
  330. }
  331. }
  332. }
  333. if (setter) {
  334. if (p_prop.index != -1) {
  335. const ArgumentData &idx_arg = setter->arguments.front()->get();
  336. if (idx_arg.type.name != p_context.names_cache.int_type) {
  337. // Assume the index parameter is an enum
  338. // If not an int, it can be an enum
  339. TEST_FAIL_COND(p_context.enum_types.find(idx_arg.type.name) < 0,
  340. "Invalid type '" + idx_arg.type.name + "' for index argument of property setter: '" + p_class.name + "." + String(p_prop.name) + "'.");
  341. }
  342. }
  343. }
  344. TEST_END();
  345. }
  346. TestResult validate_method(const Context &p_context, const ExposedClass &p_class, const MethodData &p_method) {
  347. TEST_START();
  348. const ExposedClass *return_class = p_context.find_exposed_class(p_method.return_type);
  349. if (return_class) {
  350. TEST_FAIL_COND(return_class->is_singleton,
  351. "Method return type is a singleton: '" + p_class.name + "." + p_method.name + "'.");
  352. }
  353. for (const List<ArgumentData>::Element *F = p_method.arguments.front(); F; F = F->next()) {
  354. const ArgumentData &arg = F->get();
  355. const ExposedClass *arg_class = p_context.find_exposed_class(arg.type);
  356. if (arg_class) {
  357. TEST_FAIL_COND(arg_class->is_singleton,
  358. "Argument type is a singleton: '" + arg.name + "' of method '" + p_class.name + "." + p_method.name + "'.");
  359. } else {
  360. TEST_FAIL_COND(!p_context.has_type(p_context, arg.type),
  361. "Argument type '" + arg.type.name + "' not found: '" + arg.name + "' of method" + p_class.name + "." + p_method.name + "'.");
  362. }
  363. if (arg.has_defval) {
  364. TEST_FAIL_COND(!arg_default_value_is_assignable_to_type(p_context, arg.defval, arg.type),
  365. "Invalid default value for parameter '" + arg.name + "' of method '" + p_class.name + "." + p_method.name + "'.");
  366. }
  367. }
  368. TEST_END();
  369. }
  370. TestResult validate_signal(const Context &p_context, const ExposedClass &p_class, const SignalData &p_signal) {
  371. TEST_START();
  372. for (const List<ArgumentData>::Element *F = p_signal.arguments.front(); F; F = F->next()) {
  373. const ArgumentData &arg = F->get();
  374. const ExposedClass *arg_class = p_context.find_exposed_class(arg.type);
  375. if (arg_class) {
  376. TEST_FAIL_COND(arg_class->is_singleton,
  377. "Argument class is a singleton: '" + arg.name + "' of signal" + p_class.name + "." + p_signal.name + "'.");
  378. } else {
  379. TEST_FAIL_COND(!p_context.has_type(p_context, arg.type),
  380. "Argument type '" + arg.type.name + "' not found: '" + arg.name + "' of signal" + p_class.name + "." + p_signal.name + "'.");
  381. }
  382. }
  383. TEST_END();
  384. }
  385. TestResult validate_class(const Context &p_context, const ExposedClass &p_exposed_class) {
  386. TEST_START();
  387. bool is_derived_type = p_exposed_class.base != StringName();
  388. if (!is_derived_type) {
  389. // Asserts about the base Object class
  390. TEST_FAIL_COND_FATAL(p_exposed_class.name != p_context.names_cache.object_class,
  391. "Class '" + p_exposed_class.name + "' has no base class.");
  392. TEST_FAIL_COND_FATAL(!p_exposed_class.is_instantiable,
  393. "Object class is not instantiable.");
  394. TEST_FAIL_COND_FATAL(p_exposed_class.api_type != ClassDB::API_CORE,
  395. "Object class is API is not API_CORE.");
  396. TEST_FAIL_COND_FATAL(p_exposed_class.is_singleton,
  397. "Object class is registered as a singleton.");
  398. }
  399. CRASH_COND_MSG(p_exposed_class.is_singleton && p_exposed_class.base != p_context.names_cache.object_class,
  400. "Singleton base class '" + String(p_exposed_class.base) + "' is not Object, for class '" + p_exposed_class.name + "'.");
  401. CRASH_COND_MSG(is_derived_type && !p_context.exposed_classes.has(p_exposed_class.base),
  402. "Base type '" + p_exposed_class.base.operator String() + "' does not exist, for class '" + p_exposed_class.name + "'.");
  403. for (const List<PropertyData>::Element *F = p_exposed_class.properties.front(); F; F = F->next()) {
  404. TEST_CHECK(validate_property(p_context, p_exposed_class, F->get()));
  405. }
  406. for (const List<MethodData>::Element *F = p_exposed_class.methods.front(); F; F = F->next()) {
  407. TEST_CHECK(validate_method(p_context, p_exposed_class, F->get()));
  408. }
  409. for (const List<SignalData>::Element *F = p_exposed_class.signals_.front(); F; F = F->next()) {
  410. TEST_CHECK(validate_signal(p_context, p_exposed_class, F->get()));
  411. }
  412. TEST_END();
  413. }
  414. TestResult add_exposed_classes(Context &r_context) {
  415. TEST_START();
  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. OS::get_singleton()->print("Ignoring class '%s' because it's not exposed\n", String(class_name).utf8().get_data());
  428. class_list.pop_front();
  429. continue;
  430. }
  431. if (!ClassDB::is_class_enabled(class_name)) {
  432. OS::get_singleton()->print("Ignoring class '%s' because it's not enabled\n", String(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.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. if (exposed_class.name != r_context.names_cache.object_class || String(method.name) != "free") {
  499. WARN_PRINT("Notification: New unexpected virtual non-overridable method found."
  500. " We only expected Object.free, but found '" +
  501. exposed_class.name + "." + method.name + "'.");
  502. }
  503. } else if (return_info.type == Variant::INT && return_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  504. method.return_type.name = return_info.class_name;
  505. method.return_type.is_enum = true;
  506. } else if (return_info.class_name != StringName()) {
  507. method.return_type.name = return_info.class_name;
  508. bool bad_reference_hint = !method.is_virtual && return_info.hint != PROPERTY_HINT_RESOURCE_TYPE &&
  509. ClassDB::is_parent_class(return_info.class_name, r_context.names_cache.reference_class);
  510. TEST_FAIL_COND(bad_reference_hint, String() + "Return type is reference but hint is not '" _STR(PROPERTY_HINT_RESOURCE_TYPE) "'." +
  511. " Are you returning a reference type by pointer? Method: '" +
  512. exposed_class.name + "." + method.name + "'.");
  513. } else if (return_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  514. method.return_type.name = return_info.hint_string;
  515. } else if (return_info.type == Variant::NIL && return_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
  516. method.return_type.name = r_context.names_cache.variant_type;
  517. } else if (return_info.type == Variant::NIL) {
  518. method.return_type.name = r_context.names_cache.void_type;
  519. } else {
  520. // NOTE: We don't care about the size and sign of int and float in these tests
  521. method.return_type.name = Variant::get_type_name(return_info.type);
  522. }
  523. for (int i = 0; i < argc; i++) {
  524. PropertyInfo arg_info = method_info.arguments[i];
  525. String orig_arg_name = arg_info.name;
  526. ArgumentData arg;
  527. arg.name = orig_arg_name;
  528. if (arg_info.type == Variant::INT && arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  529. arg.type.name = arg_info.class_name;
  530. arg.type.is_enum = true;
  531. } else if (arg_info.class_name != StringName()) {
  532. arg.type.name = arg_info.class_name;
  533. } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  534. arg.type.name = arg_info.hint_string;
  535. } else if (arg_info.type == Variant::NIL) {
  536. arg.type.name = r_context.names_cache.variant_type;
  537. } else {
  538. // NOTE: We don't care about the size and sign of int and float in these tests
  539. arg.type.name = Variant::get_type_name(arg_info.type);
  540. }
  541. if (m && m->has_default_argument(i)) {
  542. arg.has_defval = true;
  543. arg.defval = m->get_default_argument(i);
  544. }
  545. method.arguments.push_back(arg);
  546. }
  547. if (method.is_vararg) {
  548. ArgumentData vararg;
  549. vararg.type.name = r_context.names_cache.vararg_stub_type;
  550. vararg.name = "@varargs@";
  551. method.arguments.push_back(vararg);
  552. }
  553. TEST_FAIL_COND(exposed_class.find_property_by_name(method.name),
  554. "Method name conflicts with property: '" + String(class_name) + "." + String(method.name) + "'.");
  555. // Classes starting with an underscore are ignored unless they're used as a property setter or getter
  556. if (!method.is_virtual && String(method.name)[0] == '_') {
  557. for (const List<PropertyData>::Element *F = exposed_class.properties.front(); F; F = F->next()) {
  558. const PropertyData &prop = F->get();
  559. if (prop.setter == method.name || prop.getter == method.name) {
  560. exposed_class.methods.push_back(method);
  561. break;
  562. }
  563. }
  564. } else {
  565. exposed_class.methods.push_back(method);
  566. }
  567. }
  568. // Add signals
  569. const HashMap<StringName, MethodInfo> &signal_map = class_info->signal_map;
  570. const StringName *k = nullptr;
  571. while ((k = signal_map.next(k))) {
  572. SignalData signal;
  573. const MethodInfo &method_info = signal_map.get(*k);
  574. signal.name = method_info.name;
  575. int argc = method_info.arguments.size();
  576. for (int i = 0; i < argc; i++) {
  577. PropertyInfo arg_info = method_info.arguments[i];
  578. String orig_arg_name = arg_info.name;
  579. ArgumentData arg;
  580. arg.name = orig_arg_name;
  581. if (arg_info.type == Variant::INT && arg_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  582. arg.type.name = arg_info.class_name;
  583. arg.type.is_enum = true;
  584. } else if (arg_info.class_name != StringName()) {
  585. arg.type.name = arg_info.class_name;
  586. } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  587. arg.type.name = arg_info.hint_string;
  588. } else if (arg_info.type == Variant::NIL) {
  589. arg.type.name = r_context.names_cache.variant_type;
  590. } else {
  591. // NOTE: We don't care about the size and sign of int and float in these tests
  592. arg.type.name = Variant::get_type_name(arg_info.type);
  593. }
  594. signal.arguments.push_back(arg);
  595. }
  596. bool method_conflict = exposed_class.find_property_by_name(signal.name);
  597. if (method_conflict || exposed_class.find_method_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. WARN_PRINT("Signal name conflicts with " + String(method_conflict ? "method" : "property") +
  604. ": '" + String(class_name) + "." + String(signal.name) + "'.");
  605. }
  606. exposed_class.signals_.push_back(signal);
  607. }
  608. // Add enums and constants
  609. List<String> constants;
  610. ClassDB::get_integer_constant_list(class_name, &constants, true);
  611. const HashMap<StringName, List<StringName>> &enum_map = class_info->enum_map;
  612. k = nullptr;
  613. while ((k = enum_map.next(k))) {
  614. EnumData enum_;
  615. enum_.name = *k;
  616. const List<StringName> &enum_constants = enum_map.get(*k);
  617. for (const List<StringName>::Element *E = enum_constants.front(); E; E = E->next()) {
  618. const StringName &constant_name = E->get();
  619. int *value = class_info->constant_map.getptr(constant_name);
  620. TEST_FAIL_COND(!value, "Missing enum constant value: '" +
  621. String(class_name) + "." + String(enum_.name) + "." + String(constant_name) + "'.");
  622. constants.erase(constant_name);
  623. ConstantData constant;
  624. constant.name = constant_name;
  625. constant.value = *value;
  626. enum_.constants.push_back(constant);
  627. }
  628. exposed_class.enums.push_back(enum_);
  629. r_context.enum_types.push_back(String(class_name) + "." + String(*k));
  630. }
  631. for (const List<String>::Element *E = constants.front(); E; E = E->next()) {
  632. const String &constant_name = E->get();
  633. int *value = class_info->constant_map.getptr(StringName(E->get()));
  634. TEST_FAIL_COND(!value, "Missing enum constant value: '" + String(class_name) + "." + String(constant_name) + "'.");
  635. ConstantData constant;
  636. constant.name = constant_name;
  637. constant.value = *value;
  638. exposed_class.constants.push_back(constant);
  639. }
  640. r_context.exposed_classes.insert(class_name, exposed_class);
  641. class_list.pop_front();
  642. }
  643. TEST_END();
  644. }
  645. void add_builtin_types(Context &r_context) {
  646. // NOTE: We don't care about the size and sign of int and float in these tests
  647. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  648. r_context.builtin_types.push_back(Variant::get_type_name(Variant::Type(i)));
  649. }
  650. r_context.builtin_types.push_back(_STR(Variant));
  651. r_context.builtin_types.push_back(r_context.names_cache.vararg_stub_type);
  652. r_context.builtin_types.push_back("void");
  653. }
  654. void add_global_enums(Context &r_context) {
  655. int global_constants_count = GlobalConstants::get_global_constant_count();
  656. if (global_constants_count > 0) {
  657. for (int i = 0; i < global_constants_count; i++) {
  658. StringName enum_name = GlobalConstants::get_global_constant_enum(i);
  659. if (enum_name != StringName()) {
  660. ConstantData constant;
  661. constant.name = GlobalConstants::get_global_constant_name(i);
  662. constant.value = GlobalConstants::get_global_constant_value(i);
  663. EnumData enum_;
  664. enum_.name = enum_name;
  665. List<EnumData>::Element *enum_match = r_context.global_enums.find(enum_);
  666. if (enum_match) {
  667. enum_match->get().constants.push_back(constant);
  668. } else {
  669. enum_.constants.push_back(constant);
  670. r_context.global_enums.push_back(enum_);
  671. }
  672. }
  673. }
  674. for (List<EnumData>::Element *E = r_context.global_enums.front(); E; E = E->next()) {
  675. r_context.enum_types.push_back(E->get().name);
  676. }
  677. }
  678. // HARDCODED
  679. List<StringName> hardcoded_enums;
  680. hardcoded_enums.push_back("Vector2.Axis");
  681. hardcoded_enums.push_back("Vector2i.Axis");
  682. hardcoded_enums.push_back("Vector3.Axis");
  683. hardcoded_enums.push_back("Vector3i.Axis");
  684. for (List<StringName>::Element *E = hardcoded_enums.front(); E; E = E->next()) {
  685. // These enums are not generated and must be written manually (e.g.: Vector3.Axis)
  686. // Here, we assume core types do not begin with underscore
  687. r_context.enum_types.push_back(E->get());
  688. }
  689. }
  690. TestResult run_class_db_tests() {
  691. TEST_START();
  692. Context context;
  693. TEST_CHECK_FATAL(add_exposed_classes(context));
  694. add_builtin_types(context);
  695. add_global_enums(context);
  696. const ExposedClass *object_class = context.find_exposed_class(context.names_cache.object_class);
  697. TEST_FAIL_COND_FATAL(!object_class, "Object class not found.");
  698. TEST_FAIL_COND_FATAL(object_class->base != StringName(),
  699. "Object class derives from another class: '" + object_class->base + "'.");
  700. for (ExposedClasses::Element E = context.exposed_classes.front(); E; E = E.next()) {
  701. TEST_CHECK(validate_class(context, E.value()));
  702. }
  703. TEST_END();
  704. }
  705. MainLoop *test() {
  706. TestResult pass = run_class_db_tests();
  707. OS::get_singleton()->print("ClassDB tests: %s\n", pass == TestResult::PASS ? "PASS" : "FAILED");
  708. if (pass == TestResult::FAILED) {
  709. OS::get_singleton()->set_exit_code(pass == TestResult::PASS ? 0 : 1);
  710. }
  711. return nullptr;
  712. }
  713. } // namespace TestClassDB