-
Notifications
You must be signed in to change notification settings - Fork 0
test.sh
Built-in test harness for writing automated tests. Follows the same conventions as library modules: include guard, set -euo pipefail, test:: namespace. Auto-loads ansi.sh for colored pass/fail output.
Bash requirement: 4.0+
Dependencies: ansi.sh (auto-loaded)
Include guard: test::equal
#!/usr/bin/env bash
set -euo pipefail
source "$(dirname "$(realpath "$0")")/../test.sh"
source "$test_lib_dir/string.sh"
echo "string.sh"
test::equal "$(string::make_pad 3 '.')" "..." "make_pad dots"
test::equal "$(string::pad 'hi' 10 l)" "hi " "pad left"
test::contains "hello world" "world" "contains world"
test::done-
test_lib_dir— absolute path to the library root directory. Use this to source modules under test. -
test_pass— number of passed assertions so far. -
test_fail— number of failed assertions so far. -
test_total— total number of assertions so far. -
test_name— current test group label (set viatest::name).
Assert that ACTUAL equals EXPECTED (string comparison).
test::equal "$(string::length 'hello')" "5" "length of hello"Assert that ACTUAL does not equal EXPECTED.
test::not_equal "${RED:-}" "" "RED is defined"Assert that ACTUAL matches the extended regex PATTERN.
test::match "$(ansi::make bold)" $'\e' "contains escape byte"Assert that HAYSTACK contains the substring NEEDLE (uses grep -qF).
test::contains "${args_cleaned[*]}" "foo" "positional arg present"Unconditional pass. Useful for marking that a code path was reached.
test::ok "sourced without error"Unconditional fail. The trailing underscore avoids collision with a potential Bash builtin.
test::fail_ "should not reach here"Set a label for the current test group. This is informational only.
Print a summary of pass/fail counts and exit. Exits 0 if all assertions passed, 1 if any failed. Every test file must call this at the end.
The runner script tests/run.sh discovers all tests/test-*.sh files and executes each in a separate bash process. It reports which files passed or failed and exits 1 if any file failed.
bash tests/run.sh- Create
tests/test-<module>.sh. - Source
test.shrelative to the file location. - Source the module(s) under test via
$test_lib_dir. - Write assertions.
- Call
test::doneat the end.
#!/usr/bin/env bash
set -euo pipefail
script_dir="$(dirname "$(realpath "$0")")"
source "$script_dir/../test.sh"
source "$test_lib_dir/box.sh"
echo "box.sh"
test::equal "$(box::get_width 'hello')" "5" "width of single line"
test::equal "$(box::get_height $'a\nb')" "2" "height of two lines"
test::done