Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dnd-character tests #316

Merged
merged 1 commit into from
Nov 20, 2023
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
8 changes: 7 additions & 1 deletion exercises/practice/dnd-character/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,10 @@ description = "random ability is within range"
description = "random character is valid"

[2ca77b9b-c099-46c3-a02c-0d0f68ffa0fe]
description = "each ability is only calculated once"
description = "each ability is only calculated once"
include = false

[dca2b2ec-f729-4551-84b9-078876bb4808]
description = "each ability is only calculated once"
reimplements = "2ca77b9b-c099-46c3-a02c-0d0f68ffa0fe"
include = false
48 changes: 28 additions & 20 deletions exercises/practice/dnd-character/dnd-character-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@
(module+ test
(require rackunit rackunit/text-ui)

(define character (make-character))
(define (in-range? n)
(<= 3 n 18))
(define (constitution->hitpoints n)
(+ (modifier n) 10))
(define (validate-character instance pred)
(and (pred (character-strength instance))
(pred (character-dexterity instance))
(pred (character-constitution instance))
(pred (character-intelligence instance))
(pred (character-wisdom instance))
(pred (character-charisma instance))
(eqv? (constitution->hitpoints (character-constitution instance))
(character-hitpoints instance))))
(define (ability-in-range? score)
(<= 3 score 18))

(define (all-scores-in-range? char)
(define abilities (list character-strength
character-dexterity
character-constitution
character-intelligence
character-wisdom
character-charisma))
(define valid-hitpoints (<= 6 (character-hitpoints char) 14))
(and (for/and ([f abilities])
(ability-in-range? (f char)))
valid-hitpoints))

(define (characters-are-random?)
(define previous (make-character))
(for/and ([_ (in-range 1000)])
(define current (make-character))
(define result (not (equal? current previous)))
(set! previous current)
result))

(define suite
(test-suite
Expand Down Expand Up @@ -73,13 +81,13 @@
(modifier 18) 4)

(test-true "random ability is within range"
(in-range? (ability)))
(ability-in-range? (ability)))

(test-true "random character is valid"
(validate-character character in-range?))
(test-eqv? "each ability is only calculated once"
(character-strength character)
(character-strength character))))
(all-scores-in-range? (make-character)))

; track-specific test
(test-true "all characters are randomly generated"
(characters-are-random?))))

(run-tests suite))