Giter Site home page Giter Site logo

Comments (8)

amerry avatar amerry commented on June 7, 2024

I think just using std::forward in a couple of places should be enough to fix it. I'll try to put together a pull request.

from fakeit.

eranpeer avatar eranpeer commented on June 7, 2024

Thanks,
I will take a look at it as soon as I can.
If you find a solution go ahead and submit a pull request.

from fakeit.

amerry avatar amerry commented on June 7, 2024

It wasn't nearly as simple as I thought it would be, mostly because perfect forwarding only works for template functions, not functions in templated classes. ActualInvocation is a major sticking point: you can do something like

template<typename ... carglist>
ActualInvocation(unsigned int ordinal, MethodInfo &method, carglist &&... args) :
    Invocation(ordinal, method), _matcher{nullptr}, actualArguments{std::forward<carglist>(args)...} {
}

to get perfect forwarding, but then the actualArguments member needs adjusting so that rvalue refs can be stored (some sort of transformation to remove rvalue references but preserve lvalue references). And this doesn't include a check that carglist is the same as arglist in this context (see this stackoverflow answer for an example of how to do that).

The assignment-style variant of AlwaysReturns() also seems to have its own compile errors.

from fakeit.

eranpeer avatar eranpeer commented on June 7, 2024

Ok,
I pushed a fix for this problem. It still needs some code polish but it looks like working now.
you can now do:

struct foo {
        virtual int bar(int &&) = 0;
};

Mock<foo> foo_mock;
When(Method(foo_mock, bar)).AlwaysReturn(100);
When(Method(foo_mock, bar).Using(1)).AlwaysReturn(1);
When(Method(foo_mock, bar).Using(2)).AlwaysDo([](int &){return 2; });
When(Method(foo_mock, bar).Using(3)).Do([](int){return 3; });
Method(foo_mock, bar).Using(4) = 4;

ASSERT_EQUAL(100, foo_mock.get().bar(5));
ASSERT_EQUAL(1, foo_mock.get().bar(1));
ASSERT_EQUAL(2, foo_mock.get().bar(2));
ASSERT_EQUAL(3, foo_mock.get().bar(3));
ASSERT_EQUAL(4, foo_mock.get().bar(4));

Please try using it and update me. If all OK I'll create a new release tag.

from fakeit.

amerry avatar amerry commented on June 7, 2024

It seems to work fine with non-overloaded methods, but not with overloaded ones:

struct RValueTypesTests: tpunit::TestFixture {

class AbstractType {
public:
    virtual void foo() = 0;
};

class ConcreteType: public AbstractType {
public:
    int state;
    ConcreteType() :
            state(10) {
    }
    virtual void foo() override {
    }

    bool operator==(const ConcreteType& other) {
        return (other.state == this->state);
    }
};

using CompositeType = std::pair<int,int>;

struct RValueInterface {
    virtual int intRValueArg(int&&) = 0;
    virtual int compositeRValueArg(CompositeType&&) = 0;
    virtual int overloadedMethod(CompositeType&&) = 0;
    virtual CompositeType overloadedMethod(int&&) = 0;
            virtual RValueInterface& operator=(RValueInterface&&) = 0;
            virtual RValueInterface& operator=(const RValueInterface&) = 0;
};

RValueTypesTests() :
    tpunit::TestFixture(
        //
        TEST(RValueTypesTests::explicitStubbingDefaultReturnValues),
        TEST(RValueTypesTests::explicitStubbingReturnValues_with_AlwaysReturn),
        TEST(RValueTypesTests::explicitStubbingReturnValues_by_assignment),
        TEST(RValueTypesTests::explicitStubbingReturnValues)
        //
    ) {
}

void explicitStubbingDefaultReturnValues() {
    Mock<RValueInterface> mock;     //
    When(Method(mock,intRValueArg)).Return(); //
    When(Method(mock,compositeRValueArg)).Return(); //
    When(OverloadedMethod(mock,overloadedMethod, int(CompositeType&&))).Return(); //
    When(OverloadedMethod(mock,overloadedMethod, int(int&&))).Return(); //

    RValueInterface & i = mock.get();

    ASSERT_EQUAL(0, i.intRValueArg(0));
    ASSERT_EQUAL(0, i.compositeRValueArg(CompositeType{0, 1}));
    ASSERT_EQUAL(0, i.overloadedMethod(CompositeType{0, 1}));
    ASSERT_EQUAL(std::make_pair(0,0), i.overloadedMethod(0));
}

void explicitStubbingReturnValues() {
    Mock<RValueInterface> mock;     //
    When(Method(mock,intRValueArg)).Return(1); //
    When(Method(mock,compositeRValueArg)).Return(2); //
    When(OverloadedMethod(mock,overloadedMethod, int(CompositeType&&))).Return(3); //
    When(OverloadedMethod(mock,overloadedMethod, int(int&&))).Return(std::make_pair(4,5)); //
    When(OverloadedMethod(mock,operator=, RValueInterface&(RValueInterface&&))).Return(mock.get()); //
    When(OverloadedMethod(mock,operator=, RValueInterface&(const RValueInterface&))).Return(mock.get()); //

    RValueInterface & i = mock.get();

    ASSERT_EQUAL(1, i.intRValueArg(0));
    ASSERT_EQUAL(2, i.compositeRValueArg(CompositeType{0, 1}));
    ASSERT_EQUAL(3, i.overloadedMethod(CompositeType{0, 1}));
    ASSERT_EQUAL(std::make_pair(4,5), i.overloadedMethod(0));
    ASSERT_EQUAL(&i, &i.operator=(i));
    ASSERT_EQUAL(&i, &i.operator=(std::move(i)));
}

void explicitStubbingReturnValues_with_AlwaysReturn() {
    Mock<RValueInterface> mock;     //
    When(Method(mock,intRValueArg)).AlwaysReturn(1); //
    When(Method(mock,compositeRValueArg)).AlwaysReturn(2); //
    When(OverloadedMethod(mock,overloadedMethod, int(CompositeType&&))).AlwaysReturn(3); //
    When(OverloadedMethod(mock,overloadedMethod, int(int&&))).AlwaysReturn(std::make_pair(4,5)); //
    When(OverloadedMethod(mock,operator=, RValueInterface&(RValueInterface&&))).AlwaysReturn(mock.get()); //
    When(OverloadedMethod(mock,operator=, RValueInterface&(const RValueInterface&))).AlwaysReturn(mock.get()); //

    RValueInterface & i = mock.get();

    ASSERT_EQUAL(1, i.intRValueArg(0));
    ASSERT_EQUAL(2, i.compositeRValueArg(CompositeType{0, 1}));
    ASSERT_EQUAL(3, i.overloadedMethod(CompositeType{0, 1}));
    ASSERT_EQUAL(std::make_pair(4,5), i.overloadedMethod(0));
    ASSERT_EQUAL(&i, &i.operator=(i));
    ASSERT_EQUAL(&i, &i.operator=(std::move(i)));
}

void explicitStubbingReturnValues_by_assignment() {
    Mock<RValueInterface> mock;     //
    Method(mock,intRValueArg) = 1; //
    Method(mock,compositeRValueArg) = 2; //
    OverloadedMethod(mock,overloadedMethod, int(CompositeType&&)) = 3; //
    OverloadedMethod(mock,overloadedMethod, int(int&&)) = std::make_pair(4,5); //
    OverloadedMethod(mock,operator=, RValueInterface&(RValueInterface&&)) = mock.get(); //
    OverloadedMethod(mock,operator=, RValueInterface&(const RValueInterface&)) = mock.get(); //

    RValueInterface & i = mock.get();

    ASSERT_EQUAL(1, i.intRValueArg(0));
    ASSERT_EQUAL(2, i.compositeRValueArg(CompositeType{0, 1}));
    ASSERT_EQUAL(3, i.overloadedMethod(CompositeType{0, 1}));
    ASSERT_EQUAL(std::make_pair(4,5), i.overloadedMethod(0));
    ASSERT_EQUAL(&i, &i.operator=(i));
    ASSERT_EQUAL(&i, &i.operator=(std::move(i)));
}

} __RValueTypesTests;

produces compiler errors like:

In file included from /home/amerry/third-party/FakeIt/tests/../include/fakeit/fakeit_root.hpp:11:0,
             from /home/amerry/third-party/FakeIt/tests/../config/standalone/fakeit.hpp:4,
             from /home/amerry/third-party/FakeIt/tests/rvalue_types_tests.cpp:14:
/home/amerry/third-party/FakeIt/tests/rvalue_types_tests.cpp: In member function ‘void RValueTypesTests::explicitStubbingReturnValues()’:
/home/amerry/third-party/FakeIt/tests/../include/fakeit/api_macros.hpp:11:92: error: no matching function for call to fakeit::Prototype<int(int&&)>::MemberType<RValueTypesTests::RValueInterface>::get(<unresolved overloaded function type>)’
     fakeit::Prototype<prototype>::MemberType<MOCK_TYPE(mock)>::get(&MOCK_TYPE(mock)::method)
                                                                                            ^
/home/amerry/third-party/FakeIt/tests/../include/fakeit/api_macros.hpp:41:10: note: in definition of macro ‘When’
     When(call)
          ^
/home/amerry/third-party/FakeIt/tests/../include/fakeit/api_macros.hpp:23:37: note: in expansion of macro ‘OVERLOADED_METHOD_PTR’
     mock.template stub<__COUNTER__>(OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method)
                                     ^
/home/amerry/third-party/FakeIt/tests/rvalue_types_tests.cpp:81:8: note: in expansion of macro ‘OverloadedMethod’
   When(OverloadedMethod(mock,overloadedMethod, int(int&&))).Return(std::make_pair(4,5)); //
        ^
/home/amerry/third-party/FakeIt/tests/../include/fakeit/api_macros.hpp:11:92: note: candidate is:
     fakeit::Prototype<prototype>::MemberType<MOCK_TYPE(mock)>::get(&MOCK_TYPE(mock)::method)

from fakeit.

eranpeer avatar eranpeer commented on June 7, 2024

Thanks,
I'll take a look at it soon.
On Jul 21, 2015 11:28, "Alex Merry" [email protected] wrote:

It seems to work fine with non-overloaded methods, but not with overloaded
ones:

struct RValueTypesTests: tpunit::TestFixture {

class AbstractType {
public:
virtual void foo() = 0;
};

class ConcreteType: public AbstractType {
public:
int state;
ConcreteType() :
state(10) {
}
virtual void foo() override {
}

bool operator==(const ConcreteType& other) {
    return (other.state == this->state);
}

};

using CompositeType = std::pair<int,int>;

struct RValueInterface {
virtual int intRValueArg(int&&) = 0;
virtual int compositeRValueArg(CompositeType&&) = 0;
virtual int overloadedMethod(CompositeType&&) = 0;
virtual CompositeType overloadedMethod(int&&) = 0;
virtual RValueInterface& operator=(RValueInterface&&) = 0;
virtual RValueInterface& operator=(const RValueInterface&) = 0;
};

RValueTypesTests() :
tpunit::TestFixture(
//
TEST(RValueTypesTests::explicitStubbingDefaultReturnValues),
TEST(RValueTypesTests::explicitStubbingReturnValues_with_AlwaysReturn),
TEST(RValueTypesTests::explicitStubbingReturnValues_by_assignment),
TEST(RValueTypesTests::explicitStubbingReturnValues)
//
) {
}

void explicitStubbingDefaultReturnValues() {
Mock mock; //
When(Method(mock,intRValueArg)).Return(); //
When(Method(mock,compositeRValueArg)).Return(); //
When(OverloadedMethod(mock,overloadedMethod, int(CompositeType&&))).Return(); //
When(OverloadedMethod(mock,overloadedMethod, int(int&&))).Return(); //

RValueInterface & i = mock.get();

ASSERT_EQUAL(0, i.intRValueArg(0));
ASSERT_EQUAL(0, i.compositeRValueArg(CompositeType{0, 1}));
ASSERT_EQUAL(0, i.overloadedMethod(CompositeType{0, 1}));
ASSERT_EQUAL(std::make_pair(0,0), i.overloadedMethod(0));

}

void explicitStubbingReturnValues() {
Mock mock; //
When(Method(mock,intRValueArg)).Return(1); //
When(Method(mock,compositeRValueArg)).Return(2); //
When(OverloadedMethod(mock,overloadedMethod, int(CompositeType&&))).Return(3); //
When(OverloadedMethod(mock,overloadedMethod, int(int&&))).Return(std::make_pair(4,5)); //
When(OverloadedMethod(mock,operator=, RValueInterface&(RValueInterface&&))).Return(mock.get()); //
When(OverloadedMethod(mock,operator=, RValueInterface&(const RValueInterface&))).Return(mock.get()); //

RValueInterface & i = mock.get();

ASSERT_EQUAL(1, i.intRValueArg(0));
ASSERT_EQUAL(2, i.compositeRValueArg(CompositeType{0, 1}));
ASSERT_EQUAL(3, i.overloadedMethod(CompositeType{0, 1}));
ASSERT_EQUAL(std::make_pair(4,5), i.overloadedMethod(0));
ASSERT_EQUAL(&i, &i.operator=(i));
ASSERT_EQUAL(&i, &i.operator=(std::move(i)));

}

void explicitStubbingReturnValues_with_AlwaysReturn() {
Mock mock; //
When(Method(mock,intRValueArg)).AlwaysReturn(1); //
When(Method(mock,compositeRValueArg)).AlwaysReturn(2); //
When(OverloadedMethod(mock,overloadedMethod, int(CompositeType&&))).AlwaysReturn(3); //
When(OverloadedMethod(mock,overloadedMethod, int(int&&))).AlwaysReturn(std::make_pair(4,5)); //
When(OverloadedMethod(mock,operator=, RValueInterface&(RValueInterface&&))).AlwaysReturn(mock.get()); //
When(OverloadedMethod(mock,operator=, RValueInterface&(const RValueInterface&))).AlwaysReturn(mock.get()); //

RValueInterface & i = mock.get();

ASSERT_EQUAL(1, i.intRValueArg(0));
ASSERT_EQUAL(2, i.compositeRValueArg(CompositeType{0, 1}));
ASSERT_EQUAL(3, i.overloadedMethod(CompositeType{0, 1}));
ASSERT_EQUAL(std::make_pair(4,5), i.overloadedMethod(0));
ASSERT_EQUAL(&i, &i.operator=(i));
ASSERT_EQUAL(&i, &i.operator=(std::move(i)));

}

void explicitStubbingReturnValues_by_assignment() {
Mock mock; //
Method(mock,intRValueArg) = 1; //
Method(mock,compositeRValueArg) = 2; //
OverloadedMethod(mock,overloadedMethod, int(CompositeType&&)) = 3; //
OverloadedMethod(mock,overloadedMethod, int(int&&)) = std::make_pair(4,5); //
OverloadedMethod(mock,operator=, RValueInterface&(RValueInterface&&)) = mock.get(); //
OverloadedMethod(mock,operator=, RValueInterface&(const RValueInterface&)) = mock.get(); //

RValueInterface & i = mock.get();

ASSERT_EQUAL(1, i.intRValueArg(0));
ASSERT_EQUAL(2, i.compositeRValueArg(CompositeType{0, 1}));
ASSERT_EQUAL(3, i.overloadedMethod(CompositeType{0, 1}));
ASSERT_EQUAL(std::make_pair(4,5), i.overloadedMethod(0));
ASSERT_EQUAL(&i, &i.operator=(i));
ASSERT_EQUAL(&i, &i.operator=(std::move(i)));

}

} __RValueTypesTests;

produces compiler errors like:

In file included from /home/amerry/third-party/FakeIt/tests/../include/fakeit/fakeit_root.hpp:11:0,
from /home/amerry/third-party/FakeIt/tests/../config/standalone/fakeit.hpp:4,
from /home/amerry/third-party/FakeIt/tests/rvalue_types_tests.cpp:14:
/home/amerry/third-party/FakeIt/tests/rvalue_types_tests.cpp: In member function ‘void RValueTypesTests::explicitStubbingReturnValues()’:
/home/amerry/third-party/FakeIt/tests/../include/fakeit/api_macros.hpp:11:92: error: no matching function for call to fakeit::Prototype<int(int&&)>::MemberTypeRValueTypesTests::RValueInterface::get()’
fakeit::Prototype::MemberType<MOCK_TYPE(mock)>::get(&MOCK_TYPE(mock)::method)
^
/home/amerry/third-party/FakeIt/tests/../include/fakeit/api_macros.hpp:41:10: note: in definition of macro ‘When’
When(call)
^
/home/amerry/third-party/FakeIt/tests/../include/fakeit/api_macros.hpp:23:37: note: in expansion of macro ‘OVERLOADED_METHOD_PTR’
mock.template stub<COUNTER>(OVERLOADED_METHOD_PTR( mock , method, prototype )).setMethodDetails(#mock,#method)
^
/home/amerry/third-party/FakeIt/tests/rvalue_types_tests.cpp:81:8: note: in expansion of macro ‘OverloadedMethod’
When(OverloadedMethod(mock,overloadedMethod, int(int&&))).Return(std::make_pair(4,5)); //
^
/home/amerry/third-party/FakeIt/tests/../include/fakeit/api_macros.hpp:11:92: note: candidate is:
fakeit::Prototype::MemberType<MOCK_TYPE(mock)>::get(&MOCK_TYPE(mock)::method)


Reply to this email directly or view it on GitHub
#23 (comment).

from fakeit.

eranpeer avatar eranpeer commented on June 7, 2024

Well,
It looks like you have a typo.
There is no method with prototype int(int&&) in RValueInterface.
That's the reason why a line like:
When(OverloadedMethod(mock,overloadedMethod, int(int&&))).Return(std::make_pair(4,5));
did not compile.
There where some problems too. I did not handle rvalue arguments of abstract classes. That's the reason the line:
When(OverloadedMethod(mock,operator=, RValueInterface&(RValueInterface&&))).Return(mock.get());
did not compile.
Anyway. I fixed it (both typos & problems). You can pull the latest and give it a try.
Thanks,

from fakeit.

amerry avatar amerry commented on June 7, 2024

The code I was originally trying to compile now works, thanks.

from fakeit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.