Asserting
Assertions can be used to ensure a certain condition is met when
running a contract. The predefined function Assert.assert is used to
check whether a given a Boolean condition holds. The function
Assert.some is used to check if an option value is not None. The
function assert_some_with_error is like Assert.some but an error
message can be given. Whenever the assertion fails, the contract will
stop and an error will be left on the execution stack.
function incr_if_true (b: bool, n: int) : int {
Assert.assert(b);
return n+1;
};
function incr_if_some (b: option<unit>, n: int) : int {
Assert.some(b);
return n+1;
};
You can use Assert.Error.assert or Assert.Error.some to use a
custom error message.
const incr_if_true = (b: bool, n: int) : int => {
Assert.Error.assert (b, "My custom error message.");
return n+1;
};
Likewise, we can check for None instead of Some by using
Assert.none and Assert.Error.none.