Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gdscript/tests/gdscript_test_runner_suite.h
20997 views
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
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
// TODO: Handle some cases failing on release builds. See: https://github.com/godotengine/godot/pull/88452
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 // TOOLS_ENABLED
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
// A spurious `Condition "err" is true` message is printed (despite parsing being successful and returning `OK`).
76
// Silence it.
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
// Run the script by assigning it to a reference-counted object.
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
// Validate methods.
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
// Validate annotations.
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
} // namespace GDScriptTests
139
140