[Bf-blender-cvs] [85d92afbd4d] master: Cleanup: Move asset catalog tree tests to own file

Julian Eisel noreply at git.blender.org
Mon Dec 12 17:10:24 CET 2022


Commit: 85d92afbd4dd961327191ecc68579d25a8e653ea
Author: Julian Eisel
Date:   Mon Dec 12 17:07:55 2022 +0100
Branches: master
https://developer.blender.org/rB85d92afbd4dd961327191ecc68579d25a8e653ea

Cleanup: Move asset catalog tree tests to own file

The catalog tree is a unit on its own, and should be tested separately.
This makes the testing files smaller and more focused, which can help
maintaining them.

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

M	source/blender/asset_system/CMakeLists.txt
M	source/blender/asset_system/tests/asset_catalog_test.cc
A	source/blender/asset_system/tests/asset_catalog_tree_test.cc
M	source/blender/asset_system/tests/asset_library_test_common.hh

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

diff --git a/source/blender/asset_system/CMakeLists.txt b/source/blender/asset_system/CMakeLists.txt
index 6f4e058e78d..f8e1df40d80 100644
--- a/source/blender/asset_system/CMakeLists.txt
+++ b/source/blender/asset_system/CMakeLists.txt
@@ -48,6 +48,7 @@ if(WITH_GTESTS)
   set(TEST_SRC
     tests/asset_catalog_path_test.cc
     tests/asset_catalog_test.cc
+    tests/asset_catalog_tree_test.cc
     tests/asset_library_service_test.cc
     tests/asset_library_test.cc
 
diff --git a/source/blender/asset_system/tests/asset_catalog_test.cc b/source/blender/asset_system/tests/asset_catalog_test.cc
index 144912d62a1..87819f3683b 100644
--- a/source/blender/asset_system/tests/asset_catalog_test.cc
+++ b/source/blender/asset_system/tests/asset_catalog_test.cc
@@ -80,77 +80,6 @@ class TestableAssetCatalogService : public AssetCatalogService {
 
 class AssetCatalogTest : public AssetLibraryTestBase {
  protected:
-  void assert_expected_item(const AssetCatalogPath &expected_path,
-                            const AssetCatalogTreeItem &actual_item)
-  {
-    if (expected_path != actual_item.catalog_path().str()) {
-      /* This will fail, but with a nicer error message than just calling FAIL(). */
-      EXPECT_EQ(expected_path, actual_item.catalog_path());
-      return;
-    }
-
-    /* Is the catalog name as expected? "character", "Ellie", ... */
-    EXPECT_EQ(expected_path.name(), actual_item.get_name());
-
-    /* Does the computed number of parents match? */
-    const std::string expected_path_str = expected_path.str();
-    const size_t expected_parent_count = std::count(
-        expected_path_str.begin(), expected_path_str.end(), AssetCatalogPath::SEPARATOR);
-    EXPECT_EQ(expected_parent_count, actual_item.count_parents());
-  }
-
-  /**
-   * Recursively iterate over all tree items using #AssetCatalogTree::foreach_item() and check if
-   * the items map exactly to \a expected_paths.
-   */
-  void assert_expected_tree_items(AssetCatalogTree *tree,
-                                  const std::vector<AssetCatalogPath> &expected_paths)
-  {
-    int i = 0;
-    tree->foreach_item([&](const AssetCatalogTreeItem &actual_item) {
-      ASSERT_LT(i, expected_paths.size())
-          << "More catalogs in tree than expected; did not expect " << actual_item.catalog_path();
-      assert_expected_item(expected_paths[i], actual_item);
-      i++;
-    });
-  }
-
-  /**
-   * Iterate over the root items of \a tree and check if the items map exactly to \a
-   * expected_paths. Similar to #assert_expected_tree_items() but calls
-   * #AssetCatalogTree::foreach_root_item() instead of #AssetCatalogTree::foreach_item().
-   */
-  void assert_expected_tree_root_items(AssetCatalogTree *tree,
-                                       const std::vector<AssetCatalogPath> &expected_paths)
-  {
-    int i = 0;
-    tree->foreach_root_item([&](const AssetCatalogTreeItem &actual_item) {
-      ASSERT_LT(i, expected_paths.size())
-          << "More catalogs in tree root than expected; did not expect "
-          << actual_item.catalog_path();
-      assert_expected_item(expected_paths[i], actual_item);
-      i++;
-    });
-  }
-
-  /**
-   * Iterate over the child items of \a parent_item and check if the items map exactly to \a
-   * expected_paths. Similar to #assert_expected_tree_items() but calls
-   * #AssetCatalogTreeItem::foreach_child() instead of #AssetCatalogTree::foreach_item().
-   */
-  void assert_expected_tree_item_child_items(AssetCatalogTreeItem *parent_item,
-                                             const std::vector<AssetCatalogPath> &expected_paths)
-  {
-    int i = 0;
-    parent_item->foreach_child([&](const AssetCatalogTreeItem &actual_item) {
-      ASSERT_LT(i, expected_paths.size())
-          << "More catalogs in tree item than expected; did not expect "
-          << actual_item.catalog_path();
-      assert_expected_item(expected_paths[i], actual_item);
-      i++;
-    });
-  }
-
   /* Used by on_blendfile_save__from_memory_into_existing_asset_lib* test functions. */
   void save_from_memory_into_existing_asset_lib(const bool should_top_level_cdf_exist)
   {
@@ -307,149 +236,6 @@ TEST_F(AssetCatalogTest, is_first_loaded_flag)
       << "The first-seen definition of a catalog should be returned";
 }
 
-TEST_F(AssetCatalogTest, insert_item_into_tree)
-{
-  {
-    AssetCatalogTree tree;
-    std::unique_ptr<AssetCatalog> catalog_empty_path = AssetCatalog::from_path("");
-    tree.insert_item(*catalog_empty_path);
-
-    assert_expected_tree_items(&tree, {});
-  }
-
-  {
-    AssetCatalogTree tree;
-
-    std::unique_ptr<AssetCatalog> catalog = AssetCatalog::from_path("item");
-    tree.insert_item(*catalog);
-    assert_expected_tree_items(&tree, {"item"});
-
-    /* Insert child after parent already exists. */
-    std::unique_ptr<AssetCatalog> child_catalog = AssetCatalog::from_path("item/child");
-    tree.insert_item(*catalog);
-    assert_expected_tree_items(&tree, {"item", "item/child"});
-
-    std::vector<AssetCatalogPath> expected_paths;
-
-    /* Test inserting multi-component sub-path. */
-    std::unique_ptr<AssetCatalog> grandgrandchild_catalog = AssetCatalog::from_path(
-        "item/child/grandchild/grandgrandchild");
-    tree.insert_item(*catalog);
-    expected_paths = {
-        "item", "item/child", "item/child/grandchild", "item/child/grandchild/grandgrandchild"};
-    assert_expected_tree_items(&tree, expected_paths);
-
-    std::unique_ptr<AssetCatalog> root_level_catalog = AssetCatalog::from_path("root level");
-    tree.insert_item(*catalog);
-    expected_paths = {"item",
-                      "item/child",
-                      "item/child/grandchild",
-                      "item/child/grandchild/grandgrandchild",
-                      "root level"};
-    assert_expected_tree_items(&tree, expected_paths);
-  }
-
-  {
-    AssetCatalogTree tree;
-
-    std::unique_ptr<AssetCatalog> catalog = AssetCatalog::from_path("item/child");
-    tree.insert_item(*catalog);
-    assert_expected_tree_items(&tree, {"item", "item/child"});
-  }
-
-  {
-    AssetCatalogTree tree;
-
-    std::unique_ptr<AssetCatalog> catalog = AssetCatalog::from_path("white space");
-    tree.insert_item(*catalog);
-    assert_expected_tree_items(&tree, {"white space"});
-  }
-
-  {
-    AssetCatalogTree tree;
-
-    std::unique_ptr<AssetCatalog> catalog = AssetCatalog::from_path("/item/white space");
-    tree.insert_item(*catalog);
-    assert_expected_tree_items(&tree, {"item", "item/white space"});
-  }
-
-  {
-    AssetCatalogTree tree;
-
-    std::unique_ptr<AssetCatalog> catalog_unicode_path = AssetCatalog::from_path("Ružena");
-    tree.insert_item(*catalog_unicode_path);
-    assert_expected_tree_items(&tree, {"Ružena"});
-
-    catalog_unicode_path = AssetCatalog::from_path("Ružena/Ružena");
-    tree.insert_item(*catalog_unicode_path);
-    assert_expected_tree_items(&tree, {"Ružena", "Ružena/Ružena"});
-  }
-}
-
-TEST_F(AssetCatalogTest, load_single_file_into_tree)
-{
-  AssetCatalogService service(asset_library_root_);
-  service.load_from_disk(asset_library_root_ + SEP_STR + "blender_assets.cats.txt");
-
-  /* Contains not only paths from the CDF but also the missing parents (implicitly defined
-   * catalogs). */
-  std::vector<AssetCatalogPath> expected_paths{
-      "character",
-      "character/Ellie",
-      "character/Ellie/backslashes",
-      "character/Ellie/poselib",
-      "character/Ellie/poselib/tailslash",
-      "character/Ellie/poselib/white space",
-      "character/Ružena",
-      "character/Ružena/poselib",
-      "character/Ružena/poselib/face",
-      "character/Ružena/poselib/hand",
-      "path",                    /* Implicit. */
-      "path/without",            /* Implicit. */
-      "path/without/simplename", /* From CDF. */
-  };
-
-  AssetCatalogTree *tree = service.get_catalog_tree();
-  assert_expected_tree_items(tree, expected_paths);
-}
-
-TEST_F(AssetCatalogTest, foreach_in_tree)
-{
-  {
-    AssetCatalogTree tree{};
-    const std::vector<AssetCatalogPath> no_catalogs{};
-
-    assert_expected_tree_items(&tree, no_catalogs);
-    assert_expected_tree_root_items(&tree, no_catalogs);
-    /* Need a root item to check child items. */
-    std::unique_ptr<AssetCatalog> catalog = AssetCatalog::from_path("something");
-    tree.insert_item(*catalog);
-    tree.foreach_root_item([&no_catalogs, this](AssetCatalogTreeItem &item) {
-      assert_expected_tree_item_child_items(&item, no_catalogs);
-    });
-  }
-
-  AssetCatalogService service(asset_library_root_);
-  service.load_from_disk(asset_library_root_ + SEP_STR + "blender_assets.cats.txt");
-
-  std::vector<AssetCatalogPath> expected_root_items{{"character", "path"}};
-  AssetCatalogTree *tree = service.get_catalog_tree();
-  assert_expected_tree_root_items(tree, expected_root_items);
-
-  /* Test if the direct children of the root item are what's expected. */
-  std::vector<std::vector<AssetCatalogPath>> expected_root_child_items = {
-      /* Children of the "character" root item. */
-      {"character/Ellie", "character/Ružena"},
-      /* Children of the "path" root item. */
-      {"path/without"},
-  };
-  int i = 0;
-  tree->foreach_root_item([&expected_root_child_items, &i, this](AssetCatalogTreeItem &item) {
-    assert_expected_tree_item_child_items(&item, expected_root_child_items[i]);
-    i++;
-  });
-}
-
 TEST_F(AssetCatalogTest, find_catalog_by_path)
 {
   TestableAssetCatalogService service(asset_library_root_);
@@ -783,7 +569,7 @@ TEST_F(AssetCatalogTest, delete_catalog_leaf)
   };
 
   AssetCatalogTree *tree = service.get_catalog_tree();
-  assert_expected_tree_items(tree, expected_paths);
+  AssetCatalogTreeTestFunctions::expect_tree_items(tree, expected_paths);
 }
 
 TEST_F(AssetCatalogTest, delete_catalog_parent_by_id)
@@ -837,7 +623,7 @@ TEST_F(AssetCatalogTest, delete_catalog_parent_by_path)
   };
 
   AssetCatalogTree *tree = service.get_catalog_tree();
-  assert_expected_tree_items(tree, expected_paths);
+  AssetCatalogTreeTestFunctions::expect_tree_items(tree, expected_paths);
 }
 
 TEST_F(AssetCatalogTest, delete_catalog_write_to_disk)
diff --git a/source/blender/asset_system/tests/asset_catalog_tree_tes

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list