1 from unittest import TestCase
3 from naja.constants import BITS
4 from naja.gameboard import GameBoard, LocationCard
5 from naja import actions
8 class TestGameBoard(TestCase):
9 def assert_state(self, state1, state2, exclude=(), player_exclude=()):
10 def filter_dict(source, exclude_keys):
11 return dict((k, v) for k, v in source.items()
12 if k not in exclude_keys)
14 state1 = filter_dict(state1, exclude)
15 if 'player' in state1:
16 state1['player'] = filter_dict(state1['player'], player_exclude)
17 state2 = filter_dict(state2, exclude)
18 if 'player' in state2:
19 state2['player'] = filter_dict(state2['player'], player_exclude)
21 self.assertEqual(state1, state2)
23 def test_export_new_board(self):
24 board = GameBoard.new_game([{'actions': [{
25 'action_class': 'LoseHealthOrMSB',
28 exported_state = board.export()
29 board_locations = exported_state.pop('board_locations')
30 self.assertEqual(exported_state, {
35 'locations': [{'actions': [{
36 'action_class': 'LoseHealthOrMSB',
39 'player': board.player.export(),
42 set(board_locations.keys()),
43 set((x, y) for x in range(5) for y in range(5)))
44 for location_state in board_locations.values():
46 sorted(location_state.keys()), ['actions', 'bitwise_operand'])
47 self.assertEqual(location_state['actions'], [{
48 'action_class': 'LoseHealthOrMSB',
52 self.assertTrue(2 <= len(location_state['bitwise_operand']) <= 3)
54 def test_lose_health(self):
55 board = GameBoard.new_game([{'actions': []}])
56 self.assertEqual(board.health, 4)
57 state_1 = board.export()
60 self.assertEqual(board.health, 3)
61 state_2 = board.export()
63 self.assert_state(state_1, state_2, exclude=['health'])
65 def test_gain_health(self):
66 board = GameBoard.new_game([{'actions': []}])
68 self.assertEqual(board.health, 2)
69 state_1 = board.export()
72 self.assertEqual(board.health, 3)
73 state_2 = board.export()
75 self.assert_state(state_1, state_2, exclude=['health'])
77 def test_gain_health_at_max(self):
78 board = GameBoard.new_game([{'actions': []}])
79 self.assertEqual(board.health, 4)
80 state_1 = board.export()
83 self.assertEqual(board.health, 4)
84 state_2 = board.export()
86 self.assert_state(state_1, state_2)
89 class TestLocationCard(TestCase):
90 def test_generate_bitwise_operand(self):
91 # This is testing a random process, so it may fail occasionally.
94 operand_sets.append(LocationCard.generate_bitwise_operand())
97 for operand_set in operand_sets:
98 sizes[len(operand_set)] += 1
99 bits.update(operand_set)
100 # TODO: Test that there's at least one condition and one direction.
101 self.assertTrue(sizes[2] > 0)
102 self.assertTrue(sizes[3] > 0)
103 self.assertTrue(sizes[2] > sizes[3])
104 self.assertEqual(bits, set(BITS.values()))
106 def test_new_location_no_actions(self):
107 location = LocationCard.new_location({'actions': []})
108 [action] = location.actions
109 self.assertEqual(type(action), actions.DoNothing)
110 self.assertEqual(action.required_bits, set())
112 def test_new_location_one_action(self):
113 location = LocationCard.new_location({'actions': [
114 {'required_bits': [], 'action_class': 'DoNothing'},
116 [action] = location.actions
117 self.assertEqual(type(action), actions.DoNothing)
118 self.assertEqual(action.required_bits, set())