mirror of
https://github.com/dylanaraps/pure-bash-bible.git
synced 2025-09-09 22:51:16 +02:00
test: Added error handling to hex_to_rgb
This commit is contained in:
@@ -1585,10 +1585,8 @@ $ get_cursor_pos
|
|||||||
```sh
|
```sh
|
||||||
hex_to_rgb() {
|
hex_to_rgb() {
|
||||||
# Usage: hex_to_rgb "#FFFFFF"
|
# Usage: hex_to_rgb "#FFFFFF"
|
||||||
((r=16#${1:1:2}))
|
: "${1/\#}"
|
||||||
((g=16#${1:3:2}))
|
((r=16#${_:0:2},g=16#${_:2:2},b=16#${_:4:2}))
|
||||||
((b=16#${1:5:6}))
|
|
||||||
|
|
||||||
printf '%s\n' "$r $g $b"
|
printf '%s\n' "$r $g $b"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
9
test.sh
9
test.sh
@@ -112,6 +112,9 @@ test_basename() {
|
|||||||
test_hex_to_rgb() {
|
test_hex_to_rgb() {
|
||||||
result="$(hex_to_rgb "#FFFFFF")"
|
result="$(hex_to_rgb "#FFFFFF")"
|
||||||
assert_equals "$result" "255 255 255"
|
assert_equals "$result" "255 255 255"
|
||||||
|
|
||||||
|
result="$(hex_to_rgb "000000")"
|
||||||
|
assert_equals "$result" "0 0 0"
|
||||||
}
|
}
|
||||||
|
|
||||||
test_rgb_to_hex() {
|
test_rgb_to_hex() {
|
||||||
@@ -158,7 +161,7 @@ assert_equals() {
|
|||||||
else
|
else
|
||||||
((fail+=1))
|
((fail+=1))
|
||||||
status=$'\e[31m✖'
|
status=$'\e[31m✖'
|
||||||
local err="($1 != $2)"
|
local err="(\"$1\" != \"$2\")"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf ' %s\e[m | %s\n' "$status" "${FUNCNAME[1]/test_} $err"
|
printf ' %s\e[m | %s\n' "$status" "${FUNCNAME[1]/test_} $err"
|
||||||
@@ -184,10 +187,10 @@ main() {
|
|||||||
# Generate the list of tests to run.
|
# Generate the list of tests to run.
|
||||||
IFS=$'\n' read -d "" -ra funcs < <(declare -F)
|
IFS=$'\n' read -d "" -ra funcs < <(declare -F)
|
||||||
for func in "${funcs[@]//declare -f }"; do
|
for func in "${funcs[@]//declare -f }"; do
|
||||||
[[ "$func" == test_* ]] && { "$func"; ((tot+=1)); }
|
[[ "$func" == test_* ]] && "$func";
|
||||||
done
|
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"
|
printf '%s\n%s\n\n' "${comp//?/-}" "$comp"
|
||||||
|
|
||||||
# If a test failed, exit with '1'.
|
# If a test failed, exit with '1'.
|
||||||
|
Reference in New Issue
Block a user