// GNU Lesser General Public License v3.0 // Copyright (c) 2023 Bart Beumer // // This program is free software; you can redistribute it and/or modify it // under the terms of the GNU Lesser General Public License v3.0 as published by // the Free Software Foundation. // #include "MockDataSource.hpp" #include "bmrshared/function_wrapper.hpp" #include #include using ::testing::An; using ::testing::ElementsAre; using ::testing::Return; using ::testing::SetArgReferee; using ::testing::TypedEq; using ::bmrshared::flexible_value; using ::bmrshared::function_wrapper; using ::bmrshared::IDataSource; using ::bmrshared::MockDataSource; namespace { class TestAddition { public: int64_t AddNumbers(int64_t left, int64_t right) { return left + (right * 2); } int64_t AddNumbersConst(int64_t left, int64_t right) const { return left + (right * 2); } }; using TestWrapperType = function_wrapper; using TestConstWrapperType = function_wrapper; } // namespace TEST(WrapTest, Ptr_to_member) { TestAddition obj; TestWrapperType::wrapper_type wrapped = TestWrapperType::Wrap(&TestAddition::AddNumbers, "a", "b"); MockDataSource mockSrc; EXPECT_CALL(mockSrc, GetKeys()).WillRepeatedly(Return(std::vector{"a", "b"})); EXPECT_CALL(mockSrc, Get("a", An())).WillRepeatedly(SetArgReferee<1>(42)); EXPECT_CALL(mockSrc, Get("b", An())).WillRepeatedly(SetArgReferee<1>(21)); flexible_value returnValue = wrapped(obj, mockSrc); EXPECT_EQ(returnValue.as(), 84); } TEST(WrapTest, Const_Ptr_to_member) { const TestAddition obj; TestConstWrapperType::wrapper_type wrapped = TestConstWrapperType::Wrap(&TestAddition::AddNumbersConst, "a", "b"); MockDataSource mockSrc; EXPECT_CALL(mockSrc, GetKeys()).WillRepeatedly(Return(std::vector{"a", "b"})); EXPECT_CALL(mockSrc, Get("a", An())).WillRepeatedly(SetArgReferee<1>(42)); EXPECT_CALL(mockSrc, Get("b", An())).WillRepeatedly(SetArgReferee<1>(21)); flexible_value returnValue = wrapped(obj, mockSrc); EXPECT_EQ(returnValue.as(), 84); } TEST(WrapTest, Const_Ptr_to_member_as_non_const) { const TestAddition obj; TestConstWrapperType::wrapper_type wrapped = TestConstWrapperType::Wrap(&TestAddition::AddNumbersConst, "a", "b"); MockDataSource mockSrc; EXPECT_CALL(mockSrc, GetKeys()).WillRepeatedly(Return(std::vector{"a", "b"})); EXPECT_CALL(mockSrc, Get("a", An())).WillRepeatedly(SetArgReferee<1>(42)); EXPECT_CALL(mockSrc, Get("b", An())).WillRepeatedly(SetArgReferee<1>(21)); flexible_value returnValue = wrapped(obj, mockSrc); EXPECT_EQ(returnValue.as(), 84); }