2011-12-28 19:40:14 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -e
|
|
|
|
|
|
|
|
encode_name() {
|
|
|
|
local name="$1"
|
|
|
|
local result="test_"
|
2017-02-15 17:01:34 +01:00
|
|
|
local hex_code
|
2011-12-28 19:40:14 +01:00
|
|
|
|
|
|
|
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
|
2017-02-15 17:01:34 +01:00
|
|
|
printf -v 'hex_code' -- "-%02x" \'"$char"
|
|
|
|
result+="$hex_code"
|
2011-12-28 19:40:14 +01:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2017-02-15 17:01:34 +01:00
|
|
|
printf -v "$2" '%s' "$result"
|
2011-12-28 19:40:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tests=()
|
|
|
|
index=0
|
2013-11-17 20:04:57 +01:00
|
|
|
pattern='^ *@test *([^ ].*) *\{ *(.*)$'
|
2011-12-28 19:40:14 +01:00
|
|
|
|
|
|
|
while IFS= read -r line; do
|
2017-02-15 16:44:11 +01:00
|
|
|
line="${line//$'\r'}"
|
2013-10-29 03:01:51 +01:00
|
|
|
let index+=1
|
2013-11-17 19:31:07 +01:00
|
|
|
if [[ "$line" =~ $pattern ]]; then
|
|
|
|
quoted_name="${BASH_REMATCH[1]}"
|
2013-11-17 20:04:57 +01:00
|
|
|
body="${BASH_REMATCH[2]}"
|
2011-12-28 19:40:14 +01:00
|
|
|
name="$(eval echo "$quoted_name")"
|
2017-02-15 17:01:34 +01:00
|
|
|
encode_name "$name" 'encoded_name'
|
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
|
2013-04-06 14:01:03 +02:00
|
|
|
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
|