Awkl
Loading...
Searching...
No Matches
test_basic.awk

Basic example on how to write unit tests

#!/usr/bin/env -S gawk -E
@include "test"
BEGIN {
# By default:
# - The testing functions like test::assertEq() can also be called
# like @assertEq() as they are imported into the awk namespace
# - The functions test_* are recognized as tests
}
# Write your functions, or @include them instead
func div(x, y) {
return x/y
}
# Write test_* functions
func test_div() {
# Write a description for the test
test::it("divides a number over another");
# Write assertions
test::assertEq(div(4, 2), 2);
test::assertNe(div(4, 2), 1);
test::assertApprox(div(0.3, 3), 0.1);
}
# More sample assertions
func test_types() {
test::it("recognizes types");
test::assertTrue(mkbool(1));
test::assertFalse(mkbool(0));
test::assertType("abc", "string");
test::assertType(@/abc/, "regexp");
}
number div(numeric x, numeric y)
Division operator /. Arithmetic division for numeric values .
Definition math.awk:194
unassigned assertTruthy(any x)
Assert whether a variable is truthy.
Definition test.awk:99
auto runTests(string|array< string > names, number exit_f=0, number import_f=1) -> bool|never
Run a set of tests.
Definition test.awk:76
unassigned assertTrue(any x)
Assert whether a variable is true (number|bool 1)
Definition test.awk:113
unassigned assertType(any x, string type)
Assert whether variable is of a specific type.
Definition test.awk:355
unassigned assertApprox(number x, number e, numeric rt=0.01, numeric at=0.00)
Assert whether a variable is approximately equal to another.
Definition test.awk:289
unassigned assertEq(scalar x, scalar e)
Assert whether a variable is equal to another.
Definition test.awk:159
unassigned assertNe(scalar x, scalar e)
Assert whether a variable is not equal to another.
Definition test.awk:175
unassigned assertFalse(any x)
Assert whether a variable is true (number|bool 0)
Definition test.awk:120
unassigned it(string d)
Set a description for the current test.
Definition test.awk:83
unassigned assertFalsy(any x)
Assert whether a variable is falsy.
Definition test.awk:106