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 | |
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.
-rw-r--r-- | include/cru/common/Event2.h | 2 | ||||
-rw-r--r-- | test/common/Event2Test.cpp | 37 |
2 files changed, 31 insertions, 8 deletions
diff --git a/include/cru/common/Event2.h b/include/cru/common/Event2.h index 7d7bda82..5677828a 100644 --- a/include/cru/common/Event2.h +++ b/include/cru/common/Event2.h @@ -188,7 +188,7 @@ template <typename TEvent2> void EventHandlerToken<TEvent2>::RevokeHandler() const { auto event = this->event_resolver_.Resolve(); if (event) { - event->RevokeHandler(this->token_value); + event->RevokeHandler(this->token_value_); } } } // namespace cru 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); + } } |