โœ๏ธ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:

// Custon Assertion functions are best created with messages
function assertEven<T>(
  value: i32,
  message: string = "actual value should be even",
): void {
  // take advantage of compile time errors
  if (!isInteger(value)) {
    ERROR("assertEven should only be called with integers!");
  }
  
  // Tell the host the actual and expected values
  Actual.report(value);
  Expected.report("Even");

  // check if the first bit is 0
  let isEven = (value & 1) == 0;
  
  // use assert function with message
  assert(isEven, message);
  
  // Clear the host actual and expected values afterwards
  Actual.clear();
  Expected.clear();
}

Last updated