Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion bin/generate_tests
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,38 @@ def filter_tojson(data, separators=(',', ':'), indent=None) -> str:
def jinja_env(exercise: pathlib.Path) -> jinja2.Environment:
"""Return a configured Jinja env with filters added."""
env = jinja2.Environment(loader=jinja2.FileSystemLoader(exercise / ".meta"))
# Shell quoting
env.filters["quote"] = shlex.quote
# JSON formatting, default to compact form (`jq -c`).
env.filters["tojson"] = filter_tojson
# String escaping, ANSI-C style.
env.filters["repr"] = repr
# Return a dict with only specified keys kepts.
env.filters["camel_to_snake"] = lambda x: re.sub(r"([a-z])([A-Z])", (lambda m: f"{m.group(1)}_{m.group(2).lower()}"), x)
env.filters["format_list"] = lambda x: shlex.quote(
"[" + ",".join(f'"{i}"' if isinstance(i, str) else str(i) for i in x) + "]"
)
return env


def bool_to_str(obj):
"""Convert boolean values to strings."""
if isinstance(obj, dict):
return {key: bool_to_str(val) for key, val in obj.items()}
if isinstance(obj, list):
return [bool_to_str(val) for val in obj]
if obj is True:
return "true"
if obj is False:
return "false"
return obj


def generate(specs: pathlib.Path, exercise: pathlib.Path) -> None:
"""Generate and write test file for a given spec and exercise."""
cases = get_cases(specs, exercise)
for case in cases:
case["expected"] = bool_to_str(case["expected"])

timestamp = datetime.datetime.now(tz=datetime.UTC).replace(microsecond=0).isoformat()
header = textwrap.dedent(f"""\
Expand All @@ -106,7 +125,12 @@ def generate(specs: pathlib.Path, exercise: pathlib.Path) -> None:
}

# Render the template.
out = jinja_env(exercise).get_template("template.j2").render(data).strip()
try:
template = jinja_env(exercise).get_template("template.j2")
out = template.render(data)
except jinja2.exceptions.TemplateAssertionError as e:
e.add_note(f"Error rendering template for {exercise.name}")
raise

# Check for changes or the lack thereof.
test_file = exercise / json.loads((exercise / ".meta/config.json").read_text())["files"]["test"][0]
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/allergies/.meta/template.j2
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
run bash {{ solution }} {{ case["input"]["score"] }} allergic_to {{ case["input"]["item"] }}

assert_success
assert_output "{{ case["expected"] | lower }}"
assert_output "{{ case["expected"] }}"
{%- else %}
run bash {{ solution }} {{ case["input"]["score"] }} list

Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/armstrong-numbers/.meta/template.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
{% if idx == 0 %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash {{ solution }} {{ case["input"]["number"] }}
assert_success
assert_output "{{ case["expected"] | lower }}"
assert_output "{{ case["expected"] }}"
}
{% endfor %}
2 changes: 1 addition & 1 deletion exercises/practice/isbn-verifier/.meta/template.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
{% if idx == 0 %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash {{ solution }} '{{ case["input"]["isbn"] }}'
assert_success
assert_output "{{ case["expected"] | lower }}"
assert_output "{{ case["expected"] }}"
}
{% endfor %}
2 changes: 1 addition & 1 deletion exercises/practice/isogram/.meta/template.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
{% if idx == 0 %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash {{ solution }} '{{ case["input"]["phrase"] }}'
assert_success
assert_output "{{ case["expected"] | lower }}"
assert_output "{{ case["expected"] }}"
}
{% endfor %}
2 changes: 1 addition & 1 deletion exercises/practice/leap/.meta/template.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
run bash {{ solution }} {{ case["input"]["year"] }}

assert_success
assert_output "{{ case["expected"] | lower }}"
assert_output "{{ case["expected"] }}"
}
{% endfor %}
@test 'No input should return an error' {
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/luhn/.meta/template.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
{% if idx == 0 %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash {{ solution }} "{{ case["input"]["value"] }}"
assert_success
assert_output "{{ case["expected"] | lower }}"
assert_output "{{ case["expected"] }}"
}
{% endfor %}
2 changes: 1 addition & 1 deletion exercises/practice/matching-brackets/.meta/template.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
{% if idx == 0 %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash {{ solution }} "{{ case["input"]["value"] }}"
assert_success
assert_output "{{ case["expected"] | lower }}"
assert_output "{{ case["expected"] }}"
}
{% endfor %}
2 changes: 1 addition & 1 deletion exercises/practice/pangram/.meta/template.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
{% if idx == 0 %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash {{ solution }} '{{ case["input"]["sentence"] }}'
assert_success
assert_output "{{ case["expected"] | lower }}"
assert_output "{{ case["expected"] }}"
}
{% endfor %}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env bats
load bats-extra

# generated on 2026-06-29T06:52:43+00:00
# local version: 2.0.0.0
# generated on 2026-06-30T03:19:10+00:00

@test "triplets whose sum is 12" {
# [[ $BATS_RUN_SKIPPED == "true" ]] || skip
Expand Down
3 changes: 1 addition & 2 deletions exercises/practice/satellite/satellite.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env bats
load bats-extra

# generated on 2026-06-28T23:19:01+00:00
# local version: 2.0.0.0
# generated on 2026-06-30T03:19:10+00:00

@test "Empty tree" {
# [[ $BATS_RUN_SKIPPED == "true" ]] || skip
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/triangle/.meta/template.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
{% if idx == 0 %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash {{ solution }} {{ case["property"] }} {{ case["input"]["sides"] | join(" ") }}
assert_success
assert_output "{{ case["expected"] | lower }}"
assert_output "{{ case["expected"] }}"
}
{% endfor %}