浏览代码

Begin testing engine code with basic serialization functions.

Sam Jaffe 6 年之前
父节点
当前提交
066a10fdfa

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

@@ -20,6 +20,8 @@
 		CDB1F8C81D7A312B00700C6B /* game_dispatch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDB1F8C61D7A312B00700C6B /* game_dispatch.cpp */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
 		CDB1F8CC1D7A319A00700C6B /* scene.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDB1F8CA1D7A319A00700C6B /* scene.cpp */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
 		CDB1F8D21D7A32A300700C6B /* events.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDB1F8D01D7A32A300700C6B /* events.cpp */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
+		CDED9C5122A4114F00AE5CE5 /* serial_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDED9C5022A4114F00AE5CE5 /* serial_test.cxx */; };
+		CDED9C5222A4130700AE5CE5 /* libjsoncpp.1.8.4.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD62FD3922936E9C00376440 /* libjsoncpp.1.8.4.dylib */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -84,6 +86,8 @@
 		CDB1F8C61D7A312B00700C6B /* game_dispatch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = game_dispatch.cpp; sourceTree = "<group>"; };
 		CDB1F8CA1D7A319A00700C6B /* scene.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = scene.cpp; sourceTree = "<group>"; };
 		CDB1F8D01D7A32A300700C6B /* events.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = events.cpp; sourceTree = "<group>"; };
+		CDED9C4C22A4112200AE5CE5 /* mock_renderer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = mock_renderer.h; sourceTree = "<group>"; };
+		CDED9C5022A4114F00AE5CE5 /* serial_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = serial_test.cxx; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -91,6 +95,7 @@
 			isa = PBXFrameworksBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CDED9C5222A4130700AE5CE5 /* libjsoncpp.1.8.4.dylib in Frameworks */,
 				CD62FCD622904A9B00376440 /* GoogleMock.framework in Frameworks */,
 				CD62FCCE22904A8900376440 /* libengine.dylib in Frameworks */,
 			);
@@ -143,6 +148,8 @@
 		CDA34D8522515CA9008036A7 /* test */ = {
 			isa = PBXGroup;
 			children = (
+				CDED9C4C22A4112200AE5CE5 /* mock_renderer.h */,
+				CDED9C5022A4114F00AE5CE5 /* serial_test.cxx */,
 			);
 			path = test;
 			sourceTree = "<group>";
@@ -342,6 +349,7 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CDED9C5122A4114F00AE5CE5 /* serial_test.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 67 - 0
engine/engine.xcodeproj/xcshareddata/xcschemes/engine-test.xcscheme

@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Scheme
+   LastUpgradeVersion = "1010"
+   version = "1.3">
+   <BuildAction
+      parallelizeBuildables = "YES"
+      buildImplicitDependencies = "YES">
+   </BuildAction>
+   <TestAction
+      buildConfiguration = "Debug"
+      selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
+      selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
+      codeCoverageEnabled = "YES"
+      onlyGenerateCoverageForSpecifiedTargets = "YES"
+      shouldUseLaunchSchemeArgsEnv = "YES">
+      <CodeCoverageTargets>
+         <BuildableReference
+            BuildableIdentifier = "primary"
+            BlueprintIdentifier = "CDB1F8AD1D7A30CD00700C6B"
+            BuildableName = "libengine.dylib"
+            BlueprintName = "engine"
+            ReferencedContainer = "container:engine.xcodeproj">
+         </BuildableReference>
+      </CodeCoverageTargets>
+      <Testables>
+         <TestableReference
+            skipped = "NO">
+            <BuildableReference
+               BuildableIdentifier = "primary"
+               BlueprintIdentifier = "CD62FCC822904A8900376440"
+               BuildableName = "engine-test.xctest"
+               BlueprintName = "engine-test"
+               ReferencedContainer = "container:engine.xcodeproj">
+            </BuildableReference>
+         </TestableReference>
+      </Testables>
+      <AdditionalOptions>
+      </AdditionalOptions>
+   </TestAction>
+   <LaunchAction
+      buildConfiguration = "Debug"
+      selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
+      selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
+      launchStyle = "0"
+      useCustomWorkingDirectory = "NO"
+      ignoresPersistentStateOnLaunch = "NO"
+      debugDocumentVersioning = "YES"
+      debugServiceExtension = "internal"
+      allowLocationSimulation = "YES">
+      <AdditionalOptions>
+      </AdditionalOptions>
+   </LaunchAction>
+   <ProfileAction
+      buildConfiguration = "Release"
+      shouldUseLaunchSchemeArgsEnv = "YES"
+      savedToolIdentifier = ""
+      useCustomWorkingDirectory = "NO"
+      debugDocumentVersioning = "YES">
+   </ProfileAction>
+   <AnalyzeAction
+      buildConfiguration = "Debug">
+   </AnalyzeAction>
+   <ArchiveAction
+      buildConfiguration = "Release"
+      revealArchiveInOrganizer = "YES">
+   </ArchiveAction>
+</Scheme>

+ 16 - 0
engine/test/mock_renderer.h

@@ -0,0 +1,16 @@
+//
+//  mock_renderer.h
+//  engine
+//
+//  Created by Sam Jaffe on 6/2/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#pragma once
+
+#include "game/graphics/renderer.hpp"
+#include "game/util/identity.hpp"
+
+template <typename T> inline identity<T> cast(unsigned int id) {
+  return *reinterpret_cast<identity<T> *>(&id);
+}

+ 69 - 0
engine/test/serial_test.cxx

@@ -0,0 +1,69 @@
+//
+//  serial_test.cxx
+//  engine-test
+//
+//  Created by Sam Jaffe on 6/2/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#include <sstream>
+
+#include "mock_renderer.h"
+#include <gmock/gmock.h>
+
+#include <json/json.h>
+#include <json/reader.h>
+
+#include "game/engine/serial.hpp"
+
+Json::Value to_json(std::string const & str) {
+  Json::Value json;
+  std::stringstream(str) >> json;
+  return json;
+}
+
+struct SerialTest : testing::Test {};
+
+using testing::Eq;
+
+TEST_F(SerialTest, ReadsIntegerVector) {
+  math::vec2i const expected{{1, 5}};
+  std::string const data = "[1,5]";
+  EXPECT_THAT(engine::to_vec2i(to_json(data)), Eq(expected));
+}
+
+TEST_F(SerialTest, ReadsIntegerVectorFromFloats) {
+  math::vec2i const expected{{1, 5}};
+  std::string const data = "[1.5,5.01]";
+  EXPECT_THAT(engine::to_vec2i(to_json(data)), Eq(expected));
+}
+
+TEST_F(SerialTest, ReadsFloatVector) {
+  math::vec2 const expected{{1.5, 5.01}};
+  std::string const data = "[1.5,5.01]";
+  EXPECT_THAT(engine::to_vec2(to_json(data)), Eq(expected));
+}
+
+TEST_F(SerialTest, ReadsFloatVectorFromInts) {
+  math::vec2 const expected{{1, 5}};
+  std::string const data = "[1,5]";
+  EXPECT_THAT(engine::to_vec2(to_json(data)), Eq(expected));
+}
+
+TEST_F(SerialTest, MissingFloatDataIsZero) {
+  math::vec2 const expected{{1, 0}};
+  std::string const data = "[1]";
+  EXPECT_THAT(engine::to_vec2(to_json(data)), Eq(expected));
+}
+
+TEST_F(SerialTest, FallbackReaderDoesntInvokeIfArray) {
+  math::vec2 const expected{{0, 0}};
+  std::string const data = "[]";
+  EXPECT_THAT(engine::to_vec2(to_json(data), {{1, 5}}), Eq(expected));
+}
+
+TEST_F(SerialTest, FallbackReaderActivatesOnNull) {
+  math::vec2 const expected{{1, 5}};
+  std::string const data = "null";
+  EXPECT_THAT(engine::to_vec2(to_json(data), {{1, 5}}), Eq(expected));
+}