Blender V4.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_callbacks.hh"
17
18#include "BLI_fileops.h"
19#include "BLI_path_utils.hh"
20
21#include "CLG_log.h"
22
23#include "testing/testing.h"
24
25namespace blender::asset_system {
26class AssetCatalogTree;
27class AssetCatalogTreeItem;
28class AssetCatalogPath;
29} // namespace blender::asset_system
30
32
37class AssetLibraryTestBase : public testing::Test {
38 protected:
40 std::string temp_library_path_;
41
42 static void SetUpTestSuite()
43 {
44 testing::Test::SetUpTestSuite();
45 CLG_init();
46 /* Current File library needs this. */
48 }
49
50 static void TearDownTestSuite()
51 {
53 CLG_exit();
54 testing::Test::TearDownTestSuite();
55 }
56
57 void SetUp() override
58 {
59 const std::string test_files_dir = blender::tests::flags_test_asset_dir();
60 if (test_files_dir.empty()) {
61 FAIL();
62 }
63
64 asset_library_root_ = test_files_dir + SEP_STR + "asset_library";
66 }
67
68 void TearDown() override
69 {
71
72 if (!temp_library_path_.empty()) {
73 BLI_delete(temp_library_path_.c_str(), true, true);
75 }
76 }
77
78 /* Register a temporary path, which will be removed at the end of the test.
79 * The returned path ends in a slash. */
80 std::string use_temp_path()
81 {
82 BKE_tempdir_init(nullptr);
83 const std::string tempdir = BKE_tempdir_session();
84 temp_library_path_ = tempdir + "test-temporary-path" + SEP_STR;
85 return temp_library_path_;
86 }
87
88 std::string create_temp_path()
89 {
90 std::string path = use_temp_path();
91 BLI_dir_create_recursive(path.c_str());
92 return path;
93 }
94};
95
97 public:
102 static void expect_tree_items(const AssetCatalogTree &tree,
103 const std::vector<AssetCatalogPath> &expected_paths);
104
111 const std::vector<AssetCatalogPath> &expected_paths);
112
118 static void expect_tree_item_child_items(const AssetCatalogTreeItem &parent_item,
119 const std::vector<AssetCatalogPath> &expected_paths);
120};
121
122static inline void compare_item_with_path(const AssetCatalogPath &expected_path,
123 const AssetCatalogTreeItem &actual_item)
124{
125 if (expected_path != actual_item.catalog_path().str()) {
126 /* This will fail, but with a nicer error message than just calling FAIL(). */
127 EXPECT_EQ(expected_path, actual_item.catalog_path());
128 return;
129 }
130
131 /* Is the catalog name as expected? "character", "Ellie", ... */
132 EXPECT_EQ(expected_path.name(), actual_item.get_name());
133
134 /* Does the computed number of parents match? */
135 const std::string expected_path_str = expected_path.str();
136 const size_t expected_parent_count = std::count(
137 expected_path_str.begin(), expected_path_str.end(), AssetCatalogPath::SEPARATOR);
138 EXPECT_EQ(expected_parent_count, actual_item.count_parents());
139}
140
142 const AssetCatalogTree &tree, const std::vector<AssetCatalogPath> &expected_paths)
143{
144 int i = 0;
145 tree.foreach_item([&](const AssetCatalogTreeItem &actual_item) {
146 ASSERT_LT(i, expected_paths.size())
147 << "More catalogs in tree than expected; did not expect " << actual_item.catalog_path();
148 compare_item_with_path(expected_paths[i], actual_item);
149 i++;
150 });
151}
152
154 const AssetCatalogTree &tree, const std::vector<AssetCatalogPath> &expected_paths)
155{
156 int i = 0;
157 tree.foreach_root_item([&](const AssetCatalogTreeItem &actual_item) {
158 ASSERT_LT(i, expected_paths.size())
159 << "More catalogs in tree root than expected; did not expect "
160 << actual_item.catalog_path();
161 compare_item_with_path(expected_paths[i], actual_item);
162 i++;
163 });
164}
165
167 const AssetCatalogTreeItem &parent_item, const std::vector<AssetCatalogPath> &expected_paths)
168{
169 int i = 0;
170 parent_item.foreach_child([&](const AssetCatalogTreeItem &actual_item) {
171 ASSERT_LT(i, expected_paths.size())
172 << "More catalogs in tree item than expected; did not expect "
173 << actual_item.catalog_path();
174 compare_item_with_path(expected_paths[i], actual_item);
175 i++;
176 });
177}
178
179} // namespace blender::asset_system::tests
void BKE_tempdir_init(const char *userdir)
Definition appdir.cc:1190
void BKE_callback_global_finalize()
Definition callbacks.cc:108
void BKE_callback_global_init()
Definition callbacks.cc:103
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
File and directory operations.
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
Definition fileops_c.cc:391
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
void CLG_exit(void)
Definition clog.c:706
void CLG_init(void)
Definition clog.c:699
void foreach_child(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)
KDTree_3d * tree
static void compare_item_with_path(const AssetCatalogPath &expected_path, const AssetCatalogTreeItem &actual_item)
void * BKE_tempdir_session
Definition stubs.c:38
#define SEP_STR
Definition unit.cc:39