trompeloeil icon indicating copy to clipboard operation
trompeloeil copied to clipboard

Add WILL expectation

Open chghehe opened this issue 7 months ago • 2 comments

We still need a straight way to completely define mock implementation at least as a fallback method, especially for C++ coroutines.

E.g. (by now, trompeloeil cannot handle such coroutines):

REQUIRE_CALL(m, foo())
    .WILL([](auto&, auto& arg2) { 
        co_yield 1;
        co_await arg2.send();
        co_yield "qwerty";
        co_yield 3.14;
        throw; 
        })
    .TIMES(1)
    ; 

Operator WILL might be used both for functions and coroutines. IMO, this shall override (or conflict) with any other behaviour-operators like RETURN, SIDE_EFFECT, etc.

chghehe avatar Apr 18 '25 11:04 chghehe

Thank you. I've had this as a vague idea in the back of my head for many years, mostly because it allows you to control what you capture and how. However, I have absolutely no idea about how to do this for coroutines.

rollbear avatar Apr 21 '25 08:04 rollbear

I guess, the implementation for coroutines will be the same as for functions, except some things around lifetime management.

foo() return type is known and it is enough just to call co-lambda and forward return value. I've made a mistake in the example above:

.WILL([](auto&, auto& arg2) -> /*foo return type*/ { 
        co_yield 1;
        co_await arg2.send();
        co_yield "qwerty";
        co_yield 3.14;
        throw; 
        })

Of course, lambda shall return foo return type. As a suggestion, since the foo() return type is known, macro .WILL may prepare lambda "header" and the user shall only provide the body:

.WILL({ 
        co_yield 1;
        co_await _2.send();
        co_yield "qwerty";
        co_yield 3.14;
        throw; 
        })

Not sure, that it is the great idea, but considerable.

chghehe avatar Apr 21 '25 09:04 chghehe