1
0
mirror of https://github.com/sstephenson/bats.git synced 2024-12-28 15:39:44 +01:00
bats/libexec/bats-preprocess

57 lines
1.2 KiB
Plaintext
Raw Normal View History

2011-12-28 19:40:14 +01:00
#!/usr/bin/env bash
set -e
encode_name() {
local name="$1"
local result="test_"
if [[ ! "$name" =~ [^[:alnum:]\ _-] ]]; then
name="${name//_/-5f}"
name="${name//-/-2d}"
name="${name// /_}"
result+="$name"
else
local length="${#name}"
local char i
for ((i=0; i<length; i++)); do
char="${name:$i:1}"
if [ "$char" = " " ]; then
result+="_"
elif [[ "$char" =~ [[:alnum:]] ]]; then
result+="$char"
else
result+="$(printf -- "-%02x" \'"$char")"
fi
done
fi
echo "$result"
}
tests=()
index=0
2018-05-21 12:49:44 +02:00
pattern='^ *@test(\[([^ ])[^ ]*\])? *([^ ].*) *\{ *(.*)$'
2011-12-28 19:40:14 +01:00
while IFS= read -r line; do
let index+=1
2013-11-17 19:31:07 +01:00
if [[ "$line" =~ $pattern ]]; then
2018-05-21 12:49:44 +02:00
quoted_name="${BASH_REMATCH[3]}"
2019-01-08 10:49:27 +01:00
if [ -n "${BASH_REMATCH[2]}" ] ; then
2018-05-21 12:49:44 +02:00
encoded_name="test_${BASH_REMATCH[2]}"
else
name="$(eval echo "$quoted_name")"
encoded_name="$(encode_name "$name")"
fi
body="${BASH_REMATCH[4]}"
2011-12-28 19:40:14 +01:00
tests["${#tests[@]}"]="$encoded_name"
2013-11-17 20:04:57 +01:00
echo "${encoded_name}() { bats_test_begin ${quoted_name} ${index}; ${body}"
2011-12-28 19:40:14 +01:00
else
printf "%s\n" "$line"
2011-12-28 19:40:14 +01:00
fi
done
for test_name in "${tests[@]}"; do
echo "bats_test_function ${test_name}"
done