0

I wrote an unit test with hippomocks, but got error while compiling it.

The compiler is VS 2010.

How can I fix it ?

#include "hippomocks.h"
#include <functional>

using namespace HippoMocks;

struct A
{
    virtual void f(std::function<void (int)> arg);
};

int main(void)
{
    MockRepository mock;
    A* aptr = mock.Mock<A>();

    mock.ExpectCall(aptr, A::f);  // error

    return 0;
}

The output is :

main.cpp
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xlocale(323) : wa
rning C4530: C++ exception handler used, but unwind semantics are not enabled. S
pecify /EHsc
c:\users\cong\project\test\test\hippomocks.h(466) : error C2593: 'operator <<' i
s ambiguous
        c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\ostream(2
06): could be 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Trai
ts>::operator <<(std::_Bool)'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>
        ]
        c:\users\cong\project\test\test\hippomocks.h(441): or       'std::ostrea
m &HippoMocks::operator <<(std::ostream &,const HippoMocks::NotPrintable &)'
        while trying to match the argument list '(std::ostream, std::tr1::functi
on<_Fty>)'
        with
        [
            _Fty=void (int)
        ]
        c:\users\cong\project\test\test\hippomocks.h(463) : while compiling clas
s template member function 'void HippoMocks::printArg<T>::print(std::ostream &,T
,bool)'
        with
        [
            T=std::tr1::function<void (int)>
        ]
        c:\users\cong\project\test\test\hippomocks.h(614) : see reference to cla
ss template instantiation 'HippoMocks::printArg<T>' being compiled
        with
        [
            T=std::tr1::function<void (int)>
        ]
Praetorian
  • 106,671
  • 19
  • 240
  • 328
compass00
  • 167
  • 2
  • 9
  • 1
    You use no lambdas in the code above. Are you under the impression that `std::function` means lambda? – Yakk - Adam Nevraumont Jul 07 '14 at 20:41
  • HippoMocks is trying to print your function (in case of a call mismatch) and it has no idea how to print an std::function, and apparently MSVC has an equally weakly binding print function for a _Bool. Best thing would be to define some way of printing an std::function, say with an operator<<. – dascandy Aug 28 '14 at 14:06

1 Answers1

0

Enhancing @dascandy's comment here is how his method can look like. Place it after including hippomocks.h:

template<>
struct printArg<std::function<void (int)> >
{
    static inline void print(std::ostream &os, std::function<void (int)> arg, bool withComma)
    {
        if (withComma)
        {
            os << ",";
        }
        if (arg)
        {
            os << "true";
        }
        else
        {
            os << "false";
        }
    }
};

Note that I didn't test this very example but rather took our solution and adapted the type to the original post's example. I'd be happy to know whether this works for you.

Roland Sarrazin
  • 1,203
  • 11
  • 29