Refactor to support more tests
Extend testcases: assertion test and competition tests
This commit is contained in:
@@ -1,71 +1,143 @@
|
|||||||
#!/bin/bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
tool_a="/opt/rdlink/rdlink.sh"
|
# Automatic tests for rdlink
|
||||||
tool_b="$(command -v readlink) -f"
|
# Two kinds of tests are supported
|
||||||
|
# * `tocompete` : competition between readlink and rdlink
|
||||||
|
# * `toassert` : assert expected path and input path
|
||||||
|
|
||||||
test_links=( "Valid links"
|
# test_rdlink.sh [PATH TO COMPETE]
|
||||||
|
#
|
||||||
|
# Internal tests will be executed if no arguments are found
|
||||||
|
|
||||||
|
test_dir="/opt/rdlink/test"
|
||||||
|
tool_rdlink="/opt/rdlink/rdlink.sh"
|
||||||
|
tool_readlink="$(command -v readlink) -f"
|
||||||
|
|
||||||
|
########## Competition Block ##########
|
||||||
|
# Each competition link is given to both tools
|
||||||
|
# and the output is compared
|
||||||
|
# [0] = Title
|
||||||
|
# [1] = path for competition
|
||||||
|
# [2] = path for competition
|
||||||
|
# ...
|
||||||
|
compete_links=( "Valid links"
|
||||||
"/dev/stdin"
|
"/dev/stdin"
|
||||||
"$HOME/tmp/a"*
|
"$test_dir/tmp/a"*
|
||||||
"$HOME/tmp/a 3"
|
"$test_dir/tmp/a 3"
|
||||||
"/bin/adb"
|
"/bin/adb"
|
||||||
)
|
)
|
||||||
|
|
||||||
test_all=( "Test everything starting from /"
|
compete_all=( "Test everything starting from /"
|
||||||
/**/*
|
/**/*
|
||||||
)
|
)
|
||||||
|
|
||||||
test_args=( "Tests from command line"
|
compete_args=( "Tests from command line"
|
||||||
"$@"
|
"$@"
|
||||||
)
|
)
|
||||||
|
|
||||||
totest=(
|
# Lists for competition test between rdlink and readlink -f
|
||||||
test_links
|
tocompete=(
|
||||||
|
compete_links
|
||||||
)
|
)
|
||||||
|
|
||||||
[[ "$@" ]] && totest=(test_args)
|
########## Assertion Block ##########
|
||||||
|
# Assertion string compare test
|
||||||
|
# [0] = Title
|
||||||
|
# [1] expected == [2] input
|
||||||
|
# [3] expected == [4] input
|
||||||
|
# ...
|
||||||
|
assert_string=( "Assert invalid files"
|
||||||
|
"/opt/rdlink/test/a" "a"
|
||||||
|
"/opt/rdlink/test/b" "b"
|
||||||
|
"/opt/rdlink/test/c" "c"
|
||||||
|
)
|
||||||
|
|
||||||
|
# List of assertian test arrays
|
||||||
|
toassert=(
|
||||||
|
assert_string
|
||||||
|
)
|
||||||
|
|
||||||
|
# Only run `compete_args` if arguments are available
|
||||||
|
[[ "$@" ]] && tocompete=(compete_args) && toassert=()
|
||||||
|
|
||||||
|
# rl::testcmp <TEST NUMBER> <TEST INPUT> <EXPECTED RESULT> <ACTUAL RESULT>
|
||||||
|
rl::testcmp() {
|
||||||
|
local testnum=
|
||||||
|
local expect=
|
||||||
|
local actual=
|
||||||
|
local input=
|
||||||
|
printf -- "\n%-3d --- %-30s ---" ${testnum:=${1:-0}} "${input:=${2:-"-"}}"
|
||||||
|
if [[ "${expect:=${3:-"-"}}" == "${actual:=${4:-"-"}}" ]]; then
|
||||||
|
# Test successful
|
||||||
|
printf " OK\n"
|
||||||
|
else
|
||||||
|
# Test failed
|
||||||
|
printf " FAIL\n"
|
||||||
|
printf " Result:\nExp: %s\nAct: %s\n" "${expect}" "${actual}"
|
||||||
|
[ -e "${input}" ] && printf "\n Subject:\n" && ls -al "${input}"
|
||||||
|
|
||||||
|
# Print debug output of tool_a
|
||||||
|
printf "\n Debug:\n"
|
||||||
|
( "${tool_rdlink}" -d "${input}" )
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
rl::test() {
|
rl::test() {
|
||||||
local i=0
|
local i=0
|
||||||
local ra=
|
|
||||||
local rb=
|
|
||||||
local path=
|
local path=
|
||||||
local testarray=
|
local arraywalker=
|
||||||
|
local firstelement=
|
||||||
local testend=0
|
local testend=0
|
||||||
|
|
||||||
for testarray in "${totest[@]}"; do
|
# Compare against expected result
|
||||||
|
for testarray in "${toassert[@]}"; do
|
||||||
i=0
|
i=0
|
||||||
testarray="$testarray"[@]
|
arraywalker="$testarray"[@]
|
||||||
for path in "${!testarray}"; do
|
for path in "${!arraywalker}"; do
|
||||||
|
if (( ! i )); then
|
||||||
|
# Print title in array element 0
|
||||||
|
printf " ### %s ###\n" "$path"
|
||||||
|
elif [ -z "$firstelement" ]; then
|
||||||
|
# Save first element for string compare
|
||||||
|
firstelement="${path}"
|
||||||
|
else
|
||||||
|
# Do the compare between two following elements
|
||||||
|
if ! rl::testcmp "$((i-1))" "${path}" \
|
||||||
|
"${firstelement}" "$("$tool_rdlink" "${path}")"; then
|
||||||
|
testend=1
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
|
||||||
|
firstelement=
|
||||||
|
fi
|
||||||
|
((i++))
|
||||||
|
done
|
||||||
|
(( testend )) && return 1
|
||||||
|
done
|
||||||
|
|
||||||
|
# Compare output of rdlink and readlink -f
|
||||||
|
for testarray in "${tocompete[@]}"; do
|
||||||
|
i=0
|
||||||
|
arraywalker="$testarray"[@]
|
||||||
|
for path in "${!arraywalker}"; do
|
||||||
|
|
||||||
if (( ! i )); then
|
if (( ! i )); then
|
||||||
# Print title in array element 0
|
# Print title in array element 0
|
||||||
printf " ### %s ###\n" "$path"
|
printf " ### %s ###\n" "$path"
|
||||||
else
|
else
|
||||||
# Execute tests
|
# Execute tests
|
||||||
printf -- "%-3d --- %-30s ---" $i "$path"
|
if ! rl::testcmp "${i}" "${path}" \
|
||||||
ra="$(${tool_a} "$path")"
|
"$(${tool_readlink} "$path")" "$(${tool_rdlink} "$path")"; then
|
||||||
rb="$(${tool_b} "$path")"
|
testend=1
|
||||||
if [[ "$ra" = "$rb" ]]; then
|
break
|
||||||
# Test successful
|
|
||||||
printf " OK\n"
|
|
||||||
else
|
|
||||||
# Test failed
|
|
||||||
printf " FAIL\n"
|
|
||||||
printf "\n Result:\n%s\n%s\n" "$ra" "$rb"
|
|
||||||
printf "\n Subject:\n"
|
|
||||||
ls -al "$path"
|
|
||||||
|
|
||||||
# Print debug output of tool_a
|
|
||||||
printf "\n Debug:\n"
|
|
||||||
( "${tool_a}" -d "${path}" )
|
|
||||||
|
|
||||||
testend=1 && break
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
((i++))
|
((i++))
|
||||||
done
|
done
|
||||||
(( testend )) && break
|
(( testend )) && return 1
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user