eranpeer / FakeIt

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

Single header does not compile with catch2, MSVC2015 #124

Closed dmcminn closed 2 years ago

dmcminn commented 6 years ago

Hello,

I am trying out FakeIt (master, from 13th November 2017) with Catch2 (2.1.0 release). I'm using MSVC2015. I have set the include path to point to both the catch and fakeit single header folders. My test code is like the sample in the readme.md.

#include <catch.hpp>
#include <fakeit.hpp>
#include <string>

using namespace fakeit;

struct SomeInterface {
    virtual int foo(int) = 0;
    virtual int bar(std::string) = 0;
};

TEST_CASE("SomeCase", "Some test")
{
    Mock<SomeInterface> mock;
    When(Method(mock, foo)).Return(5);
    SomeInterface &i = mock.get();
    i.foo(1);
    Verify(Method(mock,foo));
    Verify(Method(mock,foo).Using(1));
}

But I get compiler errors.

1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1173): error C2319: 'try' must be followed by a compound statement. Missing '{'
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1173): error C2059: syntax error: '('
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1173): error C2143: syntax error: missing ';' before '{'
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1173): error C2039: 'handle': is not a member of 'Catch::AssertionHandler'
1>  c:\users\user\appdata\local\dl\sw\testing\catch2\2.1.0\catch2-2.1.0\single_include\catch.hpp(1462): note: see declaration of 'Catch::AssertionHandler'
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1173): error C2059: syntax error: 'catch'
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1173): error C3927: '->': trailing return type is not allowed after a non-function declarator
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1173): error C3484: syntax error: expected '->' before the return type
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1173): error C3613: missing return type after '->' ('int' assumed)
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1173): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1173): error C2146: syntax error: missing ';' before identifier 'complete'
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1201): error C2504: 'DefaultFakeit': base class undefined
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1221): error C2143: syntax error: missing ';' before '&'
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1221): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1221): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1227): error C3646: '_formatter': unknown override specifier
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1227): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1228): error C3646: '_catchAdapter': unknown override specifier
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1228): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1217): error C3668: 'CatchFakeit::accessTestingFrameworkAdapter': method with override specifier 'override' did not override any base class methods
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1208): error C2065: '_formatter': undeclared identifier
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1208): error C2614: 'CatchFakeit': illegal member initialization: '_formatter' is not a base or member
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1208): error C2614: 'CatchFakeit': illegal member initialization: '_catchAdapter' is not a base or member
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1218): error C2065: '_catchAdapter': undeclared identifier
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1231): error C2059: syntax error: '}'
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1231): error C2143: syntax error: missing ';' before '}'
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1233): error C3083: 'CatchFakeit': the symbol to the left of a '::' must be a type
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1233): error C2039: 'getInstance': is not a member of 'fakeit'
1>  c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(1091): note: see declaration of 'fakeit'
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(9235): error C2065: 'Fakeit': undeclared identifier
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(9236): error C2065: 'Fakeit': undeclared identifier
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(9237): error C2065: 'Fakeit': undeclared identifier
1>c:\users\user\appdata\local\dl\sw\testing\fakeit\fakeit-master\single_header\catch\fakeit.hpp(9238): error C2065: 'Fakeit': undeclared identifier

It looks as if the compiler is seeing the parameter list to INTERNAL_CATCH_TRY on line 1173 as something that shouldn't be there that is subsequently causing a lot of the other errors.

On line 1175 it appears the handle() method has been renamed in catch to handleMessage().

jlkalberer commented 6 years ago

Any workaround for this?

jlkalberer commented 6 years ago

Change the code so it looks like this:

                        INTERNAL_CATCH_TRY { \
                    CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \
                    catchAssertionHandler.handleMessage(resultWas, fomattedMessage); \
                    CATCH_INTERNAL_UNSUPPRESS_PARENTHESES_WARNINGS \
            } INTERNAL_CATCH_CATCH(catchAssertionHandler) { \
                    INTERNAL_CATCH_REACT(catchAssertionHandler) \
            }
emersonmx commented 6 years ago

I have same problem with catch 2.1.0 in linux with g++ 7.2.1. I apply the patch of @jlkalberer and everything is ok.

Edit: https://github.com/emersonmx/FakeIt/blob/master/config/catch/CatchFakeit.hpp#L90

mje-nz commented 6 years ago

I have the same problem with Catch 2.1.2 on macOS with LLVM 9.0.0, and @jlkalberer's patch fixed the error for me too.

sidwarkd commented 6 years ago

Confirmed @jlkalberer 's patch fixed error on macOS Catch 2.2.2 with LLVM 9.1.0

helmesjo commented 5 years ago

@eranpeer Sorry for spamming, but I think you should do a sweep and close fixed issues. :)

FranckRJ commented 2 years ago

It was fixed in #125 which is available in FakeIt 2.0.5.