mirror of
https://github.com/dylanaraps/pure-bash-bible.git
synced 2025-08-31 19:13:43 +02:00
test: Added error handling to hex_to_rgb
This commit is contained in:
@@ -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"
|
||||
}
|
||||
```
|
||||
|
9
test.sh
9
test.sh
@@ -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() {
|
||||
@@ -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"
|
||||
@@ -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'.
|
||||
|
Reference in New Issue
Block a user