Skip to content

Commit 1a038af

Browse files
authored
tournament: add test template (exercism#1898)
1 parent 916b850 commit 1a038af

2 files changed

Lines changed: 115 additions & 76 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{%- import "generator_macros.j2" as macros with context -%}
2+
{{ macros.header() }}
3+
4+
class {{ exercise | camel_case }}Test(unittest.TestCase):
5+
{% for case in cases %}
6+
{%- set input = case["input"] -%}
7+
def test_{{ case["description"] | to_snake }}(self):
8+
results = {{ input["rows"] }}
9+
table = {{ case["expected"] }}
10+
self.assertEqual({{ case["property"] }}(results), table)
11+
12+
{% endfor %}
13+
14+
{{ macros.footer() }}

exercises/tournament/tournament_test.py

Lines changed: 101 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,116 +2,141 @@
22

33
from tournament import tally
44

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

7+
88
class TournamentTest(unittest.TestCase):
99
def test_just_the_header_if_no_input(self):
10-
self.assertEqual(
11-
tally([]),
12-
['Team | MP | W | D | L | P']
13-
)
10+
results = []
11+
table = ["Team | MP | W | D | L | P"]
12+
self.assertEqual(tally(results), table)
1413

1514
def test_a_win_is_three_points_a_loss_is_zero_points(self):
16-
results = ['Allegoric Alaskans;Blithering Badgers;win']
17-
table = ['Team | MP | W | D | L | P',
18-
'Allegoric Alaskans | 1 | 1 | 0 | 0 | 3',
19-
'Blithering Badgers | 1 | 0 | 0 | 1 | 0']
15+
results = ["Allegoric Alaskans;Blithering Badgers;win"]
16+
table = [
17+
"Team | MP | W | D | L | P",
18+
"Allegoric Alaskans | 1 | 1 | 0 | 0 | 3",
19+
"Blithering Badgers | 1 | 0 | 0 | 1 | 0",
20+
]
2021
self.assertEqual(tally(results), table)
2122

2223
def test_a_win_can_also_be_expressed_as_a_loss(self):
23-
results = ['Blithering Badgers;Allegoric Alaskans;loss']
24-
table = ['Team | MP | W | D | L | P',
25-
'Allegoric Alaskans | 1 | 1 | 0 | 0 | 3',
26-
'Blithering Badgers | 1 | 0 | 0 | 1 | 0']
24+
results = ["Blithering Badgers;Allegoric Alaskans;loss"]
25+
table = [
26+
"Team | MP | W | D | L | P",
27+
"Allegoric Alaskans | 1 | 1 | 0 | 0 | 3",
28+
"Blithering Badgers | 1 | 0 | 0 | 1 | 0",
29+
]
2730
self.assertEqual(tally(results), table)
2831

2932
def test_a_different_team_can_win(self):
30-
results = ['Blithering Badgers;Allegoric Alaskans;win']
31-
table = ['Team | MP | W | D | L | P',
32-
'Blithering Badgers | 1 | 1 | 0 | 0 | 3',
33-
'Allegoric Alaskans | 1 | 0 | 0 | 1 | 0']
33+
results = ["Blithering Badgers;Allegoric Alaskans;win"]
34+
table = [
35+
"Team | MP | W | D | L | P",
36+
"Blithering Badgers | 1 | 1 | 0 | 0 | 3",
37+
"Allegoric Alaskans | 1 | 0 | 0 | 1 | 0",
38+
]
3439
self.assertEqual(tally(results), table)
3540

3641
def test_a_draw_is_one_point_each(self):
37-
results = ['Allegoric Alaskans;Blithering Badgers;draw']
38-
table = ['Team | MP | W | D | L | P',
39-
'Allegoric Alaskans | 1 | 0 | 1 | 0 | 1',
40-
'Blithering Badgers | 1 | 0 | 1 | 0 | 1']
42+
results = ["Allegoric Alaskans;Blithering Badgers;draw"]
43+
table = [
44+
"Team | MP | W | D | L | P",
45+
"Allegoric Alaskans | 1 | 0 | 1 | 0 | 1",
46+
"Blithering Badgers | 1 | 0 | 1 | 0 | 1",
47+
]
4148
self.assertEqual(tally(results), table)
4249

4350
def test_there_can_be_more_than_one_match(self):
44-
results = ['Allegoric Alaskans;Blithering Badgers;win',
45-
'Allegoric Alaskans;Blithering Badgers;win']
46-
table = ['Team | MP | W | D | L | P',
47-
'Allegoric Alaskans | 2 | 2 | 0 | 0 | 6',
48-
'Blithering Badgers | 2 | 0 | 0 | 2 | 0']
51+
results = [
52+
"Allegoric Alaskans;Blithering Badgers;win",
53+
"Allegoric Alaskans;Blithering Badgers;win",
54+
]
55+
table = [
56+
"Team | MP | W | D | L | P",
57+
"Allegoric Alaskans | 2 | 2 | 0 | 0 | 6",
58+
"Blithering Badgers | 2 | 0 | 0 | 2 | 0",
59+
]
4960
self.assertEqual(tally(results), table)
5061

5162
def test_there_can_be_more_than_one_winner(self):
52-
results = ['Allegoric Alaskans;Blithering Badgers;loss',
53-
'Allegoric Alaskans;Blithering Badgers;win']
54-
table = ['Team | MP | W | D | L | P',
55-
'Allegoric Alaskans | 2 | 1 | 0 | 1 | 3',
56-
'Blithering Badgers | 2 | 1 | 0 | 1 | 3']
63+
results = [
64+
"Allegoric Alaskans;Blithering Badgers;loss",
65+
"Allegoric Alaskans;Blithering Badgers;win",
66+
]
67+
table = [
68+
"Team | MP | W | D | L | P",
69+
"Allegoric Alaskans | 2 | 1 | 0 | 1 | 3",
70+
"Blithering Badgers | 2 | 1 | 0 | 1 | 3",
71+
]
5772
self.assertEqual(tally(results), table)
5873

5974
def test_there_can_be_more_than_two_teams(self):
60-
results = ['Allegoric Alaskans;Blithering Badgers;win',
61-
'Blithering Badgers;Courageous Californians;win',
62-
'Courageous Californians;Allegoric Alaskans;loss']
63-
table = ['Team | MP | W | D | L | P',
64-
'Allegoric Alaskans | 2 | 2 | 0 | 0 | 6',
65-
'Blithering Badgers | 2 | 1 | 0 | 1 | 3',
66-
'Courageous Californians | 2 | 0 | 0 | 2 | 0']
75+
results = [
76+
"Allegoric Alaskans;Blithering Badgers;win",
77+
"Blithering Badgers;Courageous Californians;win",
78+
"Courageous Californians;Allegoric Alaskans;loss",
79+
]
80+
table = [
81+
"Team | MP | W | D | L | P",
82+
"Allegoric Alaskans | 2 | 2 | 0 | 0 | 6",
83+
"Blithering Badgers | 2 | 1 | 0 | 1 | 3",
84+
"Courageous Californians | 2 | 0 | 0 | 2 | 0",
85+
]
6786
self.assertEqual(tally(results), table)
6887

6988
def test_typical_input(self):
70-
results = ['Allegoric Alaskans;Blithering Badgers;win',
71-
'Devastating Donkeys;Courageous Californians;draw',
72-
'Devastating Donkeys;Allegoric Alaskans;win',
73-
'Courageous Californians;Blithering Badgers;loss',
74-
'Blithering Badgers;Devastating Donkeys;loss',
75-
'Allegoric Alaskans;Courageous Californians;win']
76-
77-
table = ['Team | MP | W | D | L | P',
78-
'Devastating Donkeys | 3 | 2 | 1 | 0 | 7',
79-
'Allegoric Alaskans | 3 | 2 | 0 | 1 | 6',
80-
'Blithering Badgers | 3 | 1 | 0 | 2 | 3',
81-
'Courageous Californians | 3 | 0 | 1 | 2 | 1']
82-
89+
results = [
90+
"Allegoric Alaskans;Blithering Badgers;win",
91+
"Devastating Donkeys;Courageous Californians;draw",
92+
"Devastating Donkeys;Allegoric Alaskans;win",
93+
"Courageous Californians;Blithering Badgers;loss",
94+
"Blithering Badgers;Devastating Donkeys;loss",
95+
"Allegoric Alaskans;Courageous Californians;win",
96+
]
97+
table = [
98+
"Team | MP | W | D | L | P",
99+
"Devastating Donkeys | 3 | 2 | 1 | 0 | 7",
100+
"Allegoric Alaskans | 3 | 2 | 0 | 1 | 6",
101+
"Blithering Badgers | 3 | 1 | 0 | 2 | 3",
102+
"Courageous Californians | 3 | 0 | 1 | 2 | 1",
103+
]
83104
self.assertEqual(tally(results), table)
84105

85106
def test_incomplete_competition_not_all_pairs_have_played(self):
86-
results = ['Allegoric Alaskans;Blithering Badgers;loss',
87-
'Devastating Donkeys;Allegoric Alaskans;loss',
88-
'Courageous Californians;Blithering Badgers;draw',
89-
'Allegoric Alaskans;Courageous Californians;win']
90-
91-
table = ['Team | MP | W | D | L | P',
92-
'Allegoric Alaskans | 3 | 2 | 0 | 1 | 6',
93-
'Blithering Badgers | 2 | 1 | 1 | 0 | 4',
94-
'Courageous Californians | 2 | 0 | 1 | 1 | 1',
95-
'Devastating Donkeys | 1 | 0 | 0 | 1 | 0']
96-
107+
results = [
108+
"Allegoric Alaskans;Blithering Badgers;loss",
109+
"Devastating Donkeys;Allegoric Alaskans;loss",
110+
"Courageous Californians;Blithering Badgers;draw",
111+
"Allegoric Alaskans;Courageous Californians;win",
112+
]
113+
table = [
114+
"Team | MP | W | D | L | P",
115+
"Allegoric Alaskans | 3 | 2 | 0 | 1 | 6",
116+
"Blithering Badgers | 2 | 1 | 1 | 0 | 4",
117+
"Courageous Californians | 2 | 0 | 1 | 1 | 1",
118+
"Devastating Donkeys | 1 | 0 | 0 | 1 | 0",
119+
]
97120
self.assertEqual(tally(results), table)
98121

99122
def test_ties_broken_alphabetically(self):
100-
results = ['Courageous Californians;Devastating Donkeys;win',
101-
'Allegoric Alaskans;Blithering Badgers;win',
102-
'Devastating Donkeys;Allegoric Alaskans;loss',
103-
'Courageous Californians;Blithering Badgers;win',
104-
'Blithering Badgers;Devastating Donkeys;draw',
105-
'Allegoric Alaskans;Courageous Californians;draw']
106-
107-
table = ['Team | MP | W | D | L | P',
108-
'Allegoric Alaskans | 3 | 2 | 1 | 0 | 7',
109-
'Courageous Californians | 3 | 2 | 1 | 0 | 7',
110-
'Blithering Badgers | 3 | 0 | 1 | 2 | 1',
111-
'Devastating Donkeys | 3 | 0 | 1 | 2 | 1']
112-
123+
results = [
124+
"Courageous Californians;Devastating Donkeys;win",
125+
"Allegoric Alaskans;Blithering Badgers;win",
126+
"Devastating Donkeys;Allegoric Alaskans;loss",
127+
"Courageous Californians;Blithering Badgers;win",
128+
"Blithering Badgers;Devastating Donkeys;draw",
129+
"Allegoric Alaskans;Courageous Californians;draw",
130+
]
131+
table = [
132+
"Team | MP | W | D | L | P",
133+
"Allegoric Alaskans | 3 | 2 | 1 | 0 | 7",
134+
"Courageous Californians | 3 | 2 | 1 | 0 | 7",
135+
"Blithering Badgers | 3 | 0 | 1 | 2 | 1",
136+
"Devastating Donkeys | 3 | 0 | 1 | 2 | 1",
137+
]
113138
self.assertEqual(tally(results), table)
114139

115140

116-
if __name__ == '__main__':
141+
if __name__ == "__main__":
117142
unittest.main()

0 commit comments

Comments
 (0)