modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2023-10-14 13:14:46 +04:00
parent 375dbc5042
commit f9f16ee0f1
2 changed files with 311 additions and 47 deletions

View File

@ -3,9 +3,152 @@
module: tests "tests unitaires"
require: base
function: assert "faire un test unitaire. la syntaxe est
assert [-m message] cmd
la commande doit retourner vrai pour que le test soit réussi"
function assert() {
:
function tests__get_line() {
# obtenir le nom du script depuis lequel les fonctions de ce module ont été
# appelées
local mysource="${BASH_SOURCE[0]}"
local i=1
while [ "${BASH_SOURCE[$i]}" == "$mysource" ]; do
let i=i+1
done
echo "${BASH_SOURCE[$i]}:${BASH_LINENO[$((i-1))]}"
}
function tests__set_message() {
if [ "$1" == -m ]; then
message="$2"
return 2
elif [[ "$1" == -m* ]]; then
message="${1#-m}"
return 1
else
return 0
fi
}
function: assert_ok "faire un test unitaire. la syntaxe est
assert_ok [-m message] cmd
la commande doit retourner vrai pour que le test soit réussi"
function assert_ok() {
local message; tests__set_message "$@" || shift $?
"$@" && return 0
[ -n "$message" ] && message="$message: "
message="${message}test failed at $(tests__get_line)"
die "$message"
}
function assert() { assert_ok "$@"; }
function: assert_ko "faire un test unitaire. la syntaxe est
assert_ko [-m message] cmd
la commande doit retourner faux pour que le test soit réussi"
function assert_ko() {
local message; tests__set_message "$@" || shift $?
"$@" || return 0
[ -n "$message" ] && message="$message: "
message="${message}test failed at $(tests__get_line)"
die "$message"
}
function tests__assert() {
local message="$1"; shift
"assert_${1#assert_}" -m "$message" "${@:2}"
}
function assert_n() {
local message; tests__set_message "$@" || shift $?
[ -n "$message" ] || message="$2"
[ -n "$message" ] || message="value is empty"
tests__assert "$message" ok [ -n "$1" ]
}
function assert_z() {
local message; tests__set_message "$@" || shift $?
[ -n "$message" ] || message="$2"
[ -n "$message" ] || message="value is not empty"
tests__assert "$message" ok [ -z "$1" ]
}
function assert_same() {
local message; tests__set_message "$@" || shift $?
[ -n "$message" ] || message="$3"
[ -n "$message" ] || message="'$1' and '$2' are different"
tests__assert "$message" ok [ "$1" == "$2" ]
}
function assert_diff() {
local message; tests__set_message "$@" || shift $?
[ -n "$message" ] || message="$3"
[ -n "$message" ] || message="'$1' and '$2' are the same"
tests__assert "$message" ok [ "$1" != "$2" ]
}
function assert_eq() {
local message; tests__set_message "$@" || shift $?
[ -n "$message" ] || message="$3"
[ -n "$message" ] || message="'$1' and '$2' are not equals"
tests__assert "$message" ok [ "$1" -eq "$2" ]
}
function assert_ne() {
local message; tests__set_message "$@" || shift $?
[ -n "$message" ] || message="$3"
[ -n "$message" ] || message="'$1' and '$2' are equals"
tests__assert "$message" ok [ "$1" -ne "$2" ]
}
function assert_gt() {
local message; tests__set_message "$@" || shift $?
[ -n "$message" ] || message="$3"
[ -n "$message" ] || message="'$1' is not greater than '$2'"
tests__assert "$message" ok [ "$1" -gt "$2" ]
}
function assert_ge() {
local message; tests__set_message "$@" || shift $?
[ -n "$message" ] || message="$3"
[ -n "$message" ] || message="'$1' is not greater than or equals to '$2'"
tests__assert "$message" ok [ "$1" -ge "$2" ]
}
function assert_lt() {
local message; tests__set_message "$@" || shift $?
[ -n "$message" ] || message="$3"
[ -n "$message" ] || message="'$1' is not less than '$2'"
tests__assert "$message" ok [ "$1" -lt "$2" ]
}
function assert_le() {
local message; tests__set_message "$@" || shift $?
[ -n "$message" ] || message="$3"
[ -n "$message" ] || message="'$1' is not less than or equals to '$2'"
tests__assert "$message" ok [ "$1" -le "$2" ]
}
function assert_is_defined() {
local message; tests__set_message "$@" || shift $?
[ -n "$message" ] || message="$2"
[ -n "$message" ] || message="'$1' is not defined"
tests__assert "$message" ok is_defined "$1"
}
function assert_not_defined() {
local message; tests__set_message "$@" || shift $?
[ -n "$message" ] || message="$2"
[ -n "$message" ] || message="'$1' is defined"
tests__assert "$message" ko is_defined "$1"
}
function assert_is_array() {
local message; tests__set_message "$@" || shift $?
[ -n "$message" ] || message="$2"
[ -n "$message" ] || message="'$1' is not an array"
tests__assert "$message" ok is_array "$1"
}
function assert_not_array() {
local message; tests__set_message "$@" || shift $?
[ -n "$message" ] || message="$2"
[ -n "$message" ] || message="'$1' is an array"
tests__assert "$message" ko is_array "$1"
}
function assert_array_same() {
local message; tests__set_message "$@" || shift $?
[ -n "$message" ] || message="'$1' is not an array or not equals to (${*:2})"
assert_is_array "$1" "$message"
eval "actual=\"\$(qvals \"\${$1[@]}\")\""; shift
eval "expected=\"\$(qvals \"\$@\")\""
assert_same "$actual" "$expected" "$message"
}

View File

@ -1,47 +1,168 @@
#!/bin/bash
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
source "$(dirname -- "$0")/../src/nucore.sh" || exit 1
require: tests
#NUCORE_NO_DISABLE_SET_X=1
count=
fixed=
mopt=
dmopt=
oopt=
doopt=
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 '$echo "sans_arg option=$option_, name=$name_, value=$value_"'
-S:: '$echo "avec_arg option=$option_, name=$name_, value=$value_"'
)
parse_args "$@"; set -- "${args[@]}"
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 "$@"
}
echo "\
count=$count
fixed=$fixed
mopt=$mopt
dmopt=$dmopt
oopt=$oopt
doopt=$doopt
autoinc=$autoinc
autoval=$autoval
a1=(${a1[*]}) #${#a1[*]}
a2=(${a2[*]}) #${#a2[*]}
a3=(${a3[*]}) #${#a3[*]}
a4=(${a4[*]}) #${#a4[*]}
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"