From a2d54d2d1c833a0e15ec95a615adde6e80c6813b Mon Sep 17 00:00:00 2001 From: Milo Davis Date: Fri, 15 Sep 2017 13:40:50 +0200 Subject: [PATCH] Michelson: improvements to bash test procedures --- test/contracts/map_caddaadr.tz | 6 ++--- test/contracts/set_caddaadr.tz | 9 +++---- test/lib/test_lib.inc.sh | 45 ++++++++++++++++++++++++++++++---- test/test_contracts.sh | 8 +++--- 4 files changed, 50 insertions(+), 18 deletions(-) diff --git a/test/contracts/map_caddaadr.tz b/test/contracts/map_caddaadr.tz index dc05e4643..75ea96744 100644 --- a/test/contracts/map_caddaadr.tz +++ b/test/contracts/map_caddaadr.tz @@ -1,6 +1,4 @@ parameter unit; storage (pair (pair nat (pair nat (pair (pair (pair nat tez) nat) nat))) nat); -return (pair (pair nat (pair nat (pair (pair (pair nat tez) nat) nat))) nat); -code { CDR ; - MAP_CADDAADR { PUSH tez "1.00" ; ADD } ; - DUP ; PAIR }; +return unit; +code { MAP_CDADDAADR { PUSH tez "1.00" ; ADD } }; diff --git a/test/contracts/set_caddaadr.tz b/test/contracts/set_caddaadr.tz index 467e5c8d9..d2be87afa 100644 --- a/test/contracts/set_caddaadr.tz +++ b/test/contracts/set_caddaadr.tz @@ -1,7 +1,6 @@ -parameter unit; +parameter tez; storage (pair (pair nat (pair nat (pair (pair (pair nat tez) nat) nat))) nat); -return (pair (pair nat (pair nat (pair (pair (pair nat tez) nat) nat))) nat); -code { CDR ; DUP ; - DIP { CADDAADR ; PUSH tez "1.00" ; ADD } ; +return unit; +code { DUP ; CAR ; SWAP ; CDR ; SET_CADDAADR ; - DUP ; PAIR }; + UNIT ; PAIR }; diff --git a/test/lib/test_lib.inc.sh b/test/lib/test_lib.inc.sh index 6bd361392..61cb7b9b6 100755 --- a/test/lib/test_lib.inc.sh +++ b/test/lib/test_lib.inc.sh @@ -64,10 +64,14 @@ trap cleanup EXIT INT ### Various helpers run_contract_file () { - local contract=$1 - local storage=$2 - local input=$3 - $client run program "$contract" on storage "$storage" and input "$input" + local contract="$1" + local storage="$2" + local input="$3" + local amount_flag="" + if [ ! -z "$4" ]; then + amount_flag="-amount $4" + fi + $client run program "$contract" on storage "$storage" and input "$input" $amount_flag } assert_output () { @@ -75,8 +79,9 @@ assert_output () { local input=$2; local storage=$3; local expected=$4; + local amount=$5; echo "Testing [$contract]" - local output=$(run_contract_file "$contract" "$input" "$storage" | sed '1,/output/d' | + local output=$(run_contract_file "$contract" "$input" "$storage" "$amount" | sed '1,/output/d' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' || { printf '\nTest failed with error at line %s\n' "$(caller)" 1>&2; exit 1; }); @@ -87,6 +92,24 @@ assert_output () { fi } +assert_storage () { + local contract=$1; + local input=$2; + local storage=$3; + local expected=$4; + local amount=$5; + echo "Testing [$contract]" + local storage=$(run_contract_file "$contract" "$input" "$storage" "$amount" | awk '/storage/{getline; print}' | + sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' || + { printf '\nTest failed with error at line %s\n' "$(caller)" 1>&2; + exit 1; }); + if [ "$expected" != "$storage" ]; then + echo "Test at " `caller` failed 1>&2 ; + printf "Expected %s but got %s" "$expected" "$storage" 1>&2 ; + exit 1; + fi +} + assert_balance () { local KEY="$1" local EXPECTED_BALANCE="$2" @@ -172,6 +195,18 @@ assert_fails() { fi } +assert_contract_fails() { + local contract=$1; + local input=$2; + local storage=$3; + local amount=$4; + printf "Testing failure for [$contract]\n" + if run_contract_file "$contract" "$input" "$storage" "$amount" 2> /dev/null ; then + printf "Expected contract execution to fail, but succeeded:\n" + exit 1 + fi +} + extract_operation_hash() { grep "Operation hash is" | grep -o "'.*'" | tr -d "'" } diff --git a/test/test_contracts.sh b/test/test_contracts.sh index 79a2e3184..053fa07e2 100755 --- a/test/test_contracts.sh +++ b/test/test_contracts.sh @@ -265,12 +265,12 @@ assert_output $CONTRACT_PATH/set_cdr.tz '(Pair "hello" 0)' '1' '(Pair "hello" 1 assert_output $CONTRACT_PATH/set_cdr.tz '(Pair "hello" 500)' '3' '(Pair "hello" 3)' assert_output $CONTRACT_PATH/set_cdr.tz '(Pair "hello" 7)' '100' '(Pair "hello" 100)' -assert_output $CONTRACT_PATH/set_caddaadr.tz \ +assert_storage $CONTRACT_PATH/set_caddaadr.tz \ '(Pair (Pair 1 (Pair 2 (Pair (Pair (Pair 3 "0.00") 4) 5))) 6)' \ -'Unit' \ -'(Pair (Pair 1 (Pair 2 (Pair (Pair (Pair 3 "1.00") 4) 5))) 6)' +'"3.00"' \ +'(Pair (Pair 1 (Pair 2 (Pair (Pair (Pair 3 "3.00") 4) 5))) 6)' -assert_output $CONTRACT_PATH/map_caddaadr.tz \ +assert_storage $CONTRACT_PATH/map_caddaadr.tz \ '(Pair (Pair 1 (Pair 2 (Pair (Pair (Pair 3 "0.00") 4) 5))) 6)' \ 'Unit' \ '(Pair (Pair 1 (Pair 2 (Pair (Pair (Pair 3 "1.00") 4) 5))) 6)'