diff options
author | crupest <crupest@outlook.com> | 2024-01-01 22:20:21 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2024-01-19 22:00:11 +0800 |
commit | e96f5b6713814dd59814f17cb9bb437e3a6fca47 (patch) | |
tree | 67491f210a0ee863ebcc4c7e38afcdac7f93783e /test/common/Event2Test.cpp | |
parent | df799bf394fa63dc9f39a5b09fe21c16b1cc8a57 (diff) | |
download | cru-e96f5b6713814dd59814f17cb9bb437e3a6fca47.tar.gz cru-e96f5b6713814dd59814f17cb9bb437e3a6fca47.tar.bz2 cru-e96f5b6713814dd59814f17cb9bb437e3a6fca47.zip |
NEED TEST: fix compile errors of EventHandlerToken and add revoke test.
Diffstat (limited to 'test/common/Event2Test.cpp')
-rw-r--r-- | test/common/Event2Test.cpp | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/test/common/Event2Test.cpp b/test/common/Event2Test.cpp index f63a5347..e14174cf 100644 --- a/test/common/Event2Test.cpp +++ b/test/common/Event2Test.cpp @@ -4,16 +4,39 @@ using cru::Event2; -TEST_CASE("Event2 handlers should work.", "[event2]") { +TEST_CASE("Event2", "[event2]") { Event2 event; int counter = 0; auto handler = [&counter] { counter++; }; - - event.AddHandler(handler); - - event.Raise(); - - REQUIRE(counter == 1); + 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); + } } |