Godot Engine – Multi-platform 2D and 3D game engine
GDScript: Initialize language when generating test output
HolonProduction6656935fbeb6
diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cppindex cd3b38158cc..c430ff767bc 100644--- a/modules/gdscript/gdscript.cpp+++ b/modules/gdscript/gdscript.cpp@@ -48,7 +48,10 @@#endif#ifdef TESTS_ENABLED-#include "tests/gdscript_test_runner.h"+#ifndef DISABLE_DEPRECATED+#include "core/error/error_macros.h"+#include "core/os/os.h"+#endif#endif#include "core/config/engine.h"@@ -2188,7 +2191,12 @@ void GDScriptLanguage::init() {#endif // DEBUG_ENABLED#ifdef TESTS_ENABLED- GDScriptTests::GDScriptTestRunner::handle_cmdline();+#ifndef DISABLE_DEPRECATED+ if (OS::get_singleton()->get_cmdline_args().find("--gdscript-generate-tests")) {+ ERR_PRINT(R"(The command for generating GDScript test output has changed to "--test gdscript-generate-tests")");+ exit(-1);+ }+#endif // !DISABLE_DEPRECATED#endif // TESTS_ENABLED}diff --git a/modules/gdscript/register_types.cpp b/modules/gdscript/register_types.cppindex c987d50dc0c..d1ed8f94bc6 100644--- a/modules/gdscript/register_types.cpp+++ b/modules/gdscript/register_types.cpp@@ -207,24 +207,29 @@ void uninitialize_gdscript_module(ModuleInitializationLevel p_level) {}#ifdef TESTS_ENABLED-void test_tokenizer() {+int test_tokenizer() {GDScriptTests::test(GDScriptTests::TestType::TEST_TOKENIZER);+ return 0;}-void test_tokenizer_buffer() {+int test_tokenizer_buffer() {GDScriptTests::test(GDScriptTests::TestType::TEST_TOKENIZER_BUFFER);+ return 0;}-void test_parser() {+int test_parser() {GDScriptTests::test(GDScriptTests::TestType::TEST_PARSER);+ return 0;}-void test_compiler() {+int test_compiler() {GDScriptTests::test(GDScriptTests::TestType::TEST_COMPILER);+ return 0;}-void test_bytecode() {+int test_bytecode() {GDScriptTests::test(GDScriptTests::TestType::TEST_BYTECODE);+ return 0;}REGISTER_TEST_COMMAND("gdscript-tokenizer", &test_tokenizer);@@ -232,4 +237,5 @@ REGISTER_TEST_COMMAND("gdscript-tokenizer-buffer", &test_tokenizer_buffer);REGISTER_TEST_COMMAND("gdscript-parser", &test_parser);REGISTER_TEST_COMMAND("gdscript-compiler", &test_compiler);REGISTER_TEST_COMMAND("gdscript-bytecode", &test_bytecode);+REGISTER_TEST_COMMAND("gdscript-generate-tests", &GDScriptTests::generate_tests);#endifdiff --git a/modules/gdscript/tests/gdscript_test_runner.cpp b/modules/gdscript/tests/gdscript_test_runner.cppindex c56cc5a373a..25358ccf886 100644--- a/modules/gdscript/tests/gdscript_test_runner.cpp+++ b/modules/gdscript/tests/gdscript_test_runner.cpp@@ -132,9 +132,8 @@ void finish_language() {StringName GDScriptTestRunner::test_function_name;-GDScriptTestRunner::GDScriptTestRunner(const String &p_source_dir, bool p_init_language, bool p_print_filenames, bool p_use_binary_tokens) {+GDScriptTestRunner::GDScriptTestRunner(const String &p_source_dir, bool p_print_filenames, bool p_use_binary_tokens) {test_function_name = StringName("test");- do_init_languages = p_init_language;print_filenames = p_print_filenames;binary_tokens = p_use_binary_tokens;@@ -143,9 +142,7 @@ GDScriptTestRunner::GDScriptTestRunner(const String &p_source_dir, bool p_init_lsource_dir += "/";}- if (do_init_languages) {- init_language(p_source_dir);- }+ init_language(p_source_dir);#ifdef DEBUG_ENABLED// Set all warning levels to "Warn" in order to test them properly, even the ones that default to error.@@ -171,9 +168,7 @@ GDScriptTestRunner::GDScriptTestRunner(const String &p_source_dir, bool p_init_lGDScriptTestRunner::~GDScriptTestRunner() {test_function_name = StringName();- if (do_init_languages) {- finish_language();- }+ finish_language();}#ifndef DEBUG_ENABLED@@ -413,28 +408,6 @@ GDScriptTest::GDScriptTest(const String &p_source_path, const String &p_output_p_error_handler.errfunc = error_handler;}-void GDScriptTestRunner::handle_cmdline() {- List<String> cmdline_args = OS::get_singleton()->get_cmdline_args();-- for (List<String>::Element *E = cmdline_args.front(); E; E = E->next()) {- String &cmd = E->get();- if (cmd == "--gdscript-generate-tests") {- String path;- if (E->next()) {- path = E->next()->get();- } else {- path = "modules/gdscript/tests/scripts";- }-- GDScriptTestRunner runner(path, false, cmdline_args.find("--print-filenames") != nullptr);-- bool completed = runner.generate_outputs();- int failed = completed ? 0 : -1;- 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);diff --git a/modules/gdscript/tests/gdscript_test_runner.h b/modules/gdscript/tests/gdscript_test_runner.hindex 9d8475e7236..b3cbdab6b51 100644--- a/modules/gdscript/tests/gdscript_test_runner.h+++ b/modules/gdscript/tests/gdscript_test_runner.h@@ -114,7 +114,6 @@ class GDScriptTestRunner {Vector<GDScriptTest> tests;bool is_generating = false;- bool do_init_languages = false;bool print_filenames; // Whether filenames should be printed when generated/running testsbool binary_tokens; // Test with buffer tokenizer.@@ -125,11 +124,10 @@ class GDScriptTestRunner {public:static StringName test_function_name;- static void handle_cmdline();int run_tests();bool generate_outputs();- GDScriptTestRunner(const String &p_source_dir, bool p_init_language, bool p_print_filenames = false, bool p_use_binary_tokens = false);+ GDScriptTestRunner(const String &p_source_dir, bool p_print_filenames = false, bool p_use_binary_tokens = false);~GDScriptTestRunner();};diff --git a/modules/gdscript/tests/gdscript_test_runner_suite.h b/modules/gdscript/tests/gdscript_test_runner_suite.hindex 43a0b13ffb4..cc6e825cc46 100644--- a/modules/gdscript/tests/gdscript_test_runner_suite.h+++ b/modules/gdscript/tests/gdscript_test_runner_suite.h@@ -62,7 +62,7 @@ TEST_SUITE("[Modules][GDScript]") {TEST_CASE("Script compilation and runtime") {bool print_filenames = OS::get_singleton()->get_cmdline_args().find("--print-filenames") != nullptr;bool use_binary_tokens = OS::get_singleton()->get_cmdline_args().find("--use-binary-tokens") != nullptr;- GDScriptTestRunner runner("modules/gdscript/tests/scripts", true, print_filenames, use_binary_tokens);+ GDScriptTestRunner runner("modules/gdscript/tests/scripts", print_filenames, use_binary_tokens);int fail_count = runner.run_tests();INFO("Make sure `*.out` files have expected results.");REQUIRE_MESSAGE(fail_count == 0, "All GDScript tests should pass.");diff --git a/modules/gdscript/tests/test_gdscript.cpp b/modules/gdscript/tests/test_gdscript.cppindex 9eb6f783a23..34158d07137 100644--- a/modules/gdscript/tests/test_gdscript.cpp+++ b/modules/gdscript/tests/test_gdscript.cpp@@ -380,4 +380,25 @@ void test(TestType p_type) {finish_language();}++int generate_tests() {+ List<String> cmdline_args = OS::get_singleton()->get_cmdline_args();++ const bool print_filenames = cmdline_args.erase("--print-filenames");++ String path;++ List<String>::Element *element = cmdline_args.find("gdscript-generate-tests");+ if (element && element->next()) {+ path = element->next()->get();+ } else {+ path = "modules/gdscript/tests/scripts";+ }++ GDScriptTestRunner runner(path, print_filenames);++ bool completed = runner.generate_outputs();+ return completed ? 0 : -1;+}+} // namespace GDScriptTestsdiff --git a/modules/gdscript/tests/test_gdscript.h b/modules/gdscript/tests/test_gdscript.hindex 7d26abee7bd..aaba46e0b28 100644--- a/modules/gdscript/tests/test_gdscript.h+++ b/modules/gdscript/tests/test_gdscript.h@@ -41,5 +41,6 @@ enum TestType {};void test(TestType p_type);+int generate_tests();} // namespace GDScriptTestsdiff --git a/tests/test_macros.h b/tests/test_macros.hindex 7fb9561e603..dd3eeb22e92 100644--- a/tests/test_macros.h+++ b/tests/test_macros.h@@ -141,7 +141,7 @@ DOCTEST_STRINGIFY_VARIANT(PackedVector4Array);// For instance: REGISTER_TEST_COMMAND("gdscript-parser" &test_parser_func).// Example usage: `godot --test gdscript-parser`.-typedef void (*TestFunc)();+using TestFunc = int (*)();extern HashMap<String, TestFunc> *test_commands;int register_test_command(String p_command, TestFunc p_function);diff --git a/tests/test_main.cpp b/tests/test_main.cppindex c13cfbe0243..d71d95dc010 100644--- a/tests/test_main.cpp+++ b/tests/test_main.cpp@@ -102,17 +102,18 @@ int test_main(int argc, char *argv[]) {// Run custom test tools.if (test_commands) {+ int res = 0;for (const KeyValue<String, TestFunc> &E : (*test_commands)) {if (args.find(E.key)) {const TestFunc &test_func = E.value;- test_func();+ res = test_func();run_tests = false;break;}}if (!run_tests) {delete test_commands;- return 0;+ return res;}}// Doctest runner.