From 05bc52e1532e44727acccf49ace6f125ed6f022a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Sat, 18 Nov 2023 00:21:43 -0800 Subject: [PATCH] Update dnd-character tests --- .../practice/dnd-character/.meta/tests.toml | 8 +++- .../dnd-character/dnd-character-test.rkt | 48 +++++++++++-------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/exercises/practice/dnd-character/.meta/tests.toml b/exercises/practice/dnd-character/.meta/tests.toml index 831cbae5..c077d7ff 100644 --- a/exercises/practice/dnd-character/.meta/tests.toml +++ b/exercises/practice/dnd-character/.meta/tests.toml @@ -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" \ No newline at end of file +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 \ No newline at end of file diff --git a/exercises/practice/dnd-character/dnd-character-test.rkt b/exercises/practice/dnd-character/dnd-character-test.rkt index 6c866639..7abd1554 100644 --- a/exercises/practice/dnd-character/dnd-character-test.rkt +++ b/exercises/practice/dnd-character/dnd-character-test.rkt @@ -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 @@ -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))