1
0
mirror of https://github.com/sstephenson/bats.git synced 2024-12-27 23:19:44 +01:00

Add bats_test_succeeds() function

This commit is contained in:
Francois Laupretre 2018-05-21 12:49:44 +02:00
parent 03608115df
commit 4f9253e9d5
2 changed files with 41 additions and 5 deletions

View File

@ -28,6 +28,7 @@ fi
BATS_TEST_DIRNAME="$(dirname "$BATS_TEST_FILENAME")"
BATS_TEST_NAMES=()
BATS_TEST_RESULTS=()
load() {
local name="$1"
@ -267,12 +268,43 @@ bats_exit_trap() {
exit "$status"
}
bats_test_succeeds() {
local name status index i ret
ret=0
for name in $@
do
index=''
i=0
while [ $i -lt ${#BATS_TEST_NAMES[@]} ; do
if [ "${BATS_TEST_NAMES[$i]}" = "test_$name" ] ; then
index=$i
break
fi
done
if [ -z $index ] ; then
echo "Error: $name: this test name does not exist" >&2
else
status=${BATS_TEST_RESULTS[$index]}
if [ -z $status ] ; then
echo "Warning: $name: Cannot check status as test didn't run yet"
else
let ret+=$status
fi
fi
done
return $ret
}
bats_perform_tests() {
echo "1..$#"
test_number=1
status=0
for test_name in "$@"; do
"$0" $BATS_EXTENDED_SYNTAX "$BATS_TEST_FILENAME" "$test_name" "$test_number" || status=1
BATS_TEST_RESULTS[$test_number]=$status
let test_number+=1
done
exit "$status"

View File

@ -31,15 +31,19 @@ encode_name() {
tests=()
index=0
pattern='^ *@test *([^ ].*) *\{ *(.*)$'
pattern='^ *@test(\[([^ ])[^ ]*\])? *([^ ].*) *\{ *(.*)$'
while IFS= read -r line; do
let index+=1
if [[ "$line" =~ $pattern ]]; then
quoted_name="${BASH_REMATCH[1]}"
body="${BASH_REMATCH[2]}"
name="$(eval echo "$quoted_name")"
encoded_name="$(encode_name "$name")"
quoted_name="${BASH_REMATCH[3]}"
if [ -n "${BASH_REMATCH[2]}" ; then
encoded_name="test_${BASH_REMATCH[2]}"
else
name="$(eval echo "$quoted_name")"
encoded_name="$(encode_name "$name")"
fi
body="${BASH_REMATCH[4]}"
tests["${#tests[@]}"]="$encoded_name"
echo "${encoded_name}() { bats_test_begin ${quoted_name} ${index}; ${body}"
else