From b0bd14035d1e2747ff98c46ca8715aab4f9533ea Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Wed, 21 Apr 2021 21:59:45 +0000 Subject: [PATCH] 48606 + 48607 + unposted test: zmathfunc: Force arguments to be numbers and catch errors. --- ChangeLog | 4 ++++ Functions/Math/zmathfunc | 16 +++++++++++++--- Test/Z02zmathfunc.ztst | 8 +++++++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index ac4c95c84..72c48a18b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2021-04-21 Daniel Shahaf + * 48606 + 48607 + unposted test: Functions/Math/zmathfunc, + Test/Z02zmathfunc.ztst: zmathfunc: Force arguments to be numbers + and catch errors. + * unposted (cf. 48156): Test/Z02zmathfunc.ztst: New test. * users/26635 (tweaked): diff --git a/Functions/Math/zmathfunc b/Functions/Math/zmathfunc index 8e4b78549..12d2c2f3d 100644 --- a/Functions/Math/zmathfunc +++ b/Functions/Math/zmathfunc @@ -6,7 +6,12 @@ zsh_math_func_min() { shift local arg for arg ; do - (( $arg < result )) && result=$arg + (( arg < result )) + case $? in + (0) (( result = arg ));; + (1) ;; + (*) return $?;; + esac done (( result )) true # Careful here: `return 0` evaluates an arithmetic expression @@ -19,7 +24,12 @@ zsh_math_func_max() { shift local arg for arg ; do - (( $arg > result )) && result=$arg + (( arg > result )) + case $? in + (0) (( result = arg ));; + (1) ;; + (*) return $?;; + esac done (( result )) true # Careful here: `return 0` evaluates an arithmetic expression @@ -31,7 +41,7 @@ zsh_math_func_sum() { local sum local arg for arg ; do - (( sum += $arg )) + (( sum += arg )) done (( sum )) true # Careful here: `return 0` evaluates an arithmetic expression diff --git a/Test/Z02zmathfunc.ztst b/Test/Z02zmathfunc.ztst index 05e28c07a..2be770a13 100644 --- a/Test/Z02zmathfunc.ztst +++ b/Test/Z02zmathfunc.ztst @@ -44,7 +44,13 @@ ?(eval):1: wrong number of arguments: max() zsh_math_func_min "foo bar" x y z -2dDf:check errors from an unsupported use-case (workers/48156) +2d:check errors from an unsupported use-case (workers/48156) +# We expect one non-empty line of stderr, but don't care about the specific +# error message; thus, the expectation is a pattern (*), for stderr (?), which +# matches any non-empty string (?*). +# +# Sorry, Perl, but I had to give you a run for your money. +*??* F:Calling zsh_math_func_min directly isn't a supported use-case, but if it F:returns zero, something's probably wrong.