gdscript_test_runner_suite.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**************************************************************************/
  2. /* gdscript_test_runner_suite.h */
  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. #pragma once
  31. #include "gdscript_test_runner.h"
  32. #include "modules/gdscript/gdscript_cache.h"
  33. #include "tests/test_macros.h"
  34. #include "tests/test_utils.h"
  35. namespace GDScriptTests {
  36. class TestGDScriptCacheAccessor {
  37. public:
  38. static bool has_shallow(String p_path) {
  39. return GDScriptCache::singleton->shallow_gdscript_cache.has(p_path);
  40. }
  41. static bool has_full(String p_path) {
  42. return GDScriptCache::singleton->full_gdscript_cache.has(p_path);
  43. }
  44. };
  45. // TODO: Handle some cases failing on release builds. See: https://github.com/godotengine/godot/pull/88452
  46. #ifdef TOOLS_ENABLED
  47. TEST_SUITE("[Modules][GDScript]") {
  48. TEST_CASE("Script compilation and runtime") {
  49. bool print_filenames = OS::get_singleton()->get_cmdline_args().find("--print-filenames") != nullptr;
  50. bool use_binary_tokens = OS::get_singleton()->get_cmdline_args().find("--use-binary-tokens") != nullptr;
  51. GDScriptTestRunner runner("modules/gdscript/tests/scripts", true, print_filenames, use_binary_tokens);
  52. int fail_count = runner.run_tests();
  53. INFO("Make sure `*.out` files have expected results.");
  54. REQUIRE_MESSAGE(fail_count == 0, "All GDScript tests should pass.");
  55. }
  56. }
  57. #endif // TOOLS_ENABLED
  58. TEST_CASE("[Modules][GDScript] Load source code dynamically and run it") {
  59. GDScriptLanguage::get_singleton()->init();
  60. Ref<GDScript> gdscript = memnew(GDScript);
  61. gdscript->set_source_code(R"(
  62. extends RefCounted
  63. func _init():
  64. set_meta("result", 42)
  65. )");
  66. // A spurious `Condition "err" is true` message is printed (despite parsing being successful and returning `OK`).
  67. // Silence it.
  68. ERR_PRINT_OFF;
  69. const Error error = gdscript->reload();
  70. ERR_PRINT_ON;
  71. CHECK_MESSAGE(error == OK, "The script should parse successfully.");
  72. // Run the script by assigning it to a reference-counted object.
  73. Ref<RefCounted> ref_counted = memnew(RefCounted);
  74. ref_counted->set_script(gdscript);
  75. CHECK_MESSAGE(int(ref_counted->get_meta("result")) == 42, "The script should assign object metadata successfully.");
  76. }
  77. TEST_CASE("[Modules][GDScript] Loading keeps ResourceCache and GDScriptCache in sync") {
  78. const String path = TestUtils::get_temp_path("gdscript_load_test.gd");
  79. {
  80. Ref<FileAccess> fa = FileAccess::open(path, FileAccess::ModeFlags::WRITE);
  81. fa->store_string("extends Node\n");
  82. fa->close();
  83. }
  84. CHECK(!ResourceCache::has(path));
  85. CHECK(!TestGDScriptCacheAccessor::has_shallow(path));
  86. CHECK(!TestGDScriptCacheAccessor::has_full(path));
  87. Ref<GDScript> loaded = ResourceLoader::load(path);
  88. CHECK(ResourceCache::has(path));
  89. CHECK(!TestGDScriptCacheAccessor::has_shallow(path));
  90. CHECK(TestGDScriptCacheAccessor::has_full(path));
  91. }
  92. TEST_CASE("[Modules][GDScript] Validate built-in API") {
  93. GDScriptLanguage *lang = GDScriptLanguage::get_singleton();
  94. // Validate methods.
  95. List<MethodInfo> builtin_methods;
  96. lang->get_public_functions(&builtin_methods);
  97. SUBCASE("[Modules][GDScript] Validate built-in methods") {
  98. for (const MethodInfo &mi : builtin_methods) {
  99. for (int64_t i = 0; i < mi.arguments.size(); ++i) {
  100. TEST_COND((mi.arguments[i].name.is_empty() || mi.arguments[i].name.begins_with("_unnamed_arg")),
  101. vformat("Unnamed argument in position %d of built-in method '%s'.", i, mi.name));
  102. }
  103. }
  104. }
  105. // Validate annotations.
  106. List<MethodInfo> builtin_annotations;
  107. lang->get_public_annotations(&builtin_annotations);
  108. SUBCASE("[Modules][GDScript] Validate built-in annotations") {
  109. for (const MethodInfo &ai : builtin_annotations) {
  110. for (int64_t i = 0; i < ai.arguments.size(); ++i) {
  111. TEST_COND((ai.arguments[i].name.is_empty() || ai.arguments[i].name.begins_with("_unnamed_arg")),
  112. vformat("Unnamed argument in position %d of built-in annotation '%s'.", i, ai.name));
  113. }
  114. }
  115. }
  116. }
  117. } // namespace GDScriptTests