Skip to content

Commit 57d745a

Browse files
tqa236cmccandless
authored andcommitted
Allergies: add test template (#2121)
1 parent 5d9b599 commit 57d745a

File tree

2 files changed

+68
-26
lines changed

2 files changed

+68
-26
lines changed

exercises/allergies/.meta/template.j2

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{%- import "generator_macros.j2" as macros with context -%}
2+
{{ macros.header(imports=[ exercise | camel_case ]) }}
3+
4+
class {{ exercise | camel_case }}Test(unittest.TestCase):
5+
{% for case in cases -%}
6+
{% for subcase in case["cases"] -%}
7+
{% set property = subcase["property"] -%}
8+
{% set expected = subcase["expected"] -%}
9+
{% set score = subcase["input"]["score"] -%}
10+
{% set description = subcase["description"] | to_snake -%}
11+
{% if property != "list" -%}
12+
{% set item = subcase["input"]["item"] -%}
13+
{% if item not in description -%}
14+
def test_{{ item }}_{{ description }}(self):
15+
{%- else -%}
16+
def test_{{ description }}(self):
17+
{%- endif %}
18+
self.assertIs({{ exercise | camel_case }}({{ score }}).{{ property | to_snake }}("{{ item }}"), {{ expected }})
19+
{% else -%}
20+
def test_{{ description }}(self):
21+
{%- if expected|length <= 1 %}
22+
self.assertEqual({{ exercise | camel_case }}({{ score }}).lst, {{ expected }})
23+
{%- else %}
24+
self.assertCountEqual({{ exercise | camel_case }}({{ score }}).lst, {{ expected }})
25+
{%- endif %}
26+
{% endif %}
27+
28+
{% endfor %}
29+
{% endfor %}
30+
31+
{{ macros.footer() }}

exercises/allergies/allergies_test.py

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22

33
from allergies import Allergies
44

5-
# Python 2/3 compatibility
6-
if not hasattr(unittest.TestCase, 'assertCountEqual'):
7-
unittest.TestCase.assertCountEqual = unittest.TestCase.assertItemsEqual
8-
9-
105
# Tests adapted from `problem-specifications//canonical-data.json` @ v2.0.0
116

12-
class AllergiesTest(unittest.TestCase):
137

8+
class AllergiesTest(unittest.TestCase):
149
def test_eggs_not_allergic_to_anything(self):
1510
self.assertIs(Allergies(0).allergic_to("eggs"), False)
1611

@@ -98,10 +93,10 @@ def test_allergic_to_chocolate_and_something_else(self):
9893
def test_allergic_to_something_but_not_chocolate(self):
9994
self.assertIs(Allergies(80).allergic_to("chocolate"), False)
10095

101-
def test_chocolate_alergic_to_everything(self):
96+
def test_chocolate_allergic_to_everything(self):
10297
self.assertIs(Allergies(255).allergic_to("chocolate"), True)
10398

104-
def test_pollen_not_alergic_to_anything(self):
99+
def test_pollen_not_allergic_to_anything(self):
105100
self.assertIs(Allergies(0).allergic_to("pollen"), False)
106101

107102
def test_allergic_only_to_pollen(self):
@@ -129,45 +124,61 @@ def test_allergic_to_something_but_not_cats(self):
129124
self.assertIs(Allergies(64).allergic_to("cats"), False)
130125

131126
def test_cats_allergic_to_everything(self):
132-
self.assertIs(Allergies(255).allergic_to('cats'), True)
127+
self.assertIs(Allergies(255).allergic_to("cats"), True)
133128

134129
def test_no_allergies(self):
135130
self.assertEqual(Allergies(0).lst, [])
136131

137132
def test_just_eggs(self):
138-
self.assertEqual(Allergies(1).lst, ['eggs'])
133+
self.assertEqual(Allergies(1).lst, ["eggs"])
139134

140135
def test_just_peanuts(self):
141-
self.assertEqual(Allergies(2).lst, ['peanuts'])
136+
self.assertEqual(Allergies(2).lst, ["peanuts"])
142137

143138
def test_just_strawberries(self):
144-
self.assertEqual(Allergies(8).lst, ['strawberries'])
139+
self.assertEqual(Allergies(8).lst, ["strawberries"])
145140

146141
def test_eggs_and_peanuts(self):
147-
self.assertCountEqual(Allergies(3).lst, ['eggs', 'peanuts'])
142+
self.assertCountEqual(Allergies(3).lst, ["eggs", "peanuts"])
148143

149144
def test_more_than_eggs_but_not_peanuts(self):
150-
self.assertCountEqual(Allergies(5).lst, ['eggs', 'shellfish'])
145+
self.assertCountEqual(Allergies(5).lst, ["eggs", "shellfish"])
151146

152147
def test_lots_of_stuff(self):
153148
self.assertCountEqual(
154149
Allergies(248).lst,
155-
['strawberries', 'tomatoes', 'chocolate', 'pollen', 'cats'])
150+
["strawberries", "tomatoes", "chocolate", "pollen", "cats"],
151+
)
156152

157153
def test_everything(self):
158154
self.assertCountEqual(
159-
Allergies(255).lst, [
160-
'eggs', 'peanuts', 'shellfish', 'strawberries', 'tomatoes',
161-
'chocolate', 'pollen', 'cats'
162-
])
155+
Allergies(255).lst,
156+
[
157+
"eggs",
158+
"peanuts",
159+
"shellfish",
160+
"strawberries",
161+
"tomatoes",
162+
"chocolate",
163+
"pollen",
164+
"cats",
165+
],
166+
)
163167

164168
def test_no_allergen_score_parts(self):
165169
self.assertCountEqual(
166-
Allergies(509).lst, [
167-
'eggs', 'shellfish', 'strawberries', 'tomatoes', 'chocolate',
168-
'pollen', 'cats'
169-
])
170-
171-
172-
if __name__ == '__main__':
170+
Allergies(509).lst,
171+
[
172+
"eggs",
173+
"shellfish",
174+
"strawberries",
175+
"tomatoes",
176+
"chocolate",
177+
"pollen",
178+
"cats",
179+
],
180+
)
181+
182+
183+
if __name__ == "__main__":
173184
unittest.main()

0 commit comments

Comments
 (0)