diff options
Diffstat (limited to 'modules/gdscript/tests/test_gdscript.cpp')
-rw-r--r-- | modules/gdscript/tests/test_gdscript.cpp | 62 |
1 files changed, 39 insertions, 23 deletions
diff --git a/modules/gdscript/tests/test_gdscript.cpp b/modules/gdscript/tests/test_gdscript.cpp index 68d9984b43..80eabc1596 100644 --- a/modules/gdscript/tests/test_gdscript.cpp +++ b/modules/gdscript/tests/test_gdscript.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -30,10 +30,13 @@ #include "test_gdscript.h" -#include "core/os/file_access.h" +#include "core/config/project_settings.h" +#include "core/io/file_access.h" +#include "core/io/file_access_pack.h" #include "core/os/main_loop.h" #include "core/os/os.h" -#include "core/string_builder.h" +#include "core/string/string_builder.h" +#include "scene/resources/packed_scene.h" #include "modules/gdscript/gdscript_analyzer.h" #include "modules/gdscript/gdscript_compiler.h" @@ -44,7 +47,7 @@ #include "editor/editor_settings.h" #endif -namespace TestGDScript { +namespace GDScriptTests { static void test_tokenizer(const String &p_code, const Vector<String> &p_lines) { GDScriptTokenizer tokenizer; @@ -53,7 +56,7 @@ static void test_tokenizer(const String &p_code, const Vector<String> &p_lines) int tab_size = 4; #ifdef TOOLS_ENABLED if (EditorSettings::get_singleton()) { - tab_size = EditorSettings::get_singleton()->get_setting("text_editor/indent/size"); + tab_size = EditorSettings::get_singleton()->get_setting("text_editor/behavior/indent/size"); } #endif // TOOLS_ENABLED String tab = String(" ").repeat(tab_size); @@ -63,7 +66,7 @@ static void test_tokenizer(const String &p_code, const Vector<String> &p_lines) StringBuilder token; token += " --> "; // Padding for line number. - for (int l = current.start_line; l <= current.end_line; l++) { + for (int l = current.start_line; l <= current.end_line && l <= p_lines.size(); l++) { print_line(vformat("%04d %s", l, p_lines[l - 1]).replace("\t", tab)); } @@ -110,15 +113,25 @@ static void test_parser(const String &p_code, const String &p_script_path, const if (err != OK) { const List<GDScriptParser::ParserError> &errors = parser.get_errors(); - for (const List<GDScriptParser::ParserError>::Element *E = errors.front(); E != nullptr; E = E->next()) { - const GDScriptParser::ParserError &error = E->get(); + for (const GDScriptParser::ParserError &error : errors) { print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message)); } } - GDScriptParser::TreePrinter printer; + GDScriptAnalyzer analyzer(&parser); + analyzer.analyze(); + if (err != OK) { + const List<GDScriptParser::ParserError> &errors = parser.get_errors(); + for (const GDScriptParser::ParserError &error : errors) { + print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message)); + } + } + +#ifdef TOOLS_ENABLED + GDScriptParser::TreePrinter printer; printer.print_tree(parser); +#endif } static void test_compiler(const String &p_code, const String &p_script_path, const Vector<String> &p_lines) { @@ -128,8 +141,7 @@ static void test_compiler(const String &p_code, const String &p_script_path, con if (err != OK) { print_line("Error in parser:"); const List<GDScriptParser::ParserError> &errors = parser.get_errors(); - for (const List<GDScriptParser::ParserError>::Element *E = errors.front(); E != nullptr; E = E->next()) { - const GDScriptParser::ParserError &error = E->get(); + for (const GDScriptParser::ParserError &error : errors) { print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message)); } return; @@ -141,8 +153,7 @@ static void test_compiler(const String &p_code, const String &p_script_path, con if (err != OK) { print_line("Error in analyzer:"); const List<GDScriptParser::ParserError> &errors = parser.get_errors(); - for (const List<GDScriptParser::ParserError>::Element *E = errors.front(); E != nullptr; E = E->next()) { - const GDScriptParser::ParserError &error = E->get(); + for (const GDScriptParser::ParserError &error : errors) { print_line(vformat("%02d:%02d: %s", error.line, error.column, error.message)); } return; @@ -150,7 +161,7 @@ static void test_compiler(const String &p_code, const String &p_script_path, con GDScriptCompiler compiler; Ref<GDScript> script; - script.instance(); + script.instantiate(); script->set_path(p_script_path); err = compiler.compile(&parser, script.ptr(), false); @@ -161,8 +172,8 @@ static void test_compiler(const String &p_code, const String &p_script_path, con return; } - for (const Map<StringName, GDScriptFunction *>::Element *E = script->get_member_functions().front(); E; E = E->next()) { - const GDScriptFunction *func = E->value(); + for (const KeyValue<StringName, GDScriptFunction *> &E : script->get_member_functions()) { + const GDScriptFunction *func = E.value; String signature = "Disassembling " + func->get_name().operator String() + "("; for (int i = 0; i < func->get_argument_count(); i++) { @@ -172,8 +183,9 @@ static void test_compiler(const String &p_code, const String &p_script_path, con signature += func->get_argument_name(i); } print_line(signature + ")"); - +#ifdef TOOLS_ENABLED func->disassemble(p_lines); +#endif print_line(""); print_line(""); } @@ -182,7 +194,7 @@ static void test_compiler(const String &p_code, const String &p_script_path, con void test(TestType p_type) { List<String> cmdlargs = OS::get_singleton()->get_cmdline_args(); - if (cmdlargs.empty()) { + if (cmdlargs.is_empty()) { return; } @@ -195,9 +207,12 @@ void test(TestType p_type) { FileAccessRef fa = FileAccess::open(test, FileAccess::READ); ERR_FAIL_COND_MSG(!fa, "Could not open file: " + test); + // Initialize the language for the test routine. + init_language(fa->get_path_absolute().get_base_dir()); + Vector<uint8_t> buf; - int flen = fa->get_len(); - buf.resize(fa->get_len() + 1); + uint64_t flen = fa->get_length(); + buf.resize(flen + 1); fa->get_buffer(buf.ptrw(), flen); buf.write[flen] = 0; @@ -226,6 +241,7 @@ void test(TestType p_type) { case TEST_BYTECODE: print_line("Not implemented."); } -} -} // namespace TestGDScript + finish_language(); +} +} // namespace GDScriptTests |