Blender V5.3
path_templates_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <fmt/format.h>
6
7#include "BKE_gtest_base.hh"
9
10#include "testing/testing.h"
11
12namespace blender::bke::tests {
13
14using namespace blender::bke::path_templates;
15
16static std::string error_to_string(const Error &error)
17{
18 const char *type;
19 switch (error.type) {
21 type = "UNESCAPED_CURLY_BRACE";
22 break;
24 type = "VARIABLE_SYNTAX";
25 break;
27 type = "FORMAT_SPECIFIER";
28 break;
30 type = "UNKNOWN_VARIABLE";
31 break;
32 }
33
34 std::string s;
35 fmt::format_to(std::back_inserter(s),
36 "({}, ({}, {}))",
37 type,
38 error.byte_range.start(),
39 error.byte_range.size());
40
41 return s;
42}
43
44static std::string errors_to_string(Span<Error> errors)
45{
46 std::string s;
47
48 fmt::format_to(std::back_inserter(s), "[");
49 bool is_first = true;
50 for (const Error &error : errors) {
51 if (is_first) {
52 is_first = false;
53 }
54 else {
55 fmt::format_to(std::back_inserter(s), ", ");
56 }
57 fmt::format_to(std::back_inserter(s), "{}", error_to_string(error));
58 }
59 fmt::format_to(std::back_inserter(s), "]");
60
61 return s;
62}
63
65
67{
68 VariableMap map;
69
70 /* With in empty variable map, these should all return false / fail. */
71 EXPECT_FALSE(map.contains("hello"));
72 EXPECT_FALSE(map.remove("hello"));
73 EXPECT_EQ(std::nullopt, map.get_string("hello"));
74 EXPECT_EQ(std::nullopt, map.get_filepath("hello"));
75 EXPECT_EQ(std::nullopt, map.get_integer("hello"));
76 EXPECT_EQ(std::nullopt, map.get_float("hello"));
77
78 /* Populate the map. */
79 EXPECT_TRUE(map.add_string("hello", "What a wonderful world."));
80 EXPECT_TRUE(map.add_filepath("where", "/my/path"));
81 EXPECT_TRUE(map.add_integer("bye", 42));
82 EXPECT_TRUE(map.add_float("what", 3.14159));
83
84 /* Attempting to add variables with those names again should fail, since they
85 * already exist now. */
86 EXPECT_FALSE(map.add_string("hello", "Sup."));
87 EXPECT_FALSE(map.add_string("where", "Sup."));
88 EXPECT_FALSE(map.add_string("bye", "Sup."));
89 EXPECT_FALSE(map.add_string("what", "Sup."));
90 EXPECT_FALSE(map.add_filepath("hello", "/place"));
91 EXPECT_FALSE(map.add_filepath("where", "/place"));
92 EXPECT_FALSE(map.add_filepath("bye", "/place"));
93 EXPECT_FALSE(map.add_filepath("what", "/place"));
94 EXPECT_FALSE(map.add_integer("hello", 2));
95 EXPECT_FALSE(map.add_integer("where", 2));
96 EXPECT_FALSE(map.add_integer("bye", 2));
97 EXPECT_FALSE(map.add_integer("what", 2));
98 EXPECT_FALSE(map.add_float("hello", 2.71828));
99 EXPECT_FALSE(map.add_float("where", 2.71828));
100 EXPECT_FALSE(map.add_float("bye", 2.71828));
101 EXPECT_FALSE(map.add_float("what", 2.71828));
102
103 /* Confirm that the right variables exist. */
104 EXPECT_TRUE(map.contains("hello"));
105 EXPECT_TRUE(map.contains("where"));
106 EXPECT_TRUE(map.contains("bye"));
107 EXPECT_TRUE(map.contains("what"));
108 EXPECT_FALSE(map.contains("not here"));
109
110 /* Fetch the variables we added. */
111 EXPECT_EQ("What a wonderful world.", map.get_string("hello"));
112 EXPECT_EQ("/my/path", map.get_filepath("where"));
113 EXPECT_EQ(42, map.get_integer("bye"));
114 EXPECT_EQ(3.14159, map.get_float("what"));
115
116 /* The same variables shouldn't exist for the other types, despite our attempt
117 * to add them earlier. */
118 EXPECT_EQ(std::nullopt, map.get_filepath("hello"));
119 EXPECT_EQ(std::nullopt, map.get_integer("hello"));
120 EXPECT_EQ(std::nullopt, map.get_float("hello"));
121 EXPECT_EQ(std::nullopt, map.get_string("where"));
122 EXPECT_EQ(std::nullopt, map.get_integer("where"));
123 EXPECT_EQ(std::nullopt, map.get_float("where"));
124 EXPECT_EQ(std::nullopt, map.get_string("bye"));
125 EXPECT_EQ(std::nullopt, map.get_filepath("bye"));
126 EXPECT_EQ(std::nullopt, map.get_float("bye"));
127 EXPECT_EQ(std::nullopt, map.get_string("what"));
128 EXPECT_EQ(std::nullopt, map.get_filepath("what"));
129 EXPECT_EQ(std::nullopt, map.get_integer("what"));
130
131 /* Remove the variables. */
132 EXPECT_TRUE(map.remove("hello"));
133 EXPECT_TRUE(map.remove("where"));
134 EXPECT_TRUE(map.remove("bye"));
135 EXPECT_TRUE(map.remove("what"));
136
137 /* The variables shouldn't exist anymore. */
138 EXPECT_FALSE(map.contains("hello"));
139 EXPECT_FALSE(map.contains("where"));
140 EXPECT_FALSE(map.contains("bye"));
141 EXPECT_FALSE(map.contains("what"));
142 EXPECT_EQ(std::nullopt, map.get_string("hello"));
143 EXPECT_EQ(std::nullopt, map.get_filepath("where"));
144 EXPECT_EQ(std::nullopt, map.get_integer("bye"));
145 EXPECT_EQ(std::nullopt, map.get_float("what"));
146 EXPECT_FALSE(map.remove("hello"));
147 EXPECT_FALSE(map.remove("where"));
148 EXPECT_FALSE(map.remove("bye"));
149 EXPECT_FALSE(map.remove("what"));
150}
151
152TEST_F(PathTemplatesTest, VariableMap_add_filename_only)
153{
154 VariableMap map;
155
156 EXPECT_TRUE(map.add_filename_only("a", "/home/bob/project_joe/scene_3.blend", "fallback"));
157 EXPECT_EQ("scene_3", map.get_filepath("a"));
158
159 EXPECT_TRUE(map.add_filename_only("b", "/home/bob/project_joe/scene_3", "fallback"));
160 EXPECT_EQ("scene_3", map.get_filepath("b"));
161
162 EXPECT_TRUE(map.add_filename_only("c", "/home/bob/project_joe/scene.03.blend", "fallback"));
163 EXPECT_EQ("scene.03", map.get_filepath("c"));
164
165 EXPECT_TRUE(map.add_filename_only("d", "/home/bob/project_joe/.scene_3.blend", "fallback"));
166 EXPECT_EQ(".scene_3", map.get_filepath("d"));
167
168 EXPECT_TRUE(map.add_filename_only("e", "/home/bob/project_joe/.scene_3", "fallback"));
169 EXPECT_EQ(".scene_3", map.get_filepath("e"));
170
171 EXPECT_TRUE(map.add_filename_only("f", "scene_3.blend", "fallback"));
172 EXPECT_EQ("scene_3", map.get_filepath("f"));
173
174 EXPECT_TRUE(map.add_filename_only("g", "scene_3", "fallback"));
175 EXPECT_EQ("scene_3", map.get_filepath("g"));
176
177 /* No filename in path (ending slash means it's a directory). */
178 EXPECT_TRUE(map.add_filename_only("h", "/home/bob/project_joe/", "fallback"));
179 EXPECT_EQ("fallback", map.get_filepath("h"));
180
181 /* Empty path. */
182 EXPECT_TRUE(map.add_filename_only("i", "", "fallback"));
183 EXPECT_EQ("fallback", map.get_filepath("i"));
184
185 /* Attempt to add already-added variable. */
186 EXPECT_FALSE(map.add_filename_only("i", "", "fallback"));
187}
188
189TEST_F(PathTemplatesTest, VariableMap_add_path_up_to_file)
190{
191 VariableMap map;
192
193 EXPECT_TRUE(map.add_path_up_to_file("a", "/home/bob/project_joe/scene_3.blend", "fallback"));
194 EXPECT_EQ("/home/bob/project_joe/", map.get_filepath("a"));
195
196 EXPECT_TRUE(map.add_path_up_to_file("b", "project_joe/scene_3.blend", "fallback"));
197 EXPECT_EQ("project_joe/", map.get_filepath("b"));
198
199 EXPECT_TRUE(map.add_path_up_to_file("c", "/scene_3.blend", "fallback"));
200 EXPECT_EQ("/", map.get_filepath("c"));
201
202 /* No filename in path (ending slash means it's a directory). */
203 EXPECT_TRUE(map.add_path_up_to_file("e", "/home/bob/project_joe/", "fallback"));
204 EXPECT_EQ("/home/bob/project_joe/", map.get_filepath("e"));
205
206 EXPECT_TRUE(map.add_path_up_to_file("f", "/", "fallback"));
207 EXPECT_EQ("/", map.get_filepath("f"));
208
209 /* No leading path. */
210 EXPECT_TRUE(map.add_path_up_to_file("d", "scene_3.blend", "fallback"));
211 EXPECT_EQ("fallback", map.get_filepath("d"));
212
213 /* Empty path. */
214 EXPECT_TRUE(map.add_path_up_to_file("g", "", "fallback"));
215 EXPECT_EQ("fallback", map.get_filepath("g"));
216
217 /* Attempt to add already-added variable. */
218 EXPECT_FALSE(map.add_filename_only("g", "", "fallback"));
219}
220
226
227TEST_F(PathTemplatesTest, validate_and_apply_template)
228{
229 VariableMap variables;
230 {
231 variables.add_string("hi", "hello");
232 variables.add_string("bye", "goodbye");
233 variables.add_string("empty", "");
234 variables.add_string("sanitize", "./\\?*:|\"<>");
235 variables.add_string("long", "This string is exactly 32 bytes_");
236 variables.add_filepath("path", "C:\\and/or/../nor/");
237 variables.add_integer("the_answer", 42);
238 variables.add_integer("prime", 7);
239 variables.add_integer("i_negative", -7);
240 variables.add_float("pi", 3.14159265358979323846);
241 variables.add_float("e", 2.71828182845904523536);
242 variables.add_float("ntsc", 30.0 / 1.001);
243 variables.add_float("two", 2.0);
244 variables.add_float("f_negative", -3.14159265358979323846);
245 variables.add_float("huge", 200000000000000000000000000000000.0);
246 variables.add_float("tiny", 0.000000000000000000000000000000002);
247 }
248
249 const Vector<PathTemplateTestCase> test_cases = {
250 /* Simple case, testing all variables. */
251 {
252 "{hi}_{bye}_{empty}_{sanitize}_{path}"
253 "_{the_answer}_{prime}_{i_negative}_{pi}_{e}_{ntsc}_{two}"
254 "_{f_negative}_{huge}_{tiny}",
255 "hello_goodbye__.__________C:\\and/or/../nor/"
256 "_42_7_-7_3.141592653589793_2.718281828459045_29.970029970029973_2.0"
257 "_-3.141592653589793_2e+32_2e-33",
258 {},
259 },
260
261 /* Integer formatting. */
262 {
263 "{the_answer:#}_{the_answer:##}_{the_answer:####}_{i_negative:####}",
264 "42_42_0042_-007",
265 {},
266 },
267
268 /* Integer formatting as float. */
269 {
270 "{the_answer:.###}_{the_answer:#.##}_{the_answer:###.##}_{i_negative:###.####}",
271 "42.000_42.00_042.00_-07.0000",
272 {},
273 },
274
275 /* Float formatting: specify fractional digits only. */
276 {
277 "{pi:.####}_{e:.###}_{ntsc:.########}_{two:.##}_{f_negative:.##}_{huge:.##}_{tiny:.##}",
278 "3.1416_2.718_29.97002997_2.00_-3.14_200000000000000010732324408786944.00_0.00",
279 {},
280 },
281
282 /* Float formatting: specify both integer and fractional digits. */
283 {
284 "{pi:##.####}_{e:####.###}_{ntsc:#.########}_{two:###.##}_{f_negative:###.##}_{huge:###."
285 "##}_{tiny:###.##}",
286 "03.1416_0002.718_29.97002997_002.00_-03.14_200000000000000010732324408786944.00_000.00",
287 {},
288 },
289
290 /* Float formatting: format as integer. */
291 {
292 "{pi:##}_{e:####}_{ntsc:#}_{two:###}",
293 "03_0003_30_002",
294 {},
295 },
296
297 /* Escaping. "{{" and "}}" are the escape codes for literal "{" and "}". */
298 {
299 "{hi}_{{hi}}_{{{bye}}}_{bye}",
300 "hello_{hi}_{goodbye}_goodbye",
301 {},
302 },
303
304 /* Error: string variables do not support format specifiers. */
305 {
306 "{hi:##}_{bye:#}",
307 "{hi:##}_{bye:#}",
308 {
311 },
312 },
313
314 /* Error: float formatting: specifying integer digits only (but still wanting
315 * it printed as a float) is currently not supported. */
316 {
317 "{pi:##.}_{e:####.}_{ntsc:#.}_{two:###.}_{f_negative:###.}_{huge:###.}_{tiny:###.}",
318 "{pi:##.}_{e:####.}_{ntsc:#.}_{two:###.}_{f_negative:###.}_{huge:###.}_{tiny:###.}",
319 {
327 },
328 },
329
330 /* Error: missing variable. */
331 {
332 "{hi}_{missing}_{bye}",
333 "{hi}_{missing}_{bye}",
334 {
336 },
337 },
338
339 /* Error: incomplete variable expression. */
340 {
341 "foo{hi",
342 "foo{hi",
343 {
345 },
346 },
347
348 /* Error: incomplete variable expression after complete one. */
349 {
350 "foo{bye}{hi",
351 "foo{bye}{hi",
352 {
354 },
355 },
356
357 /* Error: invalid format specifiers. */
358 {
359 "{prime:}_{prime:.}_{prime:#.#.#}_{prime:sup}_{prime::sup}_{prime}",
360 "{prime:}_{prime:.}_{prime:#.#.#}_{prime:sup}_{prime::sup}_{prime}",
361 {
367 },
368 },
369
370 /* Error: unclosed variable. */
371 {
372 "{hi_{hi}_{bye}",
373 "{hi_{hi}_{bye}",
374 {
376 },
377 },
378
379 /* Error: escaped braces inside variable. */
380 {
381 "{hi_{{hi}}_{bye}",
382 "{hi_{{hi}}_{bye}",
383 {
385 },
386 },
387
388 /* Test what happens when the path would expand to a string that's longer than
389 * `FILE_MAX`.
390 *
391 * We don't care so much about any kind of "correctness" here, we just want to
392 * ensure that it still results in a valid null-terminated string that fits in
393 * `FILE_MAX` bytes.
394 *
395 * NOTE: this test will have to be updated if `FILE_MAX` is ever changed. */
396 {
397 "___{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}"
398 "{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{"
399 "long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{"
400 "long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{"
401 "long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{"
402 "long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{"
403 "long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{"
404 "long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{"
405 "long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{"
406 "long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{"
407 "long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{"
408 "long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{long}{"
409 "long}{long}",
410
411 "___This string is exactly 32 bytes_This string is exactly 32 bytes_This string is "
412 "exactly 32 bytes_This string is exactly 32 bytes_This string is exactly 32 bytes_This "
413 "string is exactly 32 bytes_This string is exactly 32 bytes_This string is exactly 32 "
414 "bytes_This string is exactly 32 bytes_This string is exactly 32 bytes_This string is "
415 "exactly 32 bytes_This string is exactly 32 bytes_This string is exactly 32 bytes_This "
416 "string is exactly 32 bytes_This string is exactly 32 bytes_This string is exactly 32 "
417 "bytes_This string is exactly 32 bytes_This string is exactly 32 bytes_This string is "
418 "exactly 32 bytes_This string is exactly 32 bytes_This string is exactly 32 bytes_This "
419 "string is exactly 32 bytes_This string is exactly 32 bytes_This string is exactly 32 "
420 "bytes_This string is exactly 32 bytes_This string is exactly 32 bytes_This string is "
421 "exactly 32 bytes_This string is exactly 32 bytes_This string is exactly 32 bytes_This "
422 "string is exactly 32 bytes_This string is exactly 32 bytes_This string is exactly 32 "
423 "by",
424
425 {},
426 },
427 };
428
429 for (const PathTemplateTestCase &test_case : test_cases) {
430 char path[FILE_MAX];
431 STRNCPY(path, test_case.path_in);
432
433 /* Do validation first, which shouldn't modify the path. */
434 const Vector<Error> validation_errors = BKE_path_validate_template(path, variables);
435 EXPECT_EQ(validation_errors, test_case.expected_errors)
436 << " Template errors: " << errors_to_string(validation_errors) << std::endl
437 << " Expected errors: " << errors_to_string(test_case.expected_errors) << std::endl
438 << " Note: test_case.path_in = " << test_case.path_in << std::endl;
439 EXPECT_STREQ(path, test_case.path_in)
440 << " Note: test_case.path_in = " << test_case.path_in << std::endl;
441
442 /* Then do application, which should modify the path. */
443 const Vector<Error> application_errors = BKE_path_apply_template(path, FILE_MAX, variables);
444 EXPECT_EQ(application_errors, test_case.expected_errors)
445 << " Template errors: " << errors_to_string(application_errors) << std::endl
446 << " Expected errors: " << errors_to_string(test_case.expected_errors) << std::endl
447 << " Note: test_case.path_in = " << test_case.path_in << std::endl;
448 EXPECT_STREQ(path, test_case.path_result)
449 << " Note: test_case.path_in = " << test_case.path_in << std::endl;
450 }
451}
452
453TEST_F(PathTemplatesTest, escape_template)
454{
458 EXPECT_EQ(BKE_path_template_escape("/studio/pröj/日本語.ocio"), "/studio/pröj/日本語.ocio");
459 EXPECT_EQ(BKE_path_template_escape("/studio/{pröj}/日本語.ocio"),
460 "/studio/{{pröj}}/日本語.ocio");
461}
462
463TEST_F(PathTemplatesTest, apply_template_alloc)
464{
465 VariableMap variables;
466 {
467 variables.add_string("long", "This string is exactly 32 bytes_");
468 variables.add_string("short", "hi");
469 variables.add_string("empty", "");
470 variables.add_integer("number", 42);
471 }
472
473 const int max_output_alloc_size = 40;
474 const Vector<PathTemplateTestCase> test_cases = {
475 {
476 "{long}{short}{empty}{number}",
477 "This string is exactly 32 bytes_hi42",
478 {},
479 },
480 {
481 "foo{long}bar",
482 "fooThis string is exactly 32 bytes_bar",
483 {},
484 },
485 {
486 "foo{short}bar",
487 "foohibar",
488 {},
489 },
490 {
491 "foo{empty}bar",
492 "foobar",
493 {},
494 },
495 {
496 "foo{number}bar",
497 "foo42bar",
498 {},
499 },
500 {
501 "foo{number:####}bar",
502 "foo0042bar",
503 {},
504 },
505 {
506 "{empty}",
507 "",
508 {},
509 },
510
511 /* No template expressions. */
512 {
513 "",
514 "",
515 {},
516 },
517 {
518 "No template expressions here.",
519 "No template expressions here.",
520 {},
521 },
522
523 /* Truncated due to exceeding max specified length. */
524 {
525 "foo{long}{long}{long}{long}bar",
526 "fooThis string is exactly 32 bytes_This",
527 {},
528 },
529
530 /* Errors. */
531 {
532 "foo{non_existant_variable}bar",
533 "foo{non_existant_variable}bar",
534 {
536 },
537 },
538 {
539 "foo{bar",
540 "foo{bar",
542 },
543
544 /* Error where the error isn't until after the truncation point. Should
545 * still be flagged as an error. */
546 {
547 "foo{long}{long}{long}{long}{long}{long}{long}{bar",
548 "foo{long}{long}{long}{long}{long}{long}{long}{bar",
550 },
551 };
552
553 for (const PathTemplateTestCase &test_case : test_cases) {
554 const int in_size = strlen(test_case.path_in) + 1;
555 char *buffer = MEM_new_array_uninitialized<char>(in_size, __func__);
556 BLI_strncpy(buffer, test_case.path_in, in_size);
557
558 const Vector<Error> application_errors = BKE_path_apply_template_alloc(
559 &buffer, max_output_alloc_size, variables);
560 EXPECT_EQ(application_errors, test_case.expected_errors)
561 << " Template errors: " << errors_to_string(application_errors) << std::endl
562 << " Expected errors: " << errors_to_string(test_case.expected_errors) << std::endl
563 << " Note: test_case.path_in = " << test_case.path_in << std::endl;
564 EXPECT_STREQ(buffer, test_case.path_result)
565 << " Note: test_case.path_in = " << test_case.path_in << std::endl
566 << " Note: test_case.path_result = " << test_case.path_result << std::endl;
567
568 MEM_delete(buffer);
569 }
570}
571
572} // namespace blender::bke::tests
Functions and classes for evaluating template expressions in filepaths.
#define FILE_MAX
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr int64_t start() const
std::optional< int64_t > get_integer(StringRef name) const
bool add_filename_only(StringRef var_name, StringRefNull full_path, StringRef fallback)
bool add_float(StringRef name, double value)
bool add_path_up_to_file(StringRef var_name, StringRefNull full_path, StringRef fallback)
std::optional< double > get_float(StringRef name) const
std::optional< StringRefNull > get_filepath(StringRef name) const
std::optional< StringRefNull > get_string(StringRef name) const
bool add_integer(StringRef name, int64_t value)
bool add_string(StringRef name, StringRef value)
bool add_filepath(StringRef name, StringRef value)
#define EXPECT_FALSE(result)
#define EXPECT_TRUE(result)
static std::string error_to_string(const Error &error)
static std::string errors_to_string(Span< Error > errors)
TEST_F(AnimDataTest, BKE_fcurves_id_cb_test)
char * STRNCPY(char(&dst)[N], const char *src)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
Vector< bke::path_templates::Error > BKE_path_apply_template_alloc(char **path, int path_maxncpy, const bke::path_templates::VariableMap &template_variables)
Vector< bke::path_templates::Error > BKE_path_apply_template(char *path, int path_maxncpy, const bke::path_templates::VariableMap &template_variables)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
std::string BKE_path_template_escape(StringRef text)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
Vector< bke::path_templates::Error > BKE_path_validate_template(StringRef path, const bke::path_templates::VariableMap &template_variables)