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