test_macros.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/templates/map.h"
  33. #include "core/variant/variant.h"
  34. // See documentation for doctest at:
  35. // https://github.com/onqtam/doctest/blob/master/doc/markdown/readme.md#reference
  36. #include "thirdparty/doctest/doctest.h"
  37. // The test is skipped with this, run pending tests with `--test --no-skip`.
  38. #define TEST_CASE_PENDING(name) TEST_CASE(name *doctest::skip())
  39. // The test case is marked as failed, but does not fail the entire test run.
  40. #define TEST_CASE_MAY_FAIL(name) TEST_CASE(name *doctest::may_fail())
  41. // Provide aliases to conform with Godot naming conventions (see error macros).
  42. #define TEST_COND(cond, ...) DOCTEST_CHECK_FALSE_MESSAGE(cond, __VA_ARGS__)
  43. #define TEST_FAIL(cond, ...) DOCTEST_FAIL(cond, __VA_ARGS__)
  44. #define TEST_FAIL_COND(cond, ...) DOCTEST_REQUIRE_FALSE_MESSAGE(cond, __VA_ARGS__)
  45. #define TEST_FAIL_COND_WARN(cond, ...) DOCTEST_WARN_FALSE_MESSAGE(cond, __VA_ARGS__)
  46. // Temporarily disable error prints to test failure paths.
  47. // This allows to avoid polluting the test summary with error messages.
  48. // The `_print_error_enabled` boolean is defined in `core/print_string.cpp` and
  49. // works at global scope. It's used by various loggers in `should_log()` method,
  50. // which are used by error macros which call into `OS::print_error`, effectively
  51. // disabling any error messages to be printed from the engine side (not tests).
  52. #define ERR_PRINT_OFF _print_error_enabled = false;
  53. #define ERR_PRINT_ON _print_error_enabled = true;
  54. // Stringify all `Variant` compatible types for doctest output by default.
  55. // https://github.com/onqtam/doctest/blob/master/doc/markdown/stringification.md
  56. #define DOCTEST_STRINGIFY_VARIANT(m_type) \
  57. template <> \
  58. struct doctest::StringMaker<m_type> { \
  59. static doctest::String convert(const m_type &p_val) { \
  60. const Variant val = p_val; \
  61. return val.get_construct_string().utf8().get_data(); \
  62. } \
  63. };
  64. #define DOCTEST_STRINGIFY_VARIANT_POINTER(m_type) \
  65. template <> \
  66. struct doctest::StringMaker<m_type> { \
  67. static doctest::String convert(const m_type *p_val) { \
  68. const Variant val = p_val; \
  69. return val.get_construct_string().utf8().get_data(); \
  70. } \
  71. };
  72. DOCTEST_STRINGIFY_VARIANT(Variant);
  73. DOCTEST_STRINGIFY_VARIANT(::String); // Disambiguate from `doctest::String`.
  74. DOCTEST_STRINGIFY_VARIANT(Vector2);
  75. DOCTEST_STRINGIFY_VARIANT(Vector2i);
  76. DOCTEST_STRINGIFY_VARIANT(Rect2);
  77. DOCTEST_STRINGIFY_VARIANT(Rect2i);
  78. DOCTEST_STRINGIFY_VARIANT(Vector3);
  79. DOCTEST_STRINGIFY_VARIANT(Vector3i);
  80. DOCTEST_STRINGIFY_VARIANT(Transform2D);
  81. DOCTEST_STRINGIFY_VARIANT(Plane);
  82. DOCTEST_STRINGIFY_VARIANT(Quat);
  83. DOCTEST_STRINGIFY_VARIANT(AABB);
  84. DOCTEST_STRINGIFY_VARIANT(Basis);
  85. DOCTEST_STRINGIFY_VARIANT(Transform);
  86. DOCTEST_STRINGIFY_VARIANT(::Color); // Disambiguate from `doctest::Color`.
  87. DOCTEST_STRINGIFY_VARIANT(StringName);
  88. DOCTEST_STRINGIFY_VARIANT(NodePath);
  89. DOCTEST_STRINGIFY_VARIANT(RID);
  90. DOCTEST_STRINGIFY_VARIANT_POINTER(Object);
  91. DOCTEST_STRINGIFY_VARIANT(Callable);
  92. DOCTEST_STRINGIFY_VARIANT(Signal);
  93. DOCTEST_STRINGIFY_VARIANT(Dictionary);
  94. DOCTEST_STRINGIFY_VARIANT(Array);
  95. DOCTEST_STRINGIFY_VARIANT(PackedByteArray);
  96. DOCTEST_STRINGIFY_VARIANT(PackedInt32Array);
  97. DOCTEST_STRINGIFY_VARIANT(PackedInt64Array);
  98. DOCTEST_STRINGIFY_VARIANT(PackedFloat32Array);
  99. DOCTEST_STRINGIFY_VARIANT(PackedFloat64Array);
  100. DOCTEST_STRINGIFY_VARIANT(PackedStringArray);
  101. DOCTEST_STRINGIFY_VARIANT(PackedVector2Array);
  102. DOCTEST_STRINGIFY_VARIANT(PackedVector3Array);
  103. DOCTEST_STRINGIFY_VARIANT(PackedColorArray);
  104. // Register test commands to be launched from the command-line.
  105. // For instance: REGISTER_TEST_COMMAND("gdscript-parser" &test_parser_func).
  106. // Example usage: `godot --test gdscript-parser`.
  107. typedef void (*TestFunc)();
  108. extern Map<String, TestFunc> *test_commands;
  109. int register_test_command(String p_command, TestFunc p_function);
  110. #define REGISTER_TEST_COMMAND(m_command, m_function) \
  111. DOCTEST_GLOBAL_NO_WARNINGS(DOCTEST_ANONYMOUS(_DOCTEST_ANON_VAR_)) = \
  112. register_test_command(m_command, m_function); \
  113. DOCTEST_GLOBAL_NO_WARNINGS_END()
  114. #endif // TEST_MACROS_H