From 59a0d9ef10a38d46cc38c244233a0185b3ea8f34 Mon Sep 17 00:00:00 2001 From: Albin Johansson Date: Sun, 14 Jul 2024 20:29:56 +0200 Subject: [PATCH] Add tileset view test --- .../core/document/tileset_view_impl.cpp | 13 ++- source/core/test/CMakeLists.txt | 1 + .../src/document/tileset_view_impl_test.cpp | 79 +++++++++++++++++++ 3 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 source/core/test/src/document/tileset_view_impl_test.cpp diff --git a/source/core/src/tactile/core/document/tileset_view_impl.cpp b/source/core/src/tactile/core/document/tileset_view_impl.cpp index 7045006aac..0a955478fb 100644 --- a/source/core/src/tactile/core/document/tileset_view_impl.cpp +++ b/source/core/src/tactile/core/document/tileset_view_impl.cpp @@ -2,6 +2,8 @@ #include "tactile/core/document/tileset_view_impl.hpp" +#include // count_if + #include "tactile/base/document/document_visitor.hpp" #include "tactile/base/numeric/saturate_cast.hpp" #include "tactile/core/debug/validation.hpp" @@ -46,9 +48,9 @@ auto TilesetViewImpl::get_first_tile_id() const -> TileID auto TilesetViewImpl::tile_count() const -> usize { const auto& registry = mDocument->get_registry(); - const auto& instance = registry.get(mTilesetId); + const auto& tileset = registry.get(mTilesetId); - return saturate_cast(instance.tile_range.count); + return tileset.tiles.size(); } auto TilesetViewImpl::tile_definition_count() const -> usize @@ -56,7 +58,10 @@ auto TilesetViewImpl::tile_definition_count() const -> usize const auto& registry = mDocument->get_registry(); const auto& tileset = registry.get(mTilesetId); - return tileset.tiles.size(); + return saturate_cast( + std::ranges::count_if(tileset.tiles, [®istry](const EntityID tile_id) { + return !is_tile_plain(registry, tile_id); + })); } auto TilesetViewImpl::column_count() const -> usize @@ -66,7 +71,7 @@ auto TilesetViewImpl::column_count() const -> usize const auto& tileset = registry.get(mTilesetId); const auto& texture = registry.get(mTilesetId); - return texture.size.x() / tileset.tile_size.x(); + return saturate_cast(texture.size.x() / tileset.tile_size.x()); } auto TilesetViewImpl::get_tile_size() const -> Int2 diff --git a/source/core/test/CMakeLists.txt b/source/core/test/CMakeLists.txt index 031fa542d5..2786d3b5f7 100644 --- a/source/core/test/CMakeLists.txt +++ b/source/core/test/CMakeLists.txt @@ -13,6 +13,7 @@ target_sources(tactile-core-test "src/debug/validation_test.cpp" "src/document/meta_view_impl_test.cpp" "src/document/tile_view_impl_test.cpp" + "src/document/tileset_view_impl_test.cpp" "src/entity/registry_test.cpp" "src/event/event_dispatcher_test.cpp" "src/io/ini_test.cpp" diff --git a/source/core/test/src/document/tileset_view_impl_test.cpp b/source/core/test/src/document/tileset_view_impl_test.cpp new file mode 100644 index 0000000000..120dd9e7c3 --- /dev/null +++ b/source/core/test/src/document/tileset_view_impl_test.cpp @@ -0,0 +1,79 @@ +// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0) + +#include "tactile/core/document/tileset_view_impl.hpp" + +#include + +#include "tactile/core/document/map_document.hpp" +#include "tactile/core/entity/registry.hpp" +#include "tactile/core/io/texture.hpp" +#include "tactile/core/map/map.hpp" +#include "tactile/core/map/map_spec.hpp" +#include "tactile/core/tile/animation.hpp" +#include "tactile/core/tile/tileset.hpp" +#include "test/document_testing.hpp" + +namespace tactile::test { + +class TilesetViewImplTest : public testing::Test +{ + public: + TilesetViewImplTest() + : mMapSpec {kOrthogonalMapSpec}, + mDocument {mMapSpec} + {} + + protected: + MapSpec mMapSpec; + MapDocument mDocument; +}; + +// tactile::TilesetViewImpl::get_first_tile_id +// tactile::TilesetViewImpl::tile_count +// tactile::TilesetViewImpl::tile_definition_count +// tactile::TilesetViewImpl::column_count +// tactile::TilesetViewImpl::get_tile_size +// tactile::TilesetViewImpl::get_image_size +// tactile::TilesetViewImpl::get_image_path +TEST_F(TilesetViewImplTest, Getters) +{ + auto& registry = mDocument.get_registry(); + + const auto map_id = mDocument.get_root_entity(); + auto& map = registry.get(map_id); + + add_tileset_to_map(registry, map_id, kDummyTilesetSpec).value(); + + const auto tileset_id = map.attached_tilesets.back(); + + const auto& tileset = registry.get(tileset_id); + const auto& tileset_instance = registry.get(tileset_id); + const auto& texture = registry.get(tileset_id); + + ASSERT_TRUE( + add_animation_frame(registry, + tileset.tiles.at(0), + 0, + AnimationFrame {TileIndex {0}, Milliseconds {50}})); + ASSERT_TRUE( + add_animation_frame(registry, + tileset.tiles.at(1), + 0, + AnimationFrame {TileIndex {1}, Milliseconds {50}})); + + const TilesetViewImpl tileset_view {&mDocument, tileset_id}; + + EXPECT_EQ(tileset_view.get_first_tile_id(), + tileset_instance.tile_range.first_id); + EXPECT_EQ(tileset_view.tile_count(), tileset_instance.tile_range.count); + EXPECT_EQ(tileset_view.get_tile_size(), tileset.tile_size); + + EXPECT_EQ(tileset_view.tile_definition_count(), 2); + + EXPECT_EQ(tileset_view.column_count(), + texture.size.x() / tileset.tile_size.x()); + EXPECT_EQ(tileset_view.get_image_size(), texture.size); + EXPECT_EQ(tileset_view.get_image_path(), texture.path); +} + +} // namespace tactile::test