diff options
| author | William A. Kennington III <wak@google.com> | 2018-07-17 14:40:14 -0700 |
|---|---|---|
| committer | William A. Kennington III <wak@google.com> | 2018-07-17 14:40:14 -0700 |
| commit | 48c427510b0fd9ead21978f8e20f7394c95fde8d (patch) | |
| tree | 72c7d98f537f73be1dceee344fdf2114c9b5b2e3 /test/source/base.cpp | |
| parent | 65db863e625b04e2094849702eca8a63958f6279 (diff) | |
| download | sdeventplus-48c427510b0fd9ead21978f8e20f7394c95fde8d.tar.gz sdeventplus-48c427510b0fd9ead21978f8e20f7394c95fde8d.zip | |
source/base: Add the prepare callback functionality
Diffstat (limited to 'test/source/base.cpp')
| -rw-r--r-- | test/source/base.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/test/source/base.cpp b/test/source/base.cpp index ce95f12..13c534a 100644 --- a/test/source/base.cpp +++ b/test/source/base.cpp @@ -1,3 +1,4 @@ +#include <cerrno> #include <gmock/gmock.h> #include <gtest/gtest.h> #include <sdeventplus/exception.hpp> @@ -5,6 +6,7 @@ #include <sdeventplus/source/base.hpp> #include <sdeventplus/test/sdevent.hpp> #include <string> +#include <system_error> #include <type_traits> namespace sdeventplus @@ -16,6 +18,7 @@ namespace using testing::DoAll; using testing::Return; +using testing::SaveArg; using testing::SetArgPointee; class BaseImpl : public Base @@ -132,6 +135,73 @@ TEST_F(BaseMethodTest, SetDescriptionError) EXPECT_THROW(base->set_description(expected), SdEventError); } +TEST_F(BaseMethodTest, SetPrepareCallback) +{ + bool completed = false; + Base::Callback callback = [&completed](Base&) { completed = true; }; + sd_event_handler_t event_handler; + EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_)) + .WillOnce(DoAll(SaveArg<1>(&event_handler), Return(0))); + base->set_prepare(std::move(callback)); + EXPECT_FALSE(callback); + EXPECT_FALSE(completed); + + EXPECT_EQ(0, event_handler(nullptr, base.get())); + EXPECT_TRUE(completed); +} + +TEST_F(BaseMethodTest, SetPrepareCallbackNoUserdata) +{ + Base::Callback callback = [](Base&) {}; + sd_event_handler_t event_handler; + EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_)) + .WillOnce(DoAll(SaveArg<1>(&event_handler), Return(0))); + base->set_prepare(std::move(callback)); + EXPECT_FALSE(callback); + + EXPECT_EQ(-EINVAL, event_handler(nullptr, nullptr)); +} + +TEST_F(BaseMethodTest, SetPrepareNull) +{ + EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, nullptr)) + .WillOnce(Return(0)); + base->set_prepare(nullptr); + EXPECT_EQ(-ENOSYS, base->prepareCallback()); +} + +TEST_F(BaseMethodTest, SetPrepareSystemError) +{ + Base::Callback callback = [](Base&) { + throw std::system_error(EBUSY, std::generic_category()); + }; + EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_)) + .WillOnce(Return(0)); + base->set_prepare(std::move(callback)); + EXPECT_FALSE(callback); + EXPECT_EQ(-EBUSY, base->prepareCallback()); +} + +TEST_F(BaseMethodTest, SetPrepareUnknownException) +{ + Base::Callback callback = [](Base&) { throw static_cast<int>(1); }; + EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_)) + .WillOnce(Return(0)); + base->set_prepare(std::move(callback)); + EXPECT_FALSE(callback); + EXPECT_EQ(-ENOSYS, base->prepareCallback()); +} + +TEST_F(BaseMethodTest, SetPrepareError) +{ + Base::Callback callback = [](Base&) {}; + EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_)) + .WillOnce(Return(-EINVAL)); + EXPECT_THROW(base->set_prepare(std::move(callback)), SdEventError); + EXPECT_TRUE(callback); + EXPECT_EQ(-ENOSYS, base->prepareCallback()); +} + TEST_F(BaseMethodTest, GetPendingSuccess) { EXPECT_CALL(mock, sd_event_source_get_pending(expected_source)) |

