浏览代码

Add tests for the entity class in engine.

Sam Jaffe 6 年之前
父节点
当前提交
4ae097900e
共有 2 个文件被更改,包括 109 次插入0 次删除
  1. 4 0
      engine/engine.xcodeproj/project.pbxproj
  2. 105 0
      engine/test/entity_test.cxx

+ 4 - 0
engine/engine.xcodeproj/project.pbxproj

@@ -11,6 +11,7 @@
 		CD1C8419229A095600825C4E /* text_engine.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1C8417229A095600825C4E /* text_engine.cxx */; };
 		CD39A88F22F521DB00FEC384 /* libjsoncpp.1.9.0.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD39A88D22F521A200FEC384 /* libjsoncpp.1.9.0.dylib */; };
 		CD39A89022F5225500FEC384 /* libjsoncpp.1.9.0.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD39A88D22F521A200FEC384 /* libjsoncpp.1.9.0.dylib */; };
+		CD39A89222F6052D00FEC384 /* entity_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD39A89122F6052D00FEC384 /* entity_test.cxx */; };
 		CD62D8462251A94C0023219A /* libgraphics.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD62D8452251A94C0023219A /* libgraphics.dylib */; };
 		CD62D8482251A9500023219A /* libmath.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD62D8472251A9500023219A /* libmath.dylib */; };
 		CD62FCCE22904A8900376440 /* libengine.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CDB1F8AE1D7A30CD00700C6B /* libengine.dylib */; };
@@ -81,6 +82,7 @@
 		CD1C83542298B55F00825C4E /* fps_counter.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = fps_counter.cxx; sourceTree = "<group>"; };
 		CD1C8417229A095600825C4E /* text_engine.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = text_engine.cxx; sourceTree = "<group>"; };
 		CD39A88D22F521A200FEC384 /* libjsoncpp.1.9.0.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libjsoncpp.1.9.0.dylib; path = ../../../../../../../opt/local/lib/libjsoncpp.1.9.0.dylib; sourceTree = "<group>"; };
+		CD39A89122F6052D00FEC384 /* entity_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = entity_test.cxx; sourceTree = "<group>"; };
 		CD62D8452251A94C0023219A /* libgraphics.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; path = libgraphics.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
 		CD62D8472251A9500023219A /* libmath.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; path = libmath.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
 		CD62FCB622904A7B00376440 /* GoogleMock.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = GoogleMock.xcodeproj; path = "../../gmock-xcode-master/GoogleMock.xcodeproj"; sourceTree = "<group>"; };
@@ -172,6 +174,7 @@
 				CD8064FC22D9456100B9B4E4 /* scene_test.cxx */,
 				CD8064F522D228D300B9B4E4 /* text_engine_test.cxx */,
 				CD8064FA22D69CAE00B9B4E4 /* game_dispatch_test.cxx */,
+				CD39A89122F6052D00FEC384 /* entity_test.cxx */,
 				CD8064F822D2984400B9B4E4 /* fps_counter_test.cxx */,
 			);
 			path = test;
@@ -378,6 +381,7 @@
 				CD8064FD22D9456100B9B4E4 /* scene_test.cxx in Sources */,
 				CD8064F622D228D300B9B4E4 /* text_engine_test.cxx in Sources */,
 				CDED9C5122A4114F00AE5CE5 /* serial_test.cxx in Sources */,
+				CD39A89222F6052D00FEC384 /* entity_test.cxx in Sources */,
 				CD8064FB22D69CAE00B9B4E4 /* game_dispatch_test.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;

+ 105 - 0
engine/test/entity_test.cxx

@@ -0,0 +1,105 @@
+//
+//  entity_test.cxx
+//  engine-test
+//
+//  Created by Sam Jaffe on 8/3/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#include <sstream>
+
+#include <gmock/gmock.h>
+#include <json/json.h>
+
+#include "game/engine/entity.hpp"
+
+#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{make_vector(0.f, 0.f), make_vector(1.f, 1.f)};
+  graphics::object obj{bounds, bounds, cast<graphics::material>(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{make_vector(0.f, 0.f), make_vector(1.f, 1.f)};
+  graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
+  
+  entity ent{to_json(data), obj};
+  EXPECT_THAT(ent.render_info().location, Eq(obj.location));
+}
+
+
+TEST(EntityTest, SizeParamAltersLocation) {
+  math::dim2::rectangle bounds{make_vector(0.f, 0.f), make_vector(1.f, 1.f)};
+  graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
+  
+  Json::Value json = to_json(data);
+  json["size"] = 2.f;
+  entity ent{json, obj};
+  math::dim2::rectangle expected{make_vector(0.f, 0.f), make_vector(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{make_vector(0.f, 0.f), make_vector(1.f, 1.f)};
+  graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
+  
+  entity ent{to_json(data), obj};
+  ent.update(1.f);
+  
+  math::dim2::rectangle expected{make_vector(1.f, 2.f), make_vector(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{make_vector(0.f, 0.f), make_vector(1.f, 1.f)};
+  graphics::object obj{bounds, bounds, cast<graphics::material>(1), bounds};
+  
+  entity ent{to_json(data), obj};
+  ent.update(0.5f);
+  
+  math::dim2::rectangle expected{make_vector(0.5f, 1.f), make_vector(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