Skip to content

Commit

Permalink
Add tile view test
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Jul 14, 2024
1 parent 415a055 commit 5a045d6
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions source/core/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ target_sources(tactile-core-test
"src/debug/validation_test.cpp"
"src/debug/validation_test.cpp"
"src/document/meta_view_impl_test.cpp"
"src/document/tile_view_impl_test.cpp"
"src/entity/registry_test.cpp"
"src/event/event_dispatcher_test.cpp"
"src/io/ini_test.cpp"
Expand Down
12 changes: 12 additions & 0 deletions source/core/test/inc/test/document_testing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "tactile/base/prelude.hpp"
#include "tactile/core/document/map_document.hpp"
#include "tactile/core/map/map_spec.hpp"
#include "tactile/core/tile/tileset_spec.hpp"

namespace tactile::test {

Expand All @@ -14,4 +15,15 @@ inline constexpr MapSpec kOrthogonalMapSpec {
.tile_size = Int2 {50, 50},
};

inline const TilesetSpec kDummyTilesetSpec {
.tile_size = Int2 {16, 16},
.texture =
CTexture {
.raw_handle = nullptr,
.id = TextureID {1},
.size = Int2 {96, 64},
.path = "assets/images/dummy.png",
},
};

} // namespace tactile::test
83 changes: 83 additions & 0 deletions source/core/test/src/document/tile_view_impl_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0)

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

#include <gtest/gtest.h>

#include "tactile/core/document/map_document.hpp"
#include "tactile/core/document/tileset_view_impl.hpp"
#include "tactile/core/entity/registry.hpp"
#include "tactile/core/map/map.hpp"
#include "tactile/core/map/map_spec.hpp"
#include "tactile/core/tile/animation.hpp"
#include "tactile/core/tile/tile.hpp"
#include "tactile/core/tile/tileset.hpp"
#include "tactile/core/tile/tileset_spec.hpp"
#include "test/document_testing.hpp"

namespace tactile::test {

class TileViewImplTest : public testing::Test
{
public:
TileViewImplTest()
: mMapSpec {kOrthogonalMapSpec},
mDocument {mMapSpec}
{}

protected:
MapSpec mMapSpec;
MapDocument mDocument;
};

// tactile::TileViewImpl::get_parent_tileset
// tactile::TileViewImpl::get_index
// tactile::TileViewImpl::object_count
// tactile::TileViewImpl::animation_frame_count
// tactile::TileViewImpl::get_animation_frame
TEST_F(TileViewImplTest, 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 tile_id = tileset.tiles.front();
const auto& tile = registry.get<CTile>(tile_id);

add_animation_frame(registry,
tile_id,
0,
AnimationFrame {tile.index, Milliseconds {1}})
.value();
add_animation_frame(registry,
tile_id,
1,
AnimationFrame {tile.index + 1, Milliseconds {2}})
.value();

const auto& animation = registry.get<CAnimation>(tile_id);

const TilesetViewImpl tileset_view {&mDocument, tileset_id};
const TileViewImpl tile_view {&mDocument, &tileset_view, tile_id};

EXPECT_EQ(&tile_view.get_parent_tileset(), &tileset_view);
EXPECT_EQ(tile_view.get_index(), tile.index);
EXPECT_EQ(tile_view.object_count(), tile.objects.size());
EXPECT_EQ(tile_view.animation_frame_count(), animation.frames.size());

EXPECT_EQ(tile_view.get_animation_frame(0).first, tile.index);
EXPECT_EQ(tile_view.get_animation_frame(1).first, tile.index + 1);
EXPECT_EQ(tile_view.get_animation_frame(0).second, Milliseconds {1});
EXPECT_EQ(tile_view.get_animation_frame(1).second, Milliseconds {2});

EXPECT_ANY_THROW(
(void) tile_view.get_animation_frame(tile_view.animation_frame_count()));
}

} // namespace tactile::test

0 comments on commit 5a045d6

Please sign in to comment.