Debian Codegraph @

modules/gdscript/tests/gdscript_test_runner.h

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