test_main.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*************************************************************************/
  2. /* test_main.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_main.h"
  31. #include "tests/core/io/test_config_file.h"
  32. #include "tests/core/io/test_file_access.h"
  33. #include "tests/core/io/test_image.h"
  34. #include "tests/core/io/test_json.h"
  35. #include "tests/core/io/test_marshalls.h"
  36. #include "tests/core/io/test_pck_packer.h"
  37. #include "tests/core/io/test_resource.h"
  38. #include "tests/core/io/test_xml_parser.h"
  39. #include "tests/core/math/test_aabb.h"
  40. #include "tests/core/math/test_astar.h"
  41. #include "tests/core/math/test_basis.h"
  42. #include "tests/core/math/test_color.h"
  43. #include "tests/core/math/test_expression.h"
  44. #include "tests/core/math/test_geometry_2d.h"
  45. #include "tests/core/math/test_geometry_3d.h"
  46. #include "tests/core/math/test_math.h"
  47. #include "tests/core/math/test_random_number_generator.h"
  48. #include "tests/core/math/test_rect2.h"
  49. #include "tests/core/math/test_rect2i.h"
  50. #include "tests/core/math/test_vector2.h"
  51. #include "tests/core/math/test_vector2i.h"
  52. #include "tests/core/math/test_vector3.h"
  53. #include "tests/core/math/test_vector3i.h"
  54. #include "tests/core/object/test_class_db.h"
  55. #include "tests/core/object/test_method_bind.h"
  56. #include "tests/core/object/test_object.h"
  57. #include "tests/core/string/test_node_path.h"
  58. #include "tests/core/string/test_string.h"
  59. #include "tests/core/string/test_translation.h"
  60. #include "tests/core/templates/test_command_queue.h"
  61. #include "tests/core/templates/test_list.h"
  62. #include "tests/core/templates/test_local_vector.h"
  63. #include "tests/core/templates/test_lru.h"
  64. #include "tests/core/templates/test_oa_hash_map.h"
  65. #include "tests/core/templates/test_ordered_hash_map.h"
  66. #include "tests/core/templates/test_paged_array.h"
  67. #include "tests/core/templates/test_vector.h"
  68. #include "tests/core/test_crypto.h"
  69. #include "tests/core/test_hashing_context.h"
  70. #include "tests/core/test_time.h"
  71. #include "tests/core/variant/test_array.h"
  72. #include "tests/core/variant/test_dictionary.h"
  73. #include "tests/core/variant/test_variant.h"
  74. #include "tests/scene/test_animation.h"
  75. #include "tests/scene/test_code_edit.h"
  76. #include "tests/scene/test_curve.h"
  77. #include "tests/scene/test_gradient.h"
  78. #include "tests/scene/test_gui.h"
  79. #include "tests/scene/test_path_3d.h"
  80. #include "tests/servers/test_physics_2d.h"
  81. #include "tests/servers/test_physics_3d.h"
  82. #include "tests/servers/test_render.h"
  83. #include "tests/servers/test_shader_lang.h"
  84. #include "tests/servers/test_text_server.h"
  85. #include "tests/test_validate_testing.h"
  86. #include "modules/modules_tests.gen.h"
  87. #include "tests/test_macros.h"
  88. #include "scene/resources/default_theme/default_theme.h"
  89. int test_main(int argc, char *argv[]) {
  90. bool run_tests = true;
  91. // Convert arguments to Godot's command-line.
  92. List<String> args;
  93. for (int i = 0; i < argc; i++) {
  94. args.push_back(String::utf8(argv[i]));
  95. }
  96. OS::get_singleton()->set_cmdline("", args);
  97. // Run custom test tools.
  98. if (test_commands) {
  99. for (Map<String, TestFunc>::Element *E = test_commands->front(); E; E = E->next()) {
  100. if (args.find(E->key())) {
  101. const TestFunc &test_func = E->get();
  102. test_func();
  103. run_tests = false;
  104. break;
  105. }
  106. }
  107. if (!run_tests) {
  108. delete test_commands;
  109. return 0;
  110. }
  111. }
  112. // Doctest runner.
  113. doctest::Context test_context;
  114. List<String> test_args;
  115. // Clean arguments of "--test" from the args.
  116. for (int x = 0; x < argc; x++) {
  117. String arg = String(argv[x]);
  118. if (arg != "--test") {
  119. test_args.push_back(arg);
  120. }
  121. }
  122. if (test_args.size() > 0) {
  123. // Convert Godot command line arguments back to standard arguments.
  124. char **doctest_args = new char *[test_args.size()];
  125. for (int x = 0; x < test_args.size(); x++) {
  126. // Operation to convert Godot string to non wchar string.
  127. CharString cs = test_args[x].utf8();
  128. const char *str = cs.get_data();
  129. // Allocate the string copy.
  130. doctest_args[x] = new char[strlen(str) + 1];
  131. // Copy this into memory.
  132. memcpy(doctest_args[x], str, strlen(str) + 1);
  133. }
  134. test_context.applyCommandLine(test_args.size(), doctest_args);
  135. for (int x = 0; x < test_args.size(); x++) {
  136. delete[] doctest_args[x];
  137. }
  138. delete[] doctest_args;
  139. }
  140. return test_context.run();
  141. }
  142. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  143. #include "servers/navigation_server_2d.h"
  144. #include "servers/navigation_server_3d.h"
  145. #include "servers/rendering/rendering_server_default.h"
  146. struct GodotTestCaseListener : public doctest::IReporter {
  147. GodotTestCaseListener(const doctest::ContextOptions &p_in) {}
  148. SignalWatcher *signal_watcher = nullptr;
  149. PhysicsServer3D *physics_3d_server = nullptr;
  150. PhysicsServer2D *physics_2d_server = nullptr;
  151. NavigationServer3D *navigation_3d_server = nullptr;
  152. NavigationServer2D *navigation_2d_server = nullptr;
  153. void test_case_start(const doctest::TestCaseData &p_in) override {
  154. SignalWatcher::get_singleton()->_clear_signals();
  155. String name = String(p_in.m_name);
  156. if (name.find("[SceneTree]") != -1) {
  157. GLOBAL_DEF("memory/limits/multithreaded_server/rid_pool_prealloc", 60);
  158. memnew(MessageQueue);
  159. GLOBAL_DEF("internationalization/rendering/force_right_to_left_layout_direction", false);
  160. Error err = OK;
  161. OS::get_singleton()->set_has_server_feature_callback(nullptr);
  162. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  163. if (String("headless") == DisplayServer::get_create_function_name(i)) {
  164. DisplayServer::create(i, "", DisplayServer::WindowMode::WINDOW_MODE_MINIMIZED, DisplayServer::VSyncMode::VSYNC_ENABLED, 0, Vector2i(0, 0), err);
  165. break;
  166. }
  167. }
  168. memnew(RenderingServerDefault());
  169. RenderingServerDefault::get_singleton()->init();
  170. RenderingServerDefault::get_singleton()->set_render_loop_enabled(false);
  171. physics_3d_server = PhysicsServer3DManager::new_default_server();
  172. physics_3d_server->init();
  173. physics_2d_server = PhysicsServer2DManager::new_default_server();
  174. physics_2d_server->init();
  175. navigation_3d_server = NavigationServer3DManager::new_default_server();
  176. navigation_2d_server = memnew(NavigationServer2D);
  177. memnew(InputMap);
  178. InputMap::get_singleton()->load_default();
  179. make_default_theme(1.0, Ref<Font>());
  180. memnew(SceneTree);
  181. SceneTree::get_singleton()->initialize();
  182. return;
  183. }
  184. }
  185. void test_case_end(const doctest::CurrentTestCaseStats &) override {
  186. if (SceneTree::get_singleton()) {
  187. SceneTree::get_singleton()->finalize();
  188. }
  189. if (MessageQueue::get_singleton()) {
  190. MessageQueue::get_singleton()->flush();
  191. }
  192. if (SceneTree::get_singleton()) {
  193. memdelete(SceneTree::get_singleton());
  194. }
  195. clear_default_theme();
  196. if (navigation_3d_server) {
  197. memdelete(navigation_3d_server);
  198. navigation_3d_server = nullptr;
  199. }
  200. if (navigation_2d_server) {
  201. memdelete(navigation_2d_server);
  202. navigation_2d_server = nullptr;
  203. }
  204. if (physics_3d_server) {
  205. physics_3d_server->finish();
  206. memdelete(physics_3d_server);
  207. physics_3d_server = nullptr;
  208. }
  209. if (physics_2d_server) {
  210. physics_2d_server->finish();
  211. memdelete(physics_2d_server);
  212. physics_2d_server = nullptr;
  213. }
  214. if (RenderingServer::get_singleton()) {
  215. RenderingServer::get_singleton()->sync();
  216. RenderingServer::get_singleton()->global_variables_clear();
  217. RenderingServer::get_singleton()->finish();
  218. memdelete(RenderingServer::get_singleton());
  219. }
  220. if (DisplayServer::get_singleton()) {
  221. memdelete(DisplayServer::get_singleton());
  222. }
  223. if (InputMap::get_singleton()) {
  224. memdelete(InputMap::get_singleton());
  225. }
  226. if (MessageQueue::get_singleton()) {
  227. MessageQueue::get_singleton()->flush();
  228. memdelete(MessageQueue::get_singleton());
  229. }
  230. }
  231. void test_run_start() override {
  232. signal_watcher = memnew(SignalWatcher);
  233. }
  234. void test_run_end(const doctest::TestRunStats &) override {
  235. memdelete(signal_watcher);
  236. }
  237. void test_case_reenter(const doctest::TestCaseData &) override {
  238. SignalWatcher::get_singleton()->_clear_signals();
  239. }
  240. void subcase_start(const doctest::SubcaseSignature &) override {
  241. SignalWatcher::get_singleton()->_clear_signals();
  242. }
  243. void report_query(const doctest::QueryData &) override {}
  244. void test_case_exception(const doctest::TestCaseException &) override {}
  245. void subcase_end() override {}
  246. void log_assert(const doctest::AssertData &in) override {}
  247. void log_message(const doctest::MessageData &) override {}
  248. void test_case_skipped(const doctest::TestCaseData &) override {}
  249. };
  250. REGISTER_LISTENER("GodotTestCaseListener", 1, GodotTestCaseListener);