Writing own asserts
Standard assertions are enough for most cases, but sometimes for specific cases you may need to write your own assertion to encapsulate the validation logic.
When writing assertions, the most important thing is that the tester can trace
all the way from the beginning of the assertion in the test, to the actual call
to t.fail() inside your assertion. Without this, if there are two assertions,
the fall of one will be indistinguishable from the fall of the other.
To avoid this, each function called on the path to t.fail() must have
the #[test_helper] attribute, then the compiler will automatically forward the
necessary data to t.fail().
Let's look at a simple example:
There are two main points to consider in this example:
- The
to_befunction is marked with the#[test_helper]attribute, which allows the compiler to forward data tot.fail(). - The
to_befunction calls theon_assertmethod on thetobject to inform the tester that an assertion has been called. This will allow the tester to display at the end of testing the number of assertions that were called.
Using this assertion in a test would look like this: