Skip to content

Callable & Exception Operations

Test function behavior with these assertions.

throwException

Asserts that a callable throws a specific exception type.

expect({
throw new CustomException("error");
}).to.throwException!CustomException;

With Message Checking

expect({
throw new Exception("specific error");
}).to.throwException!Exception.withMessage.equal("specific error");

throwAnyException

Asserts that a callable throws any exception.

expect({
throw new Exception("error");
}).to.throwAnyException();

haveExecutionTime

Asserts constraints on how long a callable takes to execute.

import core.time : msecs;
expect({
fastOperation();
}).to.haveExecutionTime.lessThan(100.msecs);

allocateGCMemory

Asserts that a callable allocates GC memory.

expect({
auto arr = new int[1000];
return arr.length;
}).to.allocateGCMemory();

Negation

expect({
int[4] stackArray = [1, 2, 3, 4];
return stackArray.length;
}).to.not.allocateGCMemory();