Blender V5.3
asset_catalog_path_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include "BLI_set.hh"
8#include "BLI_vector.hh"
9
10#include "BKE_gtest_base.hh"
11
12#include <set>
13#include <sstream>
14
15#include "testing/testing.h"
16
18
20
22{
23 AssetCatalogPath default_constructed;
24 /* Use `.str()` to use `std:string`'s comparison operators here, not our own (which are tested
25 * later). */
26 EXPECT_EQ(default_constructed.str(), "");
27
28 /* C++ considers this construction special, it doesn't call the default constructor but does
29 * recursive, member-wise value initialization. See https://stackoverflow.com/a/4982720. */
30 AssetCatalogPath value_initialized = AssetCatalogPath();
31 EXPECT_EQ(value_initialized.str(), "");
32
33 AssetCatalogPath from_char_literal("the/path");
34
35 const std::string str_const = "the/path";
36 AssetCatalogPath from_string_constant(str_const);
37
38 std::string str_variable = "the/path";
39 AssetCatalogPath from_string_variable(str_variable);
40
41 std::string long_string = "this is a long/string/with/a/path in the middle";
42 StringRef long_string_ref(long_string);
43 StringRef middle_bit = long_string_ref.substr(10, 23);
44 AssetCatalogPath from_string_ref(middle_bit);
45 EXPECT_EQ(from_string_ref, "long/string/with/a/path");
46}
47
49{
50 const AssetCatalogPath one("1");
51 EXPECT_EQ(1, one.length());
52
53 const AssetCatalogPath empty("");
54 EXPECT_EQ(0, empty.length());
55
56 const AssetCatalogPath utf8("some/родитель");
57 EXPECT_EQ(21, utf8.length()) << "13 characters should be 21 bytes.";
58}
59
61{
63 EXPECT_EQ(StringRefNull("word"), AssetCatalogPath("word").name());
64 EXPECT_EQ(StringRefNull("Пермь"), AssetCatalogPath("дорога/в/Пермь").name());
65 EXPECT_EQ(StringRefNull("windows\\paths"),
66 AssetCatalogPath("these/are/not/windows\\paths").name());
67}
68
69TEST_F(AssetCatalogPathTest, comparison_operators)
70{
71 const AssetCatalogPath empty("");
72 const AssetCatalogPath the_path("the/path");
73 const AssetCatalogPath the_path_child("the/path/child");
74 const AssetCatalogPath unrelated_path("unrelated/path");
75 const AssetCatalogPath other_instance_same_path("the/path");
76
77 EXPECT_LT(empty, the_path);
78 EXPECT_LT(the_path, the_path_child);
79 EXPECT_LT(the_path, unrelated_path);
80
81 EXPECT_EQ(empty, empty) << "Identical empty instances should compare equal.";
82 EXPECT_EQ(empty, "") << "Comparison to empty string should be possible.";
83 EXPECT_EQ(the_path, the_path) << "Identical non-empty instances should compare equal.";
84 EXPECT_EQ(the_path, "the/path") << "Comparison to string should be possible.";
85 EXPECT_EQ(the_path, other_instance_same_path)
86 << "Different instances with equal path should compare equal.";
87
88 EXPECT_NE(the_path, the_path_child);
89 EXPECT_NE(the_path, unrelated_path);
90 EXPECT_NE(the_path, empty);
91
92 EXPECT_FALSE(empty);
93 EXPECT_TRUE(the_path);
94}
95
97{
98 AssetCatalogPath source_path("source/path");
99 EXPECT_TRUE(source_path);
100
101 AssetCatalogPath dest_path = std::move(source_path);
102 EXPECT_FALSE(source_path); /* NOLINT: bugprone-use-after-move */
103 EXPECT_TRUE(dest_path);
104}
105
107{
108 AssetCatalogPath some_parent("some/родитель");
109 AssetCatalogPath child = some_parent / "ребенок";
110
111 EXPECT_EQ(some_parent, "some/родитель")
112 << "Appending a child path should not modify the parent.";
113 EXPECT_EQ(child, "some/родитель/ребенок");
114
115 AssetCatalogPath appended_compound_path = some_parent / "ребенок/внук";
116 EXPECT_EQ(appended_compound_path, "some/родитель/ребенок/внук");
117
118 AssetCatalogPath empty("");
119 AssetCatalogPath child_of_the_void = empty / "child";
120 EXPECT_EQ(child_of_the_void, "child")
121 << "Appending to an empty path should not create an initial slash.";
122
123 AssetCatalogPath parent_of_the_void = some_parent / empty;
124 EXPECT_EQ(parent_of_the_void, "some/родитель")
125 << "Prepending to an empty path should not create a trailing slash.";
126
127 std::string subpath = "child";
128 AssetCatalogPath concatenated_with_string = some_parent / subpath;
129 EXPECT_EQ(concatenated_with_string, "some/родитель/child");
130}
131
133{
134 AssetCatalogPath path("heyyyyy");
135
136 std::set<AssetCatalogPath> path_std_set;
137 path_std_set.insert(path);
138
139 Set<AssetCatalogPath> path_blender_set;
140 path_blender_set.add(path);
141}
142
144{
145 AssetCatalogPath path("путь/в/Пермь");
146 std::stringstream sstream;
147 sstream << path;
148 EXPECT_EQ("путь/в/Пермь", sstream.str());
149}
150
152{
153 const AssetCatalogPath catpath("simple/path/child");
154 EXPECT_FALSE(catpath.is_contained_in("unrelated"));
155 EXPECT_FALSE(catpath.is_contained_in("sim"));
156 EXPECT_FALSE(catpath.is_contained_in("simple/pathx"));
157 EXPECT_FALSE(catpath.is_contained_in("simple/path/c"));
158 EXPECT_FALSE(catpath.is_contained_in("simple/path/child/grandchild"));
159 EXPECT_FALSE(catpath.is_contained_in("simple/path/"))
160 << "Non-normalized paths are not expected to work.";
161
162 EXPECT_TRUE(catpath.is_contained_in(""));
163 EXPECT_TRUE(catpath.is_contained_in("simple"));
164 EXPECT_TRUE(catpath.is_contained_in("simple/path"));
165
166 /* Test with some UTF8 non-ASCII characters. */
167 AssetCatalogPath some_parent("some/родитель");
168 AssetCatalogPath child = some_parent / "ребенок";
169
170 EXPECT_TRUE(child.is_contained_in(some_parent));
171 EXPECT_TRUE(child.is_contained_in("some"));
172
173 AssetCatalogPath appended_compound_path = some_parent / "ребенок/внук";
174 EXPECT_TRUE(appended_compound_path.is_contained_in(some_parent));
175 EXPECT_TRUE(appended_compound_path.is_contained_in(child));
176
177 /* Test "going up" directory-style. */
178 AssetCatalogPath child_with_dotdot = some_parent / "../../other/hierarchy/part";
179 EXPECT_TRUE(child_with_dotdot.is_contained_in(some_parent))
180 << "dotdot path components should have no meaning";
181}
182
184{
185 {
186 AssetCatalogPath ugly_path("/ some / родитель / ");
187 AssetCatalogPath clean_path = ugly_path.cleanup();
188 EXPECT_EQ(AssetCatalogPath("/ some / родитель / "), ugly_path)
189 << "cleanup should not modify the path instance itself";
190 EXPECT_EQ(AssetCatalogPath("some/родитель"), clean_path);
191 }
192 {
193 AssetCatalogPath double_slashed("some//родитель");
194 EXPECT_EQ(AssetCatalogPath("some/родитель"), double_slashed.cleanup());
195 }
196 {
197 AssetCatalogPath with_colons("some/key:subkey=value/path");
198 EXPECT_EQ(AssetCatalogPath("some/key-subkey=value/path"), with_colons.cleanup());
199 }
200 {
201 const AssetCatalogPath with_backslashes("windows\\for\\life");
202 EXPECT_EQ(AssetCatalogPath("windows/for/life"), with_backslashes.cleanup());
203 }
204 {
205 const AssetCatalogPath with_mixed("windows\\for/life");
206 EXPECT_EQ(AssetCatalogPath("windows/for/life"), with_mixed.cleanup());
207 }
208 {
209 const AssetCatalogPath with_punctuation("is!/this?/¿valid?");
210 EXPECT_EQ(AssetCatalogPath("is!/this?/¿valid?"), with_punctuation.cleanup());
211 }
212}
213
214TEST_F(AssetCatalogPathTest, iterate_components)
215{
216 AssetCatalogPath path("путь/в/Пермь");
218
219 path.iterate_components([&seen_components](StringRef component_name, bool is_last_component) {
220 std::pair<std::string, bool> parameter_pair = std::make_pair<std::string, bool>(
221 component_name, bool(is_last_component));
222 seen_components.append(parameter_pair);
223 });
224
225 ASSERT_EQ(3, seen_components.size());
226
227 EXPECT_EQ("путь", seen_components[0].first);
228 EXPECT_EQ("в", seen_components[1].first);
229 EXPECT_EQ("Пермь", seen_components[2].first);
230
231 EXPECT_FALSE(seen_components[0].second);
232 EXPECT_FALSE(seen_components[1].second);
233 EXPECT_TRUE(seen_components[2].second);
234}
235
237{
238 AssetCatalogPath path("some/path/to/some/catalog");
239 EXPECT_EQ(path.rebase("some/path", "new/base"), "new/base/to/some/catalog");
240 EXPECT_EQ(path.rebase("", "new/base"), "new/base/some/path/to/some/catalog");
241
242 EXPECT_EQ(path.rebase("some/path/to/some/catalog", "some/path/to/some/catalog"),
243 "some/path/to/some/catalog")
244 << "Rebasing to itself should not change the path.";
245
246 EXPECT_EQ(path.rebase("path/to", "new/base"), "")
247 << "Non-matching base path should return empty string to indicate 'NO'.";
248
249 /* Empty strings should be handled without crashing or other nasty side-effects. */
250 AssetCatalogPath empty("");
251 EXPECT_EQ(empty.rebase("path/to", "new/base"), "");
252 EXPECT_EQ(empty.rebase("", "new/base"), "new/base");
253 EXPECT_EQ(empty.rebase("", ""), "");
254}
255
257{
258 const AssetCatalogPath ascii_path("path/with/missing/parents");
259 EXPECT_EQ(ascii_path.parent(), "path/with/missing");
260
261 const AssetCatalogPath path("путь/в/Пермь/долог/и/далек");
262 EXPECT_EQ(path.parent(), "путь/в/Пермь/долог/и");
263 EXPECT_EQ(path.parent().parent(), "путь/в/Пермь/долог");
264 EXPECT_EQ(path.parent().parent().parent(), "путь/в/Пермь");
265
266 const AssetCatalogPath one_level("one");
267 EXPECT_EQ(one_level.parent(), "");
268
269 const AssetCatalogPath empty("");
270 EXPECT_EQ(empty.parent(), "");
271}
272
273} // namespace blender::asset_system::tests
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr StringRefNull()
int64_t size() const
void append(const T &value)
AssetCatalogPath rebase(const AssetCatalogPath &from_path, const AssetCatalogPath &to_path) const
bool is_contained_in(const AssetCatalogPath &other_path) const
void iterate_components(ComponentIteratorFn callback) const
constexpr StringRef substr(int64_t start, int64_t size) const
float length(VecOp< float, D >) RET
#define EXPECT_FALSE(result)
#define EXPECT_LT(result, expect)
#define EXPECT_NE(result, expect)
#define EXPECT_TRUE(result)
TEST_F(AssetCatalogPathTest, construction)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)