New version of try and new function
This commit is contained in:
parent
6b07f1d8f8
commit
de116fcf9e
3 changed files with 45 additions and 6 deletions
23
src/fun.sh
23
src/fun.sh
|
@ -167,13 +167,18 @@ revers_str() {
|
||||||
cat - | splitc | revers | join
|
cat - | splitc | revers | join
|
||||||
}
|
}
|
||||||
|
|
||||||
try() {
|
catch() {
|
||||||
local f="$@"
|
local f="$@"
|
||||||
local cmd=$(cat -)
|
local cmd=$(cat -)
|
||||||
local ret=$(2>&1 eval "$cmd"; echo $?)
|
local val=$(2>&1 eval "$cmd"; echo $?)
|
||||||
local cnt=$(list $ret | wc -l)
|
local cnt=$(list $val | wc -l)
|
||||||
local status=$(list $ret | last)
|
local status=$(list $val | last)
|
||||||
list "$cmd" $status $(list $ret | take $((cnt - 1)) | join \#) | $f
|
list "$cmd" $status $(list $val | take $((cnt - 1)) | unlist | tup) | $f
|
||||||
|
}
|
||||||
|
|
||||||
|
try() {
|
||||||
|
local f="$@"
|
||||||
|
catch lambda cmd status val . '[[ $status -eq 0 ]] && tupl $val || list $status | '$f
|
||||||
}
|
}
|
||||||
|
|
||||||
ret() {
|
ret() {
|
||||||
|
@ -220,7 +225,13 @@ buff() {
|
||||||
}
|
}
|
||||||
|
|
||||||
tup() {
|
tup() {
|
||||||
list "$@" | map lambda x . 'echo ${x/,/u002c}' | join , '(' ')'
|
if [[ $# -eq 0 ]]; then
|
||||||
|
local arg
|
||||||
|
read arg
|
||||||
|
tup $arg
|
||||||
|
else
|
||||||
|
list "$@" | map lambda x . 'echo ${x/,/u002c}' | join , '(' ')'
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
tupx() {
|
tupx() {
|
||||||
|
|
19
test/catch_test.sh
Executable file
19
test/catch_test.sh
Executable file
|
@ -0,0 +1,19 @@
|
||||||
|
#! /bin/bash
|
||||||
|
|
||||||
|
testCatchIfSuccess() {
|
||||||
|
assertEquals 1 "$(echo 'expr 2 / 2' | catch lambda cmd status val . '[[ $status -eq 0 ]] && tupl $val || echo 0')"
|
||||||
|
}
|
||||||
|
|
||||||
|
testCatchIfError() {
|
||||||
|
assertEquals 0 $(echo 'expr 2 / 0' | catch lambda cmd status val . '[[ $status -eq 0 ]] && tupl $val || echo 0')
|
||||||
|
assertEquals 'cmd=expr 2 / 0,status=2,val=(expr:,division,by,zero)' "$(echo 'expr 2 / 0' | echo 'expr 2 / 0' | LANG=en catch lambda cmd status val . 'echo cmd=$cmd,status=$status,val=$val')"
|
||||||
|
}
|
||||||
|
|
||||||
|
testCatchEdgeCases() {
|
||||||
|
assertEquals 1 "$(echo 'expr 2 / 2' | catch lambda _ _ val . 'tupl $val')"
|
||||||
|
assertEquals 'expr 2 / 2' "$(echo 'expr 2 / 2' | catch lambda cmd . 'ret $cmd')"
|
||||||
|
assertEquals 'expr 2 / 2,0' "$(echo 'expr 2 / 2' | catch lambda cmd status . 'ret $cmd,$status')"
|
||||||
|
assertEquals 'expr 2 / 0,2' "$(echo 'expr 2 / 0' | catch lambda cmd status . 'ret $cmd,$status')"
|
||||||
|
}
|
||||||
|
|
||||||
|
. ./shunit2-init.sh
|
9
test/try_test.sh
Executable file
9
test/try_test.sh
Executable file
|
@ -0,0 +1,9 @@
|
||||||
|
#! /bin/bash
|
||||||
|
|
||||||
|
testTry() {
|
||||||
|
assertEquals 1 "$(echo 'expr 2 / 2' | try lambda _ . 'ret 0')"
|
||||||
|
assertEquals 0 "$(echo 'expr 2 / 0' | try lambda _ . 'ret 0')"
|
||||||
|
assertEquals 2 "$(echo 'expr 2 / 0' | try lambda status . 'ret $status')"
|
||||||
|
}
|
||||||
|
|
||||||
|
. ./shunit2-init.sh
|
Loading…
Reference in a new issue