Comments (5)
hey man. gMock has two major drawbacks:
- it has no support for single functions (outside a class)
- it requires member class to be declared as virtuals.
The second can be workarounded somehow, but the first is a big issue.
from fff.
FFF is not the right tool for mocking C++ code because of name-mangling.
You should be using a C++ mocking framework:
it has no support for single functions (outside a class)
it requires member class to be declared as virtuals.
That's because both cases are usually a sign that your architecture isn't quite right. You can't use run-time polymorphism unless your your methods are declared as virtual
. Using stand-alone functions means that you're abandoning OOP. If you're following the general software engineering practices, you won't have stand-alone functions and your concrete classes will be implementations of an interface class (they'll use override
). C++ doesn't have an official designation for interfaces like C# does.
I understand that the paragraph above isn't always true for deeply embedded projects. I'll way that C++ isn't a good fit to those because C++ binaries tend to be larger, especially if you use templates extensively. You're probably better off using C with a well-designed architecture (in that case FFF will fit like a glove).
If your CPU has a MMU and lots of memory, you should be adhering to SW engineering practices and GTest's limitations won't be an issue.
If you have to use C++ and you can not use virtual functions, you can always create your own stubs, like in @cakira examples. I'd never hack the FFF header, I'd just wrap them on C++ stubs.
from fff.
Gracias, @fgr-17 . So gMock isn't a good option to my projects.
As a proof of concept about using fff with C++, I put the following macros in one of my C++ file:
#define FAKE_VOID_CLASS_FUNC0(classname, funcname) \
FAKE_VOID_FUNC(classname##__##funcname); \
void classname::funcname() { return classname##__##funcname(); }
#define FAKE_VALUE_CLASS_FUNC0(rettype, classname, funcname) \
FAKE_VALUE_FUNC(rettype, classname##__##funcname); \
rettype classname::funcname() { return classname##__##funcname(); }
#define FAKE_VALUE_CLASS_FUNC1(rettype, classname, funcname, arg0type) \
FAKE_VALUE_FUNC(rettype, classname##__##funcname, arg0type); \
rettype classname::funcname(arg0type arg0) { \
return classname##__##funcname(arg0); \
}
And they worked as intended in my code, because I could use:
FAKE_VOID_CLASS_FUNC0(BLEAdvertising, start);
FAKE_VALUE_CLASS_FUNC0(BLEServer *, BLEDevice, createServer);
FAKE_VALUE_CLASS_FUNC1(BLEService *, BLEServer, createService, const char *);
Instead of:
FAKE_VOID_FUNC(BLEAdvertising__start);
FAKE_VALUE_FUNC(BLEServer *, BLEDevice__createServer);
FAKE_VALUE_FUNC(BLEService *, BLEServer__createService, const char *);
void BLEAdvertising::start() { return BLEAdvertising__start(); };
BLEServer *BLEDevice::createServer() { return BLEDevice__createServer(); }
BLEService *BLEServer::createService(const char *uuid) {
return BLEServer__createService(uuid);
}
Limitation:
It didn't work to mock up void BLEDevice::init(std::string deviceName)
because when I tried to call FAKE_VOID_FUNC(BLEDevice__init, std::string)
, the compiler generated the warning below
warning: ‘void* memset(void*, int, size_t)’ clearing an object of type ‘BLEDevice__init_Fake’ {aka ‘struct BLEDevice__init_Fake’} with no trivial copy-assignment; use assignment or value-initialization instead [-Wclass-memaccess]
Understandably, one cannot apply memset() in a std::string variable.
from fff.
Almost two years have passed since I opened this ticket, and now I tend to think that I was trying to do too much with fff.
The truth is, I attempted to learn CppUMock and Google Mock, but for some reason, I find them less intuitive compared to ThrowTheSwitch/Unity + meekrosoft/fff. As a result, I ended up trying "to use a nail as a screw".
I'll try harder to learn CppUMock or a similar tool and leave Unity + fff for pure C projects.
from fff.
As a last tidbit, I use Google Test + FFF to test C code. I find that much, much better than ThrowTheSwitch/Unity. I get an excellent test harness framework while still being able to mock C code with FFF. Just note that the code under test is C, not C++. When I have to test C++ code, I use GTest + GMock.
from fff.
Related Issues (20)
- fatal error: 'tr1/tuple' file not found HOT 1
- SET_RETURN_SEQ should reset return_val_seq_idx HOT 1
- Can FFF be used to generate the actual fake implementation?
- Argument history storing derefenced pointers HOT 1
- warning: ISO C99 requires at least one argument for the "..." in a variadic macro HOT 1
- Is it possible to mock static functions? HOT 1
- #pragma pack(1) breaks Function Return Value Sequences HOT 1
- How to install and to use? HOT 3
- Allow support for std::function as the `custom_fake` type HOT 1
- Linking issue in the example project HOT 1
- fake.callcount is always 0 when reuse a fake across multiple test-suites HOT 5
- FFF generated reset functions have mangled names HOT 2
- C++ Support for FFF Framework HOT 1
- Question: Releases and project maintenance
- Mocking static methods of the class HOT 3
- Is there a forum or fff community page for answering questions? HOT 2
- Automatically generate mocks: fff-mock-gen HOT 1
- include fff.h once
- Ddf
- [QUESTION] How to mock GTK library's GTK_IS_DIALOG macro?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fff.