Added tests

This commit is contained in:
Dylan Araps
2018-06-14 19:21:41 +10:00
parent 51013c49c5
commit 4c52026a21
2 changed files with 33 additions and 9 deletions

View File

@@ -102,8 +102,8 @@ scripts and not full blown utilities.
### Trim leading and trailing white-space from string. ### Trim leading and trailing white-space from string.
```sh ```sh
trim() { trim_string() {
# Usage: trim " example string " # Usage: trim_string " example string "
: "${1#"${1%%[![:space:]]*}"}" : "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}" : "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_" printf '%s\n' "$_"
@@ -114,7 +114,7 @@ trim() {
```sh ```sh
# shellcheck disable=SC2086,SC2048 # shellcheck disable=SC2086,SC2048
trim() { trim_all() {
# Usage: trim " example string " # Usage: trim " example string "
set -f set -f
set -- $* set -- $*

36
test.sh
View File

@@ -2,6 +2,31 @@
# #
# Tests for the Pure Bash Bible. # Tests for the Pure Bash Bible.
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"
}
assert_equals() { assert_equals() {
# Test equality. # Test equality.
local status local status
@@ -10,15 +35,14 @@ assert_equals() {
[[ "$1" == "$2" ]] || { :>/tmp/err; return 1; } && return 0 [[ "$1" == "$2" ]] || { :>/tmp/err; return 1; } && return 0
} }
test_trim() {
result="$(trim " Hello, World ")"
assert_equals "$result" "Hello, World"
}
main() { main() {
source <(awk '/```sh/{f=1;next}/```/{f=0}f' README.md) 2>/dev/null source <(awk '/```sh/{f=1;next}/```/{f=0}f' README.md) 2>/dev/null
test_trim test_trim_string
test_trim_all
test_lower
test_upper
test_trim_quotes
[[ -f /tmp/err ]] || exit 0 && { rm /tmp/err; exit 1; } [[ -f /tmp/err ]] || exit 0 && { rm /tmp/err; exit 1; }
} }