Skip to content

Commit 89b5589

Browse files
authored
Update dnd-character tests (#579)
* Update dnd-character tests * Formatter
1 parent b461853 commit 89b5589

File tree

4 files changed

+64
-112
lines changed

4 files changed

+64
-112
lines changed
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
local function ability(scores)
1+
local function roll_dice()
2+
return { math.random(6), math.random(6), math.random(6), math.random(6) }
3+
end
4+
5+
local function ability()
6+
local scores = roll_dice()
27
local total = 0
38
local smallest = 6
4-
assert(#scores == 4)
59
for _, score in ipairs(scores) do
610
total = total + score
711
if score < smallest then
@@ -11,10 +15,6 @@ local function ability(scores)
1115
return total - smallest
1216
end
1317

14-
local function roll_dice()
15-
return { math.random(6), math.random(6), math.random(6), math.random(6) }
16-
end
17-
1818
local function modifier(input)
1919
return (input - 10) // 2
2020
end
@@ -25,16 +25,16 @@ function Character:new(name)
2525
self.__index = self
2626
local meta_tbl = {
2727
name = name,
28-
strength = ability(roll_dice()),
29-
dexterity = ability(roll_dice()),
30-
constitution = ability(roll_dice()),
31-
intelligence = ability(roll_dice()),
32-
wisdom = ability(roll_dice()),
33-
charisma = ability(roll_dice())
28+
strength = ability(),
29+
dexterity = ability(),
30+
constitution = ability(),
31+
intelligence = ability(),
32+
wisdom = ability(),
33+
charisma = ability()
3434
}
3535
meta_tbl.hitpoints = 10 + modifier(meta_tbl.constitution)
3636

3737
return setmetatable(meta_tbl, self)
3838
end
3939

40-
return { Character = Character, ability = ability, roll_dice = roll_dice, modifier = modifier }
40+
return { Character = Character, ability = ability, modifier = modifier }

exercises/practice/dnd-character/.meta/tests.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,8 @@ description = "random character is valid"
6565

6666
[2ca77b9b-c099-46c3-a02c-0d0f68ffa0fe]
6767
description = "each ability is only calculated once"
68+
include = false
69+
70+
[dca2b2ec-f729-4551-84b9-078876bb4808]
71+
description = "each ability is only calculated once"
72+
reimplements = "2ca77b9b-c099-46c3-a02c-0d0f68ffa0fe"

exercises/practice/dnd-character/dnd-character.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ local Character = {}
33
local function ability(scores)
44
end
55

6-
local function roll_dice()
7-
end
8-
96
local function modifier(input)
107
end
118

12-
return { Character = Character, ability = ability, roll_dice = roll_dice, modifier = modifier }
9+
return { Character = Character, ability = ability, modifier = modifier }

exercises/practice/dnd-character/dnd-character_spec.lua

Lines changed: 45 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -69,121 +69,71 @@ describe('dnd', function()
6969
end)
7070

7171
describe('ability', function()
72-
it('uses 3 largest numbers from scores in descending order', function()
73-
assert.equal(9, dnd.ability({ 4, 3, 2, 1 }))
74-
end)
75-
76-
it('uses 3 largest numbers from scores in ascending order', function()
77-
assert.equal(9, dnd.ability({ 1, 2, 3, 4 }))
78-
end)
79-
80-
it('uses 3 largest numbers from scores in random order', function()
81-
assert.equal(9, dnd.ability({ 2, 4, 3, 1 }))
82-
end)
83-
84-
it('returns 3 with lowest equal numbers', function()
85-
assert.equal(3, dnd.ability({ 1, 1, 1, 1 }))
86-
end)
87-
88-
it('returns 18 with highest equal numbers', function()
89-
assert.equal(18, dnd.ability({ 6, 6, 6, 6 }))
90-
end)
91-
end)
92-
93-
describe('roll_dice', function()
94-
it('returns 4 numbers, each in the range 1 to 6', function()
95-
for _ = 1, 10 do
96-
local scores = dnd.roll_dice()
97-
assert.equal(4, #scores)
98-
for _, score in ipairs(scores) do
99-
assert.equal("integer", math.type(score))
100-
assert.lteq(1, score)
101-
assert.lteq(score, 6)
102-
end
72+
it('random ability is within range', function()
73+
for i = 1, 10 do
74+
local ability_score = dnd.ability()
75+
assert.equal('integer', math.type(ability_score))
76+
assert.lteq(3, ability_score)
77+
assert.lteq(ability_score, 18)
10378
end
10479
end)
10580
end)
10681

10782
describe('Character', function()
10883
it('creates a character with the supplied name', function()
10984
local names = {
110-
"Alice",
111-
"Bob",
112-
"Charlie",
113-
"David",
114-
"Eve",
115-
"Fred",
116-
"Ginny",
117-
"Harriet",
118-
"Ileana",
119-
"Joseph",
120-
"Kincaid",
121-
"Larry"
85+
'Alice',
86+
'Bob',
87+
'Charlie',
88+
'David',
89+
'Eve',
90+
'Fred',
91+
'Ginny',
92+
'Harriet',
93+
'Ileana',
94+
'Joseph',
95+
'Kincaid',
96+
'Larry'
12297
}
12398
for _, name in ipairs(names) do
12499
assert.equal(name, dnd.Character:new(name).name)
125100
end
126101
end)
127102

128-
it('creates a character with valid strength', function()
129-
local character = dnd.Character:new("Alice")
130-
local strength = character.strength
131-
assert.equal("integer", math.type(strength))
132-
assert.lteq(3, strength)
133-
assert.lteq(strength, 18)
134-
assert.equal(strength, character.strength)
135-
end)
103+
it('random character is valid', function()
104+
local character = dnd.Character:new('Alice')
136105

137-
it('creates a character with valid dexterity', function()
138-
local character = dnd.Character:new("Bob")
139-
local dexterity = character.dexterity
140-
assert.equal("integer", math.type(dexterity))
141-
assert.lteq(3, dexterity)
142-
assert.lteq(dexterity, 18)
143-
assert.equal(dexterity, character.dexterity)
144-
end)
106+
assert.lteq(3, character.strength)
107+
assert.lteq(character.strength, 18)
145108

146-
it('creates a character with valid constitution', function()
147-
local character = dnd.Character:new("Charlie")
148-
local constitution = character.constitution
149-
assert.equal("integer", math.type(constitution))
150-
assert.lteq(3, constitution)
151-
assert.lteq(constitution, 18)
152-
assert.equal(constitution, character.constitution)
153-
end)
109+
assert.lteq(3, character.dexterity)
110+
assert.lteq(character.dexterity, 18)
154111

155-
it('creates a character with valid intelligence', function()
156-
local character = dnd.Character:new("David")
157-
local intelligence = character.intelligence
158-
assert.equal("integer", math.type(intelligence))
159-
assert.lteq(3, intelligence)
160-
assert.lteq(intelligence, 18)
161-
assert.equal(intelligence, character.intelligence)
162-
end)
112+
assert.lteq(3, character.constitution)
113+
assert.lteq(character.constitution, 18)
163114

164-
it('creates a character with valid wisdom', function()
165-
local character = dnd.Character:new("Eve")
166-
local wisdom = character.wisdom
167-
assert.equal("integer", math.type(wisdom))
168-
assert.lteq(3, wisdom)
169-
assert.lteq(wisdom, 18)
170-
assert.equal(wisdom, character.wisdom)
171-
end)
115+
assert.lteq(3, character.intelligence)
116+
assert.lteq(character.intelligence, 18)
172117

173-
it('creates a character with valid charisma', function()
174-
local character = dnd.Character:new("Fred")
175-
local charisma = character.charisma
176-
assert.equal("integer", math.type(charisma))
177-
assert.lteq(3, charisma)
178-
assert.lteq(charisma, 18)
179-
assert.equal(charisma, character.charisma)
118+
assert.lteq(3, character.wisdom)
119+
assert.lteq(character.wisdom, 18)
120+
121+
assert.lteq(3, character.charisma)
122+
assert.lteq(character.charisma, 18)
123+
124+
assert.equal(10 + dnd.modifier(character.constitution), character.hitpoints)
180125
end)
181126

182-
it('creates a character with valid hitpoints', function()
183-
for i = 1, 10 do
184-
local character = dnd.Character:new(tostring(i))
185-
assert.equal(10 + dnd.modifier(character.constitution), character.hitpoints)
186-
end
127+
it('each ability is calculated once', function()
128+
local character = dnd.Character:new('Bob')
129+
130+
assert.equal(character.strength, character.strength)
131+
assert.equal(character.dexterity, character.dexterity)
132+
assert.equal(character.constitution, character.constitution)
133+
assert.equal(character.intelligence, character.intelligence)
134+
assert.equal(character.wisdom, character.wisdom)
135+
assert.equal(character.charisma, character.charisma)
136+
assert.equal(character.hitpoints, character.hitpoints)
187137
end)
188138
end)
189139
end)

0 commit comments

Comments
 (0)