You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
2.2 KiB

#!/bin/bash
flunk() {
{ if [ "$#" -eq 0 ]; then cat -
else echo -e "$@"
fi
}
return 1
}
assert_success() {
if [[ 0 -ne "$status" ]]; then
flunk "command failed with exit status ${status}\\noutput: ${output}"
elif [ "$#" -gt 0 ]; then
assert_output "$1"
fi
}
assert_failure() {
if [[ 0 -eq "$status" ]]; then
flunk "expected failed exit status"
elif [ "$#" -gt 0 ]; then
assert_output "$1"
fi
}
assert_equal() {
if [[ "$1" != "$2" ]]; then
{ echo "expected: $1"
echo "actual: $2"
} | flunk
fi
}
assert_different() {
if [[ "$1" == "$2" ]]; then
{
echo "expected: $1 != $2"
} | flunk
fi
}
assert_match() {
out="${2:-${output}}"
if [ ! $(echo "${out}" | grep -- "${1}") ]; then
{ echo "expected match: $1"
echo "actual: $out"
} | flunk
fi
}
assert_not_match() {
out="${2:-${output}}"
if [ $(echo "${out}" | grep -- "${1}") ]; then
{ echo "unexpected match: $1"
echo "source: $out"
} | flunk
fi
}
assert_output() {
local expected
if [ $# -eq 0 ]; then expected="$(cat -)"
else expected="$1"
fi
assert_equal "$expected" "$output"
}
assert_line() {
if [ "$1" -ge 0 ] 2>/dev/null; then
assert_equal "$2" "${lines[$1]}"
else
local line
for line in "${lines[@]}"; do
if [ "$line" = "$1" ]; then return 0; fi
done
flunk "expected line \`$1' in \n $output"
fi
}
refute_line() {
if [ "$1" -ge 0 ] 2>/dev/null; then
local num_lines="${#lines[@]}"
if [ "$1" -lt "$num_lines" ]; then
flunk "output has $num_lines lines"
fi
else
local line
for line in "${lines[@]}"; do
if [ "$line" = "$1" ]; then
flunk "expected to not find line \`$line'"
fi
done
fi
}
assert_include() {
eval 'local values=("${'$1'[@]}")'
local element
for element in "${values[@]}"; do
[[ "$element" == "$2" ]] && return 0
done
flunk "failed: array ${values[*]} not include $2}"
}
assert() {
if ! "$@"; then
flunk "failed: $@"
fi
}
assert_running() {
ps aux | grep $1 >> /dev/null
if [ $? -ne 0 ]; then
flunk "process with id $1 is not running"
fi
}
assert_not_running() {
if [ -d /proc/$1 ]; then
flunk "process with id $1 is running"
fi
}