rollbear / trompeloeil

Header only C++14 mocking framework
Boost Software License 1.0
802 stars 85 forks source link

trompeloeil + doctest Fail with VS2022 #308

Closed git45016683 closed 1 year ago

git45016683 commented 1 year ago

i wrote demo code like below just try trompeloeil with VS2022, but it can not work。 code:

#include <iostream>
#include "freeFuncHeader.h"
// freeFuncHeader.h just like:
// #ifdef __cplusplus
// extern "C" {
// #endif
//  int iDownLoad();
// #ifdef __cplusplus
// }
// #endif

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "doctest/trompeloeil.hpp"

class freeFuncMock {
public:
    MAKE_MOCK0(iDownLoad, int());  // this line show errors: error C2061: 语法错误: 标识符“iDownLoad”; warning C4183: “MAKE_MOCK0”: 缺少返回类型;假定为返回“int”的成员函数; error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int;
};
extern freeFuncMock freeMock;

int funcCallAPIneedmock() {
    int l_iRet = 0;
    l_iRet = iDownLoad();
    return l_iRet;
}

TEST_CASE("demo2doctestNOTmock") {
    CHECK(funcCallAPIneedmock() == -1);
}

TEST_CASE("demo2doctestBYmock") {
    CHECK(funcCallAPIneedmock() == 6);
}

extern "C" {
    int iDownLoad(){
        return freeMock.iDownLoad(); // this line show error:error C2039: "iDownLoad": 不是 "freeFuncMock" 的成员
    }
}

please help me fix this

rollbear commented 1 year ago

I'm afraid you have to give me something more to work with. I don't read Chinese, so I'm relying on google-translate to tell me the meaning of the error messages you quote, and the first says "Missing type specifier - int assumed. Note: C++ does not support default int", which makes sense, except that Trompeloeil always uses explicit type specifiers so that should not happen.

I tried as best I could to convert your case to only Trompeloeil and not doctest, in compiler explorer, and then I get this, which compiles and works fine.

https://godbolt.org/z/6sfex7qo8

Tell me in what important way this differs from your case, and also, please, tell me what version of Trompeloeil you're using.

git45016683 commented 1 year ago

Thanks for your response. I found the different between my demo with your‘s https://godbolt.org/z/6sfex7qo8

then i change the include form

#include "doctest/trompeloeil.hpp"

to

#include "trompeloeil.hpp"

it works fine. thanks.