Skip to content

Commit

Permalink
Add default class for Prototype in case compiler can't deduce it.
Browse files Browse the repository at this point in the history
  • Loading branch information
FranckRJ committed May 23, 2024
1 parent 580bf27 commit 6e04fff
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 32 deletions.
77 changes: 53 additions & 24 deletions include/fakeit/Prototype.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,64 @@ namespace fakeit {
template<class C>
using ConstRValRefType = R (C::*)(Args...) const&&;

template<class C>
static constexpr Type<C> get(Type<C> t) {
return t;
}
template <typename DefaultC>
struct MemberType {

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

template<class C>
static constexpr RefType<C> getRef(RefType<C> t) {
return t;
}
static constexpr Type<DefaultC> get(Type<DefaultC> t) {
return t;
}

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

template<class C>
static constexpr RValRefType<C> getRValRef(RValRefType<C> t) {
return t;
}
static constexpr ConstType<DefaultC> getConst(ConstType<DefaultC> t) {
return t;
}

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

static constexpr RefType<DefaultC> getRef(RefType<DefaultC> t) {
return t;
}

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

static constexpr ConstRefType<DefaultC> getConstRef(ConstRefType<DefaultC> t) {
return t;
}

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

static constexpr RValRefType<DefaultC> getRValRef(RValRefType<DefaultC> t) {
return t;
}

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

static constexpr ConstRValRefType<DefaultC> getConstRValRef(ConstRValRefType<DefaultC> 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>::get(&MOCK_TYPE(mock)::method)
fakeit::Prototype<prototype>::template MemberType<typename MOCK_TYPE(mock)>::get(&MOCK_TYPE(mock)::method)

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

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

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

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

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

#define Dtor(mock) \
(mock).dtor().setMethodDetails(#mock,"destructor")
Expand Down
25 changes: 23 additions & 2 deletions tests/inherited_funcs_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ struct InheritedFuncsTests : tpunit::TestFixture

InheritedFuncsTests() :
TestFixture(
TEST(InheritedFuncsTests::mock_base_functions)
TEST(InheritedFuncsTests::mock_base_overloaded_functions),
TEST(InheritedFuncsTests::mock_base_and_child_overloaded_functions)
)
{
}
Expand All @@ -29,15 +30,18 @@ struct InheritedFuncsTests : tpunit::TestFixture
virtual double nonOverloadedMethod() = 0;
virtual double overloadedMethod() = 0;
virtual double overloadedMethod() const = 0;
virtual double overloadedInChildMethod() = 0;
};

class Interface : public BaseInterface
{
public:
~Interface() override = default;
using BaseInterface::overloadedInChildMethod;
virtual double overloadedInChildMethod() const = 0;
};

void mock_base_functions()
void mock_base_overloaded_functions()
{
Mock<Interface> mock;

Expand All @@ -57,4 +61,21 @@ struct InheritedFuncsTests : tpunit::TestFixture
Verify(ConstOverloadedMethod(mock, overloadedMethod, double())).Exactly(1);
}

void mock_base_and_child_overloaded_functions()
{
Mock<Interface> mock;

When(OverloadedMethod(mock, overloadedInChildMethod, double())).Return(4.5);
When(ConstOverloadedMethod(mock, overloadedInChildMethod, double())).Return(5.5);

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

EXPECT_EQUAL(interface.overloadedInChildMethod(), 4.5);
EXPECT_EQUAL(constInterface.overloadedInChildMethod(), 5.5);

Verify(OverloadedMethod(mock, overloadedInChildMethod, double())).Exactly(1);
Verify(ConstOverloadedMethod(mock, overloadedInChildMethod, double())).Exactly(1);
}

} __InheritedFuncsTests;

0 comments on commit 6e04fff

Please sign in to comment.