Merge pull request #72212 from anvilfolk/gdtestnames

Add option to print filenames in GDScript unit testing
This commit is contained in:
Rémi Verschelde 2023-01-31 10:53:28 +01:00
commit b6a8b8e8f7
No known key found for this signature in database
GPG key ID: C3336907360768E1
3 changed files with 21 additions and 18 deletions

View file

@ -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("/")) {
@ -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,15 +346,10 @@ 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 (cmd == "--gdscript-generate-tests") {
if (E->next() == nullptr) {
ERR_PRINT("Needed a path for the test files.");
exit(-1);
@ -353,14 +357,10 @@ void GDScriptTestRunner::handle_cmdline() {
const String &path = E->next()->get();
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;
}
GDScriptTestRunner runner(path, false, cmdline_args.find("--print-filenames") != nullptr);
bool completed = runner.generate_outputs();
int failed = completed ? 0 : -1;
exit(failed);
}
}

View file

@ -92,6 +92,7 @@ public:
bool generate_output();
const String &get_source_file() const { return source_file; }
const String get_source_relative_filepath() const { return source_file.trim_prefix(base_dir); }
const String &get_output_file() const { return output_file; }
GDScriptTest(const String &p_source_path, const String &p_output_path, const String &p_base_dir);
@ -105,6 +106,7 @@ class GDScriptTestRunner {
bool is_generating = false;
bool do_init_languages = false;
bool print_filenames; // Whether filenames should be printed when generated/running tests
bool make_tests();
bool make_tests_for_dir(const String &p_dir);
@ -117,7 +119,7 @@ public:
int run_tests();
bool generate_outputs();
GDScriptTestRunner(const String &p_source_dir, bool p_init_language);
GDScriptTestRunner(const String &p_source_dir, bool p_init_language, bool p_print_filenames = false);
~GDScriptTestRunner();
};

View file

@ -41,7 +41,8 @@ TEST_SUITE("[Modules][GDScript]") {
// Allow the tests to fail, but do not ignore errors during development.
// Update the scripts and expected output as needed.
TEST_CASE("Script compilation and runtime") {
GDScriptTestRunner runner("modules/gdscript/tests/scripts", true);
bool print_filenames = OS::get_singleton()->get_cmdline_args().find("--print-filenames") != nullptr;
GDScriptTestRunner runner("modules/gdscript/tests/scripts", true, print_filenames);
int fail_count = runner.run_tests();
INFO("Make sure `*.out` files have expected results.");
REQUIRE_MESSAGE(fail_count == 0, "All GDScript tests should pass.");