diff options
author | Yuqian Yang <crupest@crupest.life> | 2025-09-03 12:42:10 +0800 |
---|---|---|
committer | Yuqian Yang <crupest@crupest.life> | 2025-09-03 12:42:10 +0800 |
commit | efa1266f10e90c0c46f47cc06645422142cb2d9f (patch) | |
tree | 3d8cfefb81ce4645d150c08fc52ad646b6da80e2 /test/base/Event2Test.cpp | |
parent | 5e59a8e38c9f8992e6ffd9dbbde11e1f873780e1 (diff) | |
download | cru-efa1266f10e90c0c46f47cc06645422142cb2d9f.tar.gz cru-efa1266f10e90c0c46f47cc06645422142cb2d9f.tar.bz2 cru-efa1266f10e90c0c46f47cc06645422142cb2d9f.zip |
common -> base in test dir.
Diffstat (limited to 'test/base/Event2Test.cpp')
-rw-r--r-- | test/base/Event2Test.cpp | 54 |
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); + } +} |