assert
assert.tolk standard library file
Module for assertion functions.
This module provides the foundational assertion logic used in tests.
While expect provides a high-level, fluent API, Assert offers direct static methods for verification.
Examples:
// Basic equality
Assert.equal(2 + 2, 4, "Math check");
// Gas usage check for a function
Assert.consumesLessThan(fun() {
someExpensiveOperation();
}, 10000);Definitions
ASSERTION_FAILED
const ASSERTION_FAILED = 567Assert
struct AssertRoot module for the assertion API.
This structure holds static methods for the assertion API.
Example:
Assert.equal(10, 10);For higher-level interface, use the expect function.
Example:
expect(10).toEqual(10);Assert.equal
fun Assert.equal<V1, V2>(left: V1, right: V2, message: string = "", location: string = ""): voidAsserts that two values are equal. If not, throws ASSERTION_FAILED.
Optional message can be provided to describe the assertion. Optional location can be provided to specify the location of the assertion to be printed in the error message.
This is a low-level function. Expectation API provides a higher-level interface with automatic location tracking.
Source codeAssert.notEqual
fun Assert.notEqual<V1, V2>(
left: V1,
right: V2,
message: string = "",
location: string = "",
): voidAsserts that two values are NOT equal. If they are, throws ASSERTION_FAILED.
Optional message can be provided to describe the assertion. Optional location can be provided to specify the location of the assertion to be printed in the error message.
This is a low-level function. Expectation API provides a higher-level interface with automatic location tracking.
Source codeAssert.fail
fun Assert.fail(message: string, location: string = ""): neverFails the test with the given optional message.
Optional message can be provided to describe the failure. Optional location can be provided to specify the location of the failure to be printed in the error message.
Source codeAssert.assume
fun Assert.assume(condition: bool, message: string = "", location: string = ""): voidRejects the current fuzz input when condition is false.
Outside fuzz tests this is reported as a normal test failure.
Source codeAssert.consumesLessThan
fun Assert.consumesLessThan<Ret>(fn: () -> Ret, expected: int): RetAsserts that the function consumes less than or equal to the expected amount of gas. Returns the callback result.
Gas is compared using end - start - 26.
The fixed 26 gas measurement overhead is subtracted before the <= expected check.
Example:
get fun `test gas consumption`() {
val result = Assert.consumesLessThan(fun() {
// do something that consumes gas
// (capturing outer variables is allowed)
return 42;
}, 100);
Assert.equal(result, 42);
}Assert.equalDecimal
fun Assert.equalDecimal<T>(
left: T,
right: T,
decimals: int,
message: string = "",
location: string = "",
): voidAsserts that two values are equal as decimals with given precision.
Optional message can be provided to describe the assertion. Optional location can be provided to specify the location of the assertion to be printed in the error message.
This is a low-level function. Expectation API provides a higher-level interface with automatic location tracking.
Source codeLast updated on