Skip to content

Commit

Permalink
Add object view test
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Jul 14, 2024
1 parent 59a0d9e commit e9c6d87
Show file tree
Hide file tree
Showing 2 changed files with 125 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 @@ -14,6 +14,7 @@ target_sources(tactile-core-test
"src/document/meta_view_impl_test.cpp"
"src/document/tile_view_impl_test.cpp"
"src/document/tileset_view_impl_test.cpp"
"src/document/object_view_impl_test.cpp"
"src/entity/registry_test.cpp"
"src/event/event_dispatcher_test.cpp"
"src/io/ini_test.cpp"
Expand Down
124 changes: 124 additions & 0 deletions source/core/test/src/document/object_view_impl_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0)

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

#include <gtest/gtest.h>

#include "tactile/core/document/layer_view_impl.hpp"
#include "tactile/core/document/map_document.hpp"
#include "tactile/core/document/tile_view_impl.hpp"
#include "tactile/core/document/tileset_view_impl.hpp"
#include "tactile/core/entity/registry.hpp"
#include "tactile/core/layer/object.hpp"
#include "tactile/core/layer/object_layer.hpp"
#include "tactile/core/map/map_spec.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 ObjectViewImplTest : public testing::Test
{
public:
ObjectViewImplTest()
: mMapSpec {kOrthogonalMapSpec},
mDocument {mMapSpec}
{}

protected:
MapSpec mMapSpec;
MapDocument mDocument;
};

// tactile::ObjectViewImpl::ObjectViewImpl
TEST_F(ObjectViewImplTest, Constructors)
{
const ILayerView* layer_view = nullptr;
const ITileView* tile_view = nullptr;

EXPECT_ANY_THROW(ObjectViewImpl(nullptr, layer_view, kInvalidEntity));
EXPECT_ANY_THROW(ObjectViewImpl(nullptr, tile_view, kInvalidEntity));

EXPECT_ANY_THROW(ObjectViewImpl(&mDocument, layer_view, kInvalidEntity));
EXPECT_ANY_THROW(ObjectViewImpl(&mDocument, tile_view, kInvalidEntity));
}

// tactile::ObjectViewImpl::get_parent_layer
// tactile::ObjectViewImpl::get_parent_tile
// tactile::ObjectViewImpl::get_type
// tactile::ObjectViewImpl::get_id
// tactile::ObjectViewImpl::get_position
// tactile::ObjectViewImpl::get_size
// tactile::ObjectViewImpl::get_tag
// tactile::ObjectViewImpl::is_visible
TEST_F(ObjectViewImplTest, ObjectInObjectLayer)
{
auto& registry = mDocument.get_registry();

const auto layer_id = make_object_layer(registry);
const auto object_id = make_object(registry, ObjectID {1}, ObjectType::kRect);

auto& object_layer = registry.get<CObjectLayer>(layer_id);
object_layer.objects.push_back(object_id);

auto& object = registry.get<CObject>(object_id);
object.position = Float2 {12, 34};
object.size = Float2 {56, 78};
object.tag = "tag";

const LayerViewImpl layer_view {&mDocument, nullptr, layer_id};
const ObjectViewImpl object_view {&mDocument, &layer_view, object_id};

EXPECT_EQ(object_view.get_parent_layer(), &layer_view);
EXPECT_EQ(object_view.get_parent_tile(), nullptr);
EXPECT_EQ(object_view.get_type(), object.type);
EXPECT_EQ(object_view.get_id(), object.id);
EXPECT_EQ(object_view.get_position(), object.position);
EXPECT_EQ(object_view.get_size(), object.size);
EXPECT_EQ(object_view.get_tag(), object.tag);
EXPECT_EQ(object_view.is_visible(), object.is_visible);
}

// tactile::ObjectViewImpl::get_parent_layer
// tactile::ObjectViewImpl::get_parent_tile
// tactile::ObjectViewImpl::get_type
// tactile::ObjectViewImpl::get_id
// tactile::ObjectViewImpl::get_position
// tactile::ObjectViewImpl::get_size
// tactile::ObjectViewImpl::get_tag
// tactile::ObjectViewImpl::is_visible
TEST_F(ObjectViewImplTest, ObjectInTile)
{
auto& registry = mDocument.get_registry();

const auto tileset_id = make_tileset(registry, kDummyTilesetSpec);
const auto& tileset = registry.get<CTileset>(tileset_id);

const auto tile_id = tileset.tiles.front();
const auto object_id = make_object(registry, ObjectID {1}, ObjectType::kRect);

auto& tile = registry.get<CTile>(tile_id);
tile.objects.push_back(object_id);

auto& object = registry.get<CObject>(object_id);
object.position = Float2 {-23, 15};
object.size = Float2 {10, 12};
object.tag = "foo";

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

EXPECT_EQ(object_view.get_parent_layer(), nullptr);
EXPECT_EQ(object_view.get_parent_tile(), &tile_view);
EXPECT_EQ(object_view.get_type(), object.type);
EXPECT_EQ(object_view.get_id(), object.id);
EXPECT_EQ(object_view.get_position(), object.position);
EXPECT_EQ(object_view.get_size(), object.size);
EXPECT_EQ(object_view.get_tag(), object.tag);
EXPECT_EQ(object_view.is_visible(), object.is_visible);
}

} // namespace tactile::test

0 comments on commit e9c6d87

Please sign in to comment.