mockStruct

Version of mockStruct that accepts a compile-time mapping of function name to return values. Each template parameter must be a value of type ReturnValues

  1. auto mockStruct(T returns)
  2. auto mockStruct()
    mockStruct
    (
    T...
    )
    ()
    if (
    T.length > 0 &&
    from!"std.meta".allSatisfy!(isReturnValue, T)
    )

Examples

void fun(T)(T t) {
    t.foobar;
}
auto m = mockStruct;
m.expect!"foobar";
fun(m);
m.verify;
void fun(T)(T t) {
    t.foobar(2, "quux");
}

auto m = mockStruct;
m.expect!"foobar"(2, "quux");
fun(m);
m.verify;
int fun(T)(T f) {
    return f.timesN(3) * 2;
}

auto m = mockStruct(42, 12);
assert(fun(m) == 84);
assert(fun(m) == 24);
assert(fun(m) == 0);
m.expectCalled!"timesN";
void fun(T)(T t) {
    t.foobar(2, "quux");
}

auto m = mockStruct;
fun(m);
m.expectCalled!"foobar"(2, "quux");
auto m = mockStruct!(ReturnValues!("length", 5),
                     ReturnValues!("greet", "hello"));
assert(m.length == 5);
assert(m.greet("bar") == "hello");
m.expectCalled!"length";
m.expectCalled!"greet"("bar");
auto m = mockStruct!(ReturnValues!("length", 5, 3),
                     ReturnValues!("greet", "hello", "g'day"));
assert(m.length == 5);
m.expectCalled!"length";
assert(m.length == 3);
m.expectCalled!"length";

assert(m.greet("bar") == "hello");
m.expectCalled!"greet"("bar");
assert(m.greet("quux") == "g'day");
m.expectCalled!"greet"("quux");

Meta