assert
It raises AssertionDefect on failure and the program terminates.
To ignore assert statements, compile with the --assertions:off flag or the -d:danger definition.
Output:
.../assert.nim(5) assert
.../lib/std/assertions.nim(45) failedAssertImpl
.../lib/std/assertions.nim(40) raiseAssert
.../lib/system/fatal.nim(62) sysFatal
Error: unhandled exception: .../assert.nim(5, 1) `1 + 1 == 3` [AssertionDefect]
assert is suitable for development-time checks that can be safely omitted in production.
check
It's from std/unittest . It's designed for tests, gives nicer output showing exactly what values were compared.
Use check inside test blocks for unit tests.
import std/unittest
func twice(n: int): int =
2 * n
test "twice":
check twice(5) == 10
echo "The double of 3 is ", twice(3)
Output:
[OK] twice
The double of 3 is 6
𝥶Notice that unit tests can be mixed with normal code.
When a check fails, it prints something like this:
.../check.nim(11, 17): Check failed: twice(5) == 12
twice(5) was 10
[FAILED] twice
The double of 3 is 6
If a unit test fails, the program continues to run.