| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "gdscript_test_runner.h" |
| 34 |
|
| 35 |
#include "modules/gdscript/gdscript_cache.h" |
| 36 |
#include "tests/test_macros.h" |
| 37 |
#include "tests/test_utils.h" |
| 38 |
|
| 39 |
namespace GDScriptTests { |
| 40 |
|
| 41 |
class TestGDScriptCacheAccessor { |
| 42 |
public: |
| 43 |
static bool has_shallow(String p_path) { |
| 44 |
return GDScriptCache::singleton->shallow_gdscript_cache.has(p_path); |
| 45 |
} |
| 46 |
|
| 47 |
static bool has_full(String p_path) { |
| 48 |
return GDScriptCache::singleton->full_gdscript_cache.has(p_path); |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
#ifdef TOOLS_ENABLED |
| 54 |
TEST_SUITE("[Modules][GDScript]") { |
| 55 |
TEST_CASE("Script compilation and runtime") { |
| 56 |
bool print_filenames = OS::get_singleton()->get_cmdline_args().find("--print-filenames") != nullptr; |
| 57 |
bool use_binary_tokens = OS::get_singleton()->get_cmdline_args().find("--use-binary-tokens") != nullptr; |
| 58 |
GDScriptTestRunner runner("modules/gdscript/tests/scripts", true, print_filenames, use_binary_tokens); |
| 59 |
int fail_count = runner.run_tests(); |
| 60 |
INFO("Make sure `*.out` files have expected results."); |
| 61 |
REQUIRE_MESSAGE(fail_count == 0, "All GDScript tests should pass."); |
| 62 |
} |
| 63 |
} |
| 64 |
#endif |
| 65 |
|
| 66 |
TEST_CASE("[Modules][GDScript] Load source code dynamically and run it") { |
| 67 |
GDScriptLanguage::get_singleton()->init(); |
| 68 |
Ref<GDScript> gdscript = memnew(GDScript); |
| 69 |
gdscript->set_source_code(R"( |
| 70 |
extends RefCounted |
| 71 |
|
| 72 |
func _init(): |
| 73 |
set_meta("result", 42) |
| 74 |
)"); |
| 75 |
|
| 76 |
|
| 77 |
ERR_PRINT_OFF; |
| 78 |
const Error error = gdscript->reload(); |
| 79 |
ERR_PRINT_ON; |
| 80 |
CHECK_MESSAGE(error == OK, "The script should parse successfully."); |
| 81 |
|
| 82 |
|
| 83 |
Ref<RefCounted> ref_counted = memnew(RefCounted); |
| 84 |
ref_counted->set_script(gdscript); |
| 85 |
CHECK_MESSAGE(int(ref_counted->get_meta("result")) == 42, "The script should assign object metadata successfully."); |
| 86 |
} |
| 87 |
|
| 88 |
TEST_CASE("[Modules][GDScript] Loading keeps ResourceCache and GDScriptCache in sync") { |
| 89 |
const String path = TestUtils::get_temp_path("gdscript_load_test.gd"); |
| 90 |
|
| 91 |
{ |
| 92 |
Ref<FileAccess> fa = FileAccess::open(path, FileAccess::ModeFlags::WRITE); |
| 93 |
fa->store_string("extends Node\n"); |
| 94 |
fa->close(); |
| 95 |
} |
| 96 |
|
| 97 |
CHECK(!ResourceCache::has(path)); |
| 98 |
CHECK(!TestGDScriptCacheAccessor::has_shallow(path)); |
| 99 |
CHECK(!TestGDScriptCacheAccessor::has_full(path)); |
| 100 |
|
| 101 |
Ref<GDScript> loaded = ResourceLoader::load(path); |
| 102 |
|
| 103 |
CHECK(ResourceCache::has(path)); |
| 104 |
CHECK(!TestGDScriptCacheAccessor::has_shallow(path)); |
| 105 |
CHECK(TestGDScriptCacheAccessor::has_full(path)); |
| 106 |
} |
| 107 |
|
| 108 |
TEST_CASE("[Modules][GDScript] Validate built-in API") { |
| 109 |
GDScriptLanguage *lang = GDScriptLanguage::get_singleton(); |
| 110 |
|
| 111 |
|
| 112 |
List<MethodInfo> builtin_methods; |
| 113 |
lang->get_public_functions(&builtin_methods); |
| 114 |
|
| 115 |
SUBCASE("[Modules][GDScript] Validate built-in methods") { |
| 116 |
for (const MethodInfo &mi : builtin_methods) { |
| 117 |
for (int64_t i = 0; i < mi.arguments.size(); ++i) { |
| 118 |
TEST_COND((mi.arguments[i].name.is_empty() || mi.arguments[i].name.begins_with("_unnamed_arg")), |
| 119 |
vformat("Unnamed argument in position %d of built-in method '%s'.", i, mi.name)); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
List<MethodInfo> builtin_annotations; |
| 126 |
lang->get_public_annotations(&builtin_annotations); |
| 127 |
|
| 128 |
SUBCASE("[Modules][GDScript] Validate built-in annotations") { |
| 129 |
for (const MethodInfo &ai : builtin_annotations) { |
| 130 |
for (int64_t i = 0; i < ai.arguments.size(); ++i) { |
| 131 |
TEST_COND((ai.arguments[i].name.is_empty() || ai.arguments[i].name.begins_with("_unnamed_arg")), |
| 132 |
vformat("Unnamed argument in position %d of built-in annotation '%s'.", i, ai.name)); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
} |