blob: 8f5d75c12854fac862c6980dacf78bc0adc8b913 [file] [log] [blame]
William A. Kennington III8eaf5fc2019-03-29 15:36:39 -07001#include <functional>
William A. Kennington IIIfeef68f2018-07-17 14:40:14 -07002#include <gtest/gtest.h>
William A. Kennington III8eaf5fc2019-03-29 15:36:39 -07003#include <memory>
William A. Kennington IIIfeef68f2018-07-17 14:40:14 -07004#include <sdeventplus/internal/utils.hpp>
5#include <stdexcept>
6#include <system_error>
William A. Kennington III8eaf5fc2019-03-29 15:36:39 -07007#include <utility>
William A. Kennington IIIfeef68f2018-07-17 14:40:14 -07008
9namespace sdeventplus
10{
11namespace internal
12{
13namespace
14{
15
16TEST(UtilsTest, PerformCallbackSuccess)
17{
William A. Kennington III4e9e6042018-07-17 16:54:36 -070018 EXPECT_EQ(0, performCallback(nullptr, []() {}));
William A. Kennington IIIfeef68f2018-07-17 14:40:14 -070019}
20
William A. Kennington III8eaf5fc2019-03-29 15:36:39 -070021TEST(UtilsTest, PerformCallbackAcceptsReference)
22{
23 auto f =
24 std::bind([](const std::unique_ptr<int>&) {}, std::make_unique<int>(1));
25 EXPECT_EQ(0, performCallback(nullptr, f));
26}
27
28TEST(UtilsTest, PerformCallbackAcceptsMove)
29{
30 auto f =
31 std::bind([](const std::unique_ptr<int>&) {}, std::make_unique<int>(1));
32 EXPECT_EQ(0, performCallback(nullptr, std::move(f)));
33}
34
William A. Kennington IIIfeef68f2018-07-17 14:40:14 -070035TEST(UtilsTest, SetPrepareSystemError)
36{
William A. Kennington III4e9e6042018-07-17 16:54:36 -070037 EXPECT_EQ(-EBUSY, performCallback("system_error", []() {
William A. Kennington IIIfeef68f2018-07-17 14:40:14 -070038 throw std::system_error(EBUSY, std::generic_category());
39 }));
40}
41
42TEST(UtilsTest, SetPrepareException)
43{
William A. Kennington III4e9e6042018-07-17 16:54:36 -070044 EXPECT_EQ(-ENOSYS, performCallback("runtime_error", []() {
45 throw std::runtime_error("Exception");
46 }));
William A. Kennington IIIfeef68f2018-07-17 14:40:14 -070047}
48
49TEST(UtilsTest, SetPrepareUnknownException)
50{
William A. Kennington III4e9e6042018-07-17 16:54:36 -070051 EXPECT_EQ(-ENOSYS,
52 performCallback("unknown", []() { throw static_cast<int>(1); }));
William A. Kennington IIIfeef68f2018-07-17 14:40:14 -070053}
54
55} // namespace
56} // namespace internal
57} // namespace sdeventplus