From df799bf394fa63dc9f39a5b09fe21c16b1cc8a57 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 1 Jan 2024 22:20:21 +0800 Subject: NEED TEST: fix compile errors of Event2 and create init test. --- include/cru/common/Event2.h | 15 ++++++++------- test/common/CMakeLists.txt | 1 + test/common/Event2Test.cpp | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 test/common/Event2Test.cpp diff --git a/include/cru/common/Event2.h b/include/cru/common/Event2.h index 5c3a330a..7d7bda82 100644 --- a/include/cru/common/Event2.h +++ b/include/cru/common/Event2.h @@ -51,7 +51,8 @@ class EventHandlerToken { static_assert(is_event2_v, "TEvent2 must be Event2 class."); public: - EventHandlerToken(ObjectResolver event_resolver, int token_value); + EventHandlerToken(ObjectResolver event_resolver, int token_value) + : event_resolver_(std::move(event_resolver)), token_value_(token_value) {} ObjectResolver GetEventResolver() const { return event_resolver_; } int GetTokenValue() const { return token_value_; } @@ -82,17 +83,17 @@ class Event2 : public SelfResolvable> { public: using HandlerToken = EventHandlerToken; using Context = EventContext; - using Handler = std::function; + using Handler = std::function; using SpyOnlyHandler = std::function; template static std::enable_if_t, Handler> WrapAsHandler( TFunc&& handler) { - return Handler([h = std::forward(handler)](Context&) { h(); }); + return Handler([h = std::forward(handler)](Context*) { h(); }); } template - static std::enable_if_t, Handler> + static std::enable_if_t, Handler> WrapAsHandler(TFunc&& handler) { return Handler(std::forward(handler)); } @@ -138,19 +139,19 @@ class Event2 : public SelfResolvable> { TResult Raise() { Context context; - RunInContext(context); + RunInContext(&context); return context.TakeResult(); } TResult Raise(TArgument argument) { Context context(std::move(argument)); - RunInContext(context); + RunInContext(&context); return context.TakeResult(); } TResult Raise(TArgument argument, TResult result) { Context context(std::move(argument), std::move(result)); - RunInContext(context); + RunInContext(&context); return context.TakeResult(); } diff --git a/test/common/CMakeLists.txt b/test/common/CMakeLists.txt index 602af819..f2fda47d 100644 --- a/test/common/CMakeLists.txt +++ b/test/common/CMakeLists.txt @@ -1,4 +1,5 @@ add_executable(CruBaseTest + Event2Test.cpp HandlerRegistryTest.cpp PropertyTreeTest.cpp SelfResolvableTest.cpp diff --git a/test/common/Event2Test.cpp b/test/common/Event2Test.cpp new file mode 100644 index 00000000..f63a5347 --- /dev/null +++ b/test/common/Event2Test.cpp @@ -0,0 +1,19 @@ +#include "cru/common/Event2.h" + +#include + +using cru::Event2; + +TEST_CASE("Event2 handlers should work.", "[event2]") { + Event2 event; + + int counter = 0; + + auto handler = [&counter] { counter++; }; + + event.AddHandler(handler); + + event.Raise(); + + REQUIRE(counter == 1); +} -- cgit v1.2.3