entity_test.cxx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "game/engine/entity.hpp"
  9. #include <sstream>
  10. #include <json/json.h>
  11. #include <testing/xcode_gtest_helper.h>
  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) { return !(lhs == rhs); }
  28. }}
  29. inline Json::Value to_json(std::string const & str) {
  30. Json::Value json;
  31. std::stringstream(str) >> json;
  32. return json;
  33. }
  34. TEST(CollidableTest, ConstructsUsingGraphics) {
  35. math::dim2::rectangle bounds{{0.f, 0.f}, {1.f, 1.f}};
  36. graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
  37. collidable collide{obj};
  38. EXPECT_THAT(collide.render_info().location, Eq(obj.location));
  39. }
  40. std::string const data = R"(
  41. {
  42. "velocity": [ 1.0, 2.0 ],
  43. "size": 1.0
  44. }
  45. )";
  46. TEST(EntityTest, ConstructsFromJson) {
  47. math::dim2::rectangle bounds{{0.f, 0.f}, {1.f, 1.f}};
  48. graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
  49. entity ent{to_json(data), obj};
  50. EXPECT_THAT(ent.render_info().location, Eq(obj.location));
  51. }
  52. TEST(EntityTest, SizeParamAltersLocation) {
  53. math::dim2::rectangle bounds{{0.f, 0.f}, {1.f, 1.f}};
  54. graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
  55. Json::Value json = to_json(data);
  56. json["size"] = 2.f;
  57. entity ent{json, obj};
  58. math::dim2::rectangle expected{{0.f, 0.f}, {2.f, 2.f}};
  59. EXPECT_THAT(ent.render_info().location, Ne(obj.location));
  60. EXPECT_THAT(ent.render_info().location, Eq(expected));
  61. }
  62. TEST(EntityTest, MoveWillAdjustPointsAndBounds) {
  63. math::dim2::rectangle bounds{{0.f, 0.f}, {1.f, 1.f}};
  64. graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
  65. entity ent{to_json(data), obj};
  66. ent.update(1.f);
  67. math::dim2::rectangle expected{{1.f, 2.f}, {1.f, 1.f}};
  68. EXPECT_THAT(ent.render_info().location, Eq(expected));
  69. EXPECT_THAT(ent.render_info().points, Eq(math::dim2::quad(expected)));
  70. }
  71. TEST(EntityTest, MoveIsAFunctionOfVelocity) {
  72. math::dim2::rectangle bounds{{0.f, 0.f}, {1.f, 1.f}};
  73. graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
  74. entity ent{to_json(data), obj};
  75. ent.update(0.5f);
  76. math::dim2::rectangle expected{{0.5f, 1.f}, {1.f, 1.f}};
  77. EXPECT_THAT(ent.render_info().location, Eq(expected));
  78. EXPECT_THAT(ent.render_info().points, Eq(math::dim2::quad(expected)));
  79. }
  80. // TODO: Test Acceleration and Angular-Velocity