trompeloeil icon indicating copy to clipboard operation
trompeloeil copied to clipboard

Mocking non-awaitable coroutines

Open chghehe opened this issue 6 months ago • 1 comments

Using Conan package trompeloeil/48 and gcc-14.

The code below

#include <catch2/catch_all.hpp>
#include <catch2/trompeloeil.hpp>

#include <generator>

class Mock
{
public:
  MAKE_MOCK0(fibonacci,std::generator<int>());
};

TEST_CASE("fibonacci 7")
{
  Mock m;
  ALLOW_CALL(m, fibonacci())
    .CO_YIELD(0)
    .CO_YIELD(1)
    .CO_YIELD(1)
    .CO_YIELD(2)
    .CO_YIELD(3)
    .CO_YIELD(5)
    .CO_YIELD(8)
    .CO_RETURN()
    ;

  REQUIRE_THAT(m.fibonacci(), Catch::Matchers::RangeEquals(std::array{0, 1, 1, 2, 3, 5, 8}));
}

produces compile time error:

trompeloeil/coro.hpp:54:56: error: ‘class std::generator’ has no member named ‘await_resume’

Seems that non-awaitable coroutines are not supported (yet).

More than that, I cannot directly mock these functions (like Mock::fibonacci()) with RETURN() statement due to static assertions.

Since at this moment std::generator<> is implemented in gcc-14 only, the following implementations might be used for reproduction purposes:

  • <experimental/generator> with MSVC 192
  • reference implementation from https://github.com/lewissbaker/generator
  • any other implementation of synchronous co_yield generator

chghehe avatar Aug 14 '24 07:08 chghehe