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