From 51013c49c59e202bcd15959a9fcef9ad0cdbd577 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 14 Jun 2018 18:50:53 +1000 Subject: [PATCH 1/8] Added tests --- test.sh | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100755 test.sh diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..fbc01ce --- /dev/null +++ b/test.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# +# Tests for the Pure Bash Bible. + +assert_equals() { + # Test equality. + local status + [[ "$1" == "$2" ]] && status="✔" + printf '%s\n' " ${status:-✖} : ${FUNCNAME[1]}" + [[ "$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 + + [[ -f /tmp/err ]] || exit 0 && { rm /tmp/err; exit 1; } +} + +main "$@" + From 4c52026a2164d0540a05d8c4c15b08b638626afe Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 14 Jun 2018 19:21:41 +1000 Subject: [PATCH 2/8] 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; } } From dd284224c76c6eaa6027743886e1408e6e75ffa9 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 14 Jun 2018 19:39:26 +1000 Subject: [PATCH 3/8] fix bugs in examples --- README.md | 4 +--- test.sh | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f1d24ec..125ce70 100644 --- a/README.md +++ b/README.md @@ -226,10 +226,8 @@ reverse_array() { # Usage: reverse_array "array" # reverse_array 1 2 3 4 5 6 shopt -s extdebug - f()(printf '%s ' "${BASH_ARGV[@]}"); f "$@" + f()(printf '%s\n' "${BASH_ARGV[@]}"); f "$@" shopt -u extdebug - - printf '\n' } ``` diff --git a/test.sh b/test.sh index 0971ee9..a5d8ad7 100755 --- a/test.sh +++ b/test.sh @@ -27,11 +27,36 @@ test_trim_quotes() { assert_equals "$result" "test string" } +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 " +} + assert_equals() { - # Test equality. local status [[ "$1" == "$2" ]] && status="✔" - printf '%s\n' " ${status:-✖} : ${FUNCNAME[1]}" + printf '%s\n' " ${status:-✖} : ${FUNCNAME[1]/test_}" [[ "$1" == "$2" ]] || { :>/tmp/err; return 1; } && return 0 } @@ -43,9 +68,13 @@ main() { test_lower test_upper test_trim_quotes + test_lstrip + test_rstrip + test_reverse_array + test_remove_array_dups + test_cycle [[ -f /tmp/err ]] || exit 0 && { rm /tmp/err; exit 1; } } main "$@" - From bac31a9779daccd2dbefd0eaa13f5da485aa4133 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 14 Jun 2018 19:47:50 +1000 Subject: [PATCH 4/8] Added more tests --- test.sh | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/test.sh b/test.sh index a5d8ad7..2e3bd84 100755 --- a/test.sh +++ b/test.sh @@ -53,6 +53,35 @@ test_cycle() { assert_equals "$result" "a b c " } +test_head() { + printf '%s\n%s\n\n\n' "hello" "world" > test_file + result="$(head 2 test_file)" + assert_equals "$result" $'hello\nworld' + rm test_file +} + +test_tail() { + printf '\n\n\n%s\n%s\n' "hello" "world" > test_file + result="$(tail 2 test_file)" + assert_equals "$result" $'hello\nworld' + rm test_file +} + +test_count() { + result="$(count ./{README.m,LICENSE.m,.travis.ym}*)" + assert_equals "$result" "3" +} + +test_dirname() { + result="$(dirname "/home/black/Pictures/Wallpapers/1.jpg")" + assert_equals "$result" "/home/black/Pictures/Wallpapers/" +} + +test_basename() { + result="$(basename "/home/black/Pictures/Wallpapers/1.jpg")" + assert_equals "$result" "1.jpg" +} + assert_equals() { local status [[ "$1" == "$2" ]] && status="✔" @@ -73,6 +102,11 @@ main() { test_reverse_array test_remove_array_dups test_cycle + test_head + test_tail + test_count + test_dirname + test_basename [[ -f /tmp/err ]] || exit 0 && { rm /tmp/err; exit 1; } } From b2da362950568e9ced103396b7f2ef6b2e10034b Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 14 Jun 2018 20:08:09 +1000 Subject: [PATCH 5/8] Added more tests --- test.sh | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/test.sh b/test.sh index 2e3bd84..0b4381e 100755 --- a/test.sh +++ b/test.sh @@ -82,16 +82,45 @@ test_basename() { assert_equals "$result" "1.jpg" } +test_hex_to_rgb() { + result="$(hex_to_rgb "#FFFFFF")" + assert_equals "$result" "255 255 255" +} + +test_rgb_to_hex() { + result="$(rgb_to_hex 0 0 0)" + assert_equals "$result" "#000000" +} + +test_date() { + result="$(date "%C")" + assert_equals "$result" "20" +} + assert_equals() { local status + + ((tests+=1)) + [[ "$1" == "$2" ]] && status="✔" - printf '%s\n' " ${status:-✖} : ${FUNCNAME[1]/test_}" - [[ "$1" == "$2" ]] || { :>/tmp/err; return 1; } && return 0 + printf '%s\n' " ${status:-✖} | ${FUNCNAME[1]/test_}" + + if [[ "$1" == "$2" ]]; then + ((pass+=1)) + return 0 + else + :>/tmp/err + ((err+=1)) + return 1 + fi } main() { source <(awk '/```sh/{f=1;next}/```/{f=0}f' README.md) 2>/dev/null + head="-> Running tests on the Pure Bash Bible.." + printf '\n%s\n%s\n' "$head" "${head//?/-}" + test_trim_string test_trim_all test_lower @@ -107,6 +136,12 @@ main() { test_count test_dirname test_basename + test_hex_to_rgb + test_rgb_to_hex + test_date + + comp="Completed $tests tests. ${pass:-0} passed, ${err:-0} errored." + printf '%s\n%s\n\n' "${comp//?/-}" "$comp" [[ -f /tmp/err ]] || exit 0 && { rm /tmp/err; exit 1; } } From bdb8e51ee97838d671dea01341d92c1fd9754350 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 14 Jun 2018 20:09:33 +1000 Subject: [PATCH 6/8] run tests on travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 64e1c52..60d8fd7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,3 +5,4 @@ os: script: - shellcheck -s bash --exclude=SC2034,SC2154 <(awk '/```sh/{f=1;next}/```/{f=0}f' README.md) + - ./test.sh From d9aa2f2a9725cfe40947c47c7a5e006ee7892b5d Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 14 Jun 2018 20:21:50 +1000 Subject: [PATCH 7/8] fix shellcheck --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 125ce70..c8ccc04 100644 --- a/README.md +++ b/README.md @@ -545,7 +545,7 @@ _() { # Undocumented method. # Note: This is commented to make shellcheck play nice. - for i in {1..10};{ echo "$i";} + # for i in {1..10};{ echo "$i";} # Expansion. for i in {1..10}; do echo "$i"; done @@ -584,9 +584,10 @@ _() { f()(($1)) # Using tests, loops etc. - # Note: You can also use ‘while’, ‘until’, ‘case’, ‘(())’, ‘[[]]’. - f()if true; then echo "$1"; fi - f()for i in "$@"; do echo "$i"; done + # NOTE: You can also use ‘while’, ‘until’, ‘case’, ‘(())’, ‘[[]]’. + # NOTE: These are commented to make shellcheck play nice. + # f()if true; then echo "$1"; fi + # f()for i in "$@"; do echo "$i"; done } ``` From 1032d283d0eb39466fdcf7de8e387a775316fbb2 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Thu, 14 Jun 2018 20:30:27 +1000 Subject: [PATCH 8/8] misc: cleanup --- test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.sh b/test.sh index 0b4381e..32a301f 100755 --- a/test.sh +++ b/test.sh @@ -48,6 +48,7 @@ test_remove_array_dups() { } test_cycle() { + # shellcheck disable=2034 arr=(a b c d) result="$(cycle; cycle; cycle)" assert_equals "$result" "a b c " @@ -99,7 +100,6 @@ test_date() { assert_equals() { local status - ((tests+=1)) [[ "$1" == "$2" ]] && status="✔"