Blender V5.3
asset_library_test_common.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include <string>
8#include <vector>
9
10#include "AS_asset_catalog.hh"
12
14
15#include "BKE_appdir.hh"
16#include "BKE_global.hh"
17#include "BKE_gtest_base.hh"
18#include "BKE_main.hh"
19
20#include "BLI_fileops.hh"
21#include "BLI_path_utils.hh"
22
23#include "testing/testing.h"
24
25namespace blender {
26
27namespace asset_system {
31} // namespace asset_system
32
33namespace asset_system::tests {
34
40 protected:
42 std::string temp_library_path_;
44
45 void SetUp() override
46 {
47 /* G_MAIN is needed for variable expansion in asset library paths.
48 * IE {project_root}, {blend_name}, {blend_dir} etc.
49 */
51 G_MAIN = bmain;
52
53 const std::string test_files_dir = blender::tests::flags_test_asset_dir();
54 if (test_files_dir.empty()) {
55 FAIL();
56 }
57
58 asset_library_root_ = test_files_dir + SEP_STR + "asset_library";
60 }
61
62 void TearDown() override
63 {
65
66 if (!temp_library_path_.empty()) {
67 BLI_delete(temp_library_path_.c_str(), true, true);
69 }
70
72 G_MAIN = nullptr;
73 }
74
75 /* Register a temporary path, which will be removed at the end of the test.
76 * The returned path ends in a slash. */
77 std::string use_temp_path()
78 {
79 BKE_tempdir_init(nullptr);
80 const std::string tempdir = BKE_tempdir_session();
81 temp_library_path_ = tempdir + "test-temporary-path" + SEP_STR;
82 return temp_library_path_;
83 }
84
85 std::string create_temp_path()
86 {
87 std::string path = use_temp_path();
88 BLI_dir_create_recursive(path.c_str());
89 return path;
90 }
91};
92
94 public:
99 static void expect_tree_items(const AssetCatalogTree &tree,
100 const std::vector<AssetCatalogPath> &expected_paths);
101
107 static void expect_tree_root_items(const AssetCatalogTree &tree,
108 const std::vector<AssetCatalogPath> &expected_paths);
109
115 static void expect_tree_item_child_items(const AssetCatalogTreeItem &parent_item,
116 const std::vector<AssetCatalogPath> &expected_paths);
117};
118
119static inline void compare_item_with_path(const AssetCatalogPath &expected_path,
120 const AssetCatalogTreeItem &actual_item)
121{
122 if (expected_path != actual_item.catalog_path().str()) {
123 /* This will fail, but with a nicer error message than just calling FAIL(). */
124 EXPECT_EQ(expected_path, actual_item.catalog_path());
125 return;
126 }
127
128 /* Is the catalog name as expected? "character", "Ellie", ... */
129 EXPECT_EQ(expected_path.name(), actual_item.get_name());
130
131 /* Does the computed number of parents match? */
132 const std::string expected_path_str = expected_path.str();
133 const size_t expected_parent_count = std::count(
134 expected_path_str.begin(), expected_path_str.end(), AssetCatalogPath::SEPARATOR);
135 EXPECT_EQ(expected_parent_count, actual_item.count_parents());
136}
137
139 const AssetCatalogTree &tree, const std::vector<AssetCatalogPath> &expected_paths)
140{
141 int i = 0;
142 tree.foreach_item([&](const AssetCatalogTreeItem &actual_item) {
143 ASSERT_LT(i, expected_paths.size())
144 << "More catalogs in tree than expected; did not expect " << actual_item.catalog_path();
145 compare_item_with_path(expected_paths[i], actual_item);
146 i++;
147 });
148}
149
151 const AssetCatalogTree &tree, const std::vector<AssetCatalogPath> &expected_paths)
152{
153 int i = 0;
154 tree.foreach_root_item([&](const AssetCatalogTreeItem &actual_item) {
155 ASSERT_LT(i, expected_paths.size())
156 << "More catalogs in tree root than expected; did not expect "
157 << actual_item.catalog_path();
158 compare_item_with_path(expected_paths[i], actual_item);
159 i++;
160 });
161}
162
164 const AssetCatalogTreeItem &parent_item, const std::vector<AssetCatalogPath> &expected_paths)
165{
166 int i = 0;
167 parent_item.foreach_child([&](const AssetCatalogTreeItem &actual_item) {
168 ASSERT_LT(i, expected_paths.size())
169 << "More catalogs in tree item than expected; did not expect "
170 << actual_item.catalog_path();
171 compare_item_with_path(expected_paths[i], actual_item);
172 i++;
173 });
174}
175
176} // namespace asset_system::tests
177
178} // namespace blender
#define G_MAIN
File and directory operations.
void foreach_child(ItemIterFn callback) const
void foreach_root_item(ItemIterFn callback) const
void foreach_item(ItemIterFn callback) const
static void expect_tree_items(const AssetCatalogTree &tree, const std::vector< AssetCatalogPath > &expected_paths)
static void expect_tree_root_items(const AssetCatalogTree &tree, const std::vector< AssetCatalogPath > &expected_paths)
static void expect_tree_item_child_items(const AssetCatalogTreeItem &parent_item, const std::vector< AssetCatalogPath > &expected_paths)
static void compare_item_with_path(const AssetCatalogPath &expected_path, const AssetCatalogTreeItem &actual_item)
BMesh Iterators.
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
Definition fileops_c.cc:416
Main * BKE_main_new()
Definition main.cc:93
const char * BKE_tempdir_session() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
Definition appdir.cc:1283
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
void BKE_main_free(Main *bmain)
Definition main.cc:202
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void BKE_tempdir_init(const char *userdir)
Definition appdir.cc:1245
#define SEP_STR
Definition unit.cc:41