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