Testing
Test blocks live in the module they test, and test mode runs them.
Examples: res\examples\language\testing holds small, commented programs for this page's features, each listing the output it prints; run one with adam build <file> -r.
Test blocks
Test blocks (test "name" begin .. end;) follow a module's end.. A test's name is not empty and is unique in its module, and a test block may declare its own var block. With @unitTestMode on; the exe runs every test block instead of its body, and its exit code is the number of failed tests, at most 255.
module exe testing_runner;
@unitTestMode on;
// AValue squared
func Square(const AValue: int64): int64;
begin
return AValue * AValue;
end;
begin
println("the body does not run in test mode");
end.
test "squares a positive number"
begin
testAssertEqualInt(9, Square(3));
end;
test "squares a negative number"
begin
testAssertEqualInt(16, Square(-4));
end;
╔══════════════════════════════════════════════════════════════╗
║ Unit Test Runner ║
╚══════════════════════════════════════════════════════════════╝
Running 2 test(s)...
✅ PASS: squares a positive number
✅ PASS: squares a negative number
════════════════════════════════════════════════════════════════
Results: 2 passed, 0 failed, 2 total
════════════════════════════════════════════════════════════════
With @unitTestMode off or not given, test blocks are checked but not built in, and the body runs.
Asserts
- One value:
testAssert,testAssertTrue,testAssertFalse,testAssertNil,testAssertNotNilandtestFail. - Two values:
testAssertEqualInt,testAssertEqualUInt,testAssertEqualFloat,testAssertEqualStr,testAssertEqualBoolandtestAssertEqualPtr.
The runner runs the tests of every imported unit first, then the exe's own. A failed assert prints its file, line and both values, and the test goes on; an exception raised in a test fails it, reported at the test's own line.