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

Can't stub method with vector of unique_ptr parameter #297

Closed otrempe closed 1 year ago

otrempe commented 1 year ago
class MyInterface
{
public:
    virtual ~MyInterface() = default;

    virtual void MyFunction( std::vector< std::unique_ptr< int > > uniquePtrs_ ) = 0;
};

BOOST_AUTO_TEST_CASE( MyTest )
{
    fakeit::Mock< MyInterface > mock;
    fakeit::Fake( Method( mock, MyFunction ) );
}

yields a compiler error where the copy constructor of unique_ptr is invoked in MockImpl::MethodMockingContextImpl::getOriginalMethodCopyArgsInternal(int)

otrempe commented 1 year ago

Sadly, std::is_copy_constructible<std::vector>:: returns true.

There might be an easy fix if the enable_if could use a more reliable check.

otrempe commented 1 year ago

Not so easy finally...

otrempe commented 1 year ago

Using a workaround proposed here: https://stackoverflow.com/a/18405291

This could be fixed for vector using

template<template<typename>class test, typename T>
struct smart_test : test<T> {};

template<template<typename>class test, typename T, typename A>
struct smart_test <test, std::vector<T, A>> : smart_test < test, T> {};

template<typename T>
using smart_is_copy_constructible = smart_test < std::is_copy_constructible, T >;

There is no generic solution and other containers should be added as needed to the smart_test trait. Just having it working for vectors only is already a huge step up.

I might convert this into a pull request.

FranckRJ commented 1 year ago

I wasn't aware of the issue, thanks for the fix. I agree with you, having the issue partially fixed is better than not fixing it at all.

FranckRJ commented 1 year ago

Fixed by #298 in FakeIt 2.3.1.