|
4 | 4 |
|
5 | 5 | # Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0
|
6 | 6 |
|
7 |
| -board5x5 = "\n".join([ |
| 7 | +board5x5 = [ |
8 | 8 | " B ",
|
9 | 9 | " B B ",
|
10 | 10 | "B W B",
|
11 | 11 | " W W ",
|
12 | 12 | " 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 | +] |
26 | 14 |
|
27 | 15 |
|
28 | 16 | class GoCountingTest(unittest.TestCase):
|
29 |
| - def test_5x5_for_black(self): |
| 17 | + def test_black_corner_territory_on_5x5_board(self): |
30 | 18 | board = go_counting.Board(board5x5)
|
31 |
| - stone, territory = board.territoryFor((0, 1)) |
| 19 | + stone, territory = board.territory(x=0, y=1) |
32 | 20 | 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)}) |
34 | 22 |
|
35 |
| - def test_5x5_for_white(self): |
| 23 | + def test_white_center_territory_on_5x5_board(self): |
36 | 24 | board = go_counting.Board(board5x5)
|
37 |
| - stone, territory = board.territoryFor((2, 3)) |
| 25 | + stone, territory = board.territory(x=2, y=3) |
38 | 26 | self.assertEqual(stone, go_counting.WHITE)
|
39 |
| - self.assertEqual(territory, set([(2, 3)])) |
| 27 | + self.assertEqual(territory, {(2, 3)}) |
40 | 28 |
|
41 |
| - def test_5x5_for_open_territory(self): |
| 29 | + def test_open_corner_territory_on_5x5_board(self): |
42 | 30 | board = go_counting.Board(board5x5)
|
43 |
| - stone, territory = board.territoryFor((1, 4)) |
| 31 | + stone, territory = board.territory(x=1, y=4) |
44 | 32 | 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)}) |
46 | 34 |
|
47 |
| - def test_5x5_for_non_territory(self): |
| 35 | + def test_a_stone_and_not_a_territory_on_5x5_board(self): |
48 | 36 | board = go_counting.Board(board5x5)
|
49 |
| - stone, territory = board.territoryFor((1, 1)) |
| 37 | + stone, territory = board.territory(x=1, y=1) |
50 | 38 | self.assertEqual(stone, go_counting.NONE)
|
51 | 39 | self.assertEqual(territory, set())
|
52 | 40 |
|
53 |
| - def test_5x5_for_valid_coordinate(self): |
| 41 | + def test_invalid_because_x_is_too_low(self): |
54 | 42 | 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) |
58 | 45 |
|
59 |
| - def test_5x5_for_valid_coordinate2(self): |
| 46 | + def test_invalid_because_x_is_too_high(self): |
60 | 47 | 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) |
64 | 60 |
|
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([" "]) |
67 | 63 | 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)}) |
71 | 67 |
|
72 | 68 | def test_two_territories_rectangular_board(self):
|
73 |
| - input_board = "\n".join([ |
| 69 | + input_board = [ |
74 | 70 | " BW ",
|
75 | 71 | " BW "
|
76 |
| - ]) |
| 72 | + ] |
77 | 73 | board = go_counting.Board(input_board)
|
78 | 74 | 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)}) |
81 | 77 | self.assertEqual(territories[go_counting.NONE], set())
|
82 | 78 |
|
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".+") |
89 | 96 |
|
90 | 97 |
|
91 | 98 | if __name__ == '__main__':
|
|
0 commit comments