nulib/bash/tests/test-args-base.sh

169 lines
3.6 KiB
Bash
Raw Normal View History

2023-10-13 23:51:05 +04:00
#!/bin/bash
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
2023-10-20 10:30:12 +04:00
source "$(dirname -- "$0")/../src/nulib.sh" || exit 1
2023-10-14 13:14:46 +04:00
require: tests
2023-10-20 10:30:12 +04:00
#NULIB_NO_DISABLE_SET_X=1
2023-10-13 23:51:05 +04:00
2023-10-14 13:14:46 +04:00
function pa() {
unset count fixed mopt dmopt oopt doopt autoinc autoval a1 a2 a3 a4
count=
fixed=
mopt=
dmopt=
oopt=
doopt=
autoinc=
autoval=
unset a1
a2=()
a3=
a4=x
args=(
"tester la gestion des arguments"
-o,--eopt count "incrémenter count"
-f,--fixed fixed=42 "spécifier fixed"
-a:,--mopt mopt= "spécifier mopt"
-A:,--dmopt dmopt=default "spécifier dmopt"
-b::,--oopt oopt= "spécifier oopt"
-B::,--doopt doopt=default "spécifier doopt"
-n,--autoinc . "incrémenter autoinc"
-v:,--autoval . "spécifier autoval"
-x: a1 "autoadd a1 qui n'est pas défini"
-y: a2 "autoadd a2 qui est défini à ()"
-z: a3 "autoadd a3 qui est défini à vide"
-t: a4 "autoadd a4 qui est défini à une valeur non vide"
-s,--sans-arg '$echo "sans_arg option=$option_, name=$name_, value=$value_"'
-S::,--avec-arg '$echo "avec_arg option=$option_, name=$name_, value=$value_"'
)
parse_args "$@"
}
pa
assert_z "$count"
assert_z "$fixed"
assert_z "$mopt"
assert_z "$dmopt"
assert_z "$oopt"
assert_z "$doopt"
assert_z "$autoinc"
assert_z "$autoval"
assert_not_defined a1
assert_is_array a2
assert_not_array a3
assert_z "$a3"
assert_not_array a4
assert_same x "$a4"
assert_eq "${#args[*]}" 0
pa -o
assert_eq "$count" 1
pa -oo
assert_eq "$count" 2
pa -ooo
assert_eq "$count" 3
pa -f
assert_eq "$fixed" 42
pa -ff
assert_eq "$fixed" 42
pa -fff
assert_eq "$fixed" 42
pa -a ""
assert_not_array mopt
assert_same "$mopt" ""
pa -a abc
assert_not_array mopt
assert_same "$mopt" abc
pa -a abc -a xyz
assert_not_array mopt
assert_same "$mopt" xyz
pa -A ""
assert_not_array dmopt
assert_same "$dmopt" default
pa -A abc
assert_not_array dmopt
assert_same "$dmopt" abc
pa -A abc -A xyz
assert_not_array dmopt
assert_same "$dmopt" xyz
pa -b
assert_not_array oopt
assert_same "$oopt" ""
pa -babc
assert_not_array oopt
assert_same "$oopt" abc
pa -babc -bxyz
assert_not_array oopt
assert_same "$oopt" xyz
pa -B
assert_not_array doopt
assert_same "$doopt" default
pa -Babc
assert_not_array doopt
assert_same "$doopt" abc
pa -Babc -Bxyz
assert_not_array doopt
assert_same "$doopt" xyz
pa -n
assert_eq "$autoinc" 1
pa -nn
assert_eq "$autoinc" 2
pa -nnn
assert_eq "$autoinc" 3
pa -v ""
assert_is_array autoval
assert_array_same autoval ""
pa -v abc
assert_is_array autoval
assert_array_same autoval abc
pa -v abc -v xyz
assert_is_array autoval
assert_array_same autoval abc xyz
pa -xa
assert_not_array a1
assert_same "$a1" a
pa -xa -xb
assert_is_array a1
assert_array_same a1 a b
pa -ya
assert_is_array a2
assert_array_same a2 a
pa -ya -yb
assert_is_array a2
assert_array_same a2 a b
pa -za
assert_is_array a3
assert_array_same a3 a
pa -za -zb
assert_is_array a3
assert_array_same a3 a b
pa -ta
assert_is_array a4
assert_array_same a4 x a
pa -ta -tb
assert_is_array a4
assert_array_same a4 x a b
assert_same "$(pa -s)" "sans_arg option=-s, name=sans_arg, value="
assert_same "$(pa --sans-arg)" "sans_arg option=--sans-arg, name=sans_arg, value="
assert_same "$(pa -S)" "avec_arg option=-S, name=avec_arg, value="
assert_same "$(pa -Sx)" "avec_arg option=-S, name=avec_arg, value=x"
assert_same "$(pa --avec-arg)" "avec_arg option=--avec-arg, name=avec_arg, value="
assert_same "$(pa --avec-arg=x)" "avec_arg option=--avec-arg, name=avec_arg, value=x"
pa x
assert_array_same args x
enote "tests successful"