test_macros.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /**************************************************************************/
  2. /* test_macros.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_MACROS_H
  31. #define TEST_MACROS_H
  32. #include "display_server_mock.h"
  33. #include "core/core_globals.h"
  34. #include "core/input/input_map.h"
  35. #include "core/object/message_queue.h"
  36. #include "core/variant/variant.h"
  37. // See documentation for doctest at:
  38. // https://github.com/onqtam/doctest/blob/master/doc/markdown/readme.md#reference
  39. #include "thirdparty/doctest/doctest.h"
  40. // The test is skipped with this, run pending tests with `--test --no-skip`.
  41. #define TEST_CASE_PENDING(name) TEST_CASE(name *doctest::skip())
  42. // The test case is marked as failed, but does not fail the entire test run.
  43. #define TEST_CASE_MAY_FAIL(name) TEST_CASE(name *doctest::may_fail())
  44. // Provide aliases to conform with Godot naming conventions (see error macros).
  45. #define TEST_COND(cond, ...) DOCTEST_CHECK_FALSE_MESSAGE(cond, __VA_ARGS__)
  46. #define TEST_FAIL(cond, ...) DOCTEST_FAIL(cond, __VA_ARGS__)
  47. #define TEST_FAIL_COND(cond, ...) DOCTEST_REQUIRE_FALSE_MESSAGE(cond, __VA_ARGS__)
  48. #define TEST_FAIL_COND_WARN(cond, ...) DOCTEST_WARN_FALSE_MESSAGE(cond, __VA_ARGS__)
  49. // Temporarily disable error prints to test failure paths.
  50. // This allows to avoid polluting the test summary with error messages.
  51. // The `print_error_enabled` boolean is defined in `core/core_globals.cpp` and
  52. // works at global scope. It's used by various loggers in `should_log()` method,
  53. // which are used by error macros which call into `OS::print_error`, effectively
  54. // disabling any error messages to be printed from the engine side (not tests).
  55. #define ERR_PRINT_OFF CoreGlobals::print_error_enabled = false;
  56. #define ERR_PRINT_ON CoreGlobals::print_error_enabled = true;
  57. // Stringify all `Variant` compatible types for doctest output by default.
  58. // https://github.com/onqtam/doctest/blob/master/doc/markdown/stringification.md
  59. #define DOCTEST_STRINGIFY_VARIANT(m_type) \
  60. template <> \
  61. struct doctest::StringMaker<m_type> { \
  62. static doctest::String convert(const m_type &p_val) { \
  63. const Variant val = p_val; \
  64. return val.operator ::String().utf8().get_data(); \
  65. } \
  66. };
  67. #define DOCTEST_STRINGIFY_VARIANT_POINTER(m_type) \
  68. template <> \
  69. struct doctest::StringMaker<m_type> { \
  70. static doctest::String convert(const m_type *p_val) { \
  71. const Variant val = p_val; \
  72. return val.operator ::String().utf8().get_data(); \
  73. } \
  74. };
  75. DOCTEST_STRINGIFY_VARIANT(Variant);
  76. DOCTEST_STRINGIFY_VARIANT(::String); // Disambiguate from `doctest::String`.
  77. DOCTEST_STRINGIFY_VARIANT(Vector2);
  78. DOCTEST_STRINGIFY_VARIANT(Vector2i);
  79. DOCTEST_STRINGIFY_VARIANT(Rect2);
  80. DOCTEST_STRINGIFY_VARIANT(Rect2i);
  81. DOCTEST_STRINGIFY_VARIANT(Vector3);
  82. DOCTEST_STRINGIFY_VARIANT(Vector3i);
  83. DOCTEST_STRINGIFY_VARIANT(Transform2D);
  84. DOCTEST_STRINGIFY_VARIANT(Plane);
  85. DOCTEST_STRINGIFY_VARIANT(Quaternion);
  86. DOCTEST_STRINGIFY_VARIANT(AABB);
  87. DOCTEST_STRINGIFY_VARIANT(Basis);
  88. DOCTEST_STRINGIFY_VARIANT(Transform3D);
  89. DOCTEST_STRINGIFY_VARIANT(::Color); // Disambiguate from `doctest::Color`.
  90. DOCTEST_STRINGIFY_VARIANT(StringName);
  91. DOCTEST_STRINGIFY_VARIANT(NodePath);
  92. DOCTEST_STRINGIFY_VARIANT(RID);
  93. DOCTEST_STRINGIFY_VARIANT_POINTER(Object);
  94. DOCTEST_STRINGIFY_VARIANT(Callable);
  95. DOCTEST_STRINGIFY_VARIANT(Signal);
  96. DOCTEST_STRINGIFY_VARIANT(Dictionary);
  97. DOCTEST_STRINGIFY_VARIANT(Array);
  98. DOCTEST_STRINGIFY_VARIANT(PackedByteArray);
  99. DOCTEST_STRINGIFY_VARIANT(PackedInt32Array);
  100. DOCTEST_STRINGIFY_VARIANT(PackedInt64Array);
  101. DOCTEST_STRINGIFY_VARIANT(PackedFloat32Array);
  102. DOCTEST_STRINGIFY_VARIANT(PackedFloat64Array);
  103. DOCTEST_STRINGIFY_VARIANT(PackedStringArray);
  104. DOCTEST_STRINGIFY_VARIANT(PackedVector2Array);
  105. DOCTEST_STRINGIFY_VARIANT(PackedVector3Array);
  106. DOCTEST_STRINGIFY_VARIANT(PackedColorArray);
  107. // Register test commands to be launched from the command-line.
  108. // For instance: REGISTER_TEST_COMMAND("gdscript-parser" &test_parser_func).
  109. // Example usage: `godot --test gdscript-parser`.
  110. typedef void (*TestFunc)();
  111. extern HashMap<String, TestFunc> *test_commands;
  112. int register_test_command(String p_command, TestFunc p_function);
  113. #define REGISTER_TEST_COMMAND(m_command, m_function) \
  114. DOCTEST_GLOBAL_NO_WARNINGS(DOCTEST_ANONYMOUS(DOCTEST_ANON_VAR_), \
  115. register_test_command(m_command, m_function))
  116. // Utility macros to send an event actions to a given object
  117. // Requires Message Queue and InputMap to be setup.
  118. // SEND_GUI_ACTION - takes an input map key. e.g SEND_GUI_ACTION("ui_text_newline").
  119. // SEND_GUI_KEY_EVENT - takes a keycode set. e.g SEND_GUI_KEY_EVENT(Key::A | KeyModifierMask::META).
  120. // SEND_GUI_MOUSE_BUTTON_EVENT - takes a position, mouse button, mouse mask and modifiers e.g SEND_GUI_MOUSE_BUTTON_EVENT(Vector2(50, 50), MOUSE_BUTTON_NONE, MOUSE_BUTTON_NONE, Key::None);
  121. // SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT - takes a position, mouse button, mouse mask and modifiers e.g SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(Vector2(50, 50), MOUSE_BUTTON_NONE, MOUSE_BUTTON_NONE, Key::None);
  122. // SEND_GUI_MOUSE_MOTION_EVENT - takes a position, mouse mask and modifiers e.g SEND_GUI_MOUSE_MOTION_EVENT(Vector2(50, 50), MouseButtonMask::LEFT, KeyModifierMask::META);
  123. // SEND_GUI_DOUBLE_CLICK - takes a position and modifiers. e.g SEND_GUI_DOUBLE_CLICK(Vector2(50, 50), KeyModifierMask::META);
  124. #define _SEND_DISPLAYSERVER_EVENT(m_event) ((DisplayServerMock *)(DisplayServer::get_singleton()))->simulate_event(m_event);
  125. #define SEND_GUI_ACTION(m_action) \
  126. { \
  127. const List<Ref<InputEvent>> *events = InputMap::get_singleton()->action_get_events(m_action); \
  128. const List<Ref<InputEvent>>::Element *first_event = events->front(); \
  129. Ref<InputEventKey> event = first_event->get()->duplicate(); \
  130. event->set_pressed(true); \
  131. _SEND_DISPLAYSERVER_EVENT(event); \
  132. MessageQueue::get_singleton()->flush(); \
  133. }
  134. #define SEND_GUI_KEY_EVENT(m_input) \
  135. { \
  136. Ref<InputEventKey> event = InputEventKey::create_reference(m_input); \
  137. event->set_pressed(true); \
  138. _SEND_DISPLAYSERVER_EVENT(event); \
  139. MessageQueue::get_singleton()->flush(); \
  140. }
  141. #define _UPDATE_EVENT_MODIFERS(m_event, m_modifers) \
  142. m_event->set_shift_pressed(((m_modifers)&KeyModifierMask::SHIFT) != Key::NONE); \
  143. m_event->set_alt_pressed(((m_modifers)&KeyModifierMask::ALT) != Key::NONE); \
  144. m_event->set_ctrl_pressed(((m_modifers)&KeyModifierMask::CTRL) != Key::NONE); \
  145. m_event->set_meta_pressed(((m_modifers)&KeyModifierMask::META) != Key::NONE);
  146. #define _CREATE_GUI_MOUSE_EVENT(m_screen_pos, m_input, m_mask, m_modifers) \
  147. Ref<InputEventMouseButton> event; \
  148. event.instantiate(); \
  149. event->set_position(m_screen_pos); \
  150. event->set_button_index(m_input); \
  151. event->set_button_mask(m_mask); \
  152. event->set_factor(1); \
  153. _UPDATE_EVENT_MODIFERS(event, m_modifers); \
  154. event->set_pressed(true);
  155. #define _CREATE_GUI_TOUCH_EVENT(m_screen_pos, m_pressed, m_double) \
  156. Ref<InputEventScreenTouch> event; \
  157. event.instantiate(); \
  158. event->set_position(m_screen_pos); \
  159. event->set_pressed(m_pressed); \
  160. event->set_double_tap(m_double);
  161. #define SEND_GUI_MOUSE_BUTTON_EVENT(m_screen_pos, m_input, m_mask, m_modifers) \
  162. { \
  163. _CREATE_GUI_MOUSE_EVENT(m_screen_pos, m_input, m_mask, m_modifers); \
  164. _SEND_DISPLAYSERVER_EVENT(event); \
  165. MessageQueue::get_singleton()->flush(); \
  166. }
  167. #define SEND_GUI_MOUSE_BUTTON_RELEASED_EVENT(m_screen_pos, m_input, m_mask, m_modifers) \
  168. { \
  169. _CREATE_GUI_MOUSE_EVENT(m_screen_pos, m_input, m_mask, m_modifers); \
  170. event->set_pressed(false); \
  171. _SEND_DISPLAYSERVER_EVENT(event); \
  172. MessageQueue::get_singleton()->flush(); \
  173. }
  174. #define SEND_GUI_DOUBLE_CLICK(m_screen_pos, m_modifers) \
  175. { \
  176. _CREATE_GUI_MOUSE_EVENT(m_screen_pos, MouseButton::LEFT, 0, m_modifers); \
  177. event->set_double_click(true); \
  178. _SEND_DISPLAYSERVER_EVENT(event); \
  179. MessageQueue::get_singleton()->flush(); \
  180. }
  181. // We toggle _print_error_enabled to prevent display server not supported warnings.
  182. #define SEND_GUI_MOUSE_MOTION_EVENT(m_screen_pos, m_mask, m_modifers) \
  183. { \
  184. bool errors_enabled = CoreGlobals::print_error_enabled; \
  185. CoreGlobals::print_error_enabled = false; \
  186. Ref<InputEventMouseMotion> event; \
  187. event.instantiate(); \
  188. event->set_position(m_screen_pos); \
  189. event->set_button_mask(m_mask); \
  190. _UPDATE_EVENT_MODIFERS(event, m_modifers); \
  191. _SEND_DISPLAYSERVER_EVENT(event); \
  192. MessageQueue::get_singleton()->flush(); \
  193. CoreGlobals::print_error_enabled = errors_enabled; \
  194. }
  195. #define SEND_GUI_TOUCH_EVENT(m_screen_pos, m_pressed, m_double) \
  196. { \
  197. _CREATE_GUI_TOUCH_EVENT(m_screen_pos, m_pressed, m_double) \
  198. _SEND_DISPLAYSERVER_EVENT(event); \
  199. MessageQueue::get_singleton()->flush(); \
  200. }
  201. // Utility class / macros for testing signals
  202. //
  203. // Use SIGNAL_WATCH(*object, "signal_name") to start watching
  204. // Makes sure to call SIGNAL_UNWATCH(*object, "signal_name") to stop watching in cleanup, this is not done automatically.
  205. //
  206. // The SignalWatcher will capture all signals and their args sent between checks.
  207. //
  208. // Use SIGNAL_CHECK("signal_name"), Vector<Vector<Variant>>), to check the arguments of all fired signals.
  209. // The outer vector is each fired signal, the inner vector the list of arguments for that signal. Order does matter.
  210. //
  211. // Use SIGNAL_CHECK_FALSE("signal_name") to check if a signal was not fired.
  212. //
  213. // Use SIGNAL_DISCARD("signal_name") to discard records all of the given signal, use only in placed you don't need to check.
  214. //
  215. // All signals are automatically discarded between test/sub test cases.
  216. class SignalWatcher : public Object {
  217. private:
  218. inline static SignalWatcher *singleton;
  219. /* Equal to: RBMap<String, Vector<Vector<Variant>>> */
  220. HashMap<String, Array> _signals;
  221. void _add_signal_entry(const Array &p_args, const String &p_name) {
  222. if (!_signals.has(p_name)) {
  223. _signals[p_name] = Array();
  224. }
  225. _signals[p_name].push_back(p_args);
  226. }
  227. void _signal_callback_zero(const String &p_name) {
  228. Array args;
  229. _add_signal_entry(args, p_name);
  230. }
  231. void _signal_callback_one(Variant p_arg1, const String &p_name) {
  232. Array args;
  233. args.push_back(p_arg1);
  234. _add_signal_entry(args, p_name);
  235. }
  236. void _signal_callback_two(Variant p_arg1, Variant p_arg2, const String &p_name) {
  237. Array args;
  238. args.push_back(p_arg1);
  239. args.push_back(p_arg2);
  240. _add_signal_entry(args, p_name);
  241. }
  242. void _signal_callback_three(Variant p_arg1, Variant p_arg2, Variant p_arg3, const String &p_name) {
  243. Array args;
  244. args.push_back(p_arg1);
  245. args.push_back(p_arg2);
  246. args.push_back(p_arg3);
  247. _add_signal_entry(args, p_name);
  248. }
  249. public:
  250. static SignalWatcher *get_singleton() { return singleton; }
  251. void watch_signal(Object *p_object, const String &p_signal) {
  252. MethodInfo method_info;
  253. ClassDB::get_signal(p_object->get_class(), p_signal, &method_info);
  254. switch (method_info.arguments.size()) {
  255. case 0: {
  256. p_object->connect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_zero).bind(p_signal));
  257. } break;
  258. case 1: {
  259. p_object->connect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_one).bind(p_signal));
  260. } break;
  261. case 2: {
  262. p_object->connect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_two).bind(p_signal));
  263. } break;
  264. case 3: {
  265. p_object->connect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_three).bind(p_signal));
  266. } break;
  267. default: {
  268. MESSAGE("Signal ", p_signal, " arg count not supported.");
  269. } break;
  270. }
  271. }
  272. void unwatch_signal(Object *p_object, const String &p_signal) {
  273. MethodInfo method_info;
  274. ClassDB::get_signal(p_object->get_class(), p_signal, &method_info);
  275. switch (method_info.arguments.size()) {
  276. case 0: {
  277. p_object->disconnect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_zero));
  278. } break;
  279. case 1: {
  280. p_object->disconnect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_one));
  281. } break;
  282. case 2: {
  283. p_object->disconnect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_two));
  284. } break;
  285. case 3: {
  286. p_object->disconnect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_three));
  287. } break;
  288. default: {
  289. MESSAGE("Signal ", p_signal, " arg count not supported.");
  290. } break;
  291. }
  292. }
  293. bool check(const String &p_name, const Array &p_args) {
  294. if (!_signals.has(p_name)) {
  295. MESSAGE("Signal ", p_name, " not emitted");
  296. return false;
  297. }
  298. if (p_args.size() != _signals[p_name].size()) {
  299. MESSAGE("Signal has " << _signals[p_name] << " expected " << p_args);
  300. discard_signal(p_name);
  301. return false;
  302. }
  303. bool match = true;
  304. for (int i = 0; i < p_args.size(); i++) {
  305. if (((Array)p_args[i]).size() != ((Array)_signals[p_name][i]).size()) {
  306. MESSAGE("Signal has " << _signals[p_name][i] << " expected " << p_args[i]);
  307. match = false;
  308. continue;
  309. }
  310. for (int j = 0; j < ((Array)p_args[i]).size(); j++) {
  311. if (((Array)p_args[i])[j] != ((Array)_signals[p_name][i])[j]) {
  312. MESSAGE("Signal has " << _signals[p_name][i] << " expected " << p_args[i]);
  313. match = false;
  314. break;
  315. }
  316. }
  317. }
  318. discard_signal(p_name);
  319. return match;
  320. }
  321. bool check_false(const String &p_name) {
  322. bool has = _signals.has(p_name);
  323. discard_signal(p_name);
  324. return !has;
  325. }
  326. void discard_signal(const String &p_name) {
  327. if (_signals.has(p_name)) {
  328. _signals.erase(p_name);
  329. }
  330. }
  331. void _clear_signals() {
  332. _signals.clear();
  333. }
  334. SignalWatcher() {
  335. singleton = this;
  336. }
  337. ~SignalWatcher() {
  338. singleton = nullptr;
  339. }
  340. };
  341. #define SIGNAL_WATCH(m_object, m_signal) SignalWatcher::get_singleton()->watch_signal(m_object, m_signal);
  342. #define SIGNAL_UNWATCH(m_object, m_signal) SignalWatcher::get_singleton()->unwatch_signal(m_object, m_signal);
  343. #define SIGNAL_CHECK(m_signal, m_args) CHECK(SignalWatcher::get_singleton()->check(m_signal, m_args));
  344. #define SIGNAL_CHECK_FALSE(m_signal) CHECK(SignalWatcher::get_singleton()->check_false(m_signal));
  345. #define SIGNAL_DISCARD(m_signal) SignalWatcher::get_singleton()->discard_signal(m_signal);
  346. #endif // TEST_MACROS_H