test_macros.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*************************************************************************/
  2. /* test_macros.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_MACROS_H
  31. #define TEST_MACROS_H
  32. #include "core/object/callable_method_pointer.h"
  33. #include "core/object/class_db.h"
  34. #include "core/string/print_string.h"
  35. #include "core/templates/map.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/print_string.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 _print_error_enabled = false;
  56. #define ERR_PRINT_ON _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.get_construct_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.get_construct_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 Map<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. DOCTEST_GLOBAL_NO_WARNINGS_END()
  117. // Utility macros to send an event actions to a given object
  118. // Requires Message Queue and InputMap to be setup.
  119. // SEND_GUI_ACTION - takes an object and a input map key. e.g SEND_GUI_ACTION(code_edit, "ui_text_newline").
  120. // SEND_GUI_KEY_EVENT - takes an object and a keycode set. e.g SEND_GUI_KEY_EVENT(code_edit, KEY_A | KEY_MASK_CMD).
  121. // SEND_GUI_MOUSE_EVENT - takes an object, position, mouse button and mouse mask e.g SEND_GUI_MOUSE_EVENT(code_edit, Vector2(50, 50), MOUSE_BUTTON_NONE, MOUSE_BUTTON_NONE);
  122. // SEND_GUI_DOUBLE_CLICK - takes an object and a postion. e.g SEND_GUI_DOUBLE_CLICK(code_edit, Vector2(50, 50));
  123. #define SEND_GUI_ACTION(m_object, m_action) \
  124. { \
  125. const List<Ref<InputEvent>> *events = InputMap::get_singleton()->action_get_events(m_action); \
  126. const List<Ref<InputEvent>>::Element *first_event = events->front(); \
  127. Ref<InputEventKey> event = first_event->get(); \
  128. event->set_pressed(true); \
  129. m_object->gui_input(event); \
  130. MessageQueue::get_singleton()->flush(); \
  131. }
  132. #define SEND_GUI_KEY_EVENT(m_object, m_input) \
  133. { \
  134. Ref<InputEventKey> event = InputEventKey::create_reference(m_input); \
  135. event->set_pressed(true); \
  136. m_object->gui_input(event); \
  137. MessageQueue::get_singleton()->flush(); \
  138. }
  139. #define _CREATE_GUI_MOUSE_EVENT(m_object, m_local_pos, m_input, m_mask) \
  140. Ref<InputEventMouseButton> event; \
  141. event.instantiate(); \
  142. event->set_position(m_local_pos); \
  143. event->set_button_index(m_input); \
  144. event->set_button_mask(m_mask); \
  145. event->set_pressed(true);
  146. #define SEND_GUI_MOUSE_EVENT(m_object, m_local_pos, m_input, m_mask) \
  147. { \
  148. _CREATE_GUI_MOUSE_EVENT(m_object, m_local_pos, m_input, m_mask); \
  149. m_object->get_viewport()->push_input(event); \
  150. MessageQueue::get_singleton()->flush(); \
  151. }
  152. #define SEND_GUI_DOUBLE_CLICK(m_object, m_local_pos) \
  153. { \
  154. _CREATE_GUI_MOUSE_EVENT(m_object, m_local_pos, MOUSE_BUTTON_LEFT, MOUSE_BUTTON_LEFT); \
  155. event->set_double_click(true); \
  156. m_object->get_viewport()->push_input(event); \
  157. MessageQueue::get_singleton()->flush(); \
  158. }
  159. // Utility class / macros for testing signals
  160. //
  161. // Use SIGNAL_WATCH(*object, "signal_name") to start watching
  162. // Makes sure to call SIGNAL_UNWATCH(*object, "signal_name") to stop watching in cleanup, this is not done automatically.
  163. //
  164. // The SignalWatcher will capture all signals and their args sent between checks.
  165. //
  166. // Use SIGNAL_CHECK("signal_name"), Vector<Vector<Variant>>), to check the arguments of all fired signals.
  167. // The outer vector is each fired signal, the inner vector the list of arguments for that signal. Order does matter.
  168. //
  169. // Use SIGNAL_CHECK_FALSE("signal_name") to check if a signal was not fired.
  170. //
  171. // Use SIGNAL_DISCARD("signal_name") to discard records all of the given signal, use only in placed you don't need to check.
  172. //
  173. // All signals are automaticaly discared between test/sub test cases.
  174. class SignalWatcher : public Object {
  175. private:
  176. inline static SignalWatcher *singleton;
  177. /* Equal to: Map<String, Vector<Vector<Variant>>> */
  178. Map<String, Array> _signals;
  179. void _add_signal_entry(const Array &p_args, const String &p_name) {
  180. if (!_signals.has(p_name)) {
  181. _signals[p_name] = Array();
  182. }
  183. _signals[p_name].push_back(p_args);
  184. }
  185. void _signal_callback_zero(const String &p_name) {
  186. Array args;
  187. _add_signal_entry(args, p_name);
  188. }
  189. void _signal_callback_one(Variant p_arg1, const String &p_name) {
  190. Array args;
  191. args.push_back(p_arg1);
  192. _add_signal_entry(args, p_name);
  193. }
  194. void _signal_callback_two(Variant p_arg1, Variant p_arg2, const String &p_name) {
  195. Array args;
  196. args.push_back(p_arg1);
  197. args.push_back(p_arg2);
  198. _add_signal_entry(args, p_name);
  199. }
  200. void _signal_callback_three(Variant p_arg1, Variant p_arg2, Variant p_arg3, const String &p_name) {
  201. Array args;
  202. args.push_back(p_arg1);
  203. args.push_back(p_arg2);
  204. args.push_back(p_arg3);
  205. _add_signal_entry(args, p_name);
  206. }
  207. public:
  208. static SignalWatcher *get_singleton() { return singleton; }
  209. void watch_signal(Object *p_object, const String &p_signal) {
  210. Vector<Variant> args;
  211. args.push_back(p_signal);
  212. MethodInfo method_info;
  213. ClassDB::get_signal(p_object->get_class(), p_signal, &method_info);
  214. switch (method_info.arguments.size()) {
  215. case 0: {
  216. p_object->connect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_zero), args);
  217. } break;
  218. case 1: {
  219. p_object->connect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_one), args);
  220. } break;
  221. case 2: {
  222. p_object->connect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_two), args);
  223. } break;
  224. case 3: {
  225. p_object->connect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_three), args);
  226. } break;
  227. default: {
  228. MESSAGE("Signal ", p_signal, " arg count not supported.");
  229. } break;
  230. }
  231. }
  232. void unwatch_signal(Object *p_object, const String &p_signal) {
  233. MethodInfo method_info;
  234. ClassDB::get_signal(p_object->get_class(), p_signal, &method_info);
  235. switch (method_info.arguments.size()) {
  236. case 0: {
  237. p_object->disconnect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_zero));
  238. } break;
  239. case 1: {
  240. p_object->disconnect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_one));
  241. } break;
  242. case 2: {
  243. p_object->disconnect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_two));
  244. } break;
  245. case 3: {
  246. p_object->disconnect(p_signal, callable_mp(this, &SignalWatcher::_signal_callback_three));
  247. } break;
  248. default: {
  249. MESSAGE("Signal ", p_signal, " arg count not supported.");
  250. } break;
  251. }
  252. }
  253. bool check(const String &p_name, const Array &p_args) {
  254. if (!_signals.has(p_name)) {
  255. MESSAGE("Signal ", p_name, " not emitted");
  256. return false;
  257. }
  258. if (p_args.size() != _signals[p_name].size()) {
  259. MESSAGE("Signal has " << _signals[p_name] << " expected " << p_args);
  260. discard_signal(p_name);
  261. return false;
  262. }
  263. bool match = true;
  264. for (int i = 0; i < p_args.size(); i++) {
  265. if (((Array)p_args[i]).size() != ((Array)_signals[p_name][i]).size()) {
  266. MESSAGE("Signal has " << _signals[p_name][i] << " expected " << p_args[i]);
  267. match = false;
  268. continue;
  269. }
  270. for (int j = 0; j < ((Array)p_args[i]).size(); j++) {
  271. if (((Array)p_args[i])[j] != ((Array)_signals[p_name][i])[j]) {
  272. MESSAGE("Signal has " << _signals[p_name][i] << " expected " << p_args[i]);
  273. match = false;
  274. break;
  275. }
  276. }
  277. }
  278. discard_signal(p_name);
  279. return match;
  280. }
  281. bool check_false(const String &p_name) {
  282. bool has = _signals.has(p_name);
  283. discard_signal(p_name);
  284. return !has;
  285. }
  286. void discard_signal(const String &p_name) {
  287. if (_signals.has(p_name)) {
  288. _signals.erase(p_name);
  289. }
  290. }
  291. void _clear_signals() {
  292. _signals.clear();
  293. }
  294. SignalWatcher() {
  295. singleton = this;
  296. }
  297. ~SignalWatcher() {
  298. singleton = nullptr;
  299. }
  300. };
  301. #define SIGNAL_WATCH(m_object, m_signal) SignalWatcher::get_singleton()->watch_signal(m_object, m_signal);
  302. #define SIGNAL_UNWATCH(m_object, m_signal) SignalWatcher::get_singleton()->unwatch_signal(m_object, m_signal);
  303. #define SIGNAL_CHECK(m_signal, m_args) CHECK(SignalWatcher::get_singleton()->check(m_signal, m_args));
  304. #define SIGNAL_CHECK_FALSE(m_signal) CHECK(SignalWatcher::get_singleton()->check_false(m_signal));
  305. #define SIGNAL_DISCARD(m_signal) SignalWatcher::get_singleton()->discard_signal(m_signal);
  306. #endif // TEST_MACROS_H