Giter Site home page Giter Site logo

Mocks and std::unique_ptr about fakeit HOT 8 CLOSED

eranpeer avatar eranpeer commented on June 14, 2024
Mocks and std::unique_ptr

from fakeit.

Comments (8)

eranpeer avatar eranpeer commented on June 14, 2024 4

Ok, It works! You can mock the dtor now!
The following code will work:

Mock<SomeInterface> mock;
Fake(Dtor(mock)); // Stub the dtor to do nothing!!
SomeInterface * i = &mock.get();
std::unique_ptr<SomeInterface> ptr(i);

You can do much more than that:

void mock_virtual_dtor_with_when() {
  int a = 0;
  Mock<SomeInterface> mock;
  When(Dtor(mock)).Do([&](){a++; });
  SomeInterface * i = &(mock.get());
  delete i; // a++
  delete i; // a++
  ASSERT_EQUAL(2, a);
}

Enjoy!

from fakeit.

eranpeer avatar eranpeer commented on June 14, 2024

Hi,
I made some changes trying to make Fakeit support your request, though i had to revert them all back since for some reason it does not work on gcc.
Things get tricky since the instance held by the mock is not a real instance (since the mocked class is abstract). It is a faked instance managed by the mock object. Letting the test/user delete the instance fails because the FakeObject's virtual table does not have a destructor.
I will make another effort in the next few days to make it work (by putting a fake destructor in the correct position of the virtual table) I hope I will be able to make it work.

from fakeit.

 avatar commented on June 14, 2024

Thanks for your effort! I will certainly try this out next week.

from fakeit.

 avatar commented on June 14, 2024

Hi, it works fine now. I have just a small remark:

At this line GCC 4.8 gives the warning ISO C++ forbids casting between pointer-to-function and pointer-to-object. Is there something we could do to avoid that warning?

from fakeit.

eranpeer avatar eranpeer commented on June 14, 2024

Sure, I'll fix it this weekend.
On Feb 5, 2015 9:37 AM, "Stijn Van Nieuwenhuyse" [email protected]
wrote:

Hi, it works fine now. I have just a small remark:

At this
https://github.com/eranpeer/FakeIt/blob/master/include/mockutils/gcc/VirtualTable.hpp#L80
line GCC 4.8 gives the warning ISO C++ forbids casting between
pointer-to-function and pointer-to-object. Is there something we could do
to avoid that warning?


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

from fakeit.

eranpeer avatar eranpeer commented on June 14, 2024

Done! I fixed the gcc warning.

from fakeit.

misery avatar misery commented on June 14, 2024

I have a similar crash here with Qt's QSharedPointer. Used current master branch!

#0 0x685d62 in fakeit::MethodProxyCreator<void>::methodProxy(unsigned int) [..]/external/fakeit.hpp:5815:20
#1 0x685459 in void fakeit::MethodProxyCreator<void>::methodProxyX<0>() [..]/external/fakeit.hpp:5820:20
#2 0x689a89 in fakeit::VirtualTable<ActivationContext>::dtor(int) [..]/external/fakeit.hpp:5615:13
#3 0x7fc23f73049f in QScopedPointerDeleter<ActivationContext>::cleanup(ActivationContext*) [..]/QtCore/qscopedpointer.h:54:9
#4 0x7fc23f730028 in QScopedPointer<ActivationContext, QScopedPointerDeleter<ActivationContext> >::~QScopedPointer() [..]/QtCore/qscopedpointer.h:101:9

The code:

Mock<ActivationContext> context;
When(Dtor(context));

QSharedPointer<AuthModel> model(new AuthModel(&context.get()));

from fakeit.

jessevdp avatar jessevdp commented on June 14, 2024

What happens when both the mock and the unique_ptr (that points to the mocked instance) are deleted?

{
Mock<SomeType> mock;
Fake(Dtor(mock));
std::unique_ptr<SomeType> ptr {&mock.get()};
} // <- both are deleted

I'm having some issues and I suspect it has to do with this..

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.