Skip to content

Commit

Permalink
test: Added error handling to hex_to_rgb
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanaraps committed Jun 22, 2018
1 parent cf243bc commit 809c8b5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1585,10 +1585,8 @@ $ get_cursor_pos
```sh
hex_to_rgb() {
# Usage: hex_to_rgb "#FFFFFF"
((r=16#${1:1:2}))
((g=16#${1:3:2}))
((b=16#${1:5:6}))
: "${1/\#}"
((r=16#${_:0:2},g=16#${_:2:2},b=16#${_:4:2}))
printf '%s\n' "$r $g $b"
}
```
Expand Down
9 changes: 6 additions & 3 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ test_basename() {
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"
}

test_rgb_to_hex() {
Expand Down Expand Up @@ -158,7 +161,7 @@ assert_equals() {
else
((fail+=1))
status=$'\e[31m✖'
local err="($1 != $2)"
local err="(\"$1\" != \"$2\")"
fi

printf ' %s\e[m | %s\n' "$status" "${FUNCNAME[1]/test_} $err"
Expand All @@ -184,10 +187,10 @@ main() {
# Generate the list of tests to run.
IFS=$'\n' read -d "" -ra funcs < <(declare -F)
for func in "${funcs[@]//declare -f }"; do
[[ "$func" == test_* ]] && { "$func"; ((tot+=1)); }
[[ "$func" == test_* ]] && "$func";
done

comp="Completed $tot tests. ${pass:-0} passed, ${fail:-0} failed."
comp="Completed $((fail+pass)) tests. ${pass:-0} passed, ${fail:-0} failed."
printf '%s\n%s\n\n' "${comp//?/-}" "$comp"

# If a test failed, exit with '1'.
Expand Down

0 comments on commit 809c8b5

Please sign in to comment.