How to add a stub for a Class member function?
For example, class A has a public function foo().
class A {
public:
int Foo();
}
I add the stub like this:
void CppTest_Stub_Foo() {
CPPTEST_STUB_CALLED("A::Foo");
int __return = 0;
if (CPPTEST_STUB_HAS_CALLBACK()) {
CPPTEST_STUB_CALLBACK(int __return, const A __this);
CPPTEST_STUB_INVOKE_CALLBACK(&__return, this);
}
}
But it throws execution error. The detail in real project execution is as follows.
/ehy-2023-1226-1042/ehy_main/modules/base/src/object_motion.cpp
1. Test execution: error compiling file.
during IPA pass: profile
TEST_HARNESS: In member function ‘float ehy::ObjectMotion::GetAx() const’:
TEST_HARNESS:6:71: internal compiler error: in coverage_begin_function, at coverage.c:656
0x7f22b0d67082 __libc_start_main
../csu/libc-start.c:308
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <file:///usr/share/doc/gcc-9/README.Bugs> for instructions.
Best Answer
-
@sorel ,
The stub code that you provided does not look right, but I assume that some of the issues came from the stub code being interpreted by HTML renderer of this forum post. In any case, I created a stub like this and it works fine:
int (::A::CppTest_Stub_Foo) (void) { CPPTEST_STUB_CALLED("A::Foo"); int __return = 0; if (CPPTEST_STUB_HAS_CALLBACK()) { CPPTEST_STUB_CALLBACK_PARAMS(int* __return, ::A* __this); CPPTEST_STUB_INVOKE_CALLBACK(&__return, this); } return __return; }
A few issues that I can point out in your stub:
1. The stub signature. When stubbing a method of a class, the stub itself has to be a method as well. In your case it is a function: "void CppTest_Stub_Foo()". It would not work as a stub for a class method.
2. The macro "CPPTEST_STUB_CALLBACK" does not exist. It should be "CPPTEST_STUB_CALLBACK_PARAMS".
3. Parameters to "CPPTEST_STUB_CALLBACK_PARAMS" should be pointers - see my example above.While I could not reproduce the internal compiler error issue with my GNU GCC 9.4.0, I think that fixing the stub code should help.
I would also advise to create user stubs using C/C++test's "Create User Stub" functionality available from "Stubs" view instead of adding stubs manually (see https://docs.parasoft.com/display/CPPTESTPROEC20232/Adding+and+Modifying+Stubs). C/C++test will create all the boilerplate code exactly as needed, with all the proper declarations and stub structure. You can always trim down the auto-created stub code later, after the stub is proven to work.
1