Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/string/test_node_path.h
10278 views
1
/**************************************************************************/
2
/* test_node_path.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 "core/string/node_path.h"
34
35
#include "tests/test_macros.h"
36
37
namespace TestNodePath {
38
39
TEST_CASE("[NodePath] Relative path") {
40
const NodePath node_path_relative = NodePath("Path2D/PathFollow2D/Sprite2D:position:x");
41
42
CHECK_MESSAGE(
43
node_path_relative.get_as_property_path() == NodePath(":Path2D/PathFollow2D/Sprite2D:position:x"),
44
"The returned property path should match the expected value.");
45
CHECK_MESSAGE(
46
node_path_relative.get_concatenated_subnames() == "position:x",
47
"The returned concatenated subnames should match the expected value.");
48
49
CHECK_MESSAGE(
50
node_path_relative.get_name(0) == "Path2D",
51
"The returned name at index 0 should match the expected value.");
52
CHECK_MESSAGE(
53
node_path_relative.get_name(1) == "PathFollow2D",
54
"The returned name at index 1 should match the expected value.");
55
CHECK_MESSAGE(
56
node_path_relative.get_name(2) == "Sprite2D",
57
"The returned name at index 2 should match the expected value.");
58
ERR_PRINT_OFF;
59
CHECK_MESSAGE(
60
node_path_relative.get_name(3) == "",
61
"The returned name at invalid index 3 should match the expected value.");
62
CHECK_MESSAGE(
63
node_path_relative.get_name(-1) == "",
64
"The returned name at invalid index -1 should match the expected value.");
65
ERR_PRINT_ON;
66
67
CHECK_MESSAGE(
68
node_path_relative.get_name_count() == 3,
69
"The returned number of names should match the expected value.");
70
71
CHECK_MESSAGE(
72
node_path_relative.get_subname(0) == "position",
73
"The returned subname at index 0 should match the expected value.");
74
CHECK_MESSAGE(
75
node_path_relative.get_subname(1) == "x",
76
"The returned subname at index 1 should match the expected value.");
77
ERR_PRINT_OFF;
78
CHECK_MESSAGE(
79
node_path_relative.get_subname(2) == "",
80
"The returned subname at invalid index 2 should match the expected value.");
81
CHECK_MESSAGE(
82
node_path_relative.get_subname(-1) == "",
83
"The returned subname at invalid index -1 should match the expected value.");
84
ERR_PRINT_ON;
85
86
CHECK_MESSAGE(
87
node_path_relative.get_subname_count() == 2,
88
"The returned number of subnames should match the expected value.");
89
90
CHECK_MESSAGE(
91
!node_path_relative.is_absolute(),
92
"The node path should be considered relative.");
93
94
CHECK_MESSAGE(
95
!node_path_relative.is_empty(),
96
"The node path shouldn't be considered empty.");
97
}
98
99
TEST_CASE("[NodePath] Absolute path") {
100
const NodePath node_path_absolute = NodePath("/root/Sprite2D");
101
102
CHECK_MESSAGE(
103
node_path_absolute.get_as_property_path() == NodePath(":root/Sprite2D"),
104
"The returned property path should match the expected value.");
105
CHECK_MESSAGE(
106
node_path_absolute.get_concatenated_subnames() == "",
107
"The returned concatenated subnames should match the expected value.");
108
109
CHECK_MESSAGE(
110
node_path_absolute.get_name(0) == "root",
111
"The returned name at index 0 should match the expected value.");
112
CHECK_MESSAGE(
113
node_path_absolute.get_name(1) == "Sprite2D",
114
"The returned name at index 1 should match the expected value.");
115
ERR_PRINT_OFF;
116
CHECK_MESSAGE(
117
node_path_absolute.get_name(2) == "",
118
"The returned name at invalid index 2 should match the expected value.");
119
CHECK_MESSAGE(
120
node_path_absolute.get_name(-1) == "",
121
"The returned name at invalid index -1 should match the expected value.");
122
ERR_PRINT_ON;
123
124
CHECK_MESSAGE(
125
node_path_absolute.get_name_count() == 2,
126
"The returned number of names should match the expected value.");
127
128
CHECK_MESSAGE(
129
node_path_absolute.get_subname_count() == 0,
130
"The returned number of subnames should match the expected value.");
131
132
CHECK_MESSAGE(
133
node_path_absolute.is_absolute(),
134
"The node path should be considered absolute.");
135
136
CHECK_MESSAGE(
137
!node_path_absolute.is_empty(),
138
"The node path shouldn't be considered empty.");
139
}
140
141
TEST_CASE("[NodePath] Empty path") {
142
const NodePath node_path_empty = NodePath();
143
144
CHECK_MESSAGE(
145
node_path_empty.get_as_property_path() == NodePath(),
146
"The returned property path should match the expected value.");
147
ERR_PRINT_OFF;
148
CHECK_MESSAGE(
149
node_path_empty.get_concatenated_subnames() == "",
150
"The returned concatenated subnames should match the expected value.");
151
ERR_PRINT_ON;
152
153
CHECK_MESSAGE(
154
node_path_empty.get_name_count() == 0,
155
"The returned number of names should match the expected value.");
156
157
CHECK_MESSAGE(
158
node_path_empty.get_subname_count() == 0,
159
"The returned number of subnames should match the expected value.");
160
161
CHECK_MESSAGE(
162
!node_path_empty.is_absolute(),
163
"The node path shouldn't be considered absolute.");
164
165
CHECK_MESSAGE(
166
node_path_empty.is_empty(),
167
"The node path should be considered empty.");
168
}
169
170
TEST_CASE("[NodePath] Slice") {
171
const NodePath node_path_relative = NodePath("Parent/Child:prop:subprop");
172
const NodePath node_path_absolute = NodePath("/root/Parent/Child:prop");
173
CHECK_MESSAGE(
174
node_path_relative.slice(0, 2) == NodePath("Parent/Child"),
175
"The slice lower bound should be inclusive and the slice upper bound should be exclusive.");
176
CHECK_MESSAGE(
177
node_path_relative.slice(3) == NodePath(":subprop"),
178
"Slicing on the last index (length - 1) should return the last entry.");
179
CHECK_MESSAGE(
180
node_path_relative.slice(1) == NodePath("Child:prop:subprop"),
181
"Slicing without upper bound should return remaining entries after index.");
182
CHECK_MESSAGE(
183
node_path_relative.slice(1, 3) == NodePath("Child:prop"),
184
"Slicing should include names and subnames.");
185
CHECK_MESSAGE(
186
node_path_relative.slice(-1) == NodePath(":subprop"),
187
"Slicing on -1 should return the last entry.");
188
CHECK_MESSAGE(
189
node_path_relative.slice(0, -1) == NodePath("Parent/Child:prop"),
190
"Slicing up to -1 should include the second-to-last entry.");
191
CHECK_MESSAGE(
192
node_path_relative.slice(-2, -1) == NodePath(":prop"),
193
"Slicing from negative to negative should treat lower bound as inclusive and upper bound as exclusive.");
194
CHECK_MESSAGE(
195
node_path_relative.slice(0, 10) == NodePath("Parent/Child:prop:subprop"),
196
"Slicing past the length of the path should work like slicing up to the last entry.");
197
CHECK_MESSAGE(
198
node_path_relative.slice(-10, 2) == NodePath("Parent/Child"),
199
"Slicing negatively past the length of the path should work like slicing from the first entry.");
200
CHECK_MESSAGE(
201
node_path_relative.slice(1, 1) == NodePath(""),
202
"Slicing with a lower bound equal to upper bound should return empty path.");
203
204
CHECK_MESSAGE(
205
node_path_absolute.slice(0, 2) == NodePath("/root/Parent"),
206
"Slice from beginning of an absolute path should be an absolute path.");
207
CHECK_MESSAGE(
208
node_path_absolute.slice(1, 4) == NodePath("Parent/Child:prop"),
209
"Slice of an absolute path that does not start at the beginning should be a relative path.");
210
CHECK_MESSAGE(
211
node_path_absolute.slice(3, 4) == NodePath(":prop"),
212
"Slice of an absolute path that does not start at the beginning should be a relative path.");
213
214
CHECK_MESSAGE(
215
NodePath("").slice(0, 1) == NodePath(""),
216
"Slice of an empty path should be an empty path.");
217
CHECK_MESSAGE(
218
NodePath("").slice(-1, 2) == NodePath(""),
219
"Slice of an empty path should be an empty path.");
220
CHECK_MESSAGE(
221
NodePath("/").slice(-1, 2) == NodePath("/"),
222
"Slice of an empty absolute path should be an empty absolute path.");
223
}
224
225
} // namespace TestNodePath
226
227