From 4c52026a2164d0540a05d8c4c15b08b638626afe Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 14 Jun 2018 19:21:41 +1000 Subject: [PATCH] Added tests --- README.md | 6 +++--- test.sh | 36 ++++++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index fb0c73a..f1d24ec 100644 --- a/README.md +++ b/README.md @@ -102,8 +102,8 @@ scripts and not full blown utilities. ### Trim leading and trailing white-space from string. ```sh -trim() { - # Usage: trim " example string " +trim_string() { + # Usage: trim_string " example string " : "${1#"${1%%[![:space:]]*}"}" : "${_%"${_##*[![:space:]]}"}" printf '%s\n' "$_" @@ -114,7 +114,7 @@ trim() { ```sh # shellcheck disable=SC2086,SC2048 -trim() { +trim_all() { # Usage: trim " example string " set -f set -- $* diff --git a/test.sh b/test.sh index fbc01ce..0971ee9 100755 --- a/test.sh +++ b/test.sh @@ -2,6 +2,31 @@ # # 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() { # Test equality. local status @@ -10,15 +35,14 @@ assert_equals() { [[ "$1" == "$2" ]] || { :>/tmp/err; return 1; } && return 0 } -test_trim() { - result="$(trim " Hello, World ")" - assert_equals "$result" "Hello, World" -} - main() { 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; } }