From 6e04fff972db38ca3b0eed4412cea4b472a3971b Mon Sep 17 00:00:00 2001 From: FranckRJ Date: Fri, 24 May 2024 00:16:38 +0200 Subject: [PATCH] Add default class for Prototype in case compiler can't deduce it. --- include/fakeit/Prototype.hpp | 77 +++++++++++++++++++++++---------- include/fakeit/api_macros.hpp | 12 ++--- tests/inherited_funcs_tests.cpp | 25 ++++++++++- 3 files changed, 82 insertions(+), 32 deletions(-) diff --git a/include/fakeit/Prototype.hpp b/include/fakeit/Prototype.hpp index 46e83d63..893473a2 100644 --- a/include/fakeit/Prototype.hpp +++ b/include/fakeit/Prototype.hpp @@ -21,35 +21,64 @@ namespace fakeit { template using ConstRValRefType = R (C::*)(Args...) const&&; - template - static constexpr Type get(Type t) { - return t; - } + template + struct MemberType { - template - static constexpr ConstType getConst(ConstType t) { - return t; - } + template + static constexpr Type get(Type t) { + return t; + } - template - static constexpr RefType getRef(RefType t) { - return t; - } + static constexpr Type get(Type t) { + return t; + } - template - static constexpr ConstRefType getConstRef(ConstRefType t) { - return t; - } + template + static constexpr ConstType getConst(ConstType t) { + return t; + } - template - static constexpr RValRefType getRValRef(RValRefType t) { - return t; - } + static constexpr ConstType getConst(ConstType t) { + return t; + } - template - static constexpr ConstRValRefType getConstRValRef(ConstRValRefType t) { - return t; - } + template + static constexpr RefType getRef(RefType t) { + return t; + } + + static constexpr RefType getRef(RefType t) { + return t; + } + + template + static constexpr ConstRefType getConstRef(ConstRefType t) { + return t; + } + + static constexpr ConstRefType getConstRef(ConstRefType t) { + return t; + } + + template + static constexpr RValRefType getRValRef(RValRefType t) { + return t; + } + + static constexpr RValRefType getRValRef(RValRefType t) { + return t; + } + + template + static constexpr ConstRValRefType getConstRValRef(ConstRValRefType t) { + return t; + } + + static constexpr ConstRValRefType getConstRValRef(ConstRValRefType t) { + return t; + } + + }; }; diff --git a/include/fakeit/api_macros.hpp b/include/fakeit/api_macros.hpp index 32d891af..4a964065 100644 --- a/include/fakeit/api_macros.hpp +++ b/include/fakeit/api_macros.hpp @@ -10,22 +10,22 @@ std::remove_reference::type #define OVERLOADED_METHOD_PTR(mock, method, prototype) \ - fakeit::Prototype::get(&MOCK_TYPE(mock)::method) + fakeit::Prototype::template MemberType::get(&MOCK_TYPE(mock)::method) #define CONST_OVERLOADED_METHOD_PTR(mock, method, prototype) \ - fakeit::Prototype::getConst(&MOCK_TYPE(mock)::method) + fakeit::Prototype::template MemberType::getConst(&MOCK_TYPE(mock)::method) #define REF_OVERLOADED_METHOD_PTR(mock, method, prototype) \ - fakeit::Prototype::getRef(&MOCK_TYPE(mock)::method) + fakeit::Prototype::template MemberType::getRef(&MOCK_TYPE(mock)::method) #define CONST_REF_OVERLOADED_METHOD_PTR(mock, method, prototype) \ - fakeit::Prototype::getConstRef(&MOCK_TYPE(mock)::method) + fakeit::Prototype::template MemberType::getConstRef(&MOCK_TYPE(mock)::method) #define R_VAL_REF_OVERLOADED_METHOD_PTR(mock, method, prototype) \ - fakeit::Prototype::getRValRef(&MOCK_TYPE(mock)::method) + fakeit::Prototype::template MemberType::getRValRef(&MOCK_TYPE(mock)::method) #define CONST_R_VAL_REF_OVERLOADED_METHOD_PTR(mock, method, prototype) \ - fakeit::Prototype::getConstRValRef(&MOCK_TYPE(mock)::method) + fakeit::Prototype::template MemberType::getConstRValRef(&MOCK_TYPE(mock)::method) #define Dtor(mock) \ (mock).dtor().setMethodDetails(#mock,"destructor") diff --git a/tests/inherited_funcs_tests.cpp b/tests/inherited_funcs_tests.cpp index ec559d68..185b2dd0 100644 --- a/tests/inherited_funcs_tests.cpp +++ b/tests/inherited_funcs_tests.cpp @@ -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) ) { } @@ -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 mock; @@ -57,4 +61,21 @@ struct InheritedFuncsTests : tpunit::TestFixture Verify(ConstOverloadedMethod(mock, overloadedMethod, double())).Exactly(1); } + void mock_base_and_child_overloaded_functions() + { + Mock 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;