108 lines
3.6 KiB
C++
108 lines
3.6 KiB
C++
// GNU Lesser General Public License v3.0
|
|
// Copyright (c) 2023 Bart Beumer <bart@4beumer.nl>
|
|
//
|
|
// 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 <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
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); }
|
|
};
|
|
|
|
|
|
class TestWriteOutputArgument
|
|
{
|
|
public:
|
|
bool WriteOutArg(int64_t a, int64_t& extraOutArg) const
|
|
{
|
|
return extraOutArg = a;
|
|
return true;
|
|
}
|
|
};
|
|
|
|
using TestWrapperType = function_wrapper<TestAddition>;
|
|
using TestConstWrapperType = function_wrapper<const TestAddition>;
|
|
|
|
} // 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<std::string>{"a", "b"}));
|
|
EXPECT_CALL(mockSrc, Get("a", An<int64_t&>())).WillRepeatedly(SetArgReferee<1>(42));
|
|
EXPECT_CALL(mockSrc, Get("b", An<int64_t&>())).WillRepeatedly(SetArgReferee<1>(21));
|
|
|
|
flexible_value returnValue = wrapped(obj, mockSrc);
|
|
EXPECT_EQ(returnValue.as<int64_t>(), 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<std::string>{"a", "b"}));
|
|
EXPECT_CALL(mockSrc, Get("a", An<int64_t&>())).WillRepeatedly(SetArgReferee<1>(42));
|
|
EXPECT_CALL(mockSrc, Get("b", An<int64_t&>())).WillRepeatedly(SetArgReferee<1>(21));
|
|
|
|
flexible_value returnValue = wrapped(obj, mockSrc);
|
|
EXPECT_EQ(returnValue.as<int64_t>(), 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<std::string>{"a", "b"}));
|
|
EXPECT_CALL(mockSrc, Get("a", An<int64_t&>())).WillRepeatedly(SetArgReferee<1>(42));
|
|
EXPECT_CALL(mockSrc, Get("b", An<int64_t&>())).WillRepeatedly(SetArgReferee<1>(21));
|
|
|
|
flexible_value returnValue = wrapped(obj, mockSrc);
|
|
EXPECT_EQ(returnValue.as<int64_t>(), 84);
|
|
}
|
|
|
|
TEST(WrapTest, WriteExtraOutArg)
|
|
{
|
|
const TestWriteOutputArgument obj;
|
|
using TestWrapperOutArgType = function_wrapper<const TestWriteOutputArgument, int64_t&>;
|
|
TestWrapperOutArgType::wrapper_type wrapped =
|
|
TestWrapperOutArgType::Wrap(&TestWriteOutputArgument::WriteOutArg, "a");
|
|
|
|
MockDataSource mockSrc;
|
|
EXPECT_CALL(mockSrc, GetKeys()).WillRepeatedly(Return(std::vector<std::string>{"a"}));
|
|
EXPECT_CALL(mockSrc, Get("a", An<int64_t&>())).WillRepeatedly(SetArgReferee<1>(42));
|
|
|
|
int64_t lastArg = 0;
|
|
flexible_value returnValue = wrapped(obj, mockSrc, lastArg);
|
|
EXPECT_EQ(returnValue.as<bool>(), true);
|
|
EXPECT_EQ(lastArg, 42);
|
|
}
|