Merge pull request #51 from cynnyx/master

more tests and functionalities
This commit is contained in:
Michele Caini 2016-08-19 15:16:39 +02:00 committed by GitHub
commit 6633147e31
4 changed files with 164 additions and 0 deletions

View File

@ -182,6 +182,20 @@ public:
}
}
/**
* @brief Checks if there are listeners registered for the specific event.
* @return True if there are no listeners registered for the specific event,
* false otherwise.
*/
template<typename E>
bool empty() const noexcept {
std::size_t type = E::type();
return (!(type < handlers.size()) ||
!handlers[type] ||
static_cast<Handler<E>&>(*handlers[type]).empty());
}
/**
* @brief Checks if there are listeners registered with the event emitter.
* @return True if there are no listeners registered with the event emitter,

View File

@ -23,9 +23,11 @@ set(
set(TARGET_MAIN main)
set(TARGET_ASYNC async)
set(TARGET_CHECK check)
set(TARGET_EMITTER emitter)
set(TARGET_IDLE idle)
set(TARGET_LOOP loop)
set(TARGET_PREPARE prepare)
set(TARGET_SIGNAL signal)
set(TARGET_TIMER timer)
set(TARGET_WORK work)
@ -53,6 +55,14 @@ target_include_directories(${TARGET_CHECK} PRIVATE ${COMMON_INCLUDE_DIRS})
target_link_libraries(${TARGET_CHECK} PRIVATE ${COMMON_LINK_LIBS})
add_test(NAME ${TARGET_CHECK} COMMAND ${TARGET_CHECK})
# Test TARGET_EMITTER
set(TARGET_EMITTER_SOURCES uvw/emitter.cpp)
add_executable(${TARGET_EMITTER} ${TARGET_EMITTER_SOURCES})
target_include_directories(${TARGET_EMITTER} PRIVATE ${COMMON_INCLUDE_DIRS})
target_link_libraries(${TARGET_EMITTER} PRIVATE ${COMMON_LINK_LIBS})
add_test(NAME ${TARGET_EMITTER} COMMAND ${TARGET_EMITTER})
# Test TARGET_IDLE
set(TARGET_IDLE_SOURCES uvw/idle.cpp)
@ -77,6 +87,14 @@ target_include_directories(${TARGET_PREPARE} PRIVATE ${COMMON_INCLUDE_DIRS})
target_link_libraries(${TARGET_PREPARE} PRIVATE ${COMMON_LINK_LIBS})
add_test(NAME ${TARGET_PREPARE} COMMAND ${TARGET_PREPARE})
# Test TARGET_SIGNAL
set(TARGET_SIGNAL_SOURCES uvw/signal.cpp)
add_executable(${TARGET_SIGNAL} ${TARGET_SIGNAL_SOURCES})
target_include_directories(${TARGET_SIGNAL} PRIVATE ${COMMON_INCLUDE_DIRS})
target_link_libraries(${TARGET_SIGNAL} PRIVATE ${COMMON_LINK_LIBS})
add_test(NAME ${TARGET_SIGNAL} COMMAND ${TARGET_SIGNAL})
# Test TARGET_TIMER
set(TARGET_TIMER_SOURCES uvw/timer.cpp)

108
test/uvw/emitter.cpp Normal file
View File

@ -0,0 +1,108 @@
#include <gtest/gtest.h>
#include <uvw/emitter.hpp>
#include <uvw/event.hpp>
struct FakeEvent: uvw::Event<FakeEvent> { };
struct TestEmitter: uvw::Emitter<TestEmitter> {
void emit() { publish(FakeEvent{}); }
};
TEST(Emitter, ClearAndClearAll) {
TestEmitter emitter{};
ASSERT_TRUE(emitter.empty());
emitter.on<uvw::ErrorEvent>([](const auto &, auto &){});
ASSERT_FALSE(emitter.empty());
ASSERT_FALSE(emitter.empty<uvw::ErrorEvent>());
ASSERT_TRUE(emitter.empty<FakeEvent>());
emitter.clear<FakeEvent>();
ASSERT_FALSE(emitter.empty());
ASSERT_FALSE(emitter.empty<uvw::ErrorEvent>());
ASSERT_TRUE(emitter.empty<FakeEvent>());
emitter.clear<uvw::ErrorEvent>();
ASSERT_TRUE(emitter.empty());
ASSERT_TRUE(emitter.empty<uvw::ErrorEvent>());
ASSERT_TRUE(emitter.empty<FakeEvent>());
emitter.on<uvw::ErrorEvent>([](const auto &, auto &){});
emitter.on<FakeEvent>([](const auto &, auto &){});
ASSERT_FALSE(emitter.empty());
ASSERT_FALSE(emitter.empty<uvw::ErrorEvent>());
ASSERT_FALSE(emitter.empty<FakeEvent>());
emitter.clearAll();
ASSERT_TRUE(emitter.empty());
ASSERT_TRUE(emitter.empty<uvw::ErrorEvent>());
ASSERT_TRUE(emitter.empty<FakeEvent>());
}
TEST(Emitter, On) {
TestEmitter emitter{};
emitter.on<FakeEvent>([](const auto &, auto &){});
ASSERT_FALSE(emitter.empty());
ASSERT_FALSE(emitter.empty<FakeEvent>());
emitter.emit();
ASSERT_FALSE(emitter.empty());
ASSERT_FALSE(emitter.empty<FakeEvent>());
}
TEST(Emitter, Once) {
TestEmitter emitter{};
emitter.once<FakeEvent>([](const auto &, auto &){});
ASSERT_FALSE(emitter.empty());
ASSERT_FALSE(emitter.empty<FakeEvent>());
emitter.emit();
ASSERT_TRUE(emitter.empty());
ASSERT_TRUE(emitter.empty<FakeEvent>());
}
TEST(Emitter, OnceAndErase) {
TestEmitter emitter{};
auto conn = emitter.once<FakeEvent>([](const auto &, auto &){});
ASSERT_FALSE(emitter.empty());
ASSERT_FALSE(emitter.empty<FakeEvent>());
emitter.erase(conn);
ASSERT_TRUE(emitter.empty());
ASSERT_TRUE(emitter.empty<FakeEvent>());
}
TEST(Emitter, OnAndErase) {
TestEmitter emitter{};
auto conn = emitter.on<FakeEvent>([](const auto &, auto &){});
ASSERT_FALSE(emitter.empty());
ASSERT_FALSE(emitter.empty<FakeEvent>());
emitter.erase(conn);
ASSERT_TRUE(emitter.empty());
ASSERT_TRUE(emitter.empty<FakeEvent>());
}

24
test/uvw/signal.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <gtest/gtest.h>
#include <uvw.hpp>
TEST(Signal, Fake) {
auto loop = uvw::Loop::getDefault();
auto handle = loop->resource<uvw::SignalHandle>();
auto l = [](const auto &, auto &) { ASSERT_FALSE(true); };
handle->on<uvw::ErrorEvent>(l);
handle->on<uvw::CheckEvent>(l);
handle->start(42);
ASSERT_EQ(42, handle->signal());
handle->stop();
handle->close();
ASSERT_FALSE(handle->active());
ASSERT_TRUE(handle->closing());
loop->run();
}