entity_test.cxx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // entity_test.cxx
  3. // engine-test
  4. //
  5. // Created by Sam Jaffe on 8/3/19.
  6. // Copyright © 2019 Sam Jaffe. All rights reserved.
  7. //
  8. #include <sstream>
  9. #include <gmock/gmock.h>
  10. #include <json/json.h>
  11. #include "game/engine/entity.hpp"
  12. #include "mock_renderer.h"
  13. using namespace engine;
  14. using testing::Eq;
  15. using testing::Ne;
  16. namespace math { namespace dim2 {
  17. bool operator==(rectangle const & lhs, rectangle const & rhs) {
  18. return lhs.origin == rhs.origin && lhs.size == rhs.size;
  19. }
  20. bool operator!=(rectangle const & lhs, rectangle const & rhs) {
  21. return !(lhs == rhs);
  22. }
  23. bool operator==(quad const & lhs, quad const & rhs) {
  24. return lhs.ll == rhs.ll && lhs.lr == rhs.lr && lhs.ul == rhs.ul
  25. && lhs.ur == rhs.ur;
  26. }
  27. bool operator!=(quad const & lhs, quad const & rhs) {
  28. return !(lhs == rhs);
  29. }
  30. }}
  31. inline Json::Value to_json(std::string const & str) {
  32. Json::Value json;
  33. std::stringstream(str) >> json;
  34. return json;
  35. }
  36. TEST(CollidableTest, ConstructsUsingGraphics) {
  37. math::dim2::rectangle bounds{make_vector(0.f, 0.f), make_vector(1.f, 1.f)};
  38. graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
  39. collidable collide{obj};
  40. EXPECT_THAT(collide.render_info().location, Eq(obj.location));
  41. }
  42. std::string const data = R"(
  43. {
  44. "velocity": [ 1.0, 2.0 ],
  45. "size": 1.0
  46. }
  47. )";
  48. TEST(EntityTest, ConstructsFromJson) {
  49. math::dim2::rectangle bounds{make_vector(0.f, 0.f), make_vector(1.f, 1.f)};
  50. graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
  51. entity ent{to_json(data), obj};
  52. EXPECT_THAT(ent.render_info().location, Eq(obj.location));
  53. }
  54. TEST(EntityTest, SizeParamAltersLocation) {
  55. math::dim2::rectangle bounds{make_vector(0.f, 0.f), make_vector(1.f, 1.f)};
  56. graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
  57. Json::Value json = to_json(data);
  58. json["size"] = 2.f;
  59. entity ent{json, obj};
  60. math::dim2::rectangle expected{make_vector(0.f, 0.f), make_vector(2.f, 2.f)};
  61. EXPECT_THAT(ent.render_info().location, Ne(obj.location));
  62. EXPECT_THAT(ent.render_info().location, Eq(expected));
  63. }
  64. TEST(EntityTest, MoveWillAdjustPointsAndBounds) {
  65. math::dim2::rectangle bounds{make_vector(0.f, 0.f), make_vector(1.f, 1.f)};
  66. graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
  67. entity ent{to_json(data), obj};
  68. ent.update(1.f);
  69. math::dim2::rectangle expected{make_vector(1.f, 2.f), make_vector(1.f, 1.f)};
  70. EXPECT_THAT(ent.render_info().location, Eq(expected));
  71. EXPECT_THAT(ent.render_info().points, Eq(math::dim2::quad(expected)));
  72. }
  73. TEST(EntityTest, MoveIsAFunctionOfVelocity) {
  74. math::dim2::rectangle bounds{make_vector(0.f, 0.f), make_vector(1.f, 1.f)};
  75. graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
  76. entity ent{to_json(data), obj};
  77. ent.update(0.5f);
  78. math::dim2::rectangle expected{make_vector(0.5f, 1.f), make_vector(1.f, 1.f)};
  79. EXPECT_THAT(ent.render_info().location, Eq(expected));
  80. EXPECT_THAT(ent.render_info().points, Eq(math::dim2::quad(expected)));
  81. }
  82. // TODO: Test Acceleration and Angular-Velocity