From 5f77f38573a8127e50946169e01b5ee09cfc1c96 Mon Sep 17 00:00:00 2001 From: George Marques Date: Wed, 7 Apr 2021 10:12:51 -0300 Subject: Add runner for GDScript testing This is meant for testing the GDScript implementation, not for testing user scripts nor testing the engine using scripts. Tests consists in a GDScript file and a .out file with the expected output. The .out file format is: expected status (based on the enum GDScriptTest::TestStatus) on the first line, followed by either an error message or the resulting output. Warnings are added after the first line, before the output (or compiler errors) if the parser pass without any error. The test script must have a function called `test()` which takes no argument. Such function will be called by the test runner. The test should not have any dependency unless it's part of the test too. Global classes (using `class_name`) are registered before the runner starts, so those should work if needed. Use the command `godot --gdscript-generate-tests godot-source/modules/gdscript/tests/scripts` to update the .out files with the current output (make sure the output are the expected values before committing). The tests themselves are part of the doctest suite so those can be executed with `godot --test`. Co-authored-by: Andrii Doroshenko (Xrayez) --- modules/gdscript/gdscript.cpp | 8 + modules/gdscript/register_types.cpp | 8 +- modules/gdscript/tests/gdscript_test_runner.cpp | 584 +++++++++++++++++++++ modules/gdscript/tests/gdscript_test_runner.h | 126 +++++ .../gdscript/tests/gdscript_test_runner_suite.h | 53 ++ modules/gdscript/tests/scripts/.gitignore | 2 + .../scripts/parser-errors/missing-argument.gd | 6 + .../scripts/parser-errors/missing-argument.out | 2 + .../parser-errors/missing-closing-expr-paren.gd | 2 + .../parser-errors/missing-closing-expr-paren.out | 2 + .../tests/scripts/parser-errors/missing-colon.gd | 3 + .../tests/scripts/parser-errors/missing-colon.out | 2 + .../parser-errors/missing-paren-after-args.gd | 6 + .../parser-errors/missing-paren-after-args.out | 2 + .../scripts/parser-errors/mixing-tabs-spaces.gd | 3 + .../scripts/parser-errors/mixing-tabs-spaces.out | 2 + .../scripts/parser-errors/nothing-after-dollar.gd | 3 + .../scripts/parser-errors/nothing-after-dollar.out | 2 + .../wrong-value-after-dollar-slash.gd | 3 + .../wrong-value-after-dollar-slash.out | 2 + .../parser-errors/wrong-value-after-dollar.gd | 3 + .../parser-errors/wrong-value-after-dollar.out | 2 + .../parser-features/semicolon-as-end-statement.gd | 2 + .../parser-features/semicolon-as-end-statement.out | 3 + .../trailing-comma-in-function-args.gd | 7 + .../trailing-comma-in-function-args.out | 2 + .../parser-features/variable-declaration.gd | 12 + .../parser-features/variable-declaration.out | 7 + .../scripts/parser-warnings/unused-variable.gd | 2 + .../scripts/parser-warnings/unused-variable.out | 5 + modules/gdscript/tests/scripts/project.godot | 10 + modules/gdscript/tests/test_gdscript.cpp | 76 +-- modules/gdscript/tests/test_gdscript.h | 8 +- 33 files changed, 882 insertions(+), 78 deletions(-) create mode 100644 modules/gdscript/tests/gdscript_test_runner.cpp create mode 100644 modules/gdscript/tests/gdscript_test_runner.h create mode 100644 modules/gdscript/tests/gdscript_test_runner_suite.h create mode 100644 modules/gdscript/tests/scripts/.gitignore create mode 100644 modules/gdscript/tests/scripts/parser-errors/missing-argument.gd create mode 100644 modules/gdscript/tests/scripts/parser-errors/missing-argument.out create mode 100644 modules/gdscript/tests/scripts/parser-errors/missing-closing-expr-paren.gd create mode 100644 modules/gdscript/tests/scripts/parser-errors/missing-closing-expr-paren.out create mode 100644 modules/gdscript/tests/scripts/parser-errors/missing-colon.gd create mode 100644 modules/gdscript/tests/scripts/parser-errors/missing-colon.out create mode 100644 modules/gdscript/tests/scripts/parser-errors/missing-paren-after-args.gd create mode 100644 modules/gdscript/tests/scripts/parser-errors/missing-paren-after-args.out create mode 100644 modules/gdscript/tests/scripts/parser-errors/mixing-tabs-spaces.gd create mode 100644 modules/gdscript/tests/scripts/parser-errors/mixing-tabs-spaces.out create mode 100644 modules/gdscript/tests/scripts/parser-errors/nothing-after-dollar.gd create mode 100644 modules/gdscript/tests/scripts/parser-errors/nothing-after-dollar.out create mode 100644 modules/gdscript/tests/scripts/parser-errors/wrong-value-after-dollar-slash.gd create mode 100644 modules/gdscript/tests/scripts/parser-errors/wrong-value-after-dollar-slash.out create mode 100644 modules/gdscript/tests/scripts/parser-errors/wrong-value-after-dollar.gd create mode 100644 modules/gdscript/tests/scripts/parser-errors/wrong-value-after-dollar.out create mode 100644 modules/gdscript/tests/scripts/parser-features/semicolon-as-end-statement.gd create mode 100644 modules/gdscript/tests/scripts/parser-features/semicolon-as-end-statement.out create mode 100644 modules/gdscript/tests/scripts/parser-features/trailing-comma-in-function-args.gd create mode 100644 modules/gdscript/tests/scripts/parser-features/trailing-comma-in-function-args.out create mode 100644 modules/gdscript/tests/scripts/parser-features/variable-declaration.gd create mode 100644 modules/gdscript/tests/scripts/parser-features/variable-declaration.out create mode 100644 modules/gdscript/tests/scripts/parser-warnings/unused-variable.gd create mode 100644 modules/gdscript/tests/scripts/parser-warnings/unused-variable.out create mode 100644 modules/gdscript/tests/scripts/project.godot diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index c9c5d00aa5..5f590383d0 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -45,6 +45,10 @@ #include "gdscript_parser.h" #include "gdscript_warning.h" +#ifdef TESTS_ENABLED +#include "tests/gdscript_test_runner.h" +#endif + /////////////////////////// GDScriptNativeClass::GDScriptNativeClass(const StringName &p_name) { @@ -1766,6 +1770,10 @@ void GDScriptLanguage::init() { for (List::Element *E = singletons.front(); E; E = E->next()) { _add_global(E->get().name, E->get().ptr); } + +#ifdef TESTS_ENABLED + GDScriptTests::GDScriptTestRunner::handle_cmdline(); +#endif } String GDScriptLanguage::get_type() const { diff --git a/modules/gdscript/register_types.cpp b/modules/gdscript/register_types.cpp index 19fd3daf20..2d2f94f5e0 100644 --- a/modules/gdscript/register_types.cpp +++ b/modules/gdscript/register_types.cpp @@ -163,19 +163,19 @@ void unregister_gdscript_types() { #ifdef TESTS_ENABLED void test_tokenizer() { - TestGDScript::test(TestGDScript::TestType::TEST_TOKENIZER); + GDScriptTests::test(GDScriptTests::TestType::TEST_TOKENIZER); } void test_parser() { - TestGDScript::test(TestGDScript::TestType::TEST_PARSER); + GDScriptTests::test(GDScriptTests::TestType::TEST_PARSER); } void test_compiler() { - TestGDScript::test(TestGDScript::TestType::TEST_COMPILER); + GDScriptTests::test(GDScriptTests::TestType::TEST_COMPILER); } void test_bytecode() { - TestGDScript::test(TestGDScript::TestType::TEST_BYTECODE); + GDScriptTests::test(GDScriptTests::TestType::TEST_BYTECODE); } REGISTER_TEST_COMMAND("gdscript-tokenizer", &test_tokenizer); diff --git a/modules/gdscript/tests/gdscript_test_runner.cpp b/modules/gdscript/tests/gdscript_test_runner.cpp new file mode 100644 index 0000000000..f53c3046e6 --- /dev/null +++ b/modules/gdscript/tests/gdscript_test_runner.cpp @@ -0,0 +1,584 @@ +/*************************************************************************/ +/* gdscript_test_runner.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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. */ +/*************************************************************************/ + +#include "gdscript_test_runner.h" + +#include "../gdscript.h" +#include "../gdscript_analyzer.h" +#include "../gdscript_compiler.h" +#include "../gdscript_parser.h" + +#include "core/config/project_settings.h" +#include "core/core_string_names.h" +#include "core/io/file_access_pack.h" +#include "core/os/dir_access.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() { + Map autoloads = ProjectSettings::get_singleton()->get_autoload_list(); + + // First pass, add the constants so they exist before any script is loaded. + for (Map::Element *E = autoloads.front(); E; E = E->next()) { + const ProjectSettings::AutoloadInfo &info = E->get(); + + 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. + for (Map::Element *E = autoloads.front(); E; E = E->next()) { + const ProjectSettings::AutoloadInfo &info = E->get(); + + if (!info.is_singleton) { + // Skip non-singletons since we don't have a scene tree here anyway. + continue; + } + + RES res = ResourceLoader::load(info.path); + ERR_CONTINUE_MSG(res.is_null(), "Can't autoload: " + info.path); + Node *n = nullptr; + if (res->is_class("PackedScene")) { + Ref ps = res; + n = ps->instance(); + } else if (res->is_class("Script")) { + Ref