From feef68f751e09d8d5a4f2bf2f2f4cab27de1b73e Mon Sep 17 00:00:00 2001 From: "William A. Kennington III" Date: Tue, 17 Jul 2018 14:40:14 -0700 Subject: source/base: Refactor out callback routine The callback mechanism will be used by other callback methods in the same fashion, so we can template and re-use this functionality --- test/Makefile.am | 5 +++++ test/internal/utils.cpp | 38 ++++++++++++++++++++++++++++++++++++++ test/source/base.cpp | 44 ++++++++++++++------------------------------ 3 files changed, 57 insertions(+), 30 deletions(-) create mode 100644 test/internal/utils.cpp (limited to 'test') diff --git a/test/Makefile.am b/test/Makefile.am index d1923fa..4b94144 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -27,6 +27,11 @@ internal_sdref_SOURCES = internal/sdref.cpp internal_sdref_CPPFLAGS = $(gtest_cppflags) internal_sdref_LDADD = $(gtest_ldadd) +check_PROGRAMS += internal_utils +internal_utils_SOURCES = internal/utils.cpp +internal_utils_CPPFLAGS = $(gtest_cppflags) +internal_utils_LDADD = $(gtest_ldadd) + check_PROGRAMS += source_base source_base_SOURCES = source/base.cpp source_base_CPPFLAGS = $(gtest_cppflags) diff --git a/test/internal/utils.cpp b/test/internal/utils.cpp new file mode 100644 index 0000000..2a347ca --- /dev/null +++ b/test/internal/utils.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +namespace sdeventplus +{ +namespace internal +{ +namespace +{ + +TEST(UtilsTest, PerformCallbackSuccess) +{ + EXPECT_EQ(0, performCallback([]() {})); +} + +TEST(UtilsTest, SetPrepareSystemError) +{ + EXPECT_EQ(-EBUSY, performCallback([]() { + throw std::system_error(EBUSY, std::generic_category()); + })); +} + +TEST(UtilsTest, SetPrepareException) +{ + EXPECT_EQ(-ENOSYS, + performCallback([]() { throw std::runtime_error("Exception"); })); +} + +TEST(UtilsTest, SetPrepareUnknownException) +{ + EXPECT_EQ(-ENOSYS, performCallback([]() { throw static_cast(1); })); +} + +} // namespace +} // namespace internal +} // namespace sdeventplus diff --git a/test/source/base.cpp b/test/source/base.cpp index 69ae8e2..435b6f2 100644 --- a/test/source/base.cpp +++ b/test/source/base.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -322,47 +321,32 @@ TEST_F(BaseMethodTest, SetPrepareCallbackNoUserdata) 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) +TEST_F(BaseMethodTest, SetPrepareError) { - 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)); + base->set_prepare(std::move([](Base&) {})); EXPECT_TRUE(base->get_prepare()); - EXPECT_FALSE(callback); - EXPECT_EQ(-EBUSY, base->prepareCallback()); + + 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_FALSE(base->get_prepare()); + EXPECT_TRUE(callback); } -TEST_F(BaseMethodTest, SetPrepareUnknownException) +TEST_F(BaseMethodTest, SetPrepareNull) { - Base::Callback callback = [](Base&) { throw static_cast(1); }; EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_)) .WillOnce(Return(0)); - base->set_prepare(std::move(callback)); + base->set_prepare(std::move([](Base&) {})); EXPECT_TRUE(base->get_prepare()); - 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_CALL(mock, sd_event_source_set_prepare(expected_source, nullptr)) + .WillOnce(Return(0)); + base->set_prepare(nullptr); EXPECT_FALSE(base->get_prepare()); - EXPECT_TRUE(callback); - EXPECT_EQ(-ENOSYS, base->prepareCallback()); } TEST_F(BaseMethodTest, GetPendingSuccess) -- cgit v1.2.3