gdscript_test_runner.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /**************************************************************************/
  2. /* gdscript_test_runner.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "gdscript_test_runner.h"
  31. #include "../gdscript.h"
  32. #include "../gdscript_analyzer.h"
  33. #include "../gdscript_compiler.h"
  34. #include "../gdscript_parser.h"
  35. #include "../gdscript_tokenizer_buffer.h"
  36. #include "core/config/project_settings.h"
  37. #include "core/core_globals.h"
  38. #include "core/io/dir_access.h"
  39. #include "core/io/file_access_pack.h"
  40. #include "core/os/os.h"
  41. #include "core/string/string_builder.h"
  42. #include "scene/resources/packed_scene.h"
  43. #include "tests/test_macros.h"
  44. namespace GDScriptTests {
  45. void init_autoloads() {
  46. HashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  47. // First pass, add the constants so they exist before any script is loaded.
  48. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  49. const ProjectSettings::AutoloadInfo &info = E.value;
  50. if (info.is_singleton) {
  51. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  52. ScriptServer::get_language(i)->add_global_constant(info.name, Variant());
  53. }
  54. }
  55. }
  56. // Second pass, load into global constants.
  57. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  58. const ProjectSettings::AutoloadInfo &info = E.value;
  59. if (!info.is_singleton) {
  60. // Skip non-singletons since we don't have a scene tree here anyway.
  61. continue;
  62. }
  63. Node *n = nullptr;
  64. if (ResourceLoader::get_resource_type(info.path) == "PackedScene") {
  65. // Cache the scene reference before loading it (for cyclic references)
  66. Ref<PackedScene> scn;
  67. scn.instantiate();
  68. scn->set_path(info.path);
  69. scn->reload_from_file();
  70. ERR_CONTINUE_MSG(scn.is_null(), vformat("Failed to instantiate an autoload, can't load from path: %s.", info.path));
  71. if (scn.is_valid()) {
  72. n = scn->instantiate();
  73. }
  74. } else {
  75. Ref<Resource> res = ResourceLoader::load(info.path);
  76. ERR_CONTINUE_MSG(res.is_null(), vformat("Failed to instantiate an autoload, can't load from path: %s.", info.path));
  77. Ref<Script> scr = res;
  78. if (scr.is_valid()) {
  79. StringName ibt = scr->get_instance_base_type();
  80. bool valid_type = ClassDB::is_parent_class(ibt, "Node");
  81. ERR_CONTINUE_MSG(!valid_type, vformat("Failed to instantiate an autoload, script '%s' does not inherit from 'Node'.", info.path));
  82. Object *obj = ClassDB::instantiate(ibt);
  83. ERR_CONTINUE_MSG(!obj, vformat("Failed to instantiate an autoload, cannot instantiate '%s'.", ibt));
  84. n = Object::cast_to<Node>(obj);
  85. n->set_script(scr);
  86. }
  87. }
  88. ERR_CONTINUE_MSG(!n, vformat("Failed to instantiate an autoload, path is not pointing to a scene or a script: %s.", info.path));
  89. n->set_name(info.name);
  90. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  91. ScriptServer::get_language(i)->add_global_constant(info.name, n);
  92. }
  93. }
  94. }
  95. void init_language(const String &p_base_path) {
  96. // Setup project settings since it's needed by the languages to get the global scripts.
  97. // This also sets up the base resource path.
  98. Error err = ProjectSettings::get_singleton()->setup(p_base_path, String(), true);
  99. if (err) {
  100. print_line("Could not load project settings.");
  101. // Keep going since some scripts still work without this.
  102. }
  103. // Initialize the language for the test routine.
  104. GDScriptLanguage::get_singleton()->init();
  105. init_autoloads();
  106. }
  107. void finish_language() {
  108. GDScriptLanguage::get_singleton()->finish();
  109. ScriptServer::global_classes_clear();
  110. }
  111. StringName GDScriptTestRunner::test_function_name;
  112. GDScriptTestRunner::GDScriptTestRunner(const String &p_source_dir, bool p_init_language, bool p_print_filenames, bool p_use_binary_tokens) {
  113. test_function_name = StaticCString::create("test");
  114. do_init_languages = p_init_language;
  115. print_filenames = p_print_filenames;
  116. binary_tokens = p_use_binary_tokens;
  117. source_dir = p_source_dir;
  118. if (!source_dir.ends_with("/")) {
  119. source_dir += "/";
  120. }
  121. if (do_init_languages) {
  122. init_language(p_source_dir);
  123. }
  124. #ifdef DEBUG_ENABLED
  125. // Set all warning levels to "Warn" in order to test them properly, even the ones that default to error.
  126. ProjectSettings::get_singleton()->set_setting("debug/gdscript/warnings/enable", true);
  127. for (int i = 0; i < (int)GDScriptWarning::WARNING_MAX; i++) {
  128. if (i == GDScriptWarning::UNTYPED_DECLARATION || i == GDScriptWarning::INFERRED_DECLARATION) {
  129. // TODO: Add ability for test scripts to specify which warnings to enable/disable for testing.
  130. continue;
  131. }
  132. String warning_setting = GDScriptWarning::get_settings_path_from_code((GDScriptWarning::Code)i);
  133. ProjectSettings::get_singleton()->set_setting(warning_setting, (int)GDScriptWarning::WARN);
  134. }
  135. #endif
  136. // Enable printing to show results
  137. CoreGlobals::print_line_enabled = true;
  138. CoreGlobals::print_error_enabled = true;
  139. }
  140. GDScriptTestRunner::~GDScriptTestRunner() {
  141. test_function_name = StringName();
  142. if (do_init_languages) {
  143. finish_language();
  144. }
  145. }
  146. #ifndef DEBUG_ENABLED
  147. static String strip_warnings(const String &p_expected) {
  148. // On release builds we don't have warnings. Here we remove them from the output before comparison
  149. // so it doesn't fail just because of difference in warnings.
  150. String expected_no_warnings;
  151. for (String line : p_expected.split("\n")) {
  152. if (line.begins_with("~~ ")) {
  153. continue;
  154. }
  155. expected_no_warnings += line + "\n";
  156. }
  157. return expected_no_warnings.strip_edges() + "\n";
  158. }
  159. #endif
  160. int GDScriptTestRunner::run_tests() {
  161. if (!make_tests()) {
  162. FAIL("An error occurred while making the tests.");
  163. return -1;
  164. }
  165. if (!generate_class_index()) {
  166. FAIL("An error occurred while generating class index.");
  167. return -1;
  168. }
  169. int failed = 0;
  170. for (int i = 0; i < tests.size(); i++) {
  171. GDScriptTest test = tests[i];
  172. if (print_filenames) {
  173. print_line(test.get_source_relative_filepath());
  174. }
  175. GDScriptTest::TestResult result = test.run_test();
  176. String expected = FileAccess::get_file_as_string(test.get_output_file());
  177. #ifndef DEBUG_ENABLED
  178. expected = strip_warnings(expected);
  179. #endif
  180. INFO(test.get_source_file());
  181. if (!result.passed) {
  182. INFO(expected);
  183. failed++;
  184. }
  185. CHECK_MESSAGE(result.passed, (result.passed ? String() : result.output));
  186. }
  187. return failed;
  188. }
  189. bool GDScriptTestRunner::generate_outputs() {
  190. is_generating = true;
  191. if (!make_tests()) {
  192. print_line("Failed to generate a test output.");
  193. return false;
  194. }
  195. if (!generate_class_index()) {
  196. return false;
  197. }
  198. for (int i = 0; i < tests.size(); i++) {
  199. GDScriptTest test = tests[i];
  200. if (print_filenames) {
  201. print_line(test.get_source_relative_filepath());
  202. } else {
  203. OS::get_singleton()->print(".");
  204. }
  205. bool result = test.generate_output();
  206. if (!result) {
  207. print_line("\nCould not generate output for " + test.get_source_file());
  208. return false;
  209. }
  210. }
  211. print_line("\nGenerated output files for " + itos(tests.size()) + " tests successfully.");
  212. return true;
  213. }
  214. bool GDScriptTestRunner::make_tests_for_dir(const String &p_dir) {
  215. Error err = OK;
  216. Ref<DirAccess> dir(DirAccess::open(p_dir, &err));
  217. if (err != OK) {
  218. return false;
  219. }
  220. String current_dir = dir->get_current_dir();
  221. dir->list_dir_begin();
  222. String next = dir->get_next();
  223. while (!next.is_empty()) {
  224. if (dir->current_is_dir()) {
  225. if (next == "." || next == ".." || next == "completion" || next == "lsp") {
  226. next = dir->get_next();
  227. continue;
  228. }
  229. if (!make_tests_for_dir(current_dir.path_join(next))) {
  230. return false;
  231. }
  232. } else {
  233. // `*.notest.gd` files are skipped.
  234. if (next.ends_with(".notest.gd")) {
  235. next = dir->get_next();
  236. continue;
  237. } else if (binary_tokens && next.ends_with(".textonly.gd")) {
  238. next = dir->get_next();
  239. continue;
  240. } else if (next.get_extension().to_lower() == "gd") {
  241. #ifndef DEBUG_ENABLED
  242. // On release builds, skip tests marked as debug only.
  243. Error open_err = OK;
  244. Ref<FileAccess> script_file(FileAccess::open(current_dir.path_join(next), FileAccess::READ, &open_err));
  245. if (open_err != OK) {
  246. ERR_PRINT(vformat(R"(Couldn't open test file "%s".)", next));
  247. next = dir->get_next();
  248. continue;
  249. } else {
  250. if (script_file->get_line() == "#debug-only") {
  251. next = dir->get_next();
  252. continue;
  253. }
  254. }
  255. #endif
  256. String out_file = next.get_basename() + ".out";
  257. ERR_FAIL_COND_V_MSG(!is_generating && !dir->file_exists(out_file), false, "Could not find output file for " + next);
  258. if (next.ends_with(".bin.gd")) {
  259. // Test text mode first.
  260. GDScriptTest text_test(current_dir.path_join(next), current_dir.path_join(out_file), source_dir);
  261. tests.push_back(text_test);
  262. // Test binary mode even without `--use-binary-tokens`.
  263. GDScriptTest bin_test(current_dir.path_join(next), current_dir.path_join(out_file), source_dir);
  264. bin_test.set_tokenizer_mode(GDScriptTest::TOKENIZER_BUFFER);
  265. tests.push_back(bin_test);
  266. } else {
  267. GDScriptTest test(current_dir.path_join(next), current_dir.path_join(out_file), source_dir);
  268. if (binary_tokens) {
  269. test.set_tokenizer_mode(GDScriptTest::TOKENIZER_BUFFER);
  270. }
  271. tests.push_back(test);
  272. }
  273. }
  274. }
  275. next = dir->get_next();
  276. }
  277. dir->list_dir_end();
  278. return true;
  279. }
  280. bool GDScriptTestRunner::make_tests() {
  281. Error err = OK;
  282. Ref<DirAccess> dir(DirAccess::open(source_dir, &err));
  283. ERR_FAIL_COND_V_MSG(err != OK, false, "Could not open specified test directory.");
  284. source_dir = dir->get_current_dir() + "/"; // Make it absolute path.
  285. return make_tests_for_dir(dir->get_current_dir());
  286. }
  287. static bool generate_class_index_recursive(const String &p_dir) {
  288. Error err = OK;
  289. Ref<DirAccess> dir(DirAccess::open(p_dir, &err));
  290. if (err != OK) {
  291. return false;
  292. }
  293. String current_dir = dir->get_current_dir();
  294. dir->list_dir_begin();
  295. String next = dir->get_next();
  296. StringName gdscript_name = GDScriptLanguage::get_singleton()->get_name();
  297. while (!next.is_empty()) {
  298. if (dir->current_is_dir()) {
  299. if (next == "." || next == ".." || next == "completion" || next == "lsp") {
  300. next = dir->get_next();
  301. continue;
  302. }
  303. if (!generate_class_index_recursive(current_dir.path_join(next))) {
  304. return false;
  305. }
  306. } else {
  307. if (!next.ends_with(".gd")) {
  308. next = dir->get_next();
  309. continue;
  310. }
  311. String base_type;
  312. String source_file = current_dir.path_join(next);
  313. bool is_abstract = false;
  314. bool is_tool = false;
  315. String class_name = GDScriptLanguage::get_singleton()->get_global_class_name(source_file, &base_type, nullptr, &is_abstract, &is_tool);
  316. if (class_name.is_empty()) {
  317. next = dir->get_next();
  318. continue;
  319. }
  320. ERR_FAIL_COND_V_MSG(ScriptServer::is_global_class(class_name), false,
  321. "Class name '" + class_name + "' from " + source_file + " is already used in " + ScriptServer::get_global_class_path(class_name));
  322. ScriptServer::add_global_class(class_name, base_type, gdscript_name, source_file, is_abstract, is_tool);
  323. }
  324. next = dir->get_next();
  325. }
  326. dir->list_dir_end();
  327. return true;
  328. }
  329. bool GDScriptTestRunner::generate_class_index() {
  330. Error err = OK;
  331. Ref<DirAccess> dir(DirAccess::open(source_dir, &err));
  332. ERR_FAIL_COND_V_MSG(err != OK, false, "Could not open specified test directory.");
  333. source_dir = dir->get_current_dir() + "/"; // Make it absolute path.
  334. return generate_class_index_recursive(dir->get_current_dir());
  335. }
  336. GDScriptTest::GDScriptTest(const String &p_source_path, const String &p_output_path, const String &p_base_dir) {
  337. source_file = p_source_path;
  338. output_file = p_output_path;
  339. base_dir = p_base_dir;
  340. _print_handler.printfunc = print_handler;
  341. _error_handler.errfunc = error_handler;
  342. }
  343. void GDScriptTestRunner::handle_cmdline() {
  344. List<String> cmdline_args = OS::get_singleton()->get_cmdline_args();
  345. for (List<String>::Element *E = cmdline_args.front(); E; E = E->next()) {
  346. String &cmd = E->get();
  347. if (cmd == "--gdscript-generate-tests") {
  348. String path;
  349. if (E->next()) {
  350. path = E->next()->get();
  351. } else {
  352. path = "modules/gdscript/tests/scripts";
  353. }
  354. GDScriptTestRunner runner(path, false, cmdline_args.find("--print-filenames") != nullptr);
  355. bool completed = runner.generate_outputs();
  356. int failed = completed ? 0 : -1;
  357. exit(failed);
  358. }
  359. }
  360. }
  361. void GDScriptTest::enable_stdout() {
  362. // TODO: this could likely be handled by doctest or `tests/test_macros.h`.
  363. OS::get_singleton()->set_stdout_enabled(true);
  364. OS::get_singleton()->set_stderr_enabled(true);
  365. }
  366. void GDScriptTest::disable_stdout() {
  367. // TODO: this could likely be handled by doctest or `tests/test_macros.h`.
  368. OS::get_singleton()->set_stdout_enabled(false);
  369. OS::get_singleton()->set_stderr_enabled(false);
  370. }
  371. void GDScriptTest::print_handler(void *p_this, const String &p_message, bool p_error, bool p_rich) {
  372. TestResult *result = (TestResult *)p_this;
  373. result->output += p_message + "\n";
  374. }
  375. void GDScriptTest::error_handler(void *p_this, const char *p_function, const char *p_file, int p_line, const char *p_error, const char *p_explanation, bool p_editor_notify, ErrorHandlerType p_type) {
  376. ErrorHandlerData *data = (ErrorHandlerData *)p_this;
  377. GDScriptTest *self = data->self;
  378. TestResult *result = data->result;
  379. result->status = GDTEST_RUNTIME_ERROR;
  380. // Only include the file, line, and function for script errors,
  381. // otherwise the test outputs changes based on the platform/compiler.
  382. String header;
  383. bool include_source_info = false;
  384. switch (p_type) {
  385. case ERR_HANDLER_ERROR:
  386. header = "ERROR";
  387. break;
  388. case ERR_HANDLER_WARNING:
  389. header = "WARNING";
  390. break;
  391. case ERR_HANDLER_SCRIPT:
  392. header = "SCRIPT ERROR";
  393. include_source_info = true;
  394. break;
  395. case ERR_HANDLER_SHADER:
  396. header = "SHADER ERROR";
  397. break;
  398. default:
  399. header = "UNKNOWN ERROR";
  400. break;
  401. }
  402. if (include_source_info) {
  403. header += vformat(" at %s:%d on %s()",
  404. String::utf8(p_file).trim_prefix(self->base_dir).replace("\\", "/"),
  405. p_line,
  406. String::utf8(p_function));
  407. }
  408. StringBuilder error_string;
  409. error_string.append(vformat(">> %s: %s\n", header, String::utf8(p_error)));
  410. if (strlen(p_explanation) > 0) {
  411. error_string.append(vformat(">> %s\n", String::utf8(p_explanation)));
  412. }
  413. result->output += error_string.as_string();
  414. }
  415. bool GDScriptTest::check_output(const String &p_output) const {
  416. Error err = OK;
  417. String expected = FileAccess::get_file_as_string(output_file, &err);
  418. ERR_FAIL_COND_V_MSG(err != OK, false, "Error when opening the output file.");
  419. String got = p_output.strip_edges(); // TODO: may be hacky.
  420. got += "\n"; // Make sure to insert newline for CI static checks.
  421. #ifndef DEBUG_ENABLED
  422. expected = strip_warnings(expected);
  423. #endif
  424. return got == expected;
  425. }
  426. String GDScriptTest::get_text_for_status(GDScriptTest::TestStatus p_status) const {
  427. switch (p_status) {
  428. case GDTEST_OK:
  429. return "GDTEST_OK";
  430. case GDTEST_LOAD_ERROR:
  431. return "GDTEST_LOAD_ERROR";
  432. case GDTEST_PARSER_ERROR:
  433. return "GDTEST_PARSER_ERROR";
  434. case GDTEST_ANALYZER_ERROR:
  435. return "GDTEST_ANALYZER_ERROR";
  436. case GDTEST_COMPILER_ERROR:
  437. return "GDTEST_COMPILER_ERROR";
  438. case GDTEST_RUNTIME_ERROR:
  439. return "GDTEST_RUNTIME_ERROR";
  440. }
  441. return "";
  442. }
  443. GDScriptTest::TestResult GDScriptTest::execute_test_code(bool p_is_generating) {
  444. disable_stdout();
  445. TestResult result;
  446. result.status = GDTEST_OK;
  447. result.output = String();
  448. result.passed = false;
  449. Error err = OK;
  450. // Create script.
  451. Ref<GDScript> script;
  452. script.instantiate();
  453. script->set_path(source_file);
  454. if (tokenizer_mode == TOKENIZER_TEXT) {
  455. err = script->load_source_code(source_file);
  456. } else {
  457. String code = FileAccess::get_file_as_string(source_file, &err);
  458. if (!err) {
  459. Vector<uint8_t> buffer = GDScriptTokenizerBuffer::parse_code_string(code, GDScriptTokenizerBuffer::COMPRESS_ZSTD);
  460. script->set_binary_tokens_source(buffer);
  461. }
  462. }
  463. if (err != OK) {
  464. enable_stdout();
  465. result.status = GDTEST_LOAD_ERROR;
  466. result.passed = false;
  467. ERR_FAIL_V_MSG(result, "\nCould not load source code for: '" + source_file + "'");
  468. }
  469. // Test parsing.
  470. GDScriptParser parser;
  471. if (tokenizer_mode == TOKENIZER_TEXT) {
  472. err = parser.parse(script->get_source_code(), source_file, false);
  473. } else {
  474. err = parser.parse_binary(script->get_binary_tokens_source(), source_file);
  475. }
  476. if (err != OK) {
  477. enable_stdout();
  478. result.status = GDTEST_PARSER_ERROR;
  479. result.output = get_text_for_status(result.status) + "\n";
  480. const List<GDScriptParser::ParserError> &errors = parser.get_errors();
  481. if (!errors.is_empty()) {
  482. // Only the first error since the following might be cascading.
  483. result.output += errors.front()->get().message + "\n"; // TODO: line, column?
  484. }
  485. if (!p_is_generating) {
  486. result.passed = check_output(result.output);
  487. }
  488. return result;
  489. }
  490. // Test type-checking.
  491. GDScriptAnalyzer analyzer(&parser);
  492. err = analyzer.analyze();
  493. if (err != OK) {
  494. enable_stdout();
  495. result.status = GDTEST_ANALYZER_ERROR;
  496. result.output = get_text_for_status(result.status) + "\n";
  497. StringBuilder error_string;
  498. for (const GDScriptParser::ParserError &error : parser.get_errors()) {
  499. error_string.append(vformat(">> ERROR at line %d: %s\n", error.line, error.message));
  500. }
  501. result.output += error_string.as_string();
  502. if (!p_is_generating) {
  503. result.passed = check_output(result.output);
  504. }
  505. return result;
  506. }
  507. #ifdef DEBUG_ENABLED
  508. StringBuilder warning_string;
  509. for (const GDScriptWarning &warning : parser.get_warnings()) {
  510. warning_string.append(vformat("~~ WARNING at line %d: (%s) %s\n", warning.start_line, warning.get_name(), warning.get_message()));
  511. }
  512. result.output += warning_string.as_string();
  513. #endif
  514. // Test compiling.
  515. GDScriptCompiler compiler;
  516. err = compiler.compile(&parser, script.ptr(), false);
  517. if (err != OK) {
  518. enable_stdout();
  519. result.status = GDTEST_COMPILER_ERROR;
  520. result.output = get_text_for_status(result.status) + "\n";
  521. result.output += compiler.get_error() + "\n";
  522. if (!p_is_generating) {
  523. result.passed = check_output(result.output);
  524. }
  525. return result;
  526. }
  527. // `*.norun.gd` files are allowed to not contain a `test()` function (no runtime testing).
  528. if (source_file.ends_with(".norun.gd")) {
  529. enable_stdout();
  530. result.status = GDTEST_OK;
  531. result.output = get_text_for_status(result.status) + "\n" + result.output;
  532. if (!p_is_generating) {
  533. result.passed = check_output(result.output);
  534. }
  535. return result;
  536. }
  537. // Test running.
  538. const HashMap<StringName, GDScriptFunction *>::ConstIterator test_function_element = script->get_member_functions().find(GDScriptTestRunner::test_function_name);
  539. if (!test_function_element) {
  540. enable_stdout();
  541. result.status = GDTEST_LOAD_ERROR;
  542. result.output = "";
  543. result.passed = false;
  544. ERR_FAIL_V_MSG(result, "\nCould not find test function on: '" + source_file + "'");
  545. }
  546. // Setup output handlers.
  547. ErrorHandlerData error_data(&result, this);
  548. _print_handler.userdata = &result;
  549. _error_handler.userdata = &error_data;
  550. add_print_handler(&_print_handler);
  551. add_error_handler(&_error_handler);
  552. err = script->reload();
  553. if (err) {
  554. enable_stdout();
  555. result.status = GDTEST_LOAD_ERROR;
  556. result.output = "";
  557. result.passed = false;
  558. ERR_FAIL_V_MSG(result, "\nCould not reload script: '" + source_file + "'");
  559. }
  560. // Create object instance for test.
  561. Object *obj = ClassDB::instantiate(script->get_native()->get_name());
  562. Ref<RefCounted> obj_ref;
  563. if (obj->is_ref_counted()) {
  564. obj_ref = Ref<RefCounted>(Object::cast_to<RefCounted>(obj));
  565. }
  566. obj->set_script(script);
  567. GDScriptInstance *instance = static_cast<GDScriptInstance *>(obj->get_script_instance());
  568. // Call test function.
  569. Callable::CallError call_err;
  570. instance->callp(GDScriptTestRunner::test_function_name, nullptr, 0, call_err);
  571. // Tear down output handlers.
  572. remove_print_handler(&_print_handler);
  573. remove_error_handler(&_error_handler);
  574. // Check results.
  575. if (call_err.error != Callable::CallError::CALL_OK) {
  576. enable_stdout();
  577. result.status = GDTEST_LOAD_ERROR;
  578. result.passed = false;
  579. ERR_FAIL_V_MSG(result, "\nCould not call test function on: '" + source_file + "'");
  580. }
  581. result.output = get_text_for_status(result.status) + "\n" + result.output;
  582. if (!p_is_generating) {
  583. result.passed = check_output(result.output);
  584. }
  585. if (obj_ref.is_null()) {
  586. memdelete(obj);
  587. }
  588. enable_stdout();
  589. GDScriptCache::remove_script(script->get_path());
  590. return result;
  591. }
  592. GDScriptTest::TestResult GDScriptTest::run_test() {
  593. return execute_test_code(false);
  594. }
  595. bool GDScriptTest::generate_output() {
  596. TestResult result = execute_test_code(true);
  597. if (result.status == GDTEST_LOAD_ERROR) {
  598. return false;
  599. }
  600. Error err = OK;
  601. Ref<FileAccess> out_file = FileAccess::open(output_file, FileAccess::WRITE, &err);
  602. if (err != OK) {
  603. return false;
  604. }
  605. String output = result.output.strip_edges(); // TODO: may be hacky.
  606. output += "\n"; // Make sure to insert newline for CI static checks.
  607. out_file->store_string(output);
  608. return true;
  609. }
  610. } // namespace GDScriptTests