// // entity_test.cxx // engine-test // // Created by Sam Jaffe on 8/3/19. // Copyright © 2019 Sam Jaffe. All rights reserved. // #include "game/engine/entity.hpp" #include #include #include #include "mock_renderer.h" using namespace engine; using testing::Eq; using testing::Ne; namespace math { namespace dim2 { bool operator==(rectangle const & lhs, rectangle const & rhs) { return lhs.origin == rhs.origin && lhs.size == rhs.size; } bool operator!=(rectangle const & lhs, rectangle const & rhs) { return !(lhs == rhs); } bool operator==(quad const & lhs, quad const & rhs) { return lhs.ll == rhs.ll && lhs.lr == rhs.lr && lhs.ul == rhs.ul && lhs.ur == rhs.ur; } bool operator!=(quad const & lhs, quad const & rhs) { return !(lhs == rhs); } }} inline Json::Value to_json(std::string const & str) { Json::Value json; std::stringstream(str) >> json; return json; } TEST(CollidableTest, ConstructsUsingGraphics) { math::dim2::rectangle bounds{{0.f, 0.f}, {1.f, 1.f}}; graphics::object obj{bounds, bounds, cast(1), bounds}; collidable collide{obj}; EXPECT_THAT(collide.render_info().location, Eq(obj.location)); } std::string const data = R"( { "velocity": [ 1.0, 2.0 ], "size": 1.0 } )"; TEST(EntityTest, ConstructsFromJson) { math::dim2::rectangle bounds{{0.f, 0.f}, {1.f, 1.f}}; graphics::object obj{bounds, bounds, cast(1), bounds}; entity ent{to_json(data), obj}; EXPECT_THAT(ent.render_info().location, Eq(obj.location)); } TEST(EntityTest, SizeParamAltersLocation) { math::dim2::rectangle bounds{{0.f, 0.f}, {1.f, 1.f}}; graphics::object obj{bounds, bounds, cast(1), bounds}; Json::Value json = to_json(data); json["size"] = 2.f; entity ent{json, obj}; math::dim2::rectangle expected{{0.f, 0.f}, {2.f, 2.f}}; EXPECT_THAT(ent.render_info().location, Ne(obj.location)); EXPECT_THAT(ent.render_info().location, Eq(expected)); } TEST(EntityTest, MoveWillAdjustPointsAndBounds) { math::dim2::rectangle bounds{{0.f, 0.f}, {1.f, 1.f}}; graphics::object obj{bounds, bounds, cast(1), bounds}; entity ent{to_json(data), obj}; ent.update(1.f); math::dim2::rectangle expected{{1.f, 2.f}, {1.f, 1.f}}; EXPECT_THAT(ent.render_info().location, Eq(expected)); EXPECT_THAT(ent.render_info().points, Eq(math::dim2::quad(expected))); } TEST(EntityTest, MoveIsAFunctionOfVelocity) { math::dim2::rectangle bounds{{0.f, 0.f}, {1.f, 1.f}}; graphics::object obj{bounds, bounds, cast(1), bounds}; entity ent{to_json(data), obj}; ent.update(0.5f); math::dim2::rectangle expected{{0.5f, 1.f}, {1.f, 1.f}}; EXPECT_THAT(ent.render_info().location, Eq(expected)); EXPECT_THAT(ent.render_info().points, Eq(math::dim2::quad(expected))); } // TODO: Test Acceleration and Angular-Velocity