|
| 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 | +} |
0 commit comments