Skip to content

Commit

Permalink
Use real type of the method for method pointer to base classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
FranckRJ committed May 22, 2024
1 parent 8e4c979 commit 580bf27
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 42 deletions.
63 changes: 35 additions & 28 deletions include/fakeit/Prototype.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,47 @@ namespace fakeit {
struct Prototype<R(Args...)> {

template<class C>
struct MemberType {

using Type = R (C::*)(Args...);
using ConstType = R (C::*)(Args...) const;
using RefType = R (C::*)(Args...) &;
using ConstRefType = R (C::*)(Args...) const&;
using RValRefType = R (C::*)(Args...) &&;
using ConstRValRefType = R (C::*)(Args...) const&&;

static constexpr Type get(Type t) {
return t;
}
using Type = R (C::*)(Args...);
template<class C>
using ConstType = R (C::*)(Args...) const;
template<class C>
using RefType = R (C::*)(Args...) &;
template<class C>
using ConstRefType = R (C::*)(Args...) const&;
template<class C>
using RValRefType = R (C::*)(Args...) &&;
template<class C>
using ConstRValRefType = R (C::*)(Args...) const&&;

static constexpr ConstType getConst(ConstType t) {
return t;
}
template<class C>
static constexpr Type<C> get(Type<C> t) {
return t;
}

static constexpr RefType getRef(RefType t) {
return t;
}
template<class C>
static constexpr ConstType<C> getConst(ConstType<C> t) {
return t;
}

static constexpr ConstRefType getConstRef(ConstRefType t) {
return t;
}
template<class C>
static constexpr RefType<C> getRef(RefType<C> t) {
return t;
}

static constexpr RValRefType getRValRef(RValRefType t) {
return t;
}
template<class C>
static constexpr ConstRefType<C> getConstRef(ConstRefType<C> t) {
return t;
}

static constexpr ConstRValRefType getConstRValRef(ConstRValRefType t) {
return t;
}
template<class C>
static constexpr RValRefType<C> getRValRef(RValRefType<C> t) {
return t;
}

};
template<class C>
static constexpr ConstRValRefType<C> getConstRValRef(ConstRValRefType<C> t) {
return t;
}

};

Expand Down
12 changes: 6 additions & 6 deletions include/fakeit/api_macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
std::remove_reference<decltype((mock).get())>::type

#define OVERLOADED_METHOD_PTR(mock, method, prototype) \
fakeit::Prototype<prototype>::template MemberType<typename MOCK_TYPE(mock)>::get(&MOCK_TYPE(mock)::method)
fakeit::Prototype<prototype>::get(&MOCK_TYPE(mock)::method)

#define CONST_OVERLOADED_METHOD_PTR(mock, method, prototype) \
fakeit::Prototype<prototype>::template MemberType<typename MOCK_TYPE(mock)>::getConst(&MOCK_TYPE(mock)::method)
fakeit::Prototype<prototype>::getConst(&MOCK_TYPE(mock)::method)

#define REF_OVERLOADED_METHOD_PTR(mock, method, prototype) \
fakeit::Prototype<prototype>::MemberType<typename MOCK_TYPE(mock)>::getRef(&MOCK_TYPE(mock)::method)
fakeit::Prototype<prototype>::getRef(&MOCK_TYPE(mock)::method)

#define CONST_REF_OVERLOADED_METHOD_PTR(mock, method, prototype) \
fakeit::Prototype<prototype>::MemberType<typename MOCK_TYPE(mock)>::getConstRef(&MOCK_TYPE(mock)::method)
fakeit::Prototype<prototype>::getConstRef(&MOCK_TYPE(mock)::method)

#define R_VAL_REF_OVERLOADED_METHOD_PTR(mock, method, prototype) \
fakeit::Prototype<prototype>::MemberType<typename MOCK_TYPE(mock)>::getRValRef(&MOCK_TYPE(mock)::method)
fakeit::Prototype<prototype>::getRValRef(&MOCK_TYPE(mock)::method)

#define CONST_R_VAL_REF_OVERLOADED_METHOD_PTR(mock, method, prototype) \
fakeit::Prototype<prototype>::MemberType<typename MOCK_TYPE(mock)>::getConstRValRef(&MOCK_TYPE(mock)::method)
fakeit::Prototype<prototype>::getConstRValRef(&MOCK_TYPE(mock)::method)

#define Dtor(mock) \
(mock).dtor().setMethodDetails(#mock,"destructor")
Expand Down
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ add_executable(FakeIt_tests
functional.cpp
gcc_stubbing_multiple_values_tests.cpp
gcc_type_info_tests.cpp
inherited_funcs_tests.cpp
miscellaneous_tests.cpp
move_only_return_tests.cpp
moving_mocks_around.cpp
moving_mocks_around_tests.cpp
msc_stubbing_multiple_values_tests.cpp
msc_type_info_tests.cpp
multiple_translation_units_stub.cpp
Expand Down
60 changes: 60 additions & 0 deletions tests/inherited_funcs_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2014 Eran Pe'er.
*
* This program is made available under the terms of the MIT License.
*
* Created on Mar 10, 2014
*/

#include "tpunit++.hpp"
#include "fakeit.hpp"

using namespace fakeit;

struct InheritedFuncsTests : tpunit::TestFixture
{

InheritedFuncsTests() :
TestFixture(
TEST(InheritedFuncsTests::mock_base_functions)
)
{
}

class BaseInterface
{
public:
virtual ~BaseInterface() = default;

virtual double nonOverloadedMethod() = 0;
virtual double overloadedMethod() = 0;
virtual double overloadedMethod() const = 0;
};

class Interface : public BaseInterface
{
public:
~Interface() override = default;
};

void mock_base_functions()
{
Mock<Interface> mock;

When(Method(mock, nonOverloadedMethod)).Return(1.5);
When(OverloadedMethod(mock, overloadedMethod, double())).Return(2.5);
When(ConstOverloadedMethod(mock, overloadedMethod, double())).Return(3.5);

Interface& interface = mock.get();
const Interface& constInterface = mock.get();

EXPECT_EQUAL(interface.nonOverloadedMethod(), 1.5);
EXPECT_EQUAL(interface.overloadedMethod(), 2.5);
EXPECT_EQUAL(constInterface.overloadedMethod(), 3.5);

Verify(Method(mock, nonOverloadedMethod)).Exactly(1);
Verify(OverloadedMethod(mock, overloadedMethod, double())).Exactly(1);
Verify(ConstOverloadedMethod(mock, overloadedMethod, double())).Exactly(1);
}

} __InheritedFuncsTests;
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

using namespace fakeit;

struct MovingMocksAround : tpunit::TestFixture
struct MovingMocksAroundTests : tpunit::TestFixture
{

MovingMocksAround() :
MovingMocksAroundTests() :
TestFixture(
TEST(MovingMocksAround::move_mock),
TEST(MovingMocksAround::move_mock_then_delete),
TEST(MovingMocksAround::create_mock_from_function),
TEST(MovingMocksAround::create_multiple_mocks_from_function)
TEST(MovingMocksAroundTests::move_mock),
TEST(MovingMocksAroundTests::move_mock_then_delete),
TEST(MovingMocksAroundTests::create_mock_from_function),
TEST(MovingMocksAroundTests::create_multiple_mocks_from_function)
)
{
}
Expand Down Expand Up @@ -98,4 +98,4 @@ struct MovingMocksAround : tpunit::TestFixture
Verify(Method(mock2, function).Using(paramString2)).Exactly(1);
}

} __MovingMocksAround;
} __MovingMocksAroundTests;

0 comments on commit 580bf27

Please sign in to comment.