gdscript_test_runner.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. enum TokenizerMode {
  58. TOKENIZER_TEXT,
  59. TOKENIZER_BUFFER,
  60. };
  61. private:
  62. struct ErrorHandlerData {
  63. TestResult *result = nullptr;
  64. GDScriptTest *self = nullptr;
  65. ErrorHandlerData(TestResult *p_result, GDScriptTest *p_this) {
  66. result = p_result;
  67. self = p_this;
  68. }
  69. };
  70. String source_file;
  71. String output_file;
  72. String base_dir;
  73. PrintHandlerList _print_handler;
  74. ErrorHandlerList _error_handler;
  75. TokenizerMode tokenizer_mode = TOKENIZER_TEXT;
  76. void enable_stdout();
  77. void disable_stdout();
  78. bool check_output(const String &p_output) const;
  79. String get_text_for_status(TestStatus p_status) const;
  80. TestResult execute_test_code(bool p_is_generating);
  81. public:
  82. static void print_handler(void *p_this, const String &p_message, bool p_error, bool p_rich);
  83. 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);
  84. TestResult run_test();
  85. bool generate_output();
  86. const String &get_source_file() const { return source_file; }
  87. const String get_source_relative_filepath() const { return source_file.trim_prefix(base_dir); }
  88. const String &get_output_file() const { return output_file; }
  89. void set_tokenizer_mode(TokenizerMode p_tokenizer_mode) { tokenizer_mode = p_tokenizer_mode; }
  90. TokenizerMode get_tokenizer_mode() const { return tokenizer_mode; }
  91. GDScriptTest(const String &p_source_path, const String &p_output_path, const String &p_base_dir);
  92. GDScriptTest() :
  93. GDScriptTest(String(), String(), String()) {} // Needed to use in Vector.
  94. };
  95. class GDScriptTestRunner {
  96. String source_dir;
  97. Vector<GDScriptTest> tests;
  98. bool is_generating = false;
  99. bool do_init_languages = false;
  100. bool print_filenames; // Whether filenames should be printed when generated/running tests
  101. bool binary_tokens; // Test with buffer tokenizer.
  102. bool make_tests();
  103. bool make_tests_for_dir(const String &p_dir);
  104. bool generate_class_index();
  105. public:
  106. static StringName test_function_name;
  107. static void handle_cmdline();
  108. int run_tests();
  109. bool generate_outputs();
  110. GDScriptTestRunner(const String &p_source_dir, bool p_init_language, bool p_print_filenames = false, bool p_use_binary_tokens = false);
  111. ~GDScriptTestRunner();
  112. };
  113. } // namespace GDScriptTests
  114. #endif // GDSCRIPT_TEST_RUNNER_H