Skip to content

Commit 871618b

Browse files
committed
Add template generator
1 parent 6562ae9 commit 871618b

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

bin/generator-utils/generate_tests

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
3+
# shellcheck source=/dev/null
4+
source ./bin/generator-utils/utils
5+
6+
function digest_template() {
7+
template=$(cat bin/generator-utils/test_template)
8+
# shellcheck disable=SC2001
9+
# turn every token into a jq command
10+
echo "$template" | sed 's/${\([^}]*\)\}\$/$(echo $case | jq -r '\''.\1'\'')/g'
11+
}
12+
13+
canonical_json=$(cat canonical_data.json)
14+
SLUG=$(echo "$canonical_json" | jq '.exercise')
15+
EXERCISE_DIR="exercises/practice/${SLUG}"
16+
EXERCISE_DIR="exercises/practice/${SLUG}"
17+
TEST_FILE="${EXERCISE_DIR}/tests/${SLUG}.rs"
18+
cat <<EOT >"$TEST_FILE"
19+
use $(dash_to_underscore "$SLUG")::*;
20+
// Add tests here
21+
22+
EOT
23+
rm "$TEST_FILE"
24+
cases=$(echo "$canonical_json" | jq '[ .. | objects | with_entries(select(.key | IN("uuid", "description", "input", "expected", "property"))) | select(. != {}) | select(has("uuid")) ]')
25+
26+
# shellcheck disable=SC2034
27+
jq -c '.[]' <<<"$cases" | while read -r case; do
28+
29+
# Evaluate the bash parts and replace them with their return values
30+
eval_template="$(digest_template | sed -e "s/\$(\(.*\))/\$\(\1\)/g")"
31+
eval_template="$(eval "echo \"$eval_template\"")"
32+
33+
# Turn function name unto snake_case
34+
formatted_template=$(echo "$eval_template" | sed -e ':loop' -e 's/\(fn[^(]*\)[ -]/\1_/g' -e 't loop' | sed 's/fn_/fn /')
35+
# Push to file
36+
echo "$formatted_template" >>tests.rs
37+
38+
done

bin/generator-utils/templates.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ EOT
2424
)
2525
# fetch canonical_data
2626
canonical_json=$(curl "${curlopts[@]}" "https://raw.githubusercontent.com/exercism/problem-specifications/main/exercises/${slug}/canonical-data.json")
27+
$canonical_json >>canonical_data.json
2728

2829
if [ "${canonical_json}" == "404: Not Found" ]; then
2930
canonical_json=$(jq --null-input '{cases: []}')
@@ -40,6 +41,7 @@ EOT
4041
#(see kindergarten-garden https://github.com/exercism/problem-specifications/blob/main/exercises/kindergarten-garden/canonical-data.json)
4142
# so let's flatten it
4243
cases=$(echo "$canonical_json" | jq '[ .. | objects | with_entries(select(.key | IN("uuid", "description", "input", "expected"))) | select(. != {}) | select(has("uuid")) ]')
44+
fn_name=$(echo "$canonical_json" | jq -r 'first(.. | .property? // empty)')
4345

4446
first_iteration=true
4547
# loop through each object
@@ -57,7 +59,7 @@ fn ${desc}() {
5759
let expected = ${expected};
5860
5961
// TODO: Add assertion
60-
assert_eq!(input, expected)
62+
assert_eq!(${fn_name}(input), expected)
6163
}
6264
6365
EOT

bin/generator-utils/test_template

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[test]
2+
#[ignore]
3+
fn ${description}$() {
4+
5+
let expected = vec!${expected}$;
6+
7+
assert_eq!(${property}$(${input.startVerse}$, ${input.endVerse}$), expected)
8+
}
9+

0 commit comments

Comments
 (0)