Skip to content

Instantly share code, notes, and snippets.

@jmarcher
Last active May 13, 2022 08:29
Show Gist options
  • Save jmarcher/e73a196483ba47ef04861452c4490c69 to your computer and use it in GitHub Desktop.
Save jmarcher/e73a196483ba47ef04861452c4490c69 to your computer and use it in GitHub Desktop.
Experimenting with bash scripts
#! /bin/sh
# asserts if the correct number of parameters where passed to the function
# first parameter should always be $# which is the original count of parameters
# of the function
# second parameter is the correct number of parameters
# the last parameter is the name of the function which can be hardcoded or ${FUNCNAME[0]} can be used
function assert_argument_count() {
if [ $1 -ne $2 ]; then
echo "Illegal number of parameters '$3' passed $# but $2 needed" >&2
exit 2
fi
}
# asserts if the parameters have equal values
function assert_equals() {
if [ "$1" == "$2" ]; then
LAST_ASSERT_PASSED=1
else
LAST_ASSERT_PASSED=0
echo "FAILED! Expected $2 but got $1" >&2
return 1
fi
}
#! /bin/sh
# Based on the parameter --with-returns the variable __WITH_RETURNS is set properly to 0 or 1
function function_output(){
assert_argument_count $# 1 ${FUNCNAME[0]}
if [ ${__WITH_RETURNS:-0} -eq 1 ]; then
_last_return=$1
else
echo $1
fi
}
source ./functions_string.sh
# Functions needed for the tests
source ./functions_test.sh
#! /bin/sh
function strlen() {
assert_argument_count $# 1 ${FUNCNAME[0]}
local RETURN_VALUE=${#1}
function_output $RETURN_VALUE
}
function str_replace() {
assert_argument_count $# 3 ${FUNCNAME[0]}
local RETURN_VALUE=${1//$2/$3}
function_output $RETURN_VALUE
}
function substr() {
assert_argument_count $# 2 ${FUNCNAME[0]}
local RETURN_VALUE=${1:$2}
function_output $RETURN_VALUE
}
#! /bin/sh/
function print_test_head(){
assert_argument_count $# 2 ${FUNCNAME[0]}
echo "Testing \`$1\` output should be $2"
}
function print_last_result() {
assert_argument_count $# 0 ${FUNCNAME[0]}
echo "Output: $_last_return"
}
function start_test(){
assert_argument_count $# 2 ${FUNCNAME[0]}
echo "----- TEST START -----"
print_test_head "$1" "$2"
if [ ${__WITH_RETURNS:-0} -eq 1 ]; then
eval $1
else
# We fake last return as the output of the function
_last_return=$($1)
fi
assert_equals $_last_return $2
}
function end_test(){
assert_argument_count $# 0 ${FUNCNAME[0]}
print_last_result
echo "----- TEST END -----"
echo ""
}
#! /bin/sh
# Define the _last_return value as "null"
__WITH_RETURNS=0
while [ $# -gt 0 ]; do
case "$1" in
--file*|-f*)
if [[ "$1" != *=* ]]; then shift; fi
FILE="${1#*=}"
;;
--with-returns|-r)
_last_return=
__WITH_RETURNS=1
;;
*)
>&2 printf "Error: Invalid argument\n"
exit 1
;;
esac
shift
done
source ./asserts.sh
source ./functions.sh
source ./files.sh
#! /bin/sh
#Building some functions based on bash scripts
#All functions that return something will write it's output to _last_return
source ./main.sh
#Start tests
start_test 'strlen test' 4
end_test
start_test 'str_replace test es acho' 'tachot'
end_test
start_test 'substr test 1' 'est'
end_test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment