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 test_export_new_board(self):
10 board = GameBoard.new_game([{'actions': [{
11 'action_class': 'LoseHealthOrMSB',
14 exported_state = board.export()
15 board_locations = exported_state.pop('board_locations')
16 self.assertEqual(exported_state, {
21 'locations': [{'actions': [{
22 'action_class': 'LoseHealthOrMSB',
25 'player': board.player.export(),
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():
32 sorted(location_state.keys()), ['actions', 'bitwise_operand'])
33 self.assertEqual(location_state['actions'], [{
34 'action_class': 'LoseHealthOrMSB',
38 self.assertTrue(2 <= len(location_state['bitwise_operand']) <= 3)
40 def test_lose_health(self):
41 board = GameBoard.new_game([{'actions': []}])
42 self.assertEqual(board.health, 4)
43 state_1 = board.export()
46 self.assertEqual(board.health, 3)
47 state_2 = board.export()
49 # Make sure nothing else has changed.
52 self.assertEqual(state_1, state_2)
55 class TestLocationCard(TestCase):
56 def test_generate_bitwise_operand(self):
57 # This is testing a random process, so it may fail occasionally.
60 operand_sets.append(LocationCard.generate_bitwise_operand())
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()))
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())
78 def test_new_location_one_action(self):
79 location = LocationCard.new_location({'actions': [
80 {'required_bits': [], 'action_class': 'DoNothing'},
82 [action] = location.actions
83 self.assertEqual(type(action), actions.DoNothing)
84 self.assertEqual(action.required_bits, set())