From 7b81d567945d1002d60d50cf13553d9ee3025dad Mon Sep 17 00:00:00 2001 From: Dawid Drozd Date: Sat, 5 Aug 2017 12:15:33 +0200 Subject: [PATCH] Add some unit tests --- CMakeLists.txt | 4 ++ test/CMakeLists.txt | 10 ++++ test/eventbus/EventCollectorTest.cpp | 70 ++++++++++++++++++++++++++++ test/eventbus/NotifierTest.cpp | 56 ++++++++++++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 test/CMakeLists.txt create mode 100644 test/eventbus/EventCollectorTest.cpp create mode 100644 test/eventbus/NotifierTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 245351e..b066474 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,3 +40,7 @@ INSTALL(EXPORT EventBusConfig NAMESPACE Dexode:: ) EXPORT(TARGETS EventBus FILE EventBusConfig.cmake) + + +ENABLE_TESTING() +ADD_SUBDIRECTORY(test/) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..612a72e --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,10 @@ +# http://www.levelofindirection.com/journal/2010/12/28/unit-testing-in-c-and-objective-c-just-got-easier.html +# Thanks for CATCH! + +ADD_EXECUTABLE(EventBusTest + eventbus/EventCollectorTest.cpp + eventbus/NotifierTest.cpp + ) + +TARGET_INCLUDE_DIRECTORIES(EventBusTest PRIVATE Catch/single_include/) +TARGET_LINK_LIBRARIES(EventBusTest PUBLIC Dexode::EventBus) diff --git a/test/eventbus/EventCollectorTest.cpp b/test/eventbus/EventCollectorTest.cpp new file mode 100644 index 0000000..f78407b --- /dev/null +++ b/test/eventbus/EventCollectorTest.cpp @@ -0,0 +1,70 @@ +// +// Created by Dawid Drozd aka Gelldur on 05.08.17. +// + +#include + +#include + +namespace +{ + +MAKE_NOTIFICATION(SimpleNotification, int); + +MAKE_NOTIFICATION(RefNotification, int &); + +} + +TEST_CASE("eventbus/EventCollector sample", "Simple test for EventCollector") +{ + Dexode::Notifier bus; + int callCount = 0; + { + Dexode::EventCollector listener{bus}; + listener.listen(getNotificationSimpleNotification(), [&](int value) + { + REQUIRE(value == 3); + ++callCount; + }); + bus.notify(getNotificationSimpleNotification(), 3); + REQUIRE(callCount == 1); + } + bus.notify(getNotificationSimpleNotification(), 2); + REQUIRE(callCount == 1); +} + +TEST_CASE("eventbus/EventCollector unlistenAll", "EventCollector::unlistenAll") +{ + Dexode::Notifier bus; + Dexode::EventCollector listener{bus}; + + int callCount = 0; + listener.listen(getNotificationSimpleNotification(), [&](int value) + { + REQUIRE(value == 3); + ++callCount; + }); + bus.notify(getNotificationSimpleNotification(), 3); + listener.unlistenAll(); + + bus.notify(getNotificationSimpleNotification(), 2); + REQUIRE(callCount == 1); +} + +TEST_CASE("eventbus/EventCollector reset", "EventCollector reset when we reasign") +{ + Dexode::Notifier bus; + int callCount = 0; + Dexode::EventCollector listener{bus}; + listener.listen(getNotificationSimpleNotification(), [&](int value) + { + REQUIRE(value == 3); + ++callCount; + }); + bus.notify(getNotificationSimpleNotification(), 3); + REQUIRE(callCount == 1); + listener = {}; + + bus.notify(getNotificationSimpleNotification(), 2); + REQUIRE(callCount == 1); +} diff --git a/test/eventbus/NotifierTest.cpp b/test/eventbus/NotifierTest.cpp new file mode 100644 index 0000000..7a46128 --- /dev/null +++ b/test/eventbus/NotifierTest.cpp @@ -0,0 +1,56 @@ +// +// Created by Dawid Drozd aka Gelldur on 05.08.17. +// + +#define CATCH_CONFIG_MAIN +#include + +#include + +namespace +{ + +MAKE_NOTIFICATION(SimpleNotification, int); + +MAKE_NOTIFICATION(RefNotification, int &); + +} + +TEST_CASE("eventbus/Simple test", "Simple test") +{ + Dexode::Notifier bus; + const auto token = bus.listen(getNotificationSimpleNotification(), [](int value) + { + REQUIRE(value == 3); + }); + + bus.notify(getNotificationSimpleNotification(), 3); + bus.unlistenAll(token); + bus.notify(getNotificationSimpleNotification(), 2); + + bus.listen(getNotificationSimpleNotification(), [](int value) + { + REQUIRE(value == 1); + }); + bus.notify(getNotificationSimpleNotification(), 1); +} + +TEST_CASE("eventbus/Multiple listen on same token", "Listening on the same token") +{ + Dexode::Notifier bus; + const auto token = bus.listen(getNotificationRefNotification(), [](int& value) + { + REQUIRE(value == 3); + --value; + }); + bus.listen(token, getNotificationRefNotification(), [](int& value) + { + REQUIRE(value == 2); + }); + + int value = 3; + bus.notify(getNotificationRefNotification(), value); + + bus.unlistenAll(token); + bus.notify(getNotificationSimpleNotification(), 2); +}