mirror of
https://github.com/gelldur/EventBus.git
synced 2024-12-27 12:21:02 +08:00
Add some unit tests
This commit is contained in:
parent
0012c21709
commit
7b81d56794
@ -40,3 +40,7 @@ INSTALL(EXPORT EventBusConfig
|
||||
NAMESPACE Dexode::
|
||||
)
|
||||
EXPORT(TARGETS EventBus FILE EventBusConfig.cmake)
|
||||
|
||||
|
||||
ENABLE_TESTING()
|
||||
ADD_SUBDIRECTORY(test/)
|
||||
|
10
test/CMakeLists.txt
Normal file
10
test/CMakeLists.txt
Normal file
@ -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)
|
70
test/eventbus/EventCollectorTest.cpp
Normal file
70
test/eventbus/EventCollectorTest.cpp
Normal file
@ -0,0 +1,70 @@
|
||||
//
|
||||
// Created by Dawid Drozd aka Gelldur on 05.08.17.
|
||||
//
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
#include <eventbus/EventCollector.h>
|
||||
|
||||
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);
|
||||
}
|
56
test/eventbus/NotifierTest.cpp
Normal file
56
test/eventbus/NotifierTest.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
//
|
||||
// Created by Dawid Drozd aka Gelldur on 05.08.17.
|
||||
//
|
||||
|
||||
#define CATCH_CONFIG_MAIN
|
||||
#include <catch.hpp>
|
||||
|
||||
#include <eventbus/Notifier.h>
|
||||
|
||||
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);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user