@@ -9,42 +9,44 @@ source ./bin/generator-utils/utils.sh
9
9
10
10
digest_template () {
11
11
template=$( cat bin/test_template)
12
- # turn every token into a jq command
12
+ # Turn every token into a jq command
13
13
echo " $template " | sed ' s/${\([^}]*\)\}\$/$(echo $case | jq -r ' \' ' .\1' \' ' )/g'
14
14
}
15
15
16
+ message " info" " Generating tests.."
16
17
canonical_json=$( cat canonical_data.json)
17
- SLUG=$( echo " $canonical_json " | jq ' .exercise' )
18
- # shellcheck disable=SC2001
18
+ slug=$( echo " $canonical_json " | jq ' .exercise' )
19
19
# Remove double quotes
20
- SLUG=$( echo " $SLUG " | sed ' s/"//g' )
21
- EXERCISE_DIR=" exercises/practice/$SLUG "
22
- TEST_FILE=" $EXERCISE_DIR /tests/$SLUG .rs"
23
- rm " $TEST_FILE "
20
+ slug=$( echo " $slug " | sed ' s/"//g' )
21
+ exercise_dir=" exercises/practice/$slug "
22
+ test_file=" $exercise_dir /tests/$slug .rs"
24
23
25
- cat << EOT >"$TEST_FILE "
26
- use $( dash_to_underscore " $SLUG " ) ::*;
24
+ cat << EOT >"$test_file "
25
+ use $( dash_to_underscore " $slug " ) ::*;
27
26
// Add tests here
28
27
29
28
EOT
30
29
31
30
# Flattens canonical json, extracts only the objects with a uuid
32
- CASES =$( echo " $CANONICAL_JSON " | jq ' [ .. | objects | with_entries(select(.key | IN("uuid", "description", "input", "expected", "property"))) | select(. != {}) | select(has("uuid")) ]' )
31
+ cases =$( echo " $canonical_json " | jq ' [ .. | objects | with_entries(select(.key | IN("uuid", "description", "input", "expected", "property"))) | select(. != {}) | select(has("uuid")) ]' )
33
32
33
+ # Shellcheck doesn't recognize that `case` is not unused
34
34
# shellcheck disable=SC2034
35
- jq -c ' .[]' <<< " $CASES " | while read -r case ; do
35
+ jq -c ' .[]' <<< " $cases " | while read -r case ; do
36
36
37
37
# Evaluate the bash parts and replace them with their return values
38
- EVAL_TEMPLATE =" $( digest_template | sed -e " s/\$ (\(.*\))/\$ \(\1\)/g" ) "
39
- EVAL_TEMPLATE =" $( eval " echo \" $EVAL_TEMPLATE \" " ) "
38
+ eval_template =" $( digest_template | sed -e " s/\$ (\(.*\))/\$ \(\1\)/g" ) "
39
+ eval_template =" $( eval " echo \" $eval_template \" " ) "
40
40
41
- # Turn function name unto snake_case
42
- FORMATTED_TEMPLATE=$( echo " $EVAL_TEMPLATE " | sed -e ' :loop' -e ' s/\(fn[^(]*\)[ -]/\1_/g' -e ' t loop' | sed ' s/fn_/fn /' )
43
- # Push to file
41
+ # Turn function name into snake_case
42
+ formatted_template=$( echo " $eval_template " | sed -e ' :loop' -e ' s/\(fn[^(]*\)[ -]/\1_/g' -e ' t loop' | sed ' s/fn_/fn /' )
44
43
45
- echo " $FORMATTED_TEMPLATE " >> " $TEST_FILE "
46
- printf " \\ n" >> " $TEST_FILE "
44
+ # Push to test file
45
+ echo " $formatted_template " >> " $test_file "
46
+ printf " \\ n" >> " $test_file "
47
47
48
48
done
49
49
50
- rustfmt " $TEST_FILE "
50
+ rustfmt " $test_file "
51
+
52
+ message " success" " Generated tests successfully! Check out ${test_file} "
0 commit comments