Git Product home page Git Product logo

Comments (5)

syyyr avatar syyyr commented on August 14, 2024

Also, one more thing: I'm having problems with telling my MyApp to quit. It has an exit() method, so if I could somehow know when all the expectations get fullfilled, I could just call it. Or, if I could attach side effects to existing expectations, I could just do this:

MyApp app(mock);
expectations.back().add_lr_side_effect(app.exit());
app.exec();

from trompeloeil.

rollbear avatar rollbear commented on August 14, 2024

I'm really not very familiar with doctest, and I'm not at all sure how subcases work.

What I think you can do is to create a separate collection of side effects that you want to run, and you set your expectation's .SIDE_EFFECT() to run all of them. Then you can add to, and subtract from, that collection in your subcases.

Something like this:

DOCTEST_TEST_CASE("MyTest")
{
	auto mock = new MockInterface();
	std::vector<std::unique_ptr<trompeloeil::expectation>> expectations;
        std::list<std::function<void()>> side_effects;
	auto init_expectation = NAMED_REQUIRE_CALL(*mock, init())
                                .LR_SIDE_EFFECT(for (auto& f : side_effects) { f(); });

	DOCTEST_SUBCASE("someMethod") {
                auto i = side_effects.insert(side_effects.end(), [&](){ /* blah */ });
                // work
                side_effects.erase(i);
	}

	DOCTEST_SUBCASE("otherMethod") {
                auto i = side_effects.insert(side_effects.end(), [&](){ /* bleh */ });
                // other work
                side_effects.erase(i);
	}

	// After setting up all the expectations, actually run the app.
	MyApp app(mock);
	// exec blocks until the app closes.
	app.exec();
}

For your latter problem, you can query in expectation if it is satisfied or not. See https://github.com/rollbear/trompeloeil/blob/main/docs/reference.md#is_satisfied. Although, I do find it very odd that your test program doesn't know when the unit under test has reached it's conclusion.

from trompeloeil.

syyyr avatar syyyr commented on August 14, 2024

The point of DOCTEST_SUBCASE is that for every leaf subcase, the test is run exactly once, and every iteration it changes the "current" leaf. The advantage is that you can reuse the init/common code outside of the subcases.

What I think you can do is to create a separate collection of side effects that you want to run, and you set your expectation's .SIDE_EFFECT() to run all of them. Then you can add to, and subtract from, that collection in your subcases.

This is more or less what I do in my last code example:

std::function<void()> fn;
auto init_expectation = NAMED_REQUIRE_CALL(*mock, init()).LR_SIDE_EFFECT(fn());

DOCTEST_SUBCASE("someMethod") {
	fn = invoke_side_effect_for_someMethod;
	expectations.emplace_back(NAMED_REQUIRE_CALL(*mock, someMethod()));
}

just with a single function, without a list. It's a fine solution, I don't mind using that, I just thought that maybe there could be a way to somehow set the side effect "in runtime", as in, use .LR_SIDE_EFFECT after the expectation has been created. But I understand, that it might not be possible given how many checks are in the macros (and I guess they are compile time checks).

For your latter problem, you can query in expectation if it is satisfied or not. See https://github.com/rollbear/trompeloeil/blob/main/docs/reference.md#is_satisfied.

I can't do this because after I run .exec() I can only run code inside side effects.

Although, I do find it very odd that your test program doesn't know when the unit under test has reached it's conclusion.

The way my program works is that I run it via .exec(), then it starts listening for events, and responds to them. The app itself doesn't know that it's being tested. The tests do not do anything until after I run .exec(), because the test code is in the side effects, and expectations. After all the expectations are satisfied, the app continues to run, because no one told it, it should stop. Another idea: maybe trompeloeil coud expose some sort of a callback mechanism, that would notify me, whenever an expectation is completed?

This is maybe bending trompeloeil (and doctest) too much, and my method of testing might be a little weird. Unfortunately, I haven't found of a better way to test apps that have an event loop, than define a bunch of expectations and attach side effects beforehand, and then run the app and let it do its thing. But of course I understand that figuring out how to test my app isn't in the scope of this project.

from trompeloeil.

rollbear avatar rollbear commented on August 14, 2024

It's doable, I think, but it seems like a lot of work for a very special case that is quite easy to achieve anyway.

from trompeloeil.

syyyr avatar syyyr commented on August 14, 2024

Okay, no worries. I thought it wouldn't be so easy. I'm closing this, but feel free to reopen, if you want to track this somehow.

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.