Path: blob/master/modules/gdscript/tests/gdscript_test_runner.h
10278 views
/**************************************************************************/1/* gdscript_test_runner.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "../gdscript.h"3334#include "core/error/error_macros.h"35#include "core/string/print_string.h"36#include "core/string/ustring.h"37#include "core/templates/vector.h"3839namespace GDScriptTests {4041void init_autoloads();42void init_language(const String &p_base_path);43void finish_language();4445// Single test instance in a suite.46class GDScriptTest {47public:48enum TestStatus {49GDTEST_OK,50GDTEST_LOAD_ERROR,51GDTEST_PARSER_ERROR,52GDTEST_ANALYZER_ERROR,53GDTEST_COMPILER_ERROR,54GDTEST_RUNTIME_ERROR,55};5657struct TestResult {58TestStatus status;59String output;60bool passed;61};6263enum TokenizerMode {64TOKENIZER_TEXT,65TOKENIZER_BUFFER,66};6768private:69struct ErrorHandlerData {70TestResult *result = nullptr;71GDScriptTest *self = nullptr;72ErrorHandlerData(TestResult *p_result, GDScriptTest *p_this) {73result = p_result;74self = p_this;75}76};7778String source_file;79String output_file;80String base_dir;8182PrintHandlerList _print_handler;83ErrorHandlerList _error_handler;8485TokenizerMode tokenizer_mode = TOKENIZER_TEXT;8687void enable_stdout();88void disable_stdout();89bool check_output(const String &p_output) const;90String get_text_for_status(TestStatus p_status) const;9192TestResult execute_test_code(bool p_is_generating);9394public:95static void print_handler(void *p_this, const String &p_message, bool p_error, bool p_rich);96static 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);97TestResult run_test();98bool generate_output();99100const String &get_source_file() const { return source_file; }101const String get_source_relative_filepath() const { return source_file.trim_prefix(base_dir); }102const String &get_output_file() const { return output_file; }103104void set_tokenizer_mode(TokenizerMode p_tokenizer_mode) { tokenizer_mode = p_tokenizer_mode; }105TokenizerMode get_tokenizer_mode() const { return tokenizer_mode; }106107GDScriptTest(const String &p_source_path, const String &p_output_path, const String &p_base_dir);108GDScriptTest() :109GDScriptTest(String(), String(), String()) {} // Needed to use in Vector.110};111112class GDScriptTestRunner {113String source_dir;114Vector<GDScriptTest> tests;115116bool is_generating = false;117bool do_init_languages = false;118bool print_filenames; // Whether filenames should be printed when generated/running tests119bool binary_tokens; // Test with buffer tokenizer.120121bool make_tests();122bool make_tests_for_dir(const String &p_dir);123bool generate_class_index();124125public:126static StringName test_function_name;127128static void handle_cmdline();129int run_tests();130bool generate_outputs();131132GDScriptTestRunner(const String &p_source_dir, bool p_init_language, bool p_print_filenames = false, bool p_use_binary_tokens = false);133~GDScriptTestRunner();134};135136} // namespace GDScriptTests137138139