|
|
@@ -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)));
|
|
|
+}
|