|
1 | 1 | import unittest |
2 | | -import go_counting |
3 | 2 |
|
| 3 | +from go_counting import Board, WHITE, BLACK, NONE |
4 | 4 |
|
5 | 5 | # Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0 |
6 | 6 |
|
7 | | -board5x5 = [ |
8 | | - " B ", |
9 | | - " B B ", |
10 | | - "B W B", |
11 | | - " W W ", |
12 | | - " W " |
13 | | -] |
14 | | - |
15 | 7 |
|
16 | 8 | class GoCountingTest(unittest.TestCase): |
17 | 9 | 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 "]) |
19 | 11 | stone, territory = board.territory(x=0, y=1) |
20 | | - self.assertEqual(stone, go_counting.BLACK) |
| 12 | + self.assertEqual(stone, BLACK) |
21 | 13 | self.assertSetEqual(territory, {(0, 0), (0, 1), (1, 0)}) |
22 | 14 |
|
23 | 15 | 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 "]) |
25 | 17 | stone, territory = board.territory(x=2, y=3) |
26 | | - self.assertEqual(stone, go_counting.WHITE) |
| 18 | + self.assertEqual(stone, WHITE) |
27 | 19 | self.assertSetEqual(territory, {(2, 3)}) |
28 | 20 |
|
29 | 21 | 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 "]) |
31 | 23 | stone, territory = board.territory(x=1, y=4) |
32 | | - self.assertEqual(stone, go_counting.NONE) |
| 24 | + self.assertEqual(stone, NONE) |
33 | 25 | self.assertSetEqual(territory, {(0, 3), (0, 4), (1, 4)}) |
34 | 26 |
|
35 | 27 | 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 "]) |
37 | 29 | stone, territory = board.territory(x=1, y=1) |
38 | | - self.assertEqual(stone, go_counting.NONE) |
| 30 | + self.assertEqual(stone, NONE) |
39 | 31 | self.assertSetEqual(territory, set()) |
40 | 32 |
|
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 "]) |
43 | 35 | with self.assertRaisesWithMessage(ValueError): |
44 | 36 | board.territory(x=-1, y=1) |
45 | 37 |
|
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 "]) |
48 | 40 | with self.assertRaisesWithMessage(ValueError): |
49 | 41 | board.territory(x=5, y=1) |
50 | 42 |
|
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 "]) |
53 | 45 | with self.assertRaisesWithMessage(ValueError): |
54 | 46 | board.territory(x=1, y=-1) |
55 | 47 |
|
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 "]) |
58 | 50 | with self.assertRaisesWithMessage(ValueError): |
59 | 51 | board.territory(x=1, y=5) |
60 | 52 |
|
61 | 53 | def test_one_territory_is_the_whole_board(self): |
62 | | - board = go_counting.Board([" "]) |
| 54 | + board = Board([" "]) |
63 | 55 | 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 "]) |
74 | 62 | 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()) |
78 | 66 |
|
79 | 67 | def test_two_region_rectangular_board(self): |
80 | | - input_board = [" B "] |
81 | | - board = go_counting.Board(input_board) |
| 68 | + board = Board([" B "]) |
82 | 69 | 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()) |
86 | 73 |
|
87 | 74 | # Utility functions |
88 | 75 | def assertRaisesWithMessage(self, exception): |
89 | 76 | return self.assertRaisesRegex(exception, r".+") |
90 | 77 |
|
91 | 78 |
|
92 | | -if __name__ == '__main__': |
| 79 | +if __name__ == "__main__": |
93 | 80 | unittest.main() |
0 commit comments