eranpeer / FakeIt

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

Support for const function overloads. #14

Closed c0yote closed 9 years ago

c0yote commented 9 years ago

I may just be doing it wrong, but I can't find a way to return from a const overload. I checked the project's unit test code and didn't find any examples.

Support for the following situation would be highly appreciated.

virtual Foo& GetBar();
virtual const Foo& GetBar() const;

Love the framework, thanks for all of your effort!

eranpeer commented 9 years ago

You are doing right. It seems like Fakeit doesn't support const overload. I will try to solve this issue this weekend. Thanks, Eran. On Mar 11, 2015 11:32 PM, "U.G. Wilson" notifications@github.com wrote:

I may just be doing it wrong, but I can't find a way to return from a const overload. I checked the project's unit test code and didn't find any examples.

Support for the following situation would be highly appreciated.

virtual Foo& GetBar(); virtual const Foo& GetBar() const;

Love the framework, thanks for all of your effort!

— Reply to this email directly or view it on GitHub https://github.com/eranpeer/FakeIt/issues/14.

eranpeer commented 9 years ago

Ok, I added a ConstOverloadedMethod macro that is used just like the OverloadedMethod macro. For example:

struct SomeInterface {
  virtual int func() = 0;
  virtual int func() const = 0;
}

Mock<SomeInterface> mock;
When(OverloadedMethod(mock, func, int())).Return(1);
When(ConstOverloadedMethod(mock, func, int())).Return(2);

SomeInterface &obj = mock.get();
const SomeInterface& constObj = mock.get();

ASSERT_EQUAL(1, obj.func());
ASSERT_EQUAL(2, constObj.func());

Thank you for submitting this issue. Fakeit is better now!!

eranpeer commented 9 years ago

BTW, I added some more samples to the overloadded_methods_tests.cpp file. I hope this helps.

c0yote commented 9 years ago

Thanks! Works like a champ!