shouldThrow

Used for asserting that a expression will throw an exception.

void
shouldThrow
(
T : Throwable = Exception
E
)
(
lazy E condition
,
in string file = __FILE__
,
in size_t line = __LINE__
)

Parameters

condition
Type: E

The expression that is expected to throw the exception.

file
Type: string

The file name that the assert failed in. Should be left as default.

line
Type: size_t

The file line that the assert failed in. Should be left as default.

Throws

If expression does not throw, will throw an UnitTestException.

Examples

1 // Makes sure it throws with the message "test"
2 void noThrow(){};
3 void withThrow(){throw new Exception("test");};
4 
5 // Will throw an exception like "checkit.exception.UnitTestException@test/example.d(7): Expression did not throw"
6 noThrow.shouldThrow();
7 // Will throw an exception like "checkit.exception.UnitTestException@test/example.d(7): Expression did not throw"
8 noThrow.shouldThrow!Exception();
9 
10 // Will throw an exception like "checkit.exception.UnitTestException@test/example.d(7): Expected <UnitTestException>, but throw <object.Exception>"
11 withThrow.shouldThrow!UnitTestException();

Meta