Git Product home page Git Product logo

Comments (3)

rollbear avatar rollbear commented on September 17, 2024

I'm not sure. You may have to make your example a bit more detailed and explicit.

There are many things you can do, though. For example, you can use a local variable in the test, that keeps track of what has been seen, and a .LR_SIDE_EFFECT(...) that monitors/updates that variable, and maybe does something special depending on its value.

If you're asking if a mock can know that it's called from do2Thing(), then, probably not, depending on circumstances. You need a way to be able to identify do2Thing() in the call stack. Maybe it can be done by analyzing the result from std::stacktrace() if you're using C++23?

from trompeloeil.

jim-king-2000 avatar jim-king-2000 commented on September 17, 2024

Here is the pseudo-code which is similar to our production code in practice:

char *do1Thing()
{
    char *info = new char[64];
    // populate info with some important information
    return info;
}

void do2Things()
{
    // do some dangerous operations here
}

void do3Thing(char *info)
{
    // use info here
    delete info;
}

void ProductionCode()
{
    char *info = do1Thing();
    do2Thing();
    do3Thing(info);
}

In the code above, we create a dynamic memory in do1Thing() and we release it in do3Thing().

Now we need to write a unit test. The case is to "make sure all of the resources are released when any exception is thrown". So we need invoke ProductionCode() in our test code and let do2Thing() throw an exception. Obviously, in the production code, do2Thing() seldom fails. That is why we need mock it here.

#include "ProductionCode.h"

TEST()
{
    mock("do2Thing", [](){ throw "exception"; });
    ProductionCode();
    mock.assertThrow("exception");
}

Here we write a fake do2Thing and let it throw an exception. Then we call ProductionCode() and expect it could invoke the mocked version of do2Thing(). If it were true, we can see memory leak when running this case with valgrind or other memory debugger.

In other languages, such as node.js, we can do it like this. Every function including the system function could be mocked. Does this way work in C++? If no, what is the best practice?

from trompeloeil.

jim-king-2000 avatar jim-king-2000 commented on September 17, 2024

See the mock of node.js native test:

import { mock } from "node:test";

const tt = {
  f0() {
    console.log("f0");
    this.f1();
  },

  f1() {
    console.log("f1");
  },
};

mock.method(tt, "f1", () => console.log("mock"));
tt.f0();

The output is:

f0
mock

It means that tt.f1() has been successfully mocked. Is there any mechanism similar to the one of node.js?

from trompeloeil.

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.