Skip to content

Commit 441d59c

Browse files
committed
adapt tests and example solution to canonical data 1.1.0
1 parent 1363b92 commit 441d59c

File tree

3 files changed

+64
-56
lines changed

3 files changed

+64
-56
lines changed

exercises/go-counting/example.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class Board:
1010
def __init__(self, board):
11-
self.board = board.splitlines()
11+
self.board = board
1212
self.width = len(self.board[0])
1313
self.height = len(self.board)
1414

@@ -35,10 +35,10 @@ def walk(self, x, y,
3535

3636
return (visited_territory, visited_stones)
3737

38-
def territoryFor(self, coord):
39-
assert len(coord) == 2
40-
x, y = coord[0], coord[1]
41-
if not self.valid(x, y) or self.board[y][x] in STONES:
38+
def territory(self, x, y):
39+
if not self.valid(x, y):
40+
raise ValueError('invalid coordinate')
41+
if self.board[y][x] in STONES:
4242
return (NONE, set())
4343

4444
visited_territory, visited_stones = self.walk(x, y)
@@ -55,7 +55,7 @@ def territories(self):
5555
for y in range(self.height):
5656
for x in range(self.width):
5757
if not (x, y) in visited:
58-
owner, owned_territories = self.territoryFor((x, y))
58+
owner, owned_territories = self.territory(x, y)
5959
result[owner].update(owned_territories)
6060
visited.update(owned_territories)
6161

exercises/go-counting/go_counting.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ class Board:
99
def __init__(self, board):
1010
pass
1111

12-
def territoryFor(self, coord):
12+
def territory(self, x, y):
1313
"""Find the owner and the territories given a coordinate on
1414
the board
1515
1616
Args:
17-
coord ((int,int)): Coordinate on the board
17+
x (int): Column on the board
18+
y (int): Row on the board
1819
1920
Returns:
2021
(str, set): A tuple, the first element being the owner

exercises/go-counting/go_counting_test.py

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,88 +4,95 @@
44

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

7-
board5x5 = "\n".join([
7+
board5x5 = [
88
" B ",
99
" B B ",
1010
"B W B",
1111
" W W ",
1212
" W "
13-
])
14-
15-
board9x9 = "\n".join([
16-
" B B ",
17-
"B B B",
18-
"WBBBWBBBW",
19-
"W W W W W",
20-
" ",
21-
" W W W W ",
22-
"B B B B",
23-
" W BBB W ",
24-
" B B "
25-
])
13+
]
2614

2715

2816
class GoCountingTest(unittest.TestCase):
29-
def test_5x5_for_black(self):
17+
def test_black_corner_territory_on_5x5_board(self):
3018
board = go_counting.Board(board5x5)
31-
stone, territory = board.territoryFor((0, 1))
19+
stone, territory = board.territory(x=0, y=1)
3220
self.assertEqual(stone, go_counting.BLACK)
33-
self.assertEqual(territory, set([(0, 0), (0, 1), (1, 0)]))
21+
self.assertEqual(territory, {(0, 0), (0, 1), (1, 0)})
3422

35-
def test_5x5_for_white(self):
23+
def test_white_center_territory_on_5x5_board(self):
3624
board = go_counting.Board(board5x5)
37-
stone, territory = board.territoryFor((2, 3))
25+
stone, territory = board.territory(x=2, y=3)
3826
self.assertEqual(stone, go_counting.WHITE)
39-
self.assertEqual(territory, set([(2, 3)]))
27+
self.assertEqual(territory, {(2, 3)})
4028

41-
def test_5x5_for_open_territory(self):
29+
def test_open_corner_territory_on_5x5_board(self):
4230
board = go_counting.Board(board5x5)
43-
stone, territory = board.territoryFor((1, 4))
31+
stone, territory = board.territory(x=1, y=4)
4432
self.assertEqual(stone, go_counting.NONE)
45-
self.assertEqual(territory, set([(0, 3), (0, 4), (1, 4)]))
33+
self.assertEqual(territory, {(0, 3), (0, 4), (1, 4)})
4634

47-
def test_5x5_for_non_territory(self):
35+
def test_a_stone_and_not_a_territory_on_5x5_board(self):
4836
board = go_counting.Board(board5x5)
49-
stone, territory = board.territoryFor((1, 1))
37+
stone, territory = board.territory(x=1, y=1)
5038
self.assertEqual(stone, go_counting.NONE)
5139
self.assertEqual(territory, set())
5240

53-
def test_5x5_for_valid_coordinate(self):
41+
def test_invalid_because_x_is_too_low(self):
5442
board = go_counting.Board(board5x5)
55-
stone, territory = board.territoryFor((-1, 1))
56-
self.assertEqual(stone, go_counting.NONE)
57-
self.assertEqual(territory, set())
43+
with self.assertRaisesWithMessage(ValueError):
44+
board.territory(x=-1, y=1)
5845

59-
def test_5x5_for_valid_coordinate2(self):
46+
def test_invalid_because_x_is_too_high(self):
6047
board = go_counting.Board(board5x5)
61-
stone, territory = board.territoryFor((1, 5))
62-
self.assertEqual(stone, go_counting.NONE)
63-
self.assertEqual(territory, set())
48+
with self.assertRaisesWithMessage(ValueError):
49+
board.territory(x=5, y=1)
50+
51+
def test_invalid_because_y_is_too_low(self):
52+
board = go_counting.Board(board5x5)
53+
with self.assertRaisesWithMessage(ValueError):
54+
board.territory(x=1, y=-1)
55+
56+
def test_invalid_because_y_is_too_high(self):
57+
board = go_counting.Board(board5x5)
58+
with self.assertRaisesWithMessage(ValueError):
59+
board.territory(x=1, y=5)
6460

65-
def test_one_territory_whole_board(self):
66-
board = go_counting.Board(" ")
61+
def test_one_territory_is_the_whole_board(self):
62+
board = go_counting.Board([" "])
6763
territories = board.territories()
68-
self.assertEqual(territories[go_counting.BLACK], set())
69-
self.assertEqual(territories[go_counting.WHITE], set())
70-
self.assertEqual(territories[go_counting.NONE], set([(0, 0)]))
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)})
7167

7268
def test_two_territories_rectangular_board(self):
73-
input_board = "\n".join([
69+
input_board = [
7470
" BW ",
7571
" BW "
76-
])
72+
]
7773
board = go_counting.Board(input_board)
7874
territories = board.territories()
79-
self.assertEqual(territories[go_counting.BLACK], set([(0, 0), (0, 1)]))
80-
self.assertEqual(territories[go_counting.WHITE], set([(3, 0), (3, 1)]))
75+
self.assertEqual(territories[go_counting.BLACK], {(0, 0), (0, 1)})
76+
self.assertEqual(territories[go_counting.WHITE], {(3, 0), (3, 1)})
8177
self.assertEqual(territories[go_counting.NONE], set())
8278

83-
def test_9x9_for_open_territory(self):
84-
board = go_counting.Board(board9x9)
85-
stone, territory = board.territoryFor((0, 8))
86-
self.assertEqual(stone, go_counting.NONE)
87-
self.assertEqual(territory,
88-
set([(2, 7), (2, 8), (1, 8), (0, 8), (0, 7)]))
79+
def test_two_region_rectangular_board(self):
80+
input_board = [" B "]
81+
board = go_counting.Board(input_board)
82+
territories = board.territories()
83+
self.assertEqual(territories[go_counting.BLACK], {(0, 0), (2, 0)})
84+
self.assertEqual(territories[go_counting.WHITE], set())
85+
self.assertEqual(territories[go_counting.NONE], set())
86+
87+
# Utility functions
88+
def setUp(self):
89+
try:
90+
self.assertRaisesRegex
91+
except AttributeError:
92+
self.assertRaisesRegex = self.assertRaisesRegexp
93+
94+
def assertRaisesWithMessage(self, exception):
95+
return self.assertRaisesRegex(exception, r".+")
8996

9097

9198
if __name__ == '__main__':

0 commit comments

Comments
 (0)