pure-bash-bible/test.sh

81 lines
1.7 KiB
Bash
Raw Normal View History

2018-06-14 10:50:53 +02:00
#!/usr/bin/env bash
#
# Tests for the Pure Bash Bible.
2018-06-14 11:21:41 +02:00
test_trim_string() {
result="$(trim_string " Hello, World ")"
assert_equals "$result" "Hello, World"
}
test_trim_all() {
result="$(trim_all " Hello, World ")"
assert_equals "$result" "Hello, World"
}
test_lower() {
result="$(lower "HeLlO")"
assert_equals "$result" "hello"
}
test_upper() {
result="$(upper "HeLlO")"
assert_equals "$result" "HELLO"
}
test_trim_quotes() {
result="$(trim_quotes "\"te'st' 'str'ing\"")"
assert_equals "$result" "test string"
}
2018-06-14 11:39:26 +02:00
test_lstrip() {
result="$(lstrip "!:IHello" "!:I")"
assert_equals "$result" "Hello"
}
test_rstrip() {
result="$(rstrip "Hello!:I" "!:I")"
assert_equals "$result" "Hello"
}
test_reverse_array() {
IFS=$'\n' read -d "" -ra result < <(reverse_array 1 2 3 4 5)
assert_equals "${result[*]}" "5 4 3 2 1"
}
test_remove_array_dups() {
IFS=$'\n' read -d "" -ra result < <(remove_array_dups 1 1 2 2 3 3 4 5)
assert_equals "${result[*]}" "1 2 3 4 5"
}
test_cycle() {
arr=(a b c d)
result="$(cycle; cycle; cycle)"
assert_equals "$result" "a b c "
}
2018-06-14 10:50:53 +02:00
assert_equals() {
local status
[[ "$1" == "$2" ]] && status="✔"
2018-06-14 11:39:26 +02:00
printf '%s\n' " ${status:-} : ${FUNCNAME[1]/test_}"
2018-06-14 10:50:53 +02:00
[[ "$1" == "$2" ]] || { :>/tmp/err; return 1; } && return 0
}
main() {
source <(awk '/```sh/{f=1;next}/```/{f=0}f' README.md) 2>/dev/null
2018-06-14 11:21:41 +02:00
test_trim_string
test_trim_all
test_lower
test_upper
test_trim_quotes
2018-06-14 11:39:26 +02:00
test_lstrip
test_rstrip
test_reverse_array
test_remove_array_dups
test_cycle
2018-06-14 10:50:53 +02:00
[[ -f /tmp/err ]] || exit 0 && { rm /tmp/err; exit 1; }
}
main "$@"