diff options
Diffstat (limited to 'modules/gdscript/tests/gdscript_test_runner.cpp')
-rw-r--r-- | modules/gdscript/tests/gdscript_test_runner.cpp | 80 |
1 files changed, 43 insertions, 37 deletions
diff --git a/modules/gdscript/tests/gdscript_test_runner.cpp b/modules/gdscript/tests/gdscript_test_runner.cpp index d2c8b5c317..874cbc6ee8 100644 --- a/modules/gdscript/tests/gdscript_test_runner.cpp +++ b/modules/gdscript/tests/gdscript_test_runner.cpp @@ -132,9 +132,10 @@ void finish_language() { StringName GDScriptTestRunner::test_function_name; -GDScriptTestRunner::GDScriptTestRunner(const String &p_source_dir, bool p_init_language) { +GDScriptTestRunner::GDScriptTestRunner(const String &p_source_dir, bool p_init_language, bool p_print_filenames) { test_function_name = StaticCString::create("test"); do_init_languages = p_init_language; + print_filenames = p_print_filenames; source_dir = p_source_dir; if (!source_dir.ends_with("/")) { @@ -145,11 +146,11 @@ GDScriptTestRunner::GDScriptTestRunner(const String &p_source_dir, bool p_init_l init_language(p_source_dir); } #ifdef DEBUG_ENABLED - // Enable all warnings for GDScript, so we can test them. + // Set all warning levels to "Warn" in order to test them properly, even the ones that default to error. ProjectSettings::get_singleton()->set_setting("debug/gdscript/warnings/enable", true); for (int i = 0; i < (int)GDScriptWarning::WARNING_MAX; i++) { - String warning = GDScriptWarning::get_name_from_code((GDScriptWarning::Code)i).to_lower(); - ProjectSettings::get_singleton()->set_setting("debug/gdscript/warnings/" + warning, true); + String warning_setting = GDScriptWarning::get_settings_path_from_code((GDScriptWarning::Code)i); + ProjectSettings::get_singleton()->set_setting(warning_setting, (int)GDScriptWarning::WARN); } #endif @@ -194,6 +195,9 @@ int GDScriptTestRunner::run_tests() { int failed = 0; for (int i = 0; i < tests.size(); i++) { GDScriptTest test = tests[i]; + if (print_filenames) { + print_line(test.get_source_relative_filepath()); + } GDScriptTest::TestResult result = test.run_test(); String expected = FileAccess::get_file_as_string(test.get_output_file()); @@ -225,8 +229,13 @@ bool GDScriptTestRunner::generate_outputs() { } for (int i = 0; i < tests.size(); i++) { - OS::get_singleton()->print("."); GDScriptTest test = tests[i]; + if (print_filenames) { + print_line(test.get_source_relative_filepath()); + } else { + OS::get_singleton()->print("."); + } + bool result = test.generate_output(); if (!result) { @@ -337,30 +346,21 @@ GDScriptTest::GDScriptTest(const String &p_source_path, const String &p_output_p void GDScriptTestRunner::handle_cmdline() { List<String> cmdline_args = OS::get_singleton()->get_cmdline_args(); - // TODO: this could likely be ported to use test commands: - // https://github.com/godotengine/godot/pull/41355 - // Currently requires to startup the whole engine, which is slow. - String test_cmd = "--gdscript-test"; - String gen_cmd = "--gdscript-generate-tests"; for (List<String>::Element *E = cmdline_args.front(); E; E = E->next()) { String &cmd = E->get(); - if (cmd == test_cmd || cmd == gen_cmd) { - if (E->next() == nullptr) { - ERR_PRINT("Needed a path for the test files."); - exit(-1); + if (cmd == "--gdscript-generate-tests") { + String path; + if (E->next()) { + path = E->next()->get(); + } else { + path = "modules/gdscript/tests/scripts"; } - const String &path = E->next()->get(); + GDScriptTestRunner runner(path, false, cmdline_args.find("--print-filenames") != nullptr); - GDScriptTestRunner runner(path, false); - int failed = 0; - if (cmd == test_cmd) { - failed = runner.run_tests(); - } else { - bool completed = runner.generate_outputs(); - failed = completed ? 0 : -1; - } + bool completed = runner.generate_outputs(); + int failed = completed ? 0 : -1; exit(failed); } } @@ -392,6 +392,9 @@ void GDScriptTest::error_handler(void *p_this, const char *p_function, const cha StringBuilder builder; builder.append(">> "); + // 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; switch (p_type) { case ERR_HANDLER_ERROR: builder.append("ERROR"); @@ -401,6 +404,7 @@ void GDScriptTest::error_handler(void *p_this, const char *p_function, const cha break; case ERR_HANDLER_SCRIPT: builder.append("SCRIPT ERROR"); + include_source_info = true; break; case ERR_HANDLER_SHADER: builder.append("SHADER ERROR"); @@ -410,12 +414,14 @@ void GDScriptTest::error_handler(void *p_this, const char *p_function, const cha break; } - builder.append("\n>> on function: "); - builder.append(String::utf8(p_function)); - builder.append("()\n>> "); - builder.append(String::utf8(p_file).trim_prefix(self->base_dir)); - builder.append("\n>> "); - builder.append(itos(p_line)); + if (include_source_info) { + builder.append("\n>> on function: "); + builder.append(String::utf8(p_function)); + builder.append("()\n>> "); + builder.append(String::utf8(p_file).trim_prefix(self->base_dir).replace("\\", "/")); + builder.append("\n>> "); + builder.append(itos(p_line)); + } builder.append("\n>> "); builder.append(String::utf8(p_error)); if (strlen(p_explanation) > 0) { @@ -566,6 +572,14 @@ GDScriptTest::TestResult GDScriptTest::execute_test_code(bool p_is_generating) { ERR_FAIL_V_MSG(result, "\nCould not find test function on: '" + source_file + "'"); } + // 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); + script->reload(); // Create object instance for test. @@ -577,14 +591,6 @@ GDScriptTest::TestResult GDScriptTest::execute_test_code(bool p_is_generating) { obj->set_script(script); GDScriptInstance *instance = static_cast<GDScriptInstance *>(obj->get_script_instance()); - // 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); - // Call test function. Callable::CallError call_err; instance->callp(GDScriptTestRunner::test_function_name, nullptr, 0, call_err); |