2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* gdscript_test_runner.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2021-04-07 15:12:51 +02:00
|
|
|
|
|
|
|
#include "gdscript_test_runner.h"
|
|
|
|
|
|
|
|
#include "../gdscript.h"
|
|
|
|
#include "../gdscript_analyzer.h"
|
|
|
|
#include "../gdscript_compiler.h"
|
|
|
|
#include "../gdscript_parser.h"
|
2024-01-22 15:31:55 +01:00
|
|
|
#include "../gdscript_tokenizer_buffer.h"
|
2021-04-07 15:12:51 +02:00
|
|
|
|
|
|
|
#include "core/config/project_settings.h"
|
2022-05-20 14:28:44 +02:00
|
|
|
#include "core/core_globals.h"
|
2021-04-07 15:12:51 +02:00
|
|
|
#include "core/core_string_names.h"
|
2021-06-11 14:51:48 +02:00
|
|
|
#include "core/io/dir_access.h"
|
2021-04-07 15:12:51 +02:00
|
|
|
#include "core/io/file_access_pack.h"
|
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "core/string/string_builder.h"
|
|
|
|
#include "scene/resources/packed_scene.h"
|
|
|
|
|
|
|
|
#include "tests/test_macros.h"
|
|
|
|
|
|
|
|
namespace GDScriptTests {
|
|
|
|
|
|
|
|
void init_autoloads() {
|
2022-05-08 10:09:19 +02:00
|
|
|
HashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
|
2021-04-07 15:12:51 +02:00
|
|
|
|
|
|
|
// First pass, add the constants so they exist before any script is loaded.
|
2022-05-08 10:09:19 +02:00
|
|
|
for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
|
|
|
|
const ProjectSettings::AutoloadInfo &info = E.value;
|
2021-04-07 15:12:51 +02:00
|
|
|
|
|
|
|
if (info.is_singleton) {
|
|
|
|
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
|
|
|
|
ScriptServer::get_language(i)->add_global_constant(info.name, Variant());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second pass, load into global constants.
|
2022-05-08 10:09:19 +02:00
|
|
|
for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
|
|
|
|
const ProjectSettings::AutoloadInfo &info = E.value;
|
2021-04-07 15:12:51 +02:00
|
|
|
|
|
|
|
if (!info.is_singleton) {
|
|
|
|
// Skip non-singletons since we don't have a scene tree here anyway.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *n = nullptr;
|
2022-11-24 00:13:13 +01:00
|
|
|
if (ResourceLoader::get_resource_type(info.path) == "PackedScene") {
|
|
|
|
// Cache the scene reference before loading it (for cyclic references)
|
|
|
|
Ref<PackedScene> scn;
|
|
|
|
scn.instantiate();
|
|
|
|
scn->set_path(info.path);
|
|
|
|
scn->reload_from_file();
|
2023-11-23 16:19:24 +01:00
|
|
|
ERR_CONTINUE_MSG(!scn.is_valid(), vformat("Failed to instantiate an autoload, can't load from path: %s.", info.path));
|
2022-11-24 00:13:13 +01:00
|
|
|
|
|
|
|
if (scn.is_valid()) {
|
|
|
|
n = scn->instantiate();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ref<Resource> res = ResourceLoader::load(info.path);
|
2023-11-23 16:19:24 +01:00
|
|
|
ERR_CONTINUE_MSG(res.is_null(), vformat("Failed to instantiate an autoload, can't load from path: %s.", info.path));
|
2022-11-24 00:13:13 +01:00
|
|
|
|
|
|
|
Ref<Script> scr = res;
|
|
|
|
if (scr.is_valid()) {
|
|
|
|
StringName ibt = scr->get_instance_base_type();
|
|
|
|
bool valid_type = ClassDB::is_parent_class(ibt, "Node");
|
2023-11-23 16:19:24 +01:00
|
|
|
ERR_CONTINUE_MSG(!valid_type, vformat("Failed to instantiate an autoload, script '%s' does not inherit from 'Node'.", info.path));
|
2021-04-07 15:12:51 +02:00
|
|
|
|
2022-11-24 00:13:13 +01:00
|
|
|
Object *obj = ClassDB::instantiate(ibt);
|
2023-11-23 16:19:24 +01:00
|
|
|
ERR_CONTINUE_MSG(!obj, vformat("Failed to instantiate an autoload, cannot instantiate '%s'.", ibt));
|
2021-04-07 15:12:51 +02:00
|
|
|
|
2022-11-24 00:13:13 +01:00
|
|
|
n = Object::cast_to<Node>(obj);
|
|
|
|
n->set_script(scr);
|
|
|
|
}
|
2021-04-07 15:12:51 +02:00
|
|
|
}
|
|
|
|
|
2023-11-23 16:19:24 +01:00
|
|
|
ERR_CONTINUE_MSG(!n, vformat("Failed to instantiate an autoload, path is not pointing to a scene or a script: %s.", info.path));
|
2021-04-07 15:12:51 +02:00
|
|
|
n->set_name(info.name);
|
|
|
|
|
|
|
|
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
|
|
|
|
ScriptServer::get_language(i)->add_global_constant(info.name, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_language(const String &p_base_path) {
|
|
|
|
// Setup project settings since it's needed by the languages to get the global scripts.
|
|
|
|
// This also sets up the base resource path.
|
|
|
|
Error err = ProjectSettings::get_singleton()->setup(p_base_path, String(), true);
|
|
|
|
if (err) {
|
|
|
|
print_line("Could not load project settings.");
|
|
|
|
// Keep going since some scripts still work without this.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the language for the test routine.
|
|
|
|
GDScriptLanguage::get_singleton()->init();
|
|
|
|
init_autoloads();
|
|
|
|
}
|
|
|
|
|
|
|
|
void finish_language() {
|
|
|
|
GDScriptLanguage::get_singleton()->finish();
|
|
|
|
ScriptServer::global_classes_clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
StringName GDScriptTestRunner::test_function_name;
|
|
|
|
|
2024-01-22 15:31:55 +01:00
|
|
|
GDScriptTestRunner::GDScriptTestRunner(const String &p_source_dir, bool p_init_language, bool p_print_filenames, bool p_use_binary_tokens) {
|
2021-04-07 15:12:51 +02:00
|
|
|
test_function_name = StaticCString::create("test");
|
|
|
|
do_init_languages = p_init_language;
|
2023-01-27 12:07:53 +01:00
|
|
|
print_filenames = p_print_filenames;
|
2024-01-22 15:31:55 +01:00
|
|
|
binary_tokens = p_use_binary_tokens;
|
2021-04-07 15:12:51 +02:00
|
|
|
|
|
|
|
source_dir = p_source_dir;
|
|
|
|
if (!source_dir.ends_with("/")) {
|
|
|
|
source_dir += "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (do_init_languages) {
|
|
|
|
init_language(p_source_dir);
|
2021-10-06 16:37:13 +02:00
|
|
|
}
|
2022-02-02 01:31:12 +01:00
|
|
|
#ifdef DEBUG_ENABLED
|
2023-02-02 15:57:22 +01:00
|
|
|
// Set all warning levels to "Warn" in order to test them properly, even the ones that default to error.
|
2021-10-06 16:37:13 +02:00
|
|
|
ProjectSettings::get_singleton()->set_setting("debug/gdscript/warnings/enable", true);
|
|
|
|
for (int i = 0; i < (int)GDScriptWarning::WARNING_MAX; i++) {
|
2023-09-22 19:57:24 +02:00
|
|
|
if (i == GDScriptWarning::UNTYPED_DECLARATION || i == GDScriptWarning::INFERRED_DECLARATION) {
|
2023-09-05 19:56:37 +02:00
|
|
|
// TODO: Add ability for test scripts to specify which warnings to enable/disable for testing.
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-02 15:57:22 +01:00
|
|
|
String warning_setting = GDScriptWarning::get_settings_path_from_code((GDScriptWarning::Code)i);
|
|
|
|
ProjectSettings::get_singleton()->set_setting(warning_setting, (int)GDScriptWarning::WARN);
|
2021-04-07 15:12:51 +02:00
|
|
|
}
|
2022-02-02 01:31:12 +01:00
|
|
|
#endif
|
2021-04-07 15:12:51 +02:00
|
|
|
|
|
|
|
// Enable printing to show results
|
2022-05-20 14:28:44 +02:00
|
|
|
CoreGlobals::print_line_enabled = true;
|
|
|
|
CoreGlobals::print_error_enabled = true;
|
2021-04-07 15:12:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GDScriptTestRunner::~GDScriptTestRunner() {
|
|
|
|
test_function_name = StringName();
|
|
|
|
if (do_init_languages) {
|
|
|
|
finish_language();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 01:31:12 +01:00
|
|
|
#ifndef DEBUG_ENABLED
|
|
|
|
static String strip_warnings(const String &p_expected) {
|
|
|
|
// On release builds we don't have warnings. Here we remove them from the output before comparison
|
|
|
|
// so it doesn't fail just because of difference in warnings.
|
|
|
|
String expected_no_warnings;
|
|
|
|
for (String line : p_expected.split("\n")) {
|
|
|
|
if (line.begins_with(">> ")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
expected_no_warnings += line + "\n";
|
|
|
|
}
|
|
|
|
return expected_no_warnings.strip_edges() + "\n";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-04-07 15:12:51 +02:00
|
|
|
int GDScriptTestRunner::run_tests() {
|
|
|
|
if (!make_tests()) {
|
|
|
|
FAIL("An error occurred while making the tests.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!generate_class_index()) {
|
|
|
|
FAIL("An error occurred while generating class index.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int failed = 0;
|
|
|
|
for (int i = 0; i < tests.size(); i++) {
|
|
|
|
GDScriptTest test = tests[i];
|
2023-01-27 12:07:53 +01:00
|
|
|
if (print_filenames) {
|
|
|
|
print_line(test.get_source_relative_filepath());
|
|
|
|
}
|
2021-04-07 15:12:51 +02:00
|
|
|
GDScriptTest::TestResult result = test.run_test();
|
|
|
|
|
|
|
|
String expected = FileAccess::get_file_as_string(test.get_output_file());
|
2022-02-02 01:31:12 +01:00
|
|
|
#ifndef DEBUG_ENABLED
|
|
|
|
expected = strip_warnings(expected);
|
|
|
|
#endif
|
2021-04-07 15:12:51 +02:00
|
|
|
INFO(test.get_source_file());
|
|
|
|
if (!result.passed) {
|
|
|
|
INFO(expected);
|
|
|
|
failed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_MESSAGE(result.passed, (result.passed ? String() : result.output));
|
|
|
|
}
|
|
|
|
|
|
|
|
return failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDScriptTestRunner::generate_outputs() {
|
|
|
|
is_generating = true;
|
|
|
|
|
|
|
|
if (!make_tests()) {
|
|
|
|
print_line("Failed to generate a test output.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!generate_class_index()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < tests.size(); i++) {
|
|
|
|
GDScriptTest test = tests[i];
|
2023-01-27 12:07:53 +01:00
|
|
|
if (print_filenames) {
|
|
|
|
print_line(test.get_source_relative_filepath());
|
|
|
|
} else {
|
|
|
|
OS::get_singleton()->print(".");
|
|
|
|
}
|
|
|
|
|
2021-04-07 15:12:51 +02:00
|
|
|
bool result = test.generate_output();
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
print_line("\nCould not generate output for " + test.get_source_file());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
print_line("\nGenerated output files for " + itos(tests.size()) + " tests successfully.");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDScriptTestRunner::make_tests_for_dir(const String &p_dir) {
|
|
|
|
Error err = OK;
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<DirAccess> dir(DirAccess::open(p_dir, &err));
|
2021-04-07 15:12:51 +02:00
|
|
|
|
|
|
|
if (err != OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
String current_dir = dir->get_current_dir();
|
|
|
|
|
|
|
|
dir->list_dir_begin();
|
|
|
|
String next = dir->get_next();
|
|
|
|
|
|
|
|
while (!next.is_empty()) {
|
|
|
|
if (dir->current_is_dir()) {
|
2023-11-21 16:06:43 +01:00
|
|
|
if (next == "." || next == ".." || next == "completion" || next == "lsp") {
|
2021-04-07 15:12:51 +02:00
|
|
|
next = dir->get_next();
|
|
|
|
continue;
|
|
|
|
}
|
2022-08-30 02:34:01 +02:00
|
|
|
if (!make_tests_for_dir(current_dir.path_join(next))) {
|
2021-04-07 15:12:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2022-10-09 18:41:28 +02:00
|
|
|
if (next.ends_with(".notest.gd")) {
|
|
|
|
next = dir->get_next();
|
|
|
|
continue;
|
2024-01-22 15:31:55 +01:00
|
|
|
} else if (binary_tokens && next.ends_with(".textonly.gd")) {
|
|
|
|
next = dir->get_next();
|
|
|
|
continue;
|
2022-10-09 18:41:28 +02:00
|
|
|
} else if (next.get_extension().to_lower() == "gd") {
|
2022-02-02 01:31:12 +01:00
|
|
|
#ifndef DEBUG_ENABLED
|
|
|
|
// On release builds, skip tests marked as debug only.
|
|
|
|
Error open_err = OK;
|
2022-08-30 02:34:01 +02:00
|
|
|
Ref<FileAccess> script_file(FileAccess::open(current_dir.path_join(next), FileAccess::READ, &open_err));
|
2022-02-02 01:31:12 +01:00
|
|
|
if (open_err != OK) {
|
|
|
|
ERR_PRINT(vformat(R"(Couldn't open test file "%s".)", next));
|
|
|
|
next = dir->get_next();
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if (script_file->get_line() == "#debug-only") {
|
|
|
|
next = dir->get_next();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-04-07 15:12:51 +02:00
|
|
|
String out_file = next.get_basename() + ".out";
|
2024-03-14 10:33:56 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(!is_generating && !dir->file_exists(out_file), false, "Could not find output file for " + next);
|
|
|
|
|
|
|
|
if (next.ends_with(".bin.gd")) {
|
|
|
|
// Test text mode first.
|
|
|
|
GDScriptTest text_test(current_dir.path_join(next), current_dir.path_join(out_file), source_dir);
|
|
|
|
tests.push_back(text_test);
|
|
|
|
// Test binary mode even without `--use-binary-tokens`.
|
|
|
|
GDScriptTest bin_test(current_dir.path_join(next), current_dir.path_join(out_file), source_dir);
|
|
|
|
bin_test.set_tokenizer_mode(GDScriptTest::TOKENIZER_BUFFER);
|
|
|
|
tests.push_back(bin_test);
|
|
|
|
} else {
|
|
|
|
GDScriptTest test(current_dir.path_join(next), current_dir.path_join(out_file), source_dir);
|
|
|
|
if (binary_tokens) {
|
|
|
|
test.set_tokenizer_mode(GDScriptTest::TOKENIZER_BUFFER);
|
|
|
|
}
|
|
|
|
tests.push_back(test);
|
2024-01-22 15:31:55 +01:00
|
|
|
}
|
2021-04-07 15:12:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
next = dir->get_next();
|
|
|
|
}
|
|
|
|
|
|
|
|
dir->list_dir_end();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDScriptTestRunner::make_tests() {
|
|
|
|
Error err = OK;
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<DirAccess> dir(DirAccess::open(source_dir, &err));
|
2021-04-07 15:12:51 +02:00
|
|
|
|
|
|
|
ERR_FAIL_COND_V_MSG(err != OK, false, "Could not open specified test directory.");
|
|
|
|
|
2021-05-24 19:17:20 +02:00
|
|
|
source_dir = dir->get_current_dir() + "/"; // Make it absolute path.
|
2021-04-07 15:12:51 +02:00
|
|
|
return make_tests_for_dir(dir->get_current_dir());
|
|
|
|
}
|
|
|
|
|
2024-01-22 15:31:55 +01:00
|
|
|
static bool generate_class_index_recursive(const String &p_dir) {
|
|
|
|
Error err = OK;
|
|
|
|
Ref<DirAccess> dir(DirAccess::open(p_dir, &err));
|
|
|
|
|
|
|
|
if (err != OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
String current_dir = dir->get_current_dir();
|
|
|
|
|
|
|
|
dir->list_dir_begin();
|
|
|
|
String next = dir->get_next();
|
|
|
|
|
2021-04-07 15:12:51 +02:00
|
|
|
StringName gdscript_name = GDScriptLanguage::get_singleton()->get_name();
|
2024-01-22 15:31:55 +01:00
|
|
|
while (!next.is_empty()) {
|
|
|
|
if (dir->current_is_dir()) {
|
|
|
|
if (next == "." || next == ".." || next == "completion" || next == "lsp") {
|
|
|
|
next = dir->get_next();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!generate_class_index_recursive(current_dir.path_join(next))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!next.ends_with(".gd")) {
|
|
|
|
next = dir->get_next();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
String base_type;
|
|
|
|
String source_file = current_dir.path_join(next);
|
|
|
|
String class_name = GDScriptLanguage::get_singleton()->get_global_class_name(source_file, &base_type);
|
|
|
|
if (class_name.is_empty()) {
|
|
|
|
next = dir->get_next();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ERR_FAIL_COND_V_MSG(ScriptServer::is_global_class(class_name), false,
|
|
|
|
"Class name '" + class_name + "' from " + source_file + " is already used in " + ScriptServer::get_global_class_path(class_name));
|
2021-04-07 15:12:51 +02:00
|
|
|
|
2024-01-22 15:31:55 +01:00
|
|
|
ScriptServer::add_global_class(class_name, base_type, gdscript_name, source_file);
|
2021-04-07 15:12:51 +02:00
|
|
|
}
|
|
|
|
|
2024-01-22 15:31:55 +01:00
|
|
|
next = dir->get_next();
|
2021-04-07 15:12:51 +02:00
|
|
|
}
|
2024-01-22 15:31:55 +01:00
|
|
|
|
|
|
|
dir->list_dir_end();
|
|
|
|
|
2021-04-07 15:12:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-01-22 15:31:55 +01:00
|
|
|
bool GDScriptTestRunner::generate_class_index() {
|
|
|
|
Error err = OK;
|
|
|
|
Ref<DirAccess> dir(DirAccess::open(source_dir, &err));
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V_MSG(err != OK, false, "Could not open specified test directory.");
|
|
|
|
|
|
|
|
source_dir = dir->get_current_dir() + "/"; // Make it absolute path.
|
|
|
|
return generate_class_index_recursive(dir->get_current_dir());
|
|
|
|
}
|
|
|
|
|
2021-04-07 15:12:51 +02:00
|
|
|
GDScriptTest::GDScriptTest(const String &p_source_path, const String &p_output_path, const String &p_base_dir) {
|
|
|
|
source_file = p_source_path;
|
|
|
|
output_file = p_output_path;
|
|
|
|
base_dir = p_base_dir;
|
|
|
|
_print_handler.printfunc = print_handler;
|
|
|
|
_error_handler.errfunc = error_handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDScriptTestRunner::handle_cmdline() {
|
|
|
|
List<String> cmdline_args = OS::get_singleton()->get_cmdline_args();
|
|
|
|
|
2021-07-16 05:45:57 +02:00
|
|
|
for (List<String>::Element *E = cmdline_args.front(); E; E = E->next()) {
|
2021-04-07 15:12:51 +02:00
|
|
|
String &cmd = E->get();
|
2023-01-27 12:07:53 +01:00
|
|
|
if (cmd == "--gdscript-generate-tests") {
|
2023-04-26 16:12:47 +02:00
|
|
|
String path;
|
|
|
|
if (E->next()) {
|
|
|
|
path = E->next()->get();
|
|
|
|
} else {
|
|
|
|
path = "modules/gdscript/tests/scripts";
|
2021-04-07 15:12:51 +02:00
|
|
|
}
|
|
|
|
|
2023-01-27 12:07:53 +01:00
|
|
|
GDScriptTestRunner runner(path, false, cmdline_args.find("--print-filenames") != nullptr);
|
|
|
|
|
|
|
|
bool completed = runner.generate_outputs();
|
|
|
|
int failed = completed ? 0 : -1;
|
2021-04-07 15:12:51 +02:00
|
|
|
exit(failed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDScriptTest::enable_stdout() {
|
|
|
|
// TODO: this could likely be handled by doctest or `tests/test_macros.h`.
|
|
|
|
OS::get_singleton()->set_stdout_enabled(true);
|
|
|
|
OS::get_singleton()->set_stderr_enabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDScriptTest::disable_stdout() {
|
|
|
|
// TODO: this could likely be handled by doctest or `tests/test_macros.h`.
|
|
|
|
OS::get_singleton()->set_stdout_enabled(false);
|
|
|
|
OS::get_singleton()->set_stderr_enabled(false);
|
|
|
|
}
|
|
|
|
|
2022-05-03 01:29:38 +02:00
|
|
|
void GDScriptTest::print_handler(void *p_this, const String &p_message, bool p_error, bool p_rich) {
|
2021-04-07 15:12:51 +02:00
|
|
|
TestResult *result = (TestResult *)p_this;
|
|
|
|
result->output += p_message + "\n";
|
|
|
|
}
|
|
|
|
|
2021-09-22 17:36:40 +02:00
|
|
|
void GDScriptTest::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) {
|
2021-04-07 15:12:51 +02:00
|
|
|
ErrorHandlerData *data = (ErrorHandlerData *)p_this;
|
|
|
|
GDScriptTest *self = data->self;
|
|
|
|
TestResult *result = data->result;
|
|
|
|
|
|
|
|
result->status = GDTEST_RUNTIME_ERROR;
|
|
|
|
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(">> ");
|
2023-06-14 13:28:04 +02:00
|
|
|
// Only include the function, file and line for script errors, otherwise the
|
|
|
|
// test outputs changes based on the platform/compiler.
|
|
|
|
bool include_source_info = false;
|
2021-04-07 15:12:51 +02:00
|
|
|
switch (p_type) {
|
|
|
|
case ERR_HANDLER_ERROR:
|
|
|
|
builder.append("ERROR");
|
|
|
|
break;
|
|
|
|
case ERR_HANDLER_WARNING:
|
|
|
|
builder.append("WARNING");
|
|
|
|
break;
|
|
|
|
case ERR_HANDLER_SCRIPT:
|
|
|
|
builder.append("SCRIPT ERROR");
|
2023-06-14 13:28:04 +02:00
|
|
|
include_source_info = true;
|
2021-04-07 15:12:51 +02:00
|
|
|
break;
|
|
|
|
case ERR_HANDLER_SHADER:
|
|
|
|
builder.append("SHADER ERROR");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
builder.append("Unknown error type");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-06-14 13:28:04 +02:00
|
|
|
if (include_source_info) {
|
|
|
|
builder.append("\n>> on function: ");
|
|
|
|
builder.append(String::utf8(p_function));
|
|
|
|
builder.append("()\n>> ");
|
2023-06-14 10:53:37 +02:00
|
|
|
builder.append(String::utf8(p_file).trim_prefix(self->base_dir).replace("\\", "/"));
|
|
|
|
builder.append("\n>> ");
|
|
|
|
builder.append(itos(p_line));
|
|
|
|
}
|
2023-06-14 13:28:04 +02:00
|
|
|
builder.append("\n>> ");
|
2022-01-06 10:34:10 +01:00
|
|
|
builder.append(String::utf8(p_error));
|
2021-04-07 15:12:51 +02:00
|
|
|
if (strlen(p_explanation) > 0) {
|
|
|
|
builder.append("\n>> ");
|
2022-01-06 10:34:10 +01:00
|
|
|
builder.append(String::utf8(p_explanation));
|
2021-04-07 15:12:51 +02:00
|
|
|
}
|
|
|
|
builder.append("\n");
|
|
|
|
|
|
|
|
result->output = builder.as_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDScriptTest::check_output(const String &p_output) const {
|
|
|
|
Error err = OK;
|
|
|
|
String expected = FileAccess::get_file_as_string(output_file, &err);
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V_MSG(err != OK, false, "Error when opening the output file.");
|
|
|
|
|
|
|
|
String got = p_output.strip_edges(); // TODO: may be hacky.
|
|
|
|
got += "\n"; // Make sure to insert newline for CI static checks.
|
|
|
|
|
2022-02-02 01:31:12 +01:00
|
|
|
#ifndef DEBUG_ENABLED
|
|
|
|
expected = strip_warnings(expected);
|
|
|
|
#endif
|
|
|
|
|
2021-04-07 15:12:51 +02:00
|
|
|
return got == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
String GDScriptTest::get_text_for_status(GDScriptTest::TestStatus p_status) const {
|
|
|
|
switch (p_status) {
|
|
|
|
case GDTEST_OK:
|
|
|
|
return "GDTEST_OK";
|
|
|
|
case GDTEST_LOAD_ERROR:
|
|
|
|
return "GDTEST_LOAD_ERROR";
|
|
|
|
case GDTEST_PARSER_ERROR:
|
|
|
|
return "GDTEST_PARSER_ERROR";
|
|
|
|
case GDTEST_ANALYZER_ERROR:
|
|
|
|
return "GDTEST_ANALYZER_ERROR";
|
|
|
|
case GDTEST_COMPILER_ERROR:
|
|
|
|
return "GDTEST_COMPILER_ERROR";
|
|
|
|
case GDTEST_RUNTIME_ERROR:
|
|
|
|
return "GDTEST_RUNTIME_ERROR";
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
GDScriptTest::TestResult GDScriptTest::execute_test_code(bool p_is_generating) {
|
|
|
|
disable_stdout();
|
|
|
|
|
|
|
|
TestResult result;
|
|
|
|
result.status = GDTEST_OK;
|
|
|
|
result.output = String();
|
2021-09-13 22:59:28 +02:00
|
|
|
result.passed = false;
|
2021-04-07 15:12:51 +02:00
|
|
|
|
|
|
|
Error err = OK;
|
|
|
|
|
|
|
|
// Create script.
|
|
|
|
Ref<GDScript> script;
|
2021-06-18 00:03:09 +02:00
|
|
|
script.instantiate();
|
2021-04-07 15:12:51 +02:00
|
|
|
script->set_path(source_file);
|
2024-01-22 15:31:55 +01:00
|
|
|
if (tokenizer_mode == TOKENIZER_TEXT) {
|
|
|
|
err = script->load_source_code(source_file);
|
|
|
|
} else {
|
|
|
|
String code = FileAccess::get_file_as_string(source_file, &err);
|
|
|
|
if (!err) {
|
2024-01-26 18:49:31 +01:00
|
|
|
Vector<uint8_t> buffer = GDScriptTokenizerBuffer::parse_code_string(code, GDScriptTokenizerBuffer::COMPRESS_ZSTD);
|
2024-01-22 15:31:55 +01:00
|
|
|
script->set_binary_tokens_source(buffer);
|
|
|
|
}
|
|
|
|
}
|
2021-04-07 15:12:51 +02:00
|
|
|
if (err != OK) {
|
|
|
|
enable_stdout();
|
|
|
|
result.status = GDTEST_LOAD_ERROR;
|
|
|
|
result.passed = false;
|
|
|
|
ERR_FAIL_V_MSG(result, "\nCould not load source code for: '" + source_file + "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test parsing.
|
|
|
|
GDScriptParser parser;
|
2024-01-22 15:31:55 +01:00
|
|
|
if (tokenizer_mode == TOKENIZER_TEXT) {
|
|
|
|
err = parser.parse(script->get_source_code(), source_file, false);
|
|
|
|
} else {
|
|
|
|
err = parser.parse_binary(script->get_binary_tokens_source(), source_file);
|
|
|
|
}
|
2021-04-07 15:12:51 +02:00
|
|
|
if (err != OK) {
|
|
|
|
enable_stdout();
|
|
|
|
result.status = GDTEST_PARSER_ERROR;
|
|
|
|
result.output = get_text_for_status(result.status) + "\n";
|
|
|
|
|
|
|
|
const List<GDScriptParser::ParserError> &errors = parser.get_errors();
|
2022-10-07 14:43:59 +02:00
|
|
|
if (!errors.is_empty()) {
|
|
|
|
// Only the first error since the following might be cascading.
|
|
|
|
result.output += errors[0].message + "\n"; // TODO: line, column?
|
2021-04-07 15:12:51 +02:00
|
|
|
}
|
|
|
|
if (!p_is_generating) {
|
|
|
|
result.passed = check_output(result.output);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test type-checking.
|
|
|
|
GDScriptAnalyzer analyzer(&parser);
|
|
|
|
err = analyzer.analyze();
|
|
|
|
if (err != OK) {
|
|
|
|
enable_stdout();
|
|
|
|
result.status = GDTEST_ANALYZER_ERROR;
|
|
|
|
result.output = get_text_for_status(result.status) + "\n";
|
|
|
|
|
|
|
|
const List<GDScriptParser::ParserError> &errors = parser.get_errors();
|
2022-10-07 14:43:59 +02:00
|
|
|
if (!errors.is_empty()) {
|
|
|
|
// Only the first error since the following might be cascading.
|
|
|
|
result.output += errors[0].message + "\n"; // TODO: line, column?
|
2021-04-07 15:12:51 +02:00
|
|
|
}
|
|
|
|
if (!p_is_generating) {
|
|
|
|
result.passed = check_output(result.output);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-02-02 01:31:12 +01:00
|
|
|
#ifdef DEBUG_ENABLED
|
2021-04-07 15:12:51 +02:00
|
|
|
StringBuilder warning_string;
|
2021-07-16 05:45:57 +02:00
|
|
|
for (const GDScriptWarning &E : parser.get_warnings()) {
|
|
|
|
const GDScriptWarning warning = E;
|
2021-04-07 15:12:51 +02:00
|
|
|
warning_string.append(">> WARNING");
|
|
|
|
warning_string.append("\n>> Line: ");
|
|
|
|
warning_string.append(itos(warning.start_line));
|
|
|
|
warning_string.append("\n>> ");
|
|
|
|
warning_string.append(warning.get_name());
|
|
|
|
warning_string.append("\n>> ");
|
|
|
|
warning_string.append(warning.get_message());
|
|
|
|
warning_string.append("\n");
|
|
|
|
}
|
|
|
|
result.output += warning_string.as_string();
|
2022-02-02 01:31:12 +01:00
|
|
|
#endif
|
2021-04-07 15:12:51 +02:00
|
|
|
|
|
|
|
// Test compiling.
|
|
|
|
GDScriptCompiler compiler;
|
|
|
|
err = compiler.compile(&parser, script.ptr(), false);
|
|
|
|
if (err != OK) {
|
|
|
|
enable_stdout();
|
|
|
|
result.status = GDTEST_COMPILER_ERROR;
|
|
|
|
result.output = get_text_for_status(result.status) + "\n";
|
|
|
|
result.output = compiler.get_error();
|
|
|
|
if (!p_is_generating) {
|
|
|
|
result.passed = check_output(result.output);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2021-09-11 20:38:15 +02:00
|
|
|
// Script files matching this pattern are allowed to not contain a test() function.
|
|
|
|
if (source_file.match("*.notest.gd")) {
|
2021-09-13 22:59:28 +02:00
|
|
|
enable_stdout();
|
|
|
|
result.passed = check_output(result.output);
|
2021-09-11 20:38:15 +02:00
|
|
|
return result;
|
|
|
|
}
|
2021-04-07 15:12:51 +02:00
|
|
|
// Test running.
|
2022-05-13 15:04:37 +02:00
|
|
|
const HashMap<StringName, GDScriptFunction *>::ConstIterator test_function_element = script->get_member_functions().find(GDScriptTestRunner::test_function_name);
|
|
|
|
if (!test_function_element) {
|
2021-04-07 15:12:51 +02:00
|
|
|
enable_stdout();
|
|
|
|
result.status = GDTEST_LOAD_ERROR;
|
|
|
|
result.output = "";
|
|
|
|
result.passed = false;
|
|
|
|
ERR_FAIL_V_MSG(result, "\nCould not find test function on: '" + source_file + "'");
|
|
|
|
}
|
|
|
|
|
2023-04-19 16:10:35 +02:00
|
|
|
// Setup output handlers.
|
|
|
|
ErrorHandlerData error_data(&result, this);
|
|
|
|
|
|
|
|
_print_handler.userdata = &result;
|
|
|
|
_error_handler.userdata = &error_data;
|
|
|
|
add_print_handler(&_print_handler);
|
|
|
|
add_error_handler(&_error_handler);
|
|
|
|
|
2024-01-22 15:31:55 +01:00
|
|
|
err = script->reload();
|
|
|
|
if (err) {
|
|
|
|
enable_stdout();
|
|
|
|
result.status = GDTEST_LOAD_ERROR;
|
|
|
|
result.output = "";
|
|
|
|
result.passed = false;
|
|
|
|
ERR_FAIL_V_MSG(result, "\nCould not reload script: '" + source_file + "'");
|
|
|
|
}
|
2021-04-07 15:12:51 +02:00
|
|
|
|
|
|
|
// Create object instance for test.
|
2021-06-18 00:03:09 +02:00
|
|
|
Object *obj = ClassDB::instantiate(script->get_native()->get_name());
|
2021-06-04 18:03:15 +02:00
|
|
|
Ref<RefCounted> obj_ref;
|
|
|
|
if (obj->is_ref_counted()) {
|
|
|
|
obj_ref = Ref<RefCounted>(Object::cast_to<RefCounted>(obj));
|
2021-04-07 15:12:51 +02:00
|
|
|
}
|
|
|
|
obj->set_script(script);
|
|
|
|
GDScriptInstance *instance = static_cast<GDScriptInstance *>(obj->get_script_instance());
|
|
|
|
|
|
|
|
// Call test function.
|
|
|
|
Callable::CallError call_err;
|
2022-03-09 14:58:40 +01:00
|
|
|
instance->callp(GDScriptTestRunner::test_function_name, nullptr, 0, call_err);
|
2021-04-07 15:12:51 +02:00
|
|
|
|
|
|
|
// Tear down output handlers.
|
|
|
|
remove_print_handler(&_print_handler);
|
|
|
|
remove_error_handler(&_error_handler);
|
|
|
|
|
|
|
|
// Check results.
|
|
|
|
if (call_err.error != Callable::CallError::CALL_OK) {
|
|
|
|
enable_stdout();
|
|
|
|
result.status = GDTEST_LOAD_ERROR;
|
|
|
|
result.passed = false;
|
|
|
|
ERR_FAIL_V_MSG(result, "\nCould not call test function on: '" + source_file + "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
result.output = get_text_for_status(result.status) + "\n" + result.output;
|
|
|
|
if (!p_is_generating) {
|
|
|
|
result.passed = check_output(result.output);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj_ref.is_null()) {
|
|
|
|
memdelete(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
enable_stdout();
|
2022-10-09 18:41:28 +02:00
|
|
|
|
|
|
|
GDScriptCache::remove_script(script->get_path());
|
|
|
|
|
2021-04-07 15:12:51 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
GDScriptTest::TestResult GDScriptTest::run_test() {
|
|
|
|
return execute_test_code(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDScriptTest::generate_output() {
|
|
|
|
TestResult result = execute_test_code(true);
|
|
|
|
if (result.status == GDTEST_LOAD_ERROR) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error err = OK;
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<FileAccess> out_file = FileAccess::open(output_file, FileAccess::WRITE, &err);
|
2021-04-07 15:12:51 +02:00
|
|
|
if (err != OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
String output = result.output.strip_edges(); // TODO: may be hacky.
|
|
|
|
output += "\n"; // Make sure to insert newline for CI static checks.
|
|
|
|
|
|
|
|
out_file->store_string(output);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace GDScriptTests
|