Coder Social home page Coder Social logo

Comments (5)

fgr-17 avatar fgr-17 commented on September 18, 2024 1

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.

leonardopsantos avatar leonardopsantos commented on September 18, 2024 1

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.

cakira avatar cakira commented on September 18, 2024

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.

cakira avatar cakira commented on September 18, 2024

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.

leonardopsantos avatar leonardopsantos commented on September 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.