1 from unittest import TestCase
3 from naja.constants import BITS, MOVES
4 from naja.gameboard import GameBoard, LocationCard
5 from naja.player import Player
6 from naja import actions
9 class TestActions(TestCase):
10 def make_player(self, *bits):
13 player_bits |= (1 << bit)
14 return Player(player_bits, None)
16 def make_board(self, player_bits=None, locations=None):
18 locations = [{'actions': []}]
19 board = GameBoard.new_game(locations)
20 if player_bits is not None:
21 board.player.bits.bits = 0
22 board.player.bits.set_bits(player_bits)
25 def assert_player_bits(self, board, *bits):
26 self.assertEqual(sum(1 << bit for bit in bits), board.player.bits.bits)
28 def assert_state(self, state1, state2, exclude=(), player_exclude=()):
29 def filter_dict(source, exclude_keys):
30 return dict((k, v) for k, v in source.items()
31 if k not in exclude_keys)
33 state1 = filter_dict(state1, exclude)
34 if 'player' in state1:
35 state1['player'] = filter_dict(state1['player'], player_exclude)
36 state2 = filter_dict(state2, exclude)
37 if 'player' in state2:
38 state2['player'] = filter_dict(state2['player'], player_exclude)
40 self.assertEqual(state1, state2)
42 def test_check_available(self):
43 def check_available(action_bits, player_bits, expected_result):
44 action = actions.LocationAction(action_bits)
45 player = self.make_player(*player_bits)
46 self.assertEqual(action.check_available(player), expected_result)
48 check_available(set(), [], True)
49 check_available(set(), [BITS.MSB], True)
50 check_available(set([BITS.MSB]), [], False)
51 check_available(set([BITS.MSB]), [BITS.MSB], True)
53 def test_get_text(self):
54 class LocationAction1(actions.LocationAction):
56 action1 = LocationAction1([])
57 self.assertEqual(action1.get_text(), "foo")
59 class LocationAction2(actions.LocationAction):
61 action2 = LocationAction2([], bar="baz")
62 self.assertEqual(action2.get_text(), "foo baz")
64 def test_get_text_row_column(self):
65 class DirectionAction(actions.LocationAction):
66 TEXT = "foo %(direction)s %(rowcol)s"
68 action_north = DirectionAction([], direction='NORTH')
69 self.assertEqual(action_north.get_text(), "foo {NORTH} column")
71 action_south = DirectionAction([], direction='SOUTH')
72 self.assertEqual(action_south.get_text(), "foo {SOUTH} column")
74 action_east = DirectionAction([], direction='EAST')
75 self.assertEqual(action_east.get_text(), "foo {EAST} row")
77 action_west = DirectionAction([], direction='WEST')
78 self.assertEqual(action_west.get_text(), "foo {WEST} row")
80 def test_DoNothing(self):
81 board = self.make_board()
82 state_before = board.export()
83 actions.DoNothing(set()).perform_action(board, None)
84 state_after = board.export()
85 self.assert_state(state_before, state_after)
87 def test_LoseHealthOrMSB_MSB_clear(self):
88 board = self.make_board()
89 state_before = board.export()
90 actions.LoseHealthOrMSB(set()).perform_action(board, None)
91 state_after = board.export()
92 self.assertEqual(state_after['health'], state_before['health'] - 1)
93 self.assert_state(state_before, state_after, exclude=['health'])
95 def test_LoseHealthOrMSB_MSB_set(self):
96 board = self.make_board(player_bits=[BITS.MSB])
97 state_before = board.export()
98 actions.LoseHealthOrMSB(set()).perform_action(board, None)
99 state_after = board.export()
100 self.assertEqual(board.player.bits.check_bit(BITS.MSB), False)
101 self.assert_state(state_before, state_after, player_exclude=['bits'])
103 def test_SetBits(self):
104 board = self.make_board()
105 state_before = board.export()
106 card = LocationCard(set([BITS.MSB, BITS.NORTH]), [])
107 actions.SetBits(set()).perform_action(board, card)
108 state_after = board.export()
110 board.player.bits.check_bits([BITS.MSB, BITS.NORTH]), True)
111 self.assert_state(state_before, state_after, player_exclude=['bits'])
113 def test_ToggleBits(self):
114 board = self.make_board(player_bits=[BITS.NORTH])
115 state_before = board.export()
116 card = LocationCard(set([BITS.MSB, BITS.NORTH]), [])
117 actions.ToggleBits(set()).perform_action(board, card)
118 state_after = board.export()
119 self.assertEqual(board.player.bits.check_bit(BITS.MSB), True)
120 self.assertEqual(board.player.bits.check_bit(BITS.NORTH), False)
121 self.assert_state(state_before, state_after, player_exclude=['bits'])
123 def test_LoseHealthOrMSBAndSetBits_MSB_clear(self):
124 board = self.make_board(player_bits=[])
125 state_before = board.export()
126 card = LocationCard(set([BITS.BLUE, BITS.NORTH]), [])
127 actions.LoseHealthOrMSBAndSetBits(set()).perform_action(board, card)
128 state_after = board.export()
129 self.assertEqual(state_after['health'], state_before['health'] - 1)
130 self.assert_player_bits(board, BITS.BLUE, BITS.NORTH)
132 state_before, state_after, exclude=['health'],
133 player_exclude=['bits'])
135 def test_LoseHealthOrMSBAndSetBits_MSB_set(self):
136 board = self.make_board(player_bits=[BITS.MSB])
137 state_before = board.export()
138 card = LocationCard(set([BITS.BLUE, BITS.NORTH]), [])
139 actions.LoseHealthOrMSBAndSetBits(set()).perform_action(board, card)
140 state_after = board.export()
141 self.assert_player_bits(board, BITS.BLUE, BITS.NORTH)
142 self.assert_state(state_before, state_after, player_exclude=['bits'])
144 def test_LoseHealthOrMSBAndSetBits_MSB_set_and_on_card(self):
145 board = self.make_board(player_bits=[BITS.MSB])
146 state_before = board.export()
147 card = LocationCard(set([BITS.MSB, BITS.NORTH]), [])
148 actions.LoseHealthOrMSBAndSetBits(set()).perform_action(board, card)
149 state_after = board.export()
150 self.assert_player_bits(board, BITS.MSB, BITS.NORTH)
151 self.assert_state(state_before, state_after, player_exclude=['bits'])
153 def test_AcquireWinToken(self):
154 board = self.make_board(
155 player_bits=[BITS.RED, BITS.GREEN, BITS.BLUE])
156 state_before = board.export()
157 actions.AcquireWinToken(set()).perform_action(board, None)
158 state_after = board.export()
159 self.assertEqual(state_after['wins'], state_before['wins'] + 1)
160 self.assertEqual(board.player.bits.bits, 0)
162 state_before, state_after, exclude=['wins'],
163 player_exclude=['bits'])
165 def test_GainHealthAndClearBitsOrMSB_MSB_clear(self):
166 board = self.make_board(player_bits=[BITS.NORTH])
168 state_before = board.export()
169 card = LocationCard(set([BITS.BLUE, BITS.NORTH]), [])
170 actions.GainHealthAndClearBitsOrMSB(set()).perform_action(board, card)
171 state_after = board.export()
172 self.assertEqual(state_after['health'], state_before['health'] + 1)
173 self.assert_player_bits(board)
175 state_before, state_after, exclude=['health'],
176 player_exclude=['bits'])
178 def test_GainHealthAndClearBitsOrMSB_MSB_set(self):
179 board = self.make_board(player_bits=[BITS.MSB, BITS.NORTH])
181 state_before = board.export()
182 card = LocationCard(set([BITS.BLUE, BITS.NORTH]), [])
183 actions.GainHealthAndClearBitsOrMSB(set()).perform_action(board, card)
184 state_after = board.export()
185 self.assertEqual(state_after['health'], state_before['health'] + 1)
186 self.assert_player_bits(board, BITS.NORTH)
188 state_before, state_after, exclude=['health'],
189 player_exclude=['bits'])
191 def test_AllowKnightMove(self):
192 board = self.make_board(player_bits=[BITS.RED, BITS.BLUE])
193 actions.AllowChessMove(set([BITS.RED, BITS.BLUE]), chesspiece="KNIGHT").perform_action(board, None)
194 self.assertEqual(board.player.movement_mode, MOVES.KNIGHT)
197 def test_AllowBishopMove(self):
198 board = self.make_board(player_bits=[BITS.RED, BITS.BLUE])
199 actions.AllowChessMove(set([BITS.RED, BITS.BLUE]), chesspiece="BISHOP").perform_action(board, None)
200 self.assertEqual(board.player.movement_mode, MOVES.BISHOP)
203 def test_AllowCastleMove(self):
204 board = self.make_board(player_bits=[BITS.RED, BITS.BLUE])
205 actions.AllowChessMove(set([BITS.RED, BITS.BLUE]), chesspiece="CASTLE").perform_action(board, None)
206 self.assertEqual(board.player.movement_mode, MOVES.CASTLE)