Welcome to mirror list, hosted at ThFree Co, Russian Federation.

asset_catalog_path_test.cc « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f248863ce77f0a6aad145ee40c8e9f06340a97d7 (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2020 Blender Foundation
 * All rights reserved.
 */

#include "BKE_asset_catalog_path.hh"

#include "BLI_set.hh"
#include "BLI_vector.hh"

#include <set>
#include <sstream>

#include "testing/testing.h"

namespace blender::bke::tests {

TEST(AssetCatalogPathTest, construction)
{
  AssetCatalogPath default_constructed;
  /* Use `.str()` to use `std:string`'s comparison operators here, not our own (which are tested
   * later). */
  EXPECT_EQ(default_constructed.str(), "");

  /* C++ considers this construction special, it doesn't call the default constructor but does
   * recursive, member-wise value initialization. See https://stackoverflow.com/a/4982720. */
  AssetCatalogPath value_initialized = AssetCatalogPath();
  EXPECT_EQ(value_initialized.str(), "");

  AssetCatalogPath from_char_literal("the/path");

  const std::string str_const = "the/path";
  AssetCatalogPath from_string_constant(str_const);

  std::string str_variable = "the/path";
  AssetCatalogPath from_string_variable(str_variable);

  std::string long_string = "this is a long/string/with/a/path in the middle";
  StringRef long_string_ref(long_string);
  StringRef middle_bit = long_string_ref.substr(10, 23);
  AssetCatalogPath from_string_ref(middle_bit);
  EXPECT_EQ(from_string_ref, "long/string/with/a/path");
}

TEST(AssetCatalogPathTest, length)
{
  const AssetCatalogPath one("1");
  EXPECT_EQ(1, one.length());

  const AssetCatalogPath empty("");
  EXPECT_EQ(0, empty.length());

  const AssetCatalogPath utf8("some/родитель");
  EXPECT_EQ(21, utf8.length()) << "13 characters should be 21 bytes.";
}

TEST(AssetCatalogPathTest, name)
{
  EXPECT_EQ(StringRefNull(""), AssetCatalogPath("").name());
  EXPECT_EQ(StringRefNull("word"), AssetCatalogPath("word").name());
  EXPECT_EQ(StringRefNull("Пермь"), AssetCatalogPath("дорога/в/Пермь").name());
  EXPECT_EQ(StringRefNull("windows\\paths"),
            AssetCatalogPath("these/are/not/windows\\paths").name());
}

TEST(AssetCatalogPathTest, comparison_operators)
{
  const AssetCatalogPath empty("");
  const AssetCatalogPath the_path("the/path");
  const AssetCatalogPath the_path_child("the/path/child");
  const AssetCatalogPath unrelated_path("unrelated/path");
  const AssetCatalogPath other_instance_same_path("the/path");

  EXPECT_LT(empty, the_path);
  EXPECT_LT(the_path, the_path_child);
  EXPECT_LT(the_path, unrelated_path);

  EXPECT_EQ(empty, empty) << "Identical empty instances should compare equal.";
  EXPECT_EQ(empty, "") << "Comparison to empty string should be possible.";
  EXPECT_EQ(the_path, the_path) << "Identical non-empty instances should compare equal.";
  EXPECT_EQ(the_path, "the/path") << "Comparison to string should be possible.";
  EXPECT_EQ(the_path, other_instance_same_path)
      << "Different instances with equal path should compare equal.";

  EXPECT_NE(the_path, the_path_child);
  EXPECT_NE(the_path, unrelated_path);
  EXPECT_NE(the_path, empty);

  EXPECT_FALSE(empty);
  EXPECT_TRUE(the_path);
}

TEST(AssetCatalogPathTest, move_semantics)
{
  AssetCatalogPath source_path("source/path");
  EXPECT_TRUE(source_path);

  AssetCatalogPath dest_path = std::move(source_path);
  EXPECT_FALSE(source_path); /* NOLINT: bugprone-use-after-move */
  EXPECT_TRUE(dest_path);
}

TEST(AssetCatalogPathTest, concatenation)
{
  AssetCatalogPath some_parent("some/родитель");
  AssetCatalogPath child = some_parent / "ребенок";

  EXPECT_EQ(some_parent, "some/родитель")
      << "Appending a child path should not modify the parent.";
  EXPECT_EQ(child, "some/родитель/ребенок");

  AssetCatalogPath appended_compound_path = some_parent / "ребенок/внук";
  EXPECT_EQ(appended_compound_path, "some/родитель/ребенок/внук");

  AssetCatalogPath empty("");
  AssetCatalogPath child_of_the_void = empty / "child";
  EXPECT_EQ(child_of_the_void, "child")
      << "Appending to an empty path should not create an initial slash.";

  AssetCatalogPath parent_of_the_void = some_parent / empty;
  EXPECT_EQ(parent_of_the_void, "some/родитель")
      << "Prepending to an empty path should not create a trailing slash.";

  std::string subpath = "child";
  AssetCatalogPath concatenated_with_string = some_parent / subpath;
  EXPECT_EQ(concatenated_with_string, "some/родитель/child");
}

TEST(AssetCatalogPathTest, hashable)
{
  AssetCatalogPath path("heyyyyy");

  std::set<AssetCatalogPath> path_std_set;
  path_std_set.insert(path);

  blender::Set<AssetCatalogPath> path_blender_set;
  path_blender_set.add(path);
}

TEST(AssetCatalogPathTest, stream_operator)
{
  AssetCatalogPath path("путь/в/Пермь");
  std::stringstream sstream;
  sstream << path;
  EXPECT_EQ("путь/в/Пермь", sstream.str());
}

TEST(AssetCatalogPathTest, is_contained_in)
{
  const AssetCatalogPath catpath("simple/path/child");
  EXPECT_FALSE(catpath.is_contained_in("unrelated"));
  EXPECT_FALSE(catpath.is_contained_in("sim"));
  EXPECT_FALSE(catpath.is_contained_in("simple/pathx"));
  EXPECT_FALSE(catpath.is_contained_in("simple/path/c"));
  EXPECT_FALSE(catpath.is_contained_in("simple/path/child/grandchild"));
  EXPECT_FALSE(catpath.is_contained_in("simple/path/"))
      << "Non-normalized paths are not expected to work.";

  EXPECT_TRUE(catpath.is_contained_in(""));
  EXPECT_TRUE(catpath.is_contained_in("simple"));
  EXPECT_TRUE(catpath.is_contained_in("simple/path"));

  /* Test with some UTF8 non-ASCII characters. */
  AssetCatalogPath some_parent("some/родитель");
  AssetCatalogPath child = some_parent / "ребенок";

  EXPECT_TRUE(child.is_contained_in(some_parent));
  EXPECT_TRUE(child.is_contained_in("some"));

  AssetCatalogPath appended_compound_path = some_parent / "ребенок/внук";
  EXPECT_TRUE(appended_compound_path.is_contained_in(some_parent));
  EXPECT_TRUE(appended_compound_path.is_contained_in(child));

  /* Test "going up" directory-style. */
  AssetCatalogPath child_with_dotdot = some_parent / "../../other/hierarchy/part";
  EXPECT_TRUE(child_with_dotdot.is_contained_in(some_parent))
      << "dotdot path components should have no meaning";
}

TEST(AssetCatalogPathTest, cleanup)
{
  {
    AssetCatalogPath ugly_path("/  some /   родитель  / ");
    AssetCatalogPath clean_path = ugly_path.cleanup();
    EXPECT_EQ(AssetCatalogPath("/  some /   родитель  / "), ugly_path)
        << "cleanup should not modify the path instance itself";
    EXPECT_EQ(AssetCatalogPath("some/родитель"), clean_path);
  }
  {
    AssetCatalogPath double_slashed("some//родитель");
    EXPECT_EQ(AssetCatalogPath("some/родитель"), double_slashed.cleanup());
  }
  {
    AssetCatalogPath with_colons("some/key:subkey=value/path");
    EXPECT_EQ(AssetCatalogPath("some/key-subkey=value/path"), with_colons.cleanup());
  }
  {
    const AssetCatalogPath with_backslashes("windows\\for\\life");
    EXPECT_EQ(AssetCatalogPath("windows/for/life"), with_backslashes.cleanup());
  }
  {
    const AssetCatalogPath with_mixed("windows\\for/life");
    EXPECT_EQ(AssetCatalogPath("windows/for/life"), with_mixed.cleanup());
  }
  {
    const AssetCatalogPath with_punctuation("is!/this?/¿valid?");
    EXPECT_EQ(AssetCatalogPath("is!/this?/¿valid?"), with_punctuation.cleanup());
  }
}

TEST(AssetCatalogPathTest, iterate_components)
{
  AssetCatalogPath path("путь/в/Пермь");
  Vector<std::pair<std::string, bool>> seen_components;

  path.iterate_components([&seen_components](StringRef component_name, bool is_last_component) {
    std::pair<std::string, bool> parameter_pair = std::make_pair<std::string, bool>(
        component_name, bool(is_last_component));
    seen_components.append(parameter_pair);
  });

  ASSERT_EQ(3, seen_components.size());

  EXPECT_EQ("путь", seen_components[0].first);
  EXPECT_EQ("в", seen_components[1].first);
  EXPECT_EQ("Пермь", seen_components[2].first);

  EXPECT_FALSE(seen_components[0].second);
  EXPECT_FALSE(seen_components[1].second);
  EXPECT_TRUE(seen_components[2].second);
}

TEST(AssetCatalogPathTest, rebase)
{
  AssetCatalogPath path("some/path/to/some/catalog");
  EXPECT_EQ(path.rebase("some/path", "new/base"), "new/base/to/some/catalog");
  EXPECT_EQ(path.rebase("", "new/base"), "new/base/some/path/to/some/catalog");

  EXPECT_EQ(path.rebase("some/path/to/some/catalog", "some/path/to/some/catalog"),
            "some/path/to/some/catalog")
      << "Rebasing to itself should not change the path.";

  EXPECT_EQ(path.rebase("path/to", "new/base"), "")
      << "Non-matching base path should return empty string to indicate 'NO'.";

  /* Empty strings should be handled without crashing or other nasty side-effects. */
  AssetCatalogPath empty("");
  EXPECT_EQ(empty.rebase("path/to", "new/base"), "");
  EXPECT_EQ(empty.rebase("", "new/base"), "new/base");
  EXPECT_EQ(empty.rebase("", ""), "");
}

TEST(AssetCatalogPathTest, parent)
{
  const AssetCatalogPath ascii_path("path/with/missing/parents");
  EXPECT_EQ(ascii_path.parent(), "path/with/missing");

  const AssetCatalogPath path("путь/в/Пермь/долог/и/далек");
  EXPECT_EQ(path.parent(), "путь/в/Пермь/долог/и");
  EXPECT_EQ(path.parent().parent(), "путь/в/Пермь/долог");
  EXPECT_EQ(path.parent().parent().parent(), "путь/в/Пермь");

  const AssetCatalogPath one_level("one");
  EXPECT_EQ(one_level.parent(), "");

  const AssetCatalogPath empty("");
  EXPECT_EQ(empty.parent(), "");
}

}  // namespace blender::bke::tests