-
Notifications
You must be signed in to change notification settings - Fork 546
Expand file tree
/
Copy pathgenerate_tests
More file actions
executable file
·61 lines (43 loc) · 2.01 KB
/
generate_tests
File metadata and controls
executable file
·61 lines (43 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env bash
# Exit if anything fails.
set -euo pipefail
# see comment in generator-utils/utils.sh
# shellcheck source=bin/generator-utils/utils.sh
# shellcheck source=./generator-utils/utils.sh
source ./bin/generator-utils/utils.sh
if [ ! -e bin/generator-utils/escape_double_quotes ]; then
message "info" "Building util function"
cd util/escape_double_quotes && ./build && cd ../..
fi
digest_template() {
local template
template=$(bin/generator-utils/escape_double_quotes bin/test_template)
# Turn every token into a jq command
echo "$template" | sed 's/${\([^}]*\)\}\$/$(echo $case | jq -r '\''.\1'\'')/g'
}
message "info" "Generating tests.."
canonical_json=$(cat canonical_data.json)
slug=$(echo "$canonical_json" | jq '.exercise')
# Remove double quotes
slug=$(echo "$slug" | sed 's/"//g')
exercise_dir="exercises/practice/$slug"
test_file="$exercise_dir/tests/$slug.rs"
cat <<EOT >"$test_file"
use $(dash_to_underscore "$slug")::*;
EOT
# Flattens canonical json, extracts only the objects with a uuid
cases=$(echo "$canonical_json" | jq '[ .. | objects | with_entries(select(.key | IN("uuid", "description", "input", "expected", "property"))) | select(. != {}) | select(has("uuid")) ]')
# Shellcheck doesn't recognize that `case` is not unused
# shellcheck disable=SC2034
jq -c '.[]' <<<"$cases" | while read -r case; do
# Evaluate the bash parts and replace them with their return values
eval_template="$(digest_template | sed -e "s/\$(\(.*\))/\$\(\1\)/g")"
eval_template="$(eval "echo \"$eval_template\"")"
# Turn function name into snake_case
formatted_template=$(echo "$eval_template" | sed -E -e '/^fn/!b' -e 's/[^a-zA-Z0-9_{}()[:space:]-]//g' -e 's/([[:upper:]])/ \L\1/g' -e 's/(fn[[:space:]]+)([a-z0-9_-]+)/\1\L\2/g' -e 's/ /_/g' -e 's/_\{/\{/g' -e 's/-/_/g' | sed 's/fn_/fn /' | sed 's/__\+/_/g')
# Push to test file
echo "$formatted_template" >>"$test_file"
printf "\\n" >>"$test_file"
done
rustfmt "$test_file"
message "success" "Generated tests successfully! Check out ${test_file}"