gdscript_test_runner.h 5.0 KB

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