pure-bash-bible/test.sh

252 lines
5.9 KiB
Bash
Raw Normal View History

2018-06-14 10:50:53 +02:00
#!/usr/bin/env bash
2019-09-19 16:06:26 +02:00
# shellcheck source=/dev/null disable=2178,2128
2018-06-14 10:50:53 +02:00
#
# 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"
}
2018-06-15 00:26:06 +02:00
test_regex() {
2018-06-15 00:29:28 +02:00
result="$(regex "#FFFFFF" '^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$')"
assert_equals "$result" "#FFFFFF"
2018-06-15 00:26:06 +02:00
}
2018-06-14 11:21:41 +02:00
test_lower() {
result="$(lower "HeLlO")"
assert_equals "$result" "hello"
}
test_upper() {
result="$(upper "HeLlO")"
assert_equals "$result" "HELLO"
}
2019-06-09 12:28:29 +02:00
test_reverse_case() {
result="$(reverse_case "HeLlO")"
assert_equals "$result" "hElLo"
}
2018-06-14 11:21:41 +02:00
test_trim_quotes() {
result="$(trim_quotes "\"te'st' 'str'ing\"")"
assert_equals "$result" "test string"
}
2018-06-15 00:43:35 +02:00
test_strip_all() {
result="$(strip_all "The Quick Brown Fox" "[aeiou]")"
assert_equals "$result" "Th Qck Brwn Fx"
}
test_strip() {
result="$(strip "The Quick Brown Fox" "[aeiou]")"
assert_equals "$result" "Th Quick Brown Fox"
}
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_urlencode() {
2019-06-08 16:42:48 +02:00
result="$(urlencode "https://github.com/dylanaraps/pure-bash-bible")"
assert_equals "$result" "https%3A%2F%2Fgithub.com%2Fdylanaraps%2Fpure-bash-bible"
}
test_urldecode() {
2019-06-08 16:42:48 +02:00
result="$(urldecode "https%3A%2F%2Fgithub.com%2Fdylanaraps%2Fpure-bash-bible")"
assert_equals "$result" "https://github.com/dylanaraps/pure-bash-bible"
}
2018-06-14 11:39:26 +02:00
test_reverse_array() {
2019-09-19 16:41:07 +02:00
shopt -s compat44
2018-06-14 11:39:26 +02:00
IFS=$'\n' read -d "" -ra result < <(reverse_array 1 2 3 4 5)
assert_equals "${result[*]}" "5 4 3 2 1"
2019-09-19 16:41:07 +02:00
shopt -u compat44
2018-06-14 11:39:26 +02:00
}
test_cycle() {
2018-06-14 12:30:27 +02:00
# shellcheck disable=2034
2018-06-14 11:39:26 +02:00
arr=(a b c d)
result="$(cycle; cycle; cycle)"
assert_equals "$result" "a b c "
}
2018-06-14 11:47:50 +02:00
test_head() {
printf '%s\n%s\n\n\n' "hello" "world" > test_file
result="$(head 2 test_file)"
assert_equals "$result" $'hello\nworld'
}
test_tail() {
printf '\n\n\n%s\n%s\n' "hello" "world" > test_file
result="$(tail 2 test_file)"
assert_equals "$result" $'hello\nworld'
}
2018-06-14 12:38:20 +02:00
test_lines() {
printf '\n\n\n\n\n\n\n\n' > test_file
result="$(lines test_file)"
assert_equals "$result" "8"
}
2018-06-16 01:09:56 +02:00
test_lines_loop() {
printf '\n\n\n\n\n\n\n\n' > test_file
result="$(lines_loop test_file)"
assert_equals "$result" "8"
}
2018-06-14 11:47:50 +02:00
test_count() {
result="$(count ./{README.m,LICENSE.m,.travis.ym}*)"
assert_equals "$result" "3"
}
test_dirname() {
result="$(dirname "/home/black/Pictures/Wallpapers/1.jpg")"
2019-09-19 15:23:20 +02:00
assert_equals "$result" "/home/black/Pictures/Wallpapers"
2019-09-19 16:06:26 +02:00
result="$(dirname "/")"
assert_equals "$result" "/"
result="$(dirname "/foo")"
assert_equals "$result" "/"
result="$(dirname ".")"
assert_equals "$result" "."
result="$(dirname "/foo/foo")"
assert_equals "$result" "/foo"
2019-09-19 16:38:33 +02:00
result="$(dirname "something/")"
assert_equals "$result" "."
2019-09-19 17:43:16 +02:00
result="$(dirname "//")"
assert_equals "$result" "/"
result="$(dirname "//foo")"
assert_equals "$result" "/"
result="$(dirname "")"
assert_equals "$result" "."
result="$(dirname "something//")"
assert_equals "$result" "."
result="$(dirname "something/////////////////////")"
assert_equals "$result" "."
2019-09-19 17:46:06 +02:00
result="$(dirname "something/////////////////////a")"
assert_equals "$result" "something"
result="$(dirname "something//////////.///////////")"
assert_equals "$result" "something"
2019-09-19 17:47:44 +02:00
result="$(dirname "//////")"
assert_equals "$result" "/"
2018-06-14 11:47:50 +02:00
}
test_basename() {
result="$(basename "/home/black/Pictures/Wallpapers/1.jpg")"
assert_equals "$result" "1.jpg"
}
2018-06-14 12:08:09 +02:00
test_hex_to_rgb() {
result="$(hex_to_rgb "#FFFFFF")"
assert_equals "$result" "255 255 255"
result="$(hex_to_rgb "000000")"
assert_equals "$result" "0 0 0"
2018-06-14 12:08:09 +02:00
}
test_rgb_to_hex() {
result="$(rgb_to_hex 0 0 0)"
assert_equals "$result" "#000000"
}
test_date() {
result="$(date "%C")"
assert_equals "$result" "20"
}
2018-06-15 09:24:12 +02:00
test_read_sleep() {
2018-06-18 01:20:23 +02:00
result="$((SECONDS+1))"
2018-06-15 09:24:12 +02:00
read_sleep 1
2018-06-18 01:20:23 +02:00
assert_equals "$result" "$SECONDS"
2018-06-15 09:24:12 +02:00
}
2018-06-15 10:33:01 +02:00
test_bar() {
result="$(bar 50 10)"
assert_equals "${result//$'\r'}" "[----- ]"
}
test_get_functions() {
IFS=$'\n' read -d "" -ra functions < <(get_functions)
assert_equals "${functions[0]}" "assert_equals"
}
test_extract() {
printf '{\nhello, world\n}\n' > test_file
result="$(extract test_file "{" "}")"
assert_equals "$result" "hello, world"
}
2018-06-19 09:31:23 +02:00
test_split() {
IFS=$'\n' read -d "" -ra result < <(split "hello,world,my,name,is,john" ",")
assert_equals "${result[*]}" "hello world my name is john"
}
2018-06-14 10:50:53 +02:00
assert_equals() {
2018-06-14 12:08:09 +02:00
if [[ "$1" == "$2" ]]; then
((pass+=1))
2018-06-15 02:17:17 +02:00
status=$'\e[32m✔'
2018-06-14 12:08:09 +02:00
else
2018-06-17 01:53:06 +02:00
((fail+=1))
2018-06-15 02:17:17 +02:00
status=$'\e[31m✖'
local err="(\"$1\" != \"$2\")"
2018-06-14 12:08:09 +02:00
fi
2018-06-15 02:17:17 +02:00
2018-06-15 03:12:44 +02:00
printf ' %s\e[m | %s\n' "$status" "${FUNCNAME[1]/test_} $err"
2018-06-14 10:50:53 +02:00
}
main() {
2018-06-21 03:17:45 +02:00
trap 'rm readme_code test_file' EXIT
2018-06-15 02:46:54 +02:00
# Extract code blocks from the README.
while IFS=$'\n' read -r line; do
[[ "$code" && "$line" != \`\`\` ]] && printf '%s\n' "$line"
[[ "$line" =~ ^\`\`\`sh$ ]] && code=1
[[ "$line" =~ ^\`\`\`$ ]] && code=
done < README.md > readme_code
# Run shellcheck and source the code.
shellcheck -s bash readme_code test.sh build.sh || exit 1
2018-06-15 02:46:54 +02:00
. readme_code
2018-06-14 10:50:53 +02:00
2018-06-14 12:08:09 +02:00
head="-> Running tests on the Pure Bash Bible.."
printf '\n%s\n%s\n' "$head" "${head//?/-}"
2018-06-15 02:23:21 +02:00
# Generate the list of tests to run.
2018-06-18 01:20:23 +02:00
IFS=$'\n' read -d "" -ra funcs < <(declare -F)
for func in "${funcs[@]//declare -f }"; do
[[ "$func" == test_* ]] && "$func";
2018-06-18 01:20:23 +02:00
done
2018-06-15 02:23:21 +02:00
comp="Completed $((fail+pass)) tests. ${pass:-0} passed, ${fail:-0} failed."
2018-06-14 12:08:09 +02:00
printf '%s\n%s\n\n' "${comp//?/-}" "$comp"
2018-06-14 10:50:53 +02:00
2018-06-15 02:23:21 +02:00
# If a test failed, exit with '1'.
2018-06-17 01:53:06 +02:00
((fail>0)) || exit 0 && exit 1
2018-06-14 10:50:53 +02:00
}
main "$@"