Skip to content

Commit fbc6a45

Browse files
authored
Merge branch 'master' into forth-add-test-template
2 parents 67d0886 + 355336d commit fbc6a45

2 files changed

Lines changed: 67 additions & 45 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{%- extends "master_template.j2" -%}
2+
{% set imports = ["Board", "WHITE", "BLACK", "NONE"] %}
3+
4+
{% macro tuplify(territory_list) -%}
5+
{%- if territory_list %}
6+
{
7+
{%- for v in territory_list %}
8+
({{ v | join(', ') }}),
9+
{%- endfor %}
10+
}
11+
{%- else %}
12+
set()
13+
{%- endif %}
14+
{%- endmacro %}
15+
16+
17+
{% macro test_case(case) -%}
18+
def test_{{ case["description"] | to_snake }}(self):
19+
board = Board({{ case["input"]["board"] }})
20+
{%- if case is error_case %}
21+
with self.assertRaisesWithMessage(ValueError):
22+
board.territory(x={{ case["input"]["x"] }}, y={{ case["input"]["y"] }})
23+
{%- else %}
24+
{%- if "owner" in case["expected"] %}
25+
stone, territory = board.territory(x={{ case["input"]["x"] }}, y={{ case["input"]["y"] }})
26+
self.assertEqual(stone, {{ case["expected"]["owner"] }})
27+
self.assertSetEqual(territory, {{ tuplify(case["expected"]["territory"]) }})
28+
{%- else %}
29+
territories = board.territories()
30+
{%- for (k, v) in case["expected"].items() %}
31+
self.assertSetEqual(territories[{{k.replace("territory", "") | upper }}], {{tuplify(v)}})
32+
{%- endfor %}
33+
{%- endif %}
34+
{%- endif %}
35+
{%- endmacro %}
Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,80 @@
11
import unittest
2-
import go_counting
32

3+
from go_counting import Board, WHITE, BLACK, NONE
44

55
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0
66

7-
board5x5 = [
8-
" B ",
9-
" B B ",
10-
"B W B",
11-
" W W ",
12-
" W "
13-
]
14-
157

168
class GoCountingTest(unittest.TestCase):
179
def test_black_corner_territory_on_5x5_board(self):
18-
board = go_counting.Board(board5x5)
10+
board = Board([" B ", " B B ", "B W B", " W W ", " W "])
1911
stone, territory = board.territory(x=0, y=1)
20-
self.assertEqual(stone, go_counting.BLACK)
12+
self.assertEqual(stone, BLACK)
2113
self.assertSetEqual(territory, {(0, 0), (0, 1), (1, 0)})
2214

2315
def test_white_center_territory_on_5x5_board(self):
24-
board = go_counting.Board(board5x5)
16+
board = Board([" B ", " B B ", "B W B", " W W ", " W "])
2517
stone, territory = board.territory(x=2, y=3)
26-
self.assertEqual(stone, go_counting.WHITE)
18+
self.assertEqual(stone, WHITE)
2719
self.assertSetEqual(territory, {(2, 3)})
2820

2921
def test_open_corner_territory_on_5x5_board(self):
30-
board = go_counting.Board(board5x5)
22+
board = Board([" B ", " B B ", "B W B", " W W ", " W "])
3123
stone, territory = board.territory(x=1, y=4)
32-
self.assertEqual(stone, go_counting.NONE)
24+
self.assertEqual(stone, NONE)
3325
self.assertSetEqual(territory, {(0, 3), (0, 4), (1, 4)})
3426

3527
def test_a_stone_and_not_a_territory_on_5x5_board(self):
36-
board = go_counting.Board(board5x5)
28+
board = Board([" B ", " B B ", "B W B", " W W ", " W "])
3729
stone, territory = board.territory(x=1, y=1)
38-
self.assertEqual(stone, go_counting.NONE)
30+
self.assertEqual(stone, NONE)
3931
self.assertSetEqual(territory, set())
4032

41-
def test_invalid_because_x_is_too_low(self):
42-
board = go_counting.Board(board5x5)
33+
def test_invalid_because_x_is_too_low_for_5x5_board(self):
34+
board = Board([" B ", " B B ", "B W B", " W W ", " W "])
4335
with self.assertRaisesWithMessage(ValueError):
4436
board.territory(x=-1, y=1)
4537

46-
def test_invalid_because_x_is_too_high(self):
47-
board = go_counting.Board(board5x5)
38+
def test_invalid_because_x_is_too_high_for_5x5_board(self):
39+
board = Board([" B ", " B B ", "B W B", " W W ", " W "])
4840
with self.assertRaisesWithMessage(ValueError):
4941
board.territory(x=5, y=1)
5042

51-
def test_invalid_because_y_is_too_low(self):
52-
board = go_counting.Board(board5x5)
43+
def test_invalid_because_y_is_too_low_for_5x5_board(self):
44+
board = Board([" B ", " B B ", "B W B", " W W ", " W "])
5345
with self.assertRaisesWithMessage(ValueError):
5446
board.territory(x=1, y=-1)
5547

56-
def test_invalid_because_y_is_too_high(self):
57-
board = go_counting.Board(board5x5)
48+
def test_invalid_because_y_is_too_high_for_5x5_board(self):
49+
board = Board([" B ", " B B ", "B W B", " W W ", " W "])
5850
with self.assertRaisesWithMessage(ValueError):
5951
board.territory(x=1, y=5)
6052

6153
def test_one_territory_is_the_whole_board(self):
62-
board = go_counting.Board([" "])
54+
board = Board([" "])
6355
territories = board.territories()
64-
self.assertSetEqual(territories[go_counting.BLACK], set())
65-
self.assertSetEqual(territories[go_counting.WHITE], set())
66-
self.assertSetEqual(territories[go_counting.NONE], {(0, 0)})
67-
68-
def test_two_territories_rectangular_board(self):
69-
input_board = [
70-
" BW ",
71-
" BW "
72-
]
73-
board = go_counting.Board(input_board)
56+
self.assertSetEqual(territories[BLACK], set())
57+
self.assertSetEqual(territories[WHITE], set())
58+
self.assertSetEqual(territories[NONE], {(0, 0)})
59+
60+
def test_two_territory_rectangular_board(self):
61+
board = Board([" BW ", " BW "])
7462
territories = board.territories()
75-
self.assertSetEqual(territories[go_counting.BLACK], {(0, 0), (0, 1)})
76-
self.assertSetEqual(territories[go_counting.WHITE], {(3, 0), (3, 1)})
77-
self.assertSetEqual(territories[go_counting.NONE], set())
63+
self.assertSetEqual(territories[BLACK], {(0, 0), (0, 1)})
64+
self.assertSetEqual(territories[WHITE], {(3, 0), (3, 1)})
65+
self.assertSetEqual(territories[NONE], set())
7866

7967
def test_two_region_rectangular_board(self):
80-
input_board = [" B "]
81-
board = go_counting.Board(input_board)
68+
board = Board([" B "])
8269
territories = board.territories()
83-
self.assertSetEqual(territories[go_counting.BLACK], {(0, 0), (2, 0)})
84-
self.assertSetEqual(territories[go_counting.WHITE], set())
85-
self.assertSetEqual(territories[go_counting.NONE], set())
70+
self.assertSetEqual(territories[BLACK], {(0, 0), (2, 0)})
71+
self.assertSetEqual(territories[WHITE], set())
72+
self.assertSetEqual(territories[NONE], set())
8673

8774
# Utility functions
8875
def assertRaisesWithMessage(self, exception):
8976
return self.assertRaisesRegex(exception, r".+")
9077

9178

92-
if __name__ == '__main__':
79+
if __name__ == "__main__":
9380
unittest.main()

0 commit comments

Comments
 (0)