aboutsummaryrefslogtreecommitdiff
path: root/test/base/Event2Test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test/base/Event2Test.cpp')
-rw-r--r--test/base/Event2Test.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/test/base/Event2Test.cpp b/test/base/Event2Test.cpp
new file mode 100644
index 00000000..0c67f28e
--- /dev/null
+++ b/test/base/Event2Test.cpp
@@ -0,0 +1,54 @@
+#include "cru/base/Event2.h"
+
+#include <catch2/catch_test_macros.hpp>
+
+using cru::Event2;
+
+TEST_CASE("Event2", "[event2]") {
+ Event2 event;
+
+ int counter = 0;
+
+ auto handler = [&counter] { counter++; };
+ auto token = event.AddHandler(handler);
+
+ auto handler2 = [&counter](decltype(event)::Context* context) { counter++; };
+
+ SECTION("two handlers should work.") {
+ event.Raise();
+ REQUIRE(counter == 1);
+ event.Raise();
+ REQUIRE(counter == 2);
+
+ event.AddHandler(handler2);
+ event.Raise();
+ REQUIRE(counter == 4);
+ }
+
+ SECTION("handler revoker should work.") {
+ token.RevokeHandler();
+ event.Raise();
+ REQUIRE(counter == 0);
+
+ token = event.AddHandler(handler);
+ event.AddHandler(handler2);
+ event.Raise();
+ REQUIRE(counter == 2);
+
+ token.RevokeHandler();
+ event.Raise();
+ REQUIRE(counter == 3);
+ }
+
+ SECTION("stop handling should work.") {
+ auto short_circuit_handler = [&counter](decltype(event)::Context* context) {
+ context->SetStopHandling();
+ };
+
+ event.AddHandler(short_circuit_handler);
+ event.AddHandler(handler2);
+
+ event.Raise();
+ REQUIRE(counter == 1);
+ }
+}