[Bf-blender-cvs] [e8eb5d8a7c6] temp-asset-browser-catalogs: Add tests for catalog tree

Julian Eisel noreply at git.blender.org
Thu Sep 2 15:25:31 CEST 2021


Commit: e8eb5d8a7c603cbbfb5d97a77bfc0dd93fccb663
Author: Julian Eisel
Date:   Thu Sep 2 15:23:08 2021 +0200
Branches: temp-asset-browser-catalogs
https://developer.blender.org/rBe8eb5d8a7c603cbbfb5d97a77bfc0dd93fccb663

Add tests for catalog tree

Removes the printing of the catalog tree. Also adds and tests a function
to get the full catalog path from a tree item.

===================================================================

M	source/blender/blenkernel/BKE_asset_catalog.hh
M	source/blender/blenkernel/intern/asset_catalog.cc
M	source/blender/blenkernel/intern/asset_catalog_test.cc

===================================================================

diff --git a/source/blender/blenkernel/BKE_asset_catalog.hh b/source/blender/blenkernel/BKE_asset_catalog.hh
index d978868c7da..8ee7395af4c 100644
--- a/source/blender/blenkernel/BKE_asset_catalog.hh
+++ b/source/blender/blenkernel/BKE_asset_catalog.hh
@@ -70,6 +70,8 @@ class AssetCatalogService {
   /** For testing only, get the loaded catalog definition file. */
   AssetCatalogDefinitionFile *get_catalog_definition_file();
 
+  AssetCatalogTree *get_catalog_tree();
+
   /** Return true iff there are no catalogs known. */
   bool is_empty() const;
 
@@ -101,7 +103,6 @@ class AssetCatalogService {
   bool ensure_asset_library_root();
 
   std::unique_ptr<AssetCatalogTree> read_into_tree();
-  void print_tree();
 };
 
 class AssetCatalogTreeItem {
@@ -114,6 +115,9 @@ class AssetCatalogTreeItem {
   AssetCatalogTreeItem(StringRef name, const AssetCatalogTreeItem *parent = nullptr);
 
   StringRef get_name() const;
+  /** Return the full catalog path, defined as the name of this catalog prefixed by the full
+   * catalog path of its parent and a separator. */
+  CatalogPath catalog_path() const;
   int count_parents() const;
 
   static void foreach_item_recursive(const ChildSet &children_, const ItemIterFn callback);
diff --git a/source/blender/blenkernel/intern/asset_catalog.cc b/source/blender/blenkernel/intern/asset_catalog.cc
index 60c15804be4..5e24ba30e55 100644
--- a/source/blender/blenkernel/intern/asset_catalog.cc
+++ b/source/blender/blenkernel/intern/asset_catalog.cc
@@ -143,7 +143,6 @@ void AssetCatalogService::load_from_disk(const CatalogFilePath &file_or_director
   /* TODO: Should there be a sanitize step? E.g. to remove catalogs with identical paths? */
 
   catalog_tree_ = read_into_tree();
-  print_tree();
 }
 
 void AssetCatalogService::load_directory_recursive(const CatalogFilePath &directory_path)
@@ -239,6 +238,8 @@ std::unique_ptr<AssetCatalogTree> AssetCatalogService::read_into_tree()
 
   /* Go through the catalogs, insert each path component into the tree where needed. */
   for (auto &catalog : catalogs_.values()) {
+    /* #fs::path adds useful behavior to the path. Remember that on Windows it uses "\" as
+     * separator! For catalogs it should always be "/". Use #fs::path::generic_string if needed. */
     fs::path catalog_path = catalog->path;
 
     const AssetCatalogTreeItem *parent = nullptr;
@@ -263,18 +264,6 @@ std::unique_ptr<AssetCatalogTree> AssetCatalogService::read_into_tree()
   return tree;
 }
 
-/* TODO just for testing. */
-void AssetCatalogService::print_tree()
-{
-  std::cout << "==== Printing Catalog Tree: ====" << std::endl;
-  catalog_tree_->foreach_item([](const AssetCatalogTreeItem &item) {
-    for (int i = 0; i < item.count_parents(); i++) {
-      std::cout << "  ";
-    }
-    std::cout << item.get_name() << std::endl;
-  });
-}
-
 AssetCatalogTreeItem::AssetCatalogTreeItem(StringRef name, const AssetCatalogTreeItem *parent)
     : name_(name), parent_(parent)
 {
@@ -285,6 +274,15 @@ StringRef AssetCatalogTreeItem::get_name() const
   return name_;
 }
 
+CatalogPath AssetCatalogTreeItem::catalog_path() const
+{
+  std::string current_path = name_;
+  for (const AssetCatalogTreeItem *parent = parent_; parent; parent = parent->parent_) {
+    current_path = parent->name_ + AssetCatalogService::PATH_SEPARATOR + current_path;
+  }
+  return current_path;
+}
+
 int AssetCatalogTreeItem::count_parents() const
 {
   int i = 0;
@@ -313,6 +311,11 @@ AssetCatalogDefinitionFile *AssetCatalogService::get_catalog_definition_file()
   return catalog_definition_file_.get();
 }
 
+AssetCatalogTree *AssetCatalogService::get_catalog_tree()
+{
+  return catalog_tree_.get();
+}
+
 bool AssetCatalogDefinitionFile::contains(const CatalogID &catalog_id) const
 {
   return catalogs_.contains(catalog_id);
diff --git a/source/blender/blenkernel/intern/asset_catalog_test.cc b/source/blender/blenkernel/intern/asset_catalog_test.cc
index 856008e0537..e547da86ccd 100644
--- a/source/blender/blenkernel/intern/asset_catalog_test.cc
+++ b/source/blender/blenkernel/intern/asset_catalog_test.cc
@@ -93,6 +93,45 @@ TEST_F(AssetCatalogTest, load_single_file)
   EXPECT_EQ("character/Ružena/poselib", poses_ruzena->path);
 }
 
+static int count_path_parents(const fs::path &path)
+{
+  int counter = 0;
+  for (fs::path segment : path.parent_path()) {
+    counter++;
+  }
+  return counter;
+}
+
+TEST_F(AssetCatalogTest, load_single_file_into_tree)
+{
+  AssetCatalogService service(asset_library_root_);
+  service.load_from_disk(asset_library_root_ / "blender_assets.cats.txt");
+
+  std::vector<fs::path> expected_paths{
+      "character",
+      "character/Elly",
+      "character/Elly/poselib",
+      "character/Elly/poselib/white space",
+      "character/Ružena",
+      "character/Ružena/poselib",
+      "character/Ružena/poselib/face",
+      "character/Ružena/poselib/hand",
+  };
+
+  AssetCatalogTree *tree = service.get_catalog_tree();
+
+  int i = 0;
+  tree->foreach_item([&](const AssetCatalogTreeItem &actual_item) {
+    /* Is the catalog name as expected? "character", "Elly", ... */
+    EXPECT_EQ(expected_paths[i].filename().string(), actual_item.get_name());
+    /* Does the amount of parents match? */
+    EXPECT_EQ(count_path_parents(expected_paths[i]), actual_item.count_parents());
+    EXPECT_EQ(expected_paths[i].generic_string(), actual_item.catalog_path());
+
+    i++;
+  });
+}
+
 TEST_F(AssetCatalogTest, write_single_file)
 {
   AssetCatalogService service(asset_library_root_);



More information about the Bf-blender-cvs mailing list