Skip to content

Commit 7049acc

Browse files
authored
[dnd-character] Add a Jinja test template (#879)
[no important files changed]
1 parent e1e6098 commit 7049acc

2 files changed

Lines changed: 63 additions & 4 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{{ header }}
2+
3+
# usage: {{ solution }} modifier n
4+
# -> output expected modifier
5+
6+
# usage: {{ solution }} generate
7+
# -> output each characteristic and ability value, one per line
8+
9+
10+
# ability modifier
11+
{% for idx, case in cases %}
12+
{%- if case["property"] == "modifier" %}
13+
@test "{{ case["description"] }}" {
14+
{% if idx == 0 %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip
15+
run bash {{ solution }} {{ case["property"] }} {{ case["input"]["score"] }}
16+
assert_success
17+
assert_output "{{ case["expected"] }}"
18+
}
19+
{%- endif %}
20+
{% endfor %}
21+
# generate a character, validate expected output
22+
{%- set attrs = ["strength", "dexterity", "constitution", "intelligence", "wisdom", "charisma", "hitpoints"] %}
23+
@test "generate a character" {
24+
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
25+
run bash {{ solution }} generate
26+
assert_success
27+
# these don't have to appear in any particular order
28+
{%- for attr in attrs %}
29+
assert_line --regexp '^{{ attr }} [[:digit:]]{1,2}$'
30+
{%- endfor %}
31+
# no other output: `run` populates the `lines` array
32+
assert_equal ${{ "{" }}#lines[@]} {{ attrs | length }}
33+
}
34+
35+
36+
# Usage: between $val $low $high
37+
# Value is between low (inclusive) and high (inclusive).
38+
between() {
39+
(( $2 <= $1 && $1 <= $3 ))
40+
}
41+
42+
# random ability is within range
43+
@test "validate ability range and hitpoint value" {
44+
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
45+
for _ in {1..50}; do
46+
while read -r c v; do
47+
if [[ $c == "hitpoints" ]]; then
48+
hits=$v
49+
else
50+
assert between "$v" 3 18
51+
[[ $c == "constitution" ]] && const=$v
52+
fi
53+
done < <(bash {{ solution }} generate)
54+
55+
const_mod=$(bash {{ solution }} modifier "$const")
56+
assert_equal $((10 + const_mod)) "$hits"
57+
done
58+
}

exercises/practice/dnd-character/dnd_character.bats

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env bats
22
load bats-extra
33

4-
# local version: 1.1.0.0
4+
# generated on 2026-06-29T18:26:21+00:00
5+
# local version: 2.0.0.0
56

67
# usage: dnd_character.sh modifier n
78
# -> output expected modifier
@@ -13,7 +14,7 @@ load bats-extra
1314
# ability modifier
1415

1516
@test "ability modifier for score 3 is -4" {
16-
#[[ $BATS_RUN_SKIPPED == "true" ]] || skip
17+
# [[ $BATS_RUN_SKIPPED == "true" ]] || skip
1718
run bash dnd_character.sh modifier 3
1819
assert_success
1920
assert_output "-4"
@@ -125,8 +126,8 @@ load bats-extra
125126
}
126127

127128

128-
# generate a character, validate expected output
129129

130+
# generate a character, validate expected output
130131
@test "generate a character" {
131132
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
132133
run bash dnd_character.sh generate
@@ -146,7 +147,7 @@ load bats-extra
146147

147148
# Usage: between $val $low $high
148149
# Value is between low (inclusive) and high (inclusive).
149-
between() {
150+
between() {
150151
(( $2 <= $1 && $1 <= $3 ))
151152
}
152153

0 commit comments

Comments
 (0)