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
58 changes: 58 additions & 0 deletions exercises/practice/dnd-character/.meta/template.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{{ header }}

# usage: {{ solution }} modifier n
# -> output expected modifier

# usage: {{ solution }} generate
# -> output each characteristic and ability value, one per line


# ability modifier
{% for idx, case in cases %}
{%- if case["property"] == "modifier" %}
@test "{{ case["description"] }}" {
{% if idx == 0 %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash {{ solution }} {{ case["property"] }} {{ case["input"]["score"] }}
assert_success
assert_output "{{ case["expected"] }}"
}
{%- endif %}
{% endfor %}
# generate a character, validate expected output
{%- set attrs = ["strength", "dexterity", "constitution", "intelligence", "wisdom", "charisma", "hitpoints"] %}
@test "generate a character" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash {{ solution }} generate
assert_success
# these don't have to appear in any particular order
{%- for attr in attrs %}
assert_line --regexp '^{{ attr }} [[:digit:]]{1,2}$'
{%- endfor %}
# no other output: `run` populates the `lines` array
assert_equal ${{ "{" }}#lines[@]} {{ attrs | length }}
}


# Usage: between $val $low $high
# Value is between low (inclusive) and high (inclusive).
between() {
(( $2 <= $1 && $1 <= $3 ))
}

# random ability is within range
@test "validate ability range and hitpoint value" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
for _ in {1..50}; do
while read -r c v; do
if [[ $c == "hitpoints" ]]; then
hits=$v
else
assert between "$v" 3 18
[[ $c == "constitution" ]] && const=$v
fi
done < <(bash {{ solution }} generate)

const_mod=$(bash {{ solution }} modifier "$const")
assert_equal $((10 + const_mod)) "$hits"
done
}
9 changes: 5 additions & 4 deletions exercises/practice/dnd-character/dnd_character.bats
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env bats
load bats-extra

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

# usage: dnd_character.sh modifier n
# -> output expected modifier
Expand All @@ -13,7 +14,7 @@ load bats-extra
# ability modifier

@test "ability modifier for score 3 is -4" {
#[[ $BATS_RUN_SKIPPED == "true" ]] || skip
# [[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash dnd_character.sh modifier 3
assert_success
assert_output "-4"
Expand Down Expand Up @@ -125,8 +126,8 @@ load bats-extra
}


# generate a character, validate expected output

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

# Usage: between $val $low $high
# Value is between low (inclusive) and high (inclusive).
between() {
between() {
(( $2 <= $1 && $1 <= $3 ))
}

Expand Down