Commit 4837f0f1 authored by Emeric Verschuur's avatar Emeric Verschuur

Merge branch 'fix_req_val' into 'master'

[FIX] add test case 25, 26 & 27: fix required value

See merge request !4
parents 66cbdd6f e5dd0f31
Pipeline #70 passed with stage
in 35 seconds
......@@ -238,6 +238,10 @@ bashopts_declare() {
*) bashopts_critical "Fatal error";;
esac
done
# Check imcompatible -v and -r options
if [ -n "${options[default]}" ] && [ "${options[req_value]}" == "true" ]; then
bashopts_critical "bashopts_declare: -r and -v options cannot be activated at the same time"
fi
# format the type and check/format the default value
case "${options[type]}" in
''|bool|boolean)
......@@ -245,11 +249,9 @@ bashopts_declare() {
;;
str|string)
options[type]="string"
options[req_value]="true"
;;
num|number)
options[type]="number"
options[req_value]="true"
;;
*)
bashopts_critical "Invalid type ${options[type]}"
......@@ -264,8 +266,11 @@ bashopts_declare() {
''|s|set)
# default: simple value - override
options[method]="set"
# Check the default value format
options[default]="$(${options[check]} "${options[default]}")" || exit 1
if [ "${options[type]}" != "string" ] || [[ -v options[default] ]]; then
# Check the default value format
options[default]="$(${options[check]} "${options[default]}")" \
|| bashopts_critical "Invalid default value for ${options[name]} option"
fi
;;
a|add)
# array value - add
......@@ -441,10 +446,10 @@ bashopts_parse_args() {
long_opts=()
for op in "${bashopts_optlist[@]}"; do
if [[ -v bashopts_optprop_short_opt[$op] ]]; then
short_opts="${short_opts}${bashopts_optprop_short_opt[$op]}:$(test "${bashopts_optprop_req_value[$op]}" == "true" || echo ":")"
short_opts="${short_opts}${bashopts_optprop_short_opt[$op]}:$(test "${bashopts_optprop_type[$op]}" != "boolean" || echo ":")"
fi
if [[ -v bashopts_optprop_long_opt[$op] ]]; then
long_opts+=("${bashopts_optprop_long_opt[$op]}:$(test "${bashopts_optprop_req_value[$op]}" == "true" || echo ":")")
long_opts+=("${bashopts_optprop_long_opt[$op]}:$(test "${bashopts_optprop_type[$op]}" != "boolean" || echo ":")")
fi
done
long_opts=$(bashopts_join_by , ${long_opts[@]})
......@@ -549,6 +554,9 @@ bashopts_process_option() {
if [ -z "$check" ]; then
check="${bashopts_optprop_check[$op]}"
fi
if [ "${bashopts_optprop_req_value[$op]}" == "true" ]; then
val_req="true"
fi
# eval or get default value
if [[ -v bashopts_optprop_expression[$op] ]]; then
......@@ -618,6 +626,8 @@ bashopts_process_option() {
# check format
if [ "${#tval[@]}" -eq 0 ] && [ "$val_req" == "true" ]; then
bashopts_error "At least one value required"
unset tval
continue
fi
if [ "${bashopts_optprop_method[$op]}" == "add" ]; then
# array value
......
......@@ -360,6 +360,37 @@ _test_case_24() {
|| true) | grep -E '^* A str value$' | wc -l )" "2"
}
_test_case_25() {
bashopts_setup -n "$0" -d "Test case $0"
bashopts_declare -n value -l value -d "A str value" -t string -r
req_test_eq "$( (bashopts_process_opts 2>&1 <<< "" \
|| true) | grep -E '^* A str value$' | wc -l )" "2"
}
_test_case_26() {
bashopts_setup -n "$0" -d "Test case $0"
bashopts_declare -n value -l value -d "A str value" -t string -r -k is_eq_to_null
req_test_eq "$( (bashopts_process_opts 2>&1 <<< "
NUL
NULL
") | sed 's/At least one value required/###CASE1###/g' \
| sed 's/Not equal to NULL/###CASE2###/g' \
| grep -o -E '###CASE(1|2)###' | tr '\n' ' ' )" \
"###CASE1### ###CASE1### ###CASE1### ###CASE2### "
}
_test_case_27() {
bashopts_setup -n "$0" -d "Test case $0"
req_test_eq "$(bashopts_declare -n value -l value -d "A str value" -t string -r -v "test" 2>&1)" \
"[ERROR] bashopts_declare: -r and -v options cannot be activated at the same time"
}
_test() {
bashopts_setup -n "$0" -d "Test case $0"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment