Skip to content

Commit 3091e17

Browse files
tqa236cmccandless
authored andcommitted
Dnd character add test template (#2136)
* Add test template for dnd character * Fix import statement * Write templates for the last 3 tests * Change assert and remove hardcode test * Remove hardcoded code
1 parent 6a9e5d1 commit 3091e17

File tree

2 files changed

+70
-30
lines changed

2 files changed

+70
-30
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{%- import "generator_macros.j2" as macros with context -%}
2+
{% set class = exercise | camel_case -%}
3+
{{ macros.header(["Character", "modifier"]) }}
4+
5+
class {{ exercise | camel_case }}Test(unittest.TestCase):
6+
{% for supercase in cases -%}
7+
{% set property = supercase["property"] -%}
8+
{% set description = supercase["description"] | to_snake -%}
9+
{% if "cases" in supercase -%}
10+
{% for case in supercase["cases"] -%}
11+
def test_{{ case["description"] | replace("-","n") | to_snake }}(self):
12+
{% set value = case["input"]["score"] -%}
13+
{% set expected = case["expected"] -%}
14+
self.assertEqual({{ case["property"] }}({{ value }}), {{ expected }})
15+
{% endfor %}
16+
17+
{% elif property == "ability" -%}
18+
def test_{{ description }}(self):
19+
score = Character().{{ property }}()
20+
self.assertIs({{ supercase["expected"] | replace("&&","and") }}, True)
21+
22+
{% elif property == "character" -%}
23+
def test_{{ description}}(self):
24+
Char = Character()
25+
{% for ability in supercase["expected"] -%}
26+
{% set statement = supercase["expected"][ability] | replace("&&","and") | replace(ability, ["Char.", ability ]|join("")) -%}
27+
{% if ability == "hitpoints" -%}
28+
{% set statement = statement | replace("constitution","Char.constitution") -%}
29+
{%- endif -%}
30+
self.assertIs({{ statement }}, True)
31+
{% endfor %}
32+
33+
{% elif property == "strength" -%}
34+
def test_{{ description }}(self):
35+
Char = Character()
36+
self.assertIs({{ supercase["expected"] | replace(property , ["Char.", property]|join(""))}}, True)
37+
38+
{%- endif -%}
39+
{% endfor %}
40+
41+
{{ macros.footer() }}

exercises/dnd-character/dnd_character_test.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,76 @@
22

33
from dnd_character import Character, modifier
44

5-
65
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0
76

8-
class DnDCharacterTest(unittest.TestCase):
9-
def test_modifier_for_score_3_is_n4(self):
7+
8+
class DndCharacterTest(unittest.TestCase):
9+
def test_ability_modifier_for_score_3_is_n4(self):
1010
self.assertEqual(modifier(3), -4)
1111

12-
def test_modifier_for_score_4_is_n3(self):
12+
def test_ability_modifier_for_score_4_is_n3(self):
1313
self.assertEqual(modifier(4), -3)
1414

15-
def test_modifier_for_score_5_is_n3(self):
15+
def test_ability_modifier_for_score_5_is_n3(self):
1616
self.assertEqual(modifier(5), -3)
1717

18-
def test_modifier_for_score_6_is_n2(self):
18+
def test_ability_modifier_for_score_6_is_n2(self):
1919
self.assertEqual(modifier(6), -2)
2020

21-
def test_modifier_for_score_7_is_n2(self):
21+
def test_ability_modifier_for_score_7_is_n2(self):
2222
self.assertEqual(modifier(7), -2)
2323

24-
def test_modifier_for_score_8_is_n1(self):
24+
def test_ability_modifier_for_score_8_is_n1(self):
2525
self.assertEqual(modifier(8), -1)
2626

27-
def test_modifier_for_score_9_is_n1(self):
27+
def test_ability_modifier_for_score_9_is_n1(self):
2828
self.assertEqual(modifier(9), -1)
2929

30-
def test_modifier_for_score_10_is_0(self):
30+
def test_ability_modifier_for_score_10_is_0(self):
3131
self.assertEqual(modifier(10), 0)
3232

33-
def test_modifier_for_score_11_is_0(self):
33+
def test_ability_modifier_for_score_11_is_0(self):
3434
self.assertEqual(modifier(11), 0)
3535

36-
def test_modifier_for_score_12_is_1(self):
36+
def test_ability_modifier_for_score_12_is_1(self):
3737
self.assertEqual(modifier(12), 1)
3838

39-
def test_modifier_for_score_13_is_1(self):
39+
def test_ability_modifier_for_score_13_is_1(self):
4040
self.assertEqual(modifier(13), 1)
4141

42-
def test_modifier_for_score_14_is_2(self):
42+
def test_ability_modifier_for_score_14_is_2(self):
4343
self.assertEqual(modifier(14), 2)
4444

45-
def test_modifier_for_score_15_is_2(self):
45+
def test_ability_modifier_for_score_15_is_2(self):
4646
self.assertEqual(modifier(15), 2)
4747

48-
def test_modifier_for_score_16_is_3(self):
48+
def test_ability_modifier_for_score_16_is_3(self):
4949
self.assertEqual(modifier(16), 3)
5050

51-
def test_modifier_for_score_17_is_3(self):
51+
def test_ability_modifier_for_score_17_is_3(self):
5252
self.assertEqual(modifier(17), 3)
5353

54-
def test_modifier_for_score_18_is_4(self):
54+
def test_ability_modifier_for_score_18_is_4(self):
5555
self.assertEqual(modifier(18), 4)
5656

5757
def test_random_ability_is_within_range(self):
58-
self.assertIn(Character().ability(), range(3, 19))
58+
score = Character().ability()
59+
self.assertIs(score >= 3 and score <= 18, True)
5960

6061
def test_random_character_is_valid(self):
6162
Char = Character()
62-
self.assertIn(Char.strength, range(3, 19))
63-
self.assertIn(Char.dexterity, range(3, 19))
64-
self.assertIn(Char.constitution, range(3, 19))
65-
self.assertIn(Char.intelligence, range(3, 19))
66-
self.assertIn(Char.wisdom, range(3, 19))
67-
self.assertIn(Char.charisma, range(3, 19))
68-
self.assertEqual(
69-
Char.hitpoints,
70-
10 + modifier(Char.constitution))
63+
self.assertIs(Char.strength >= 3 and Char.strength <= 18, True)
64+
self.assertIs(Char.dexterity >= 3 and Char.dexterity <= 18, True)
65+
self.assertIs(Char.constitution >= 3 and Char.constitution <= 18, True)
66+
self.assertIs(Char.intelligence >= 3 and Char.intelligence <= 18, True)
67+
self.assertIs(Char.wisdom >= 3 and Char.wisdom <= 18, True)
68+
self.assertIs(Char.charisma >= 3 and Char.charisma <= 18, True)
69+
self.assertIs(Char.hitpoints == 10 + modifier(Char.constitution), True)
7170

7271
def test_each_ability_is_only_calculated_once(self):
7372
Char = Character()
74-
self.assertEqual(Char.strength, Char.strength)
73+
self.assertIs(Char.strength == Char.strength, True)
7574

7675

77-
if __name__ == '__main__':
76+
if __name__ == "__main__":
7877
unittest.main()

0 commit comments

Comments
 (0)