Browse Source

test: add test to condition.cxx

Sam Jaffe 2 years ago
parent
commit
f3927a3c25
4 changed files with 137 additions and 2 deletions
  1. 17 0
      engine.xcodeproj/project.pbxproj
  2. 4 1
      src/condition.cxx
  3. 115 0
      test/condition_test.cxx
  4. 1 1
      test/config_test.cxx

+ 17 - 0
engine.xcodeproj/project.pbxproj

@@ -29,6 +29,7 @@
 		CDD4760129C4D93E00BDB829 /* engine_test_xc.mm in Sources */ = {isa = PBXBuildFile; fileRef = CDD4760029C4D93E00BDB829 /* engine_test_xc.mm */; };
 		CDD4760229C4D99C00BDB829 /* libshared_random_generator.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CDD475A429C4CDB500BDB829 /* libshared_random_generator.dylib */; };
 		CDD4760329C4D9AE00BDB829 /* GoogleMock.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CDD475B929C4CDBF00BDB829 /* GoogleMock.framework */; };
+		CDD4760829C4FE1900BDB829 /* condition_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDD4760729C4FE1900BDB829 /* condition_test.cxx */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -95,6 +96,13 @@
 			remoteGlobalIDString = CD592BDA29C2A399009AC14E;
 			remoteInfo = engine;
 		};
+		CDD4760929C4FFC100BDB829 /* PBXContainerItemProxy */ = {
+			isa = PBXContainerItemProxy;
+			containerPortal = CDD4759D29C4CDB500BDB829 /* shared_random_generator.xcodeproj */;
+			proxyType = 1;
+			remoteGlobalIDString = CDED6A4121B2F5A700AB91D0;
+			remoteInfo = shared_random_generator;
+		};
 /* End PBXContainerItemProxy section */
 
 /* Begin PBXFileReference section */
@@ -124,6 +132,7 @@
 		CDD475FD29C4D8C400BDB829 /* config_test */ = {isa = PBXFileReference; lastKnownFileType = folder; path = config_test; sourceTree = "<group>"; };
 		CDD4760029C4D93E00BDB829 /* engine_test_xc.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = engine_test_xc.mm; sourceTree = "<group>"; };
 		CDD4760629C4DB2400BDB829 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
+		CDD4760729C4FE1900BDB829 /* condition_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = condition_test.cxx; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -188,6 +197,7 @@
 				CDD475E029C4D49600BDB829 /* resources */,
 				CDD475DD29C4D36700BDB829 /* stub_config.h */,
 				CDD475DA29C4D35400BDB829 /* xcode_gtest_helper.h */,
+				CDD4760729C4FE1900BDB829 /* condition_test.cxx */,
 				CDD475DB29C4D35F00BDB829 /* config_test.cxx */,
 				CDD475DE29C4D38D00BDB829 /* universe_test.cxx */,
 			);
@@ -323,6 +333,7 @@
 			buildRules = (
 			);
 			dependencies = (
+				CDD4760A29C4FFC100BDB829 /* PBXTargetDependency */,
 				CDD475CA29C4D2BF00BDB829 /* PBXTargetDependency */,
 			);
 			name = "engine-test";
@@ -468,6 +479,7 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CDD4760829C4FE1900BDB829 /* condition_test.cxx in Sources */,
 				CDD475DF29C4D38D00BDB829 /* universe_test.cxx in Sources */,
 				CDD475DC29C4D35F00BDB829 /* config_test.cxx in Sources */,
 				CDD4760129C4D93E00BDB829 /* engine_test_xc.mm in Sources */,
@@ -482,6 +494,11 @@
 			target = CD592BDA29C2A399009AC14E /* engine */;
 			targetProxy = CDD475C929C4D2BF00BDB829 /* PBXContainerItemProxy */;
 		};
+		CDD4760A29C4FFC100BDB829 /* PBXTargetDependency */ = {
+			isa = PBXTargetDependency;
+			name = shared_random_generator;
+			targetProxy = CDD4760929C4FFC100BDB829 /* PBXContainerItemProxy */;
+		};
 /* End PBXTargetDependency section */
 
 /* Begin XCBuildConfiguration section */

+ 4 - 1
src/condition.cxx

@@ -23,7 +23,10 @@
 namespace engine {
 
 Condition::Condition(std::string event_type, std::vector<Predicate> predicates)
-    : event_type_(std::move(event_type)), predicates_(std::move(predicates)) {}
+    : event_type_(std::move(event_type)), predicates_(std::move(predicates)) {
+  expects(!predicates_.empty(),
+          "must provide at least one predicate in condition");
+}
 
 bool Condition::operator()(Event const & on) const {
   return on.type() == event_type_ &&

+ 115 - 0
test/condition_test.cxx

@@ -0,0 +1,115 @@
+//
+//  event_test.cpp
+//  engine-test
+//
+//  Created by Sam Jaffe on 3/17/23.
+//
+
+#include <engine/condition.h>
+
+#include <boost/preprocessor/seq/for_each.hpp>
+#include <boost/preprocessor/variadic/to_seq.hpp>
+#include <magic_enum.hpp>
+
+#include <engine/event.h>
+#include <reflection/context.h>
+#include <reflection/object.h>
+
+#include "xcode_gtest_helper.h"
+
+using engine::Condition;
+using engine::Event;
+using reflection::Property;
+
+using Cmp = engine::Condition::Comparator;
+using namespace magic_enum::bitwise_operators;
+
+auto make_predicate = [](auto... args) {
+  return engine::Condition::make_predicate(args...);
+};
+
+#define TO_ARG(r, data, elem) reflect(elem),
+#define TO_ARGS(...)                                                           \
+  BOOST_PP_SEQ_FOR_EACH(TO_ARG, _, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
+
+#define event(type, ...)                                                       \
+  engine::Event(type, *this, reflection::Context{TO_ARGS(__VA_ARGS__)})
+
+TEST(PredicateTest, RejectsOmniComparator) {
+  auto omni = Cmp::EQUAL | Cmp::LESS | Cmp::GREATER;
+  EXPECT_ANY_THROW(make_predicate(Property(), omni, 0));
+}
+
+TEST(PredicateTest, BuildsEquals) {
+  EXPECT_NO_THROW(make_predicate(Property(), Cmp::EQUAL, 0));
+}
+
+TEST(PredicateTest, BuildsNotEquals) {
+  EXPECT_NO_THROW(make_predicate(Property(), Cmp::NOT_EQUAL, 0));
+}
+
+TEST(PredicateTest, BuildsLess) {
+  EXPECT_NO_THROW(make_predicate(Property(), Cmp::LESS, 0));
+}
+
+TEST(PredicateTest, BuildsGreater) {
+  EXPECT_NO_THROW(make_predicate(Property(), Cmp::GREATER, 0));
+}
+
+TEST(PredicateTest, BuildsLessAndGreater) {
+  EXPECT_NO_THROW(make_predicate(Property(), Cmp::LESS | Cmp::GREATER, 0));
+}
+
+TEST(PredicateTest, ThrowsOnUnknownProperty) {
+  auto pred = make_predicate(Property("count"), Cmp::EQUAL, 0);
+  int value = 0;
+  EXPECT_ANY_THROW(pred(event("test", value)));
+}
+
+TEST(PredicateTest, TestsEqualityOnEvent) {
+  auto pred = make_predicate(Property("value"), Cmp::EQUAL, 0);
+  int value = 0;
+  EXPECT_TRUE(pred(event("test", value)));
+  value = -1;
+  EXPECT_FALSE(pred(event("test", value)));
+}
+
+TEST(PredicateTest, TestsLessOnEvent) {
+  auto pred = make_predicate(Property("value"), Cmp::LESS, 0);
+  int value = -1;
+  EXPECT_TRUE(pred(event("test", value)));
+  value = 0;
+  EXPECT_FALSE(pred(event("test", value)));
+}
+
+TEST(PredicateTest, TestsGreaterOnEvent) {
+  auto pred = make_predicate(Property("value"), Cmp::GREATER, 0);
+  int value = +1;
+  EXPECT_TRUE(pred(event("test", value)));
+  value = 0;
+  EXPECT_FALSE(pred(event("test", value)));
+}
+
+TEST(ConditionTest, RequiresAtLeastOnePredicate) {
+  EXPECT_ANY_THROW(Condition("test", {}));
+  EXPECT_NO_THROW(
+      Condition("test", {make_predicate(Property(), Cmp::LESS, 0)}));
+}
+
+TEST(ConditionTest, RejectsDifferentEventId) {
+  Condition cond("real", {make_predicate(Property("value"), Cmp::EQUAL, 0)});
+  int value = 0;
+  EXPECT_FALSE(cond(event("test", value)));
+  EXPECT_TRUE(cond(event("real", value)));
+}
+
+TEST(ConditionTest, RejectsAnyPredicateFails) {
+  Condition cond("test", {make_predicate(Property("value"), Cmp::EQUAL, 0),
+                          make_predicate(Property("index"), Cmp::GREATER, 0)});
+  int value = 0;
+  int index = 0;
+  EXPECT_FALSE(cond(event("test", value, index)));
+
+  index = 1;
+  EXPECT_TRUE(cond(event("test", value, index)));
+}

+ 1 - 1
test/config_test.cxx

@@ -6,10 +6,10 @@
 //  Copyright © 2018 Sam Jaffe. All rights reserved.
 //
 
-#include "xcode_gtest_helper.h"
 #include <json/json.h>
 
 #include "stub_config.h"
+#include "xcode_gtest_helper.h"
 
 using testing::Eq;