Add option to print filenames in GDScript unit testing
This commit is contained in:
@@ -132,9 +132,10 @@ void finish_language() {
|
|||||||
|
|
||||||
StringName GDScriptTestRunner::test_function_name;
|
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");
|
test_function_name = StaticCString::create("test");
|
||||||
do_init_languages = p_init_language;
|
do_init_languages = p_init_language;
|
||||||
|
print_filenames = p_print_filenames;
|
||||||
|
|
||||||
source_dir = p_source_dir;
|
source_dir = p_source_dir;
|
||||||
if (!source_dir.ends_with("/")) {
|
if (!source_dir.ends_with("/")) {
|
||||||
@@ -194,6 +195,9 @@ int GDScriptTestRunner::run_tests() {
|
|||||||
int failed = 0;
|
int failed = 0;
|
||||||
for (int i = 0; i < tests.size(); i++) {
|
for (int i = 0; i < tests.size(); i++) {
|
||||||
GDScriptTest test = tests[i];
|
GDScriptTest test = tests[i];
|
||||||
|
if (print_filenames) {
|
||||||
|
print_line(test.get_source_relative_filepath());
|
||||||
|
}
|
||||||
GDScriptTest::TestResult result = test.run_test();
|
GDScriptTest::TestResult result = test.run_test();
|
||||||
|
|
||||||
String expected = FileAccess::get_file_as_string(test.get_output_file());
|
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++) {
|
for (int i = 0; i < tests.size(); i++) {
|
||||||
OS::get_singleton()->print(".");
|
|
||||||
GDScriptTest test = tests[i];
|
GDScriptTest test = tests[i];
|
||||||
|
if (print_filenames) {
|
||||||
|
print_line(test.get_source_relative_filepath());
|
||||||
|
} else {
|
||||||
|
OS::get_singleton()->print(".");
|
||||||
|
}
|
||||||
|
|
||||||
bool result = test.generate_output();
|
bool result = test.generate_output();
|
||||||
|
|
||||||
if (!result) {
|
if (!result) {
|
||||||
@@ -337,15 +346,10 @@ GDScriptTest::GDScriptTest(const String &p_source_path, const String &p_output_p
|
|||||||
|
|
||||||
void GDScriptTestRunner::handle_cmdline() {
|
void GDScriptTestRunner::handle_cmdline() {
|
||||||
List<String> cmdline_args = OS::get_singleton()->get_cmdline_args();
|
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()) {
|
for (List<String>::Element *E = cmdline_args.front(); E; E = E->next()) {
|
||||||
String &cmd = E->get();
|
String &cmd = E->get();
|
||||||
if (cmd == test_cmd || cmd == gen_cmd) {
|
if (cmd == "--gdscript-generate-tests") {
|
||||||
if (E->next() == nullptr) {
|
if (E->next() == nullptr) {
|
||||||
ERR_PRINT("Needed a path for the test files.");
|
ERR_PRINT("Needed a path for the test files.");
|
||||||
exit(-1);
|
exit(-1);
|
||||||
@@ -353,14 +357,10 @@ void GDScriptTestRunner::handle_cmdline() {
|
|||||||
|
|
||||||
const String &path = E->next()->get();
|
const String &path = E->next()->get();
|
||||||
|
|
||||||
GDScriptTestRunner runner(path, false);
|
GDScriptTestRunner runner(path, false, cmdline_args.find("--print-filenames") != nullptr);
|
||||||
int failed = 0;
|
|
||||||
if (cmd == test_cmd) {
|
bool completed = runner.generate_outputs();
|
||||||
failed = runner.run_tests();
|
int failed = completed ? 0 : -1;
|
||||||
} else {
|
|
||||||
bool completed = runner.generate_outputs();
|
|
||||||
failed = completed ? 0 : -1;
|
|
||||||
}
|
|
||||||
exit(failed);
|
exit(failed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,6 +92,7 @@ public:
|
|||||||
bool generate_output();
|
bool generate_output();
|
||||||
|
|
||||||
const String &get_source_file() const { return source_file; }
|
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; }
|
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);
|
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 is_generating = false;
|
||||||
bool do_init_languages = false;
|
bool do_init_languages = false;
|
||||||
|
bool print_filenames; // Whether filenames should be printed when generated/running tests
|
||||||
|
|
||||||
bool make_tests();
|
bool make_tests();
|
||||||
bool make_tests_for_dir(const String &p_dir);
|
bool make_tests_for_dir(const String &p_dir);
|
||||||
@@ -117,7 +119,7 @@ public:
|
|||||||
int run_tests();
|
int run_tests();
|
||||||
bool generate_outputs();
|
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();
|
~GDScriptTestRunner();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,8 @@ TEST_SUITE("[Modules][GDScript]") {
|
|||||||
// Allow the tests to fail, but do not ignore errors during development.
|
// Allow the tests to fail, but do not ignore errors during development.
|
||||||
// Update the scripts and expected output as needed.
|
// Update the scripts and expected output as needed.
|
||||||
TEST_CASE("Script compilation and runtime") {
|
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();
|
int fail_count = runner.run_tests();
|
||||||
INFO("Make sure `*.out` files have expected results.");
|
INFO("Make sure `*.out` files have expected results.");
|
||||||
REQUIRE_MESSAGE(fail_count == 0, "All GDScript tests should pass.");
|
REQUIRE_MESSAGE(fail_count == 0, "All GDScript tests should pass.");
|
||||||
|
|||||||
Reference in New Issue
Block a user