Skip to content

Instantly share code, notes, and snippets.

@misebox
Last active October 11, 2017 11:36
Show Gist options
  • Save misebox/3eb0777593b501455303514301c8bac6 to your computer and use it in GitHub Desktop.
Save misebox/3eb0777593b501455303514301c8bac6 to your computer and use it in GitHub Desktop.
#!/bin/bash
success=true
# Echo the line number and result of the last command.
verify () {
local result=$?
local frame=($(caller 0))
local lno=${frame[0]}
echo -n "$lno: "
if [ $result -eq 0 ]; then
echo OK
else
echo NG
success=false
fi
}
# Dummy functions
dummy1 () {
echo $*
}
dummy2 () {
echo $1 $2
}
# Compare the results of two commands passed the same arguments
testargs () {
local frame=($(caller 0))
local lno=${frame[0]}
echo -n "$lno: "
# replace dummy1 and dummy2
if [ "`dummy1 $*`" = "`dummy2 $*`" ]; then
echo OK
return 0
else
echo NG
return 1
fi
}
# Test cases
echo "========== Verify result =========="
[ $(( 1 + 1 )) -eq 2 ]; verify
[ "string" = "string" ]; verify
[ "aaa
bbb
ccc" = "aaa
bbb
ccc" ]; verify
[ "aaa" = "bbb" ]; verify # NG
echo "========== Comparison =========="
testargs aaa
testargs aaa bbb
testargs aaa bbb ccc # NG
$success && echo "Done all tests"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment