1 from unittest import TestCase
3 from naja.constants import BITS, MOVES
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': [
26 'action_class': 'LoseHealthOrMSB',
29 'action_class': 'GainHealth',
30 'required_bits': [BITS.RED],
33 exported_state = board.export()
34 board_locations = exported_state.pop('board_locations')
35 self.assertEqual(exported_state, {
40 'locations': [{'actions': [
42 'action_class': 'LoseHealthOrMSB',
45 'action_class': 'GainHealth',
46 'required_bits': [BITS.RED],
49 'player': board.player.export(),
52 for position, location_state in board_locations:
53 positions.append(position)
55 sorted(location_state.keys()), ['actions', 'bitwise_operand'])
56 self.assertEqual(location_state['actions'], [
58 'action_class': 'LoseHealthOrMSB',
62 'action_class': 'GainHealth',
63 'required_bits': [BITS.RED],
67 self.assertTrue(2 <= len(location_state['bitwise_operand']) <= 3)
69 positions, sorted((x, y) for x in range(5) for y in range(5)))
71 def test_lose_health(self):
72 board = GameBoard.new_game([{'actions': []}])
73 self.assertEqual(board.health, 4)
74 state_1 = board.export()
77 self.assertEqual(board.health, 3)
78 state_2 = board.export()
80 self.assert_state(state_1, state_2, exclude=['health'])
82 def test_gain_health(self):
83 board = GameBoard.new_game([{'actions': []}])
85 self.assertEqual(board.health, 2)
86 state_1 = board.export()
89 self.assertEqual(board.health, 3)
90 state_2 = board.export()
92 self.assert_state(state_1, state_2, exclude=['health'])
94 def test_gain_health_at_max(self):
95 board = GameBoard.new_game([{'actions': []}])
96 self.assertEqual(board.health, 4)
97 state_1 = board.export()
100 self.assertEqual(board.health, 4)
101 state_2 = board.export()
103 self.assert_state(state_1, state_2)
105 def generate_locations(self, override_dict=None):
106 locations_dict = dict(((x, y), '%s%s' % (x, y))
107 for x in range(5) for y in range(5))
109 locations_dict.update(override_dict)
110 return locations_dict
112 def test_shift_locations_north(self):
113 board = GameBoard.new_game([{'actions': []}])
114 board.board_locations = self.generate_locations()
115 board.shift_locations('NORTH')
116 self.assertEqual(board.board_locations, self.generate_locations({
117 (2, 0): '21', (2, 1): '23', (2, 3): '24', (2, 4): '20',
120 def test_shift_locations_south(self):
121 board = GameBoard.new_game([{'actions': []}])
122 board.board_locations = self.generate_locations()
123 board.shift_locations('SOUTH')
124 self.assertEqual(board.board_locations, self.generate_locations({
125 (2, 0): '24', (2, 1): '20', (2, 3): '21', (2, 4): '23',
128 def test_shift_locations_east(self):
129 board = GameBoard.new_game([{'actions': []}])
130 board.board_locations = self.generate_locations()
131 board.shift_locations('EAST')
132 self.assertEqual(board.board_locations, self.generate_locations({
133 (0, 2): '42', (1, 2): '02', (3, 2): '12', (4, 2): '32',
136 def test_shift_locations_west(self):
137 board = GameBoard.new_game([{'actions': []}])
138 board.board_locations = self.generate_locations()
139 board.shift_locations('WEST')
140 self.assertEqual(board.board_locations, self.generate_locations({
141 (0, 2): '12', (1, 2): '32', (3, 2): '42', (4, 2): '02',
144 def test_rotate_locations_clockwise(self):
145 board = GameBoard.new_game([{'actions': []}])
146 board.board_locations = self.generate_locations()
147 board.rotate_locations('CLOCKWISE')
148 self.assertEqual(board.board_locations, self.generate_locations({
149 (1, 1): '21', (2, 1): '31', (3, 1): '32',
150 (1, 2): '11', (3, 2): '33',
151 (1, 3): '12', (2, 3): '13', (3, 3): '23',
154 def test_rotate_locations_clockwise_top(self):
155 board = GameBoard.new_game([{'actions': []}], initial_pos=(2, 0))
156 board.board_locations = self.generate_locations()
157 board.rotate_locations('CLOCKWISE')
158 self.assertEqual(board.board_locations, self.generate_locations({
159 (1, 0): '30', (3, 0): '31',
160 (1, 1): '10', (2, 1): '11', (3, 1): '21',
163 def test_rotate_locations_clockwise_right(self):
164 board = GameBoard.new_game([{'actions': []}], initial_pos=(0, 2))
165 board.board_locations = self.generate_locations()
166 board.rotate_locations('CLOCKWISE')
167 self.assertEqual(board.board_locations, self.generate_locations({
168 (0, 1): '11', (1, 1): '12',
170 (0, 3): '01', (1, 3): '03',
173 def test_rotate_locations_clockwise_corner(self):
174 board = GameBoard.new_game([{'actions': []}], initial_pos=(0, 4))
175 board.board_locations = self.generate_locations()
176 board.rotate_locations('CLOCKWISE')
177 self.assertEqual(board.board_locations, self.generate_locations({
178 (0, 3): '13', (1, 3): '14',
182 def test_rotate_locations_anticlockwise(self):
183 board = GameBoard.new_game([{'actions': []}])
184 board.board_locations = self.generate_locations()
185 board.rotate_locations('ANTICLOCKWISE')
186 self.assertEqual(board.board_locations, self.generate_locations({
187 (1, 1): '12', (2, 1): '11', (3, 1): '21',
188 (1, 2): '13', (3, 2): '31',
189 (1, 3): '23', (2, 3): '33', (3, 3): '32',
192 def test_allow_chess_move_knight(self):
193 board = GameBoard.new_game([{'actions': []}])
194 board.allow_chess_move(MOVES.KNIGHT)
195 self.assertEqual(board.player.movement_mode, MOVES.KNIGHT)
197 def test_allow_chess_move_bishop(self):
198 board = GameBoard.new_game([{'actions': []}])
199 board.allow_chess_move(MOVES.BISHOP)
200 self.assertEqual(board.player.movement_mode, MOVES.BISHOP)
202 def test_allow_chess_move_castle(self):
203 board = GameBoard.new_game([{'actions': []}])
204 board.allow_chess_move(MOVES.CASTLE)
205 self.assertEqual(board.player.movement_mode, MOVES.CASTLE)
208 class TestLocationCard(TestCase):
209 def test_generate_bitwise_operand(self):
210 # This is testing a random process, so it may fail occasionally.
213 operand_sets.append(LocationCard.generate_bitwise_operand())
216 for operand_set in operand_sets:
217 sizes[len(operand_set)] += 1
218 bits.update(operand_set)
219 # TODO: Test that there's at least one condition and one direction.
220 self.assertTrue(sizes[2] > 0)
221 self.assertTrue(sizes[3] > 0)
222 self.assertTrue(sizes[2] > sizes[3])
223 self.assertEqual(bits, set(BITS.values()))
225 def test_new_location_no_actions(self):
226 location = LocationCard.new_location({'actions': []})
227 [action] = location.actions
228 self.assertEqual(type(action), actions.DoNothing)
229 self.assertEqual(action.required_bits, set())
231 def test_new_location_one_action(self):
232 location = LocationCard.new_location({'actions': [
233 {'required_bits': [], 'action_class': 'DoNothing'},
235 [action] = location.actions
236 self.assertEqual(type(action), actions.DoNothing)
237 self.assertEqual(action.required_bits, set())
239 def test_parse_bits(self):
241 LocationCard.parse_bits([]), frozenset([]))
243 LocationCard.parse_bits(['RED']), frozenset([BITS.RED]))
245 LocationCard.parse_bits([BITS.BLUE]), frozenset([BITS.BLUE]))
247 LocationCard.parse_bits([BITS.NORTH, 'MSB']),
248 frozenset([BITS.NORTH, BITS.MSB]))