Skip to content

Commit

Permalink
Add tileset view test
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Jul 14, 2024
1 parent 5a045d6 commit 59a0d9e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 4 deletions.
13 changes: 9 additions & 4 deletions source/core/src/tactile/core/document/tileset_view_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "tactile/core/document/tileset_view_impl.hpp"

#include <algorithm> // count_if

#include "tactile/base/document/document_visitor.hpp"
#include "tactile/base/numeric/saturate_cast.hpp"
#include "tactile/core/debug/validation.hpp"
Expand Down Expand Up @@ -46,17 +48,20 @@ 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<CTilesetInstance>(mTilesetId);
const auto& tileset = registry.get<CTileset>(mTilesetId);

return saturate_cast<usize>(instance.tile_range.count);
return tileset.tiles.size();
}

auto TilesetViewImpl::tile_definition_count() const -> usize
{
const auto& registry = mDocument->get_registry();
const auto& tileset = registry.get<CTileset>(mTilesetId);

return tileset.tiles.size();
return saturate_cast<usize>(
std::ranges::count_if(tileset.tiles, [&registry](const EntityID tile_id) {
return !is_tile_plain(registry, tile_id);
}));
}

auto TilesetViewImpl::column_count() const -> usize
Expand All @@ -66,7 +71,7 @@ auto TilesetViewImpl::column_count() const -> usize
const auto& tileset = registry.get<CTileset>(mTilesetId);
const auto& texture = registry.get<CTexture>(mTilesetId);

return texture.size.x() / tileset.tile_size.x();
return saturate_cast<usize>(texture.size.x() / tileset.tile_size.x());
}

auto TilesetViewImpl::get_tile_size() const -> Int2
Expand Down
1 change: 1 addition & 0 deletions source/core/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
79 changes: 79 additions & 0 deletions source/core/test/src/document/tileset_view_impl_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0)

#include "tactile/core/document/tileset_view_impl.hpp"

#include <gtest/gtest.h>

#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<CMap>(map_id);

add_tileset_to_map(registry, map_id, kDummyTilesetSpec).value();

const auto tileset_id = map.attached_tilesets.back();

const auto& tileset = registry.get<CTileset>(tileset_id);
const auto& tileset_instance = registry.get<CTilesetInstance>(tileset_id);
const auto& texture = registry.get<CTexture>(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

0 comments on commit 59a0d9e

Please sign in to comment.