-
Notifications
You must be signed in to change notification settings - Fork 0
/
zshenv.zsh
116 lines (110 loc) · 6.49 KB
/
zshenv.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/zsh
export ZTEST="$HOME/.zsh_test.log"
fpath=($HOME/.zsh/ressources/autoload/**/*(/\N) $fpath)
assert(){
{
# HELP:
# assert myfunction (out, err) test function_parameters expected_output
# assert myfunction ret function_parameters expected_return_code
# EX:
# fu(){ echo "$1\n$2" }
# myfunction(){ echo "$@" }
# carre(){ echo $(( $1*$1 )) }
# assert myfunction stderr '==' "hello world" "hello world"
# assert myfunction stderr '==' "hello" "world" "hello${NEWLINE}world"
# assert carre stderr '-eq' "2" "4"
# assert true ret "" 0
# assert false ret "" 1
setopt shwordsplit
local futype # type of drivefunction : zsh function or command or raise error
local drivefunction="$1" # function name
shift
local fdreturn="$1" # out | err | ret (stdout, stderr, return code)
shift
typeset -a input # functions parameters
typeset -a expected_output # expected result (function output, return code)
local drivetest # test
local real_output # real result (function output, return code)
local err
local ret
local test_result
futype=( $(whence -vw $drivefunction) )
if [[ "${futype[2]}" == "command" ]] || [[ "${futype[2]}" == "function" ]] ; then
:
else
printf "│\x1b[1;35m██\x1b[0m ztest: error while testing \"%s\":\n" "$drivefunction" 2>&1
printf "│\x1b[1;35m██\x1b[0m \"%s\" not found in ZSH functions or in commands.\n" "$drivefunction" 2>&1
printf "└─────────────────────────────────────────────────────────────────\n" 2>&1
return 1
fi
if [[ "$fdreturn" == "ret" ]] ; then
input=( ${@[1,-2]} )
expected_output="$@[-1]"
$drivefunction $input
real_output=$?
if [[ $real_output -ne $expected_output ]] ; then
printf "│\x1b[1;31m██\x1b[0m ztest: return code of \"%s\":\n" "$drivefunction" 2>&1
printf "│\x1b[1;31m██\x1b[0m assertion failure: %s -eq %s\n" "$expected_output" "$real_output" 2>&1
printf "│\x1b[1;31m██\x1b[0m executed : %s %s --> %s\n" "$drivefunction" "$input" "$real_output"
printf "│\x1b[1;31m██\x1b[0m expected : %s %s --> %s\n" "$drivefunction" "$input" "$expected_output"
printf "└─────────────────────────────────────────────────────────────────\n" 2>&1
else
printf "│\x1b[1;32m██\x1b[0m ztest: return code of \"%s\" :\n" "$drivefunction" 2>&1
printf "│\x1b[1;32m██\x1b[0m assertion success\x1b[0m: %s -eq %s\n" "$expected_output" "$real_output" 2>&1
printf "│\x1b[1;32m██\x1b[0m executed : %s %s --> %s\n" "$drivefunction" "$input" "$real_output"
printf "└─────────────────────────────────────────────────────────────────\n" 2>&1
fi
return 0
fi
drivetest="$1"
shift
input=()
local i
unsetopt shwordsplit
for i in ${@[1,-2]} ; do
input+="$i"
done
expected_output="$@[-1]"
if [[ "$fdreturn" == "out" ]] ; then
real_output=$($drivefunction ${input[@]}) 2>/dev/null
elif [[ "$fdreturn" == "err" ]] ; then
real_output=$($drivefunction ${input[@]} 2>&1 1>/dev/null)
else
printf "│\x1b[1;35m██\x1b[0m ztest: error while testing \"%s\":\n" "$drivefunction" 2>&1
printf "│\x1b[1;35m██\x1b[0m invalid argument \"%s\": must be one of out|err|ret.\n" "$fdreturn" 2>&1
printf "└─────────────────────────────────────────────────────────────────\n" 2>&1
return 1
fi
local str_eval="[[ \"${expected_output}\" ${drivetest} \"${real_output}\" ]]"
err=$(eval "$str_eval" 2>&1 )
if [[ -n $err ]] ; then
printf "│\x1b[1;35m██\x1b[0m ztest: error while testing \"%s\":\n" "$drivefunction" 2>&1
printf "│\x1b[1;35m██\x1b[0m invalid test \"%s\".\n" "$drivetest" 2>&1
printf "│\x1b[1;35m██\x1b[0m error :\n%s\n" "$err" 2>&1
printf "└─────────────────────────────────────────────────────────────────\n" 2>&1
return 1
fi
eval "$str_eval" 1>&2 2>/dev/null
ret=$?
case $ret in
'0')
printf "│\x1b[1;32m██\x1b[0m ztest: std$fdreturn of \"%s\":\n" "$drivefunction" 2>&1
printf "│\x1b[1;32m██\x1b[0m assertion success : %s\n" "$str_eval" 2>&1
printf "│\x1b[1;32m██\x1b[0m executed : %s %s --> %s\n" "$drivefunction" "$input" "$real_output"
printf "└─────────────────────────────────────────────────────────────────\n" 2>&1
;;
'1')
printf "│\x1b[1;31m██\x1b[0m ztest: std$fdreturn of \"%s\":\n" "$drivefunction" 2>&1
printf "│\x1b[1;31m██\x1b[0m assertion failure : %s\n" "$str_eval" 2>&1
printf "│\x1b[1;31m██\x1b[0m executed : %s %s --> %s\n" "$drivefunction" "$input" "$real_output"
printf "│\x1b[1;31m██\x1b[0m expected : %s %s --> %s\n" "$drivefunction" "$input" "$expected_output"
printf "└─────────────────────────────────────────────────────────────────\n" 2>&1
;;
esac
} always {
unsetopt shwordsplit
}
}
for file in $HOME/.zsh/zshenv.d/*.init.zsh(.\N) ; do
source $file ;
done