gdscript_test_runner.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**************************************************************************/
  2. /* gdscript_test_runner.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 GDSCRIPT_TEST_RUNNER_H
  31. #define GDSCRIPT_TEST_RUNNER_H
  32. #include "../gdscript.h"
  33. #include "core/error/error_macros.h"
  34. #include "core/string/print_string.h"
  35. #include "core/string/ustring.h"
  36. #include "core/templates/vector.h"
  37. namespace GDScriptTests {
  38. void init_autoloads();
  39. void init_language(const String &p_base_path);
  40. void finish_language();
  41. // Single test instance in a suite.
  42. class GDScriptTest {
  43. public:
  44. enum TestStatus {
  45. GDTEST_OK,
  46. GDTEST_LOAD_ERROR,
  47. GDTEST_PARSER_ERROR,
  48. GDTEST_ANALYZER_ERROR,
  49. GDTEST_COMPILER_ERROR,
  50. GDTEST_RUNTIME_ERROR,
  51. };
  52. struct TestResult {
  53. TestStatus status;
  54. String output;
  55. bool passed;
  56. };
  57. private:
  58. struct ErrorHandlerData {
  59. TestResult *result = nullptr;
  60. GDScriptTest *self = nullptr;
  61. ErrorHandlerData(TestResult *p_result, GDScriptTest *p_this) {
  62. result = p_result;
  63. self = p_this;
  64. }
  65. };
  66. String source_file;
  67. String output_file;
  68. String base_dir;
  69. PrintHandlerList _print_handler;
  70. ErrorHandlerList _error_handler;
  71. void enable_stdout();
  72. void disable_stdout();
  73. bool check_output(const String &p_output) const;
  74. String get_text_for_status(TestStatus p_status) const;
  75. TestResult execute_test_code(bool p_is_generating);
  76. public:
  77. static void print_handler(void *p_this, const String &p_message, bool p_error, bool p_rich);
  78. static void error_handler(void *p_this, const char *p_function, const char *p_file, int p_line, const char *p_error, const char *p_explanation, bool p_editor_notify, ErrorHandlerType p_type);
  79. TestResult run_test();
  80. bool generate_output();
  81. const String &get_source_file() const { return source_file; }
  82. const String get_source_relative_filepath() const { return source_file.trim_prefix(base_dir); }
  83. const String &get_output_file() const { return output_file; }
  84. GDScriptTest(const String &p_source_path, const String &p_output_path, const String &p_base_dir);
  85. GDScriptTest() :
  86. GDScriptTest(String(), String(), String()) {} // Needed to use in Vector.
  87. };
  88. class GDScriptTestRunner {
  89. String source_dir;
  90. Vector<GDScriptTest> tests;
  91. bool is_generating = false;
  92. bool do_init_languages = false;
  93. bool print_filenames; // Whether filenames should be printed when generated/running tests
  94. bool make_tests();
  95. bool make_tests_for_dir(const String &p_dir);
  96. bool generate_class_index();
  97. public:
  98. static StringName test_function_name;
  99. static void handle_cmdline();
  100. int run_tests();
  101. bool generate_outputs();
  102. GDScriptTestRunner(const String &p_source_dir, bool p_init_language, bool p_print_filenames = false);
  103. ~GDScriptTestRunner();
  104. };
  105. } // namespace GDScriptTests
  106. #endif // GDSCRIPT_TEST_RUNNER_H