Custom Assertions
Set your own expectations!
Custom assertions are possible, and the interface for reporting Actual and Expected values is exposed publicly.
For example, we can write a custom assertEven function like this:
1
// Custon Assertion functions are best created with messages
2
function assertEven<T>(
3
value: i32,
4
message: string = "actual value should be even",
5
): void {
6
// take advantage of compile time errors
7
if (!isInteger(value)) {
8
ERROR("assertEven should only be called with integers!");
9
}
10
11
// Tell the host the actual and expected values
12
Actual.report(value);
13
Expected.report("Even");
14
15
// check if the first bit is 0
16
let isEven = (value & 1) == 0;
17
18
// use assert function with message
19
assert(isEven, message);
20
21
// Clear the host actual and expected values afterwards
22
Actual.clear();
23
Expected.clear();
24
}
Copied!
Last modified 1yr ago
Copy link