Git Product home page Git Product logo

Comments (6)

rollbear avatar rollbear commented on July 16, 2024

All expectations set up are valid in the scope of the set up, so ALLOW_CALL() in setupExpectations() is alive until the end of the function setupExpectations() and therefore no longer available in the line of the comment.

You can use NAMED_ALLOW_CALL() to bind the expectation to a variable that you choose maintain the lifetime of.

from trompeloeil.

ninkibah avatar ninkibah commented on July 16, 2024

Thanks for the quick answer.

I went looking for examples with NAMED_ALLOW_CALL and didn't find any, but I guess it would look something like the folowing:

struct Real {
  virtual int func() const = 0;
  virtual ~Real() = default;
};

struct Mock : public Real {
  MAKE_MOCK0(func, int(), const);
  ~Mock() override = default;
};

auto setupExpectations(Mock& obj) {
  return NAMED_ALLOW_CALL(obj, func()).RETURN(1729);
}

TEST_CASE("Expectations in their own function") {
  Mock mock;
  auto expectation = setupExpectations(mock);

  REQUIRE(mock.func() == 1729); // Throws exception saying No match for call of func with signature int() with.
}

However, my mock needs 10-15 expectations (don't ask), so I guess I will have to create a vector of std::vector<std::unique_ptr<expectation>> and push each NAMED_ALLOW_CALL into it, and then return the vector. This is a bit ugly. I suspect that people rarely use this feature.

Tomorrow, I'll make a PR for a documentation change to help other people.

from trompeloeil.

rollbear avatar rollbear commented on July 16, 2024

That looks right. I've done it that way too. The vector is nice. I'm a bit surprised that you didn't find any examples of NAMED_ALLOW_CALL. Here's the entry in the reference manual: https://github.com/rollbear/trompeloeil/blob/main/docs/reference.md#NAMED_ALLOW_CALL, with an example. I guess there's not a good enough entry point to find it?

from trompeloeil.

ninkibah avatar ninkibah commented on July 16, 2024

I was looking in the cookbook, which seemed the best place to start. All the examples there were ALLOW_CALL or REQUIRE_CALL, so I presumed that's what I should use. I'll add a section there tomorrow, when I have time.

from trompeloeil.

ninkibah avatar ninkibah commented on July 16, 2024

Good to see that I hadn't found missing functionality in your library :-)

from trompeloeil.

rollbear avatar rollbear commented on July 16, 2024

Is there something I should do here, or can I close the issue?

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.