eranpeer / FakeIt

C++ mocking made easy. A simple yet very expressive, headers only library for c++ mocking.
MIT License
1.22k stars 170 forks source link

Handle stubbing modern C++ ref overloades - partial #293

Closed malcolmdavey closed 1 year ago

malcolmdavey commented 1 year ago

Partial fix for https://github.com/eranpeer/FakeIt/issues/292

malcolmdavey commented 1 year ago

Seems it works on VS, and clang, but not GCC

Ramjii commented 1 year ago

My code now compiles, but looks like it doesn't work with the Verify macro. I tried both Verify(RefOverloadedMethod(mock, Index, unsigned&())) and Verify(ConstRefOverloadedMethod(mock, Index, unsigned())) but they both failed with expression failed, even though my unit test clearly calls them

malcolmdavey commented 1 year ago

My code now compiles, but looks like it doesn't work with the Verify macro. I tried both Verify(RefOverloadedMethod(mock, Index, unsigned&())) and Verify(ConstRefOverloadedMethod(mock, Index, unsigned())) but they both failed with expression failed, even though my unit test clearly calls them

Sorry, didn't try the Verify, so I don't know what the issue is there. Verify has some other issues but am assuming aren't related

FranckRJ commented 1 year ago

I got it working with GCC, basically we just need to replace the "double typedef" system in Prototype.hpp to a "one typedef" system. I've also made some minor changes, like replacing the typedefs by aliases, because it's more modern and more readable.

I've tried to use the "suggest changes" feature of GitHub but it doesn't work (too many lines I guess). Here's the new Prototype class:

template<typename R, typename... Args>
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 RRefType = R (C::*)(Args...) &&;
        using ConstRRefType = R (C::*)(Args...) const&&;

        static Type get(Type t) {
            return t;
        }

        static ConstType getconst(ConstType t) {
            return t;
        }

        static RefType getRef(RefType t) {
            return t;
        }

        static ConstRefType getConstRef(ConstRefType t) {
            return t;
        }

        static RRefType getRRef(RRefType t) {
            return t;
        }

        static ConstRRefType getConstRRef(ConstRRefType t) {
            return t;
        }

    };

};

Haven't yet looked the Verify issue.

FranckRJ commented 1 year ago

I have no issue with the Verify method, with either GCC or clang, with or without my changes to make GCC compatible.

Could you provide a code sample that fails @Ramjii ?

FranckRJ commented 1 year ago

Sorry, I deleted the branch dev-2.3.1 after merging it to master, I didn't know it will close the PR. I cannot rebase it to dev-2.4.0 nor re-open it.