Choose locations for the board.
[naja.git] / naja / tests / test_gameboard.py
1 from unittest import TestCase
2
3 from naja.constants import BITS
4 from naja.gameboard import GameBoard, LocationCard
5 from naja import actions
6
7
8 class TestGameBoard(TestCase):
9     def test_export_new_board(self):
10         board = GameBoard.new_game([{'actions': []}])
11         exported_state = board.export()
12         board_locations = exported_state.pop('board_locations')
13         self.assertEqual(exported_state, {
14             'max_health': 4,
15             'health': 4,
16             'wins_required': 4,
17             'wins': 0,
18             'locations': [{'actions': []}],
19             'player': board.player.export(),
20         })
21         self.assertEqual(
22             set(board_locations.keys()),
23             set((x, y) for x in range(5) for y in range(5)))
24         for location_state in board_locations.values():
25             self.assertEqual(
26                 sorted(location_state.keys()), ['actions', 'bitwise_operand'])
27             self.assertEqual(location_state['actions'], [])
28             self.assertTrue(2 <= len(location_state['bitwise_operand']) <= 3)
29
30     def test_lose_health(self):
31         board = GameBoard.new_game([{'actions': []}])
32         self.assertEqual(board.health, 4)
33         state_1 = board.export()
34
35         board.lose_health()
36         self.assertEqual(board.health, 3)
37         state_2 = board.export()
38
39         # Make sure nothing else has changed.
40         state_1.pop('health')
41         state_2.pop('health')
42         self.assertEqual(state_1, state_2)
43
44
45 class TestLocationCard(TestCase):
46     def test_generate_bitwise_operand(self):
47         # This is testing a random process, so it may fail occasionally.
48         operand_sets = []
49         for _ in range(100):
50             operand_sets.append(LocationCard.generate_bitwise_operand())
51         sizes = {2: 0, 3: 0}
52         bits = set()
53         for operand_set in operand_sets:
54             sizes[len(operand_set)] += 1
55             bits.update(operand_set)
56             # TODO: Test that there's at least one condition and one direction.
57         self.assertTrue(sizes[2] > 0)
58         self.assertTrue(sizes[3] > 0)
59         self.assertTrue(sizes[2] > sizes[3])
60         self.assertEqual(bits, set(BITS.values()))
61
62     def test_new_location_no_actions(self):
63         location = LocationCard.new_location({'actions': []})
64         self.assertEqual(location.actions, [])
65
66     def test_new_location_one_action(self):
67         location = LocationCard.new_location({'actions': [
68             {'required_bits': [], 'action_class': 'DoNothing'},
69         ]})
70         [action] = location.actions
71         self.assertEqual(type(action), actions.DoNothing)
72         self.assertEqual(action.required_bits, set())