From: Jeremy Thurgood Date: Wed, 14 May 2014 11:15:18 +0000 (+0200) Subject: Improve action tests. X-Git-Tag: 0.1~310 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=4430c92a3e7c1cd14a5a672c4899ee5406a095fb;p=naja.git Improve action tests. --- diff --git a/naja/tests/test_actions.py b/naja/tests/test_actions.py index fba7af7..a8d9201 100644 --- a/naja/tests/test_actions.py +++ b/naja/tests/test_actions.py @@ -18,9 +18,27 @@ class TestActions(TestCase): locations = [{'actions': []}] board = GameBoard.new_game(locations) if player_bits is not None: + board.player.bits.bits = 0 board.player.bits.set_bits(player_bits) return board + def assert_player_bits(self, board, *bits): + self.assertEqual(sum(1 << bit for bit in bits), board.player.bits.bits) + + def assert_state(self, state1, state2, exclude=(), player_exclude=()): + def filter_dict(source, exclude_keys): + return dict((k, v) for k, v in source.items() + if k not in exclude_keys) + + state1 = filter_dict(state1, exclude) + if 'player' in state1: + state1['player'] = filter_dict(state1['player'], player_exclude) + state2 = filter_dict(state2, exclude) + if 'player' in state2: + state2['player'] = filter_dict(state2['player'], player_exclude) + + self.assertEqual(state1, state2) + def test_check_available(self): def check_available(action_bits, player_bits, expected_result): action = actions.LocationAction(action_bits) @@ -41,7 +59,7 @@ class TestActions(TestCase): state_before = board.export() actions.DoNothing(set()).perform_action(board, None) state_after = board.export() - self.assertEqual(state_before, state_after) + self.assert_state(state_before, state_after) def test_LoseHealthOrMSB_MSB_clear(self): board = self.make_board() @@ -49,10 +67,7 @@ class TestActions(TestCase): actions.LoseHealthOrMSB(set()).perform_action(board, None) state_after = board.export() self.assertEqual(state_after['health'], state_before['health'] - 1) - - state_before.pop('health') - state_after.pop('health') - self.assertEqual(state_before, state_after) + self.assert_state(state_before, state_after, exclude=['health']) def test_LoseHealthOrMSB_MSB_set(self): board = self.make_board(player_bits=[BITS.MSB]) @@ -60,33 +75,66 @@ class TestActions(TestCase): actions.LoseHealthOrMSB(set()).perform_action(board, None) state_after = board.export() self.assertEqual(board.player.bits.check_bit(BITS.MSB), False) - - state_before['player'].pop('bits') - state_after['player'].pop('bits') - self.assertEqual(state_before, state_after) + self.assert_state(state_before, state_after, player_exclude=['bits']) def test_SetBits(self): board = self.make_board() state_before = board.export() - location = LocationCard(set([BITS.MSB, BITS.NORTH]), []) - actions.SetBits(set()).perform_action(board, location) + card = LocationCard(set([BITS.MSB, BITS.NORTH]), []) + actions.SetBits(set()).perform_action(board, card) state_after = board.export() self.assertEqual( board.player.bits.check_bits([BITS.MSB, BITS.NORTH]), True) - - state_before['player'].pop('bits') - state_after['player'].pop('bits') - self.assertEqual(state_before, state_after) + self.assert_state(state_before, state_after, player_exclude=['bits']) def test_ToggleBits(self): board = self.make_board(player_bits=[BITS.NORTH]) state_before = board.export() - location = LocationCard(set([BITS.MSB, BITS.NORTH]), []) - actions.ToggleBits(set()).perform_action(board, location) + card = LocationCard(set([BITS.MSB, BITS.NORTH]), []) + actions.ToggleBits(set()).perform_action(board, card) state_after = board.export() self.assertEqual(board.player.bits.check_bit(BITS.MSB), True) self.assertEqual(board.player.bits.check_bit(BITS.NORTH), False) + self.assert_state(state_before, state_after, player_exclude=['bits']) + + def test_LoseHealthOrMSBAndSetBits_MSB_clear(self): + board = self.make_board(player_bits=[]) + state_before = board.export() + card = LocationCard(set([BITS.CYAN, BITS.NORTH]), []) + actions.LoseHealthOrMSBAndSetBits(set()).perform_action(board, card) + state_after = board.export() + self.assertEqual(state_after['health'], state_before['health'] - 1) + self.assert_player_bits(board, BITS.CYAN, BITS.NORTH) + self.assert_state( + state_before, state_after, exclude=['health'], + player_exclude=['bits']) - state_before['player'].pop('bits') - state_after['player'].pop('bits') - self.assertEqual(state_before, state_after) + def test_LoseHealthOrMSBAndSetBits_MSB_set(self): + board = self.make_board(player_bits=[BITS.MSB]) + state_before = board.export() + card = LocationCard(set([BITS.CYAN, BITS.NORTH]), []) + actions.LoseHealthOrMSBAndSetBits(set()).perform_action(board, card) + state_after = board.export() + self.assert_player_bits(board, BITS.CYAN, BITS.NORTH) + self.assert_state(state_before, state_after, player_exclude=['bits']) + + def test_LoseHealthOrMSBAndSetBits_MSB_set_and_on_card(self): + board = self.make_board(player_bits=[BITS.MSB]) + state_before = board.export() + card = LocationCard(set([BITS.MSB, BITS.NORTH]), []) + actions.LoseHealthOrMSBAndSetBits(set()).perform_action(board, card) + state_after = board.export() + self.assert_player_bits(board, BITS.MSB, BITS.NORTH) + self.assert_state(state_before, state_after, player_exclude=['bits']) + + def test_AcquireWinToken(self): + board = self.make_board( + player_bits=[BITS.CYAN, BITS.MAGENTA, BITS.YELLOW, BITS.MSB]) + state_before = board.export() + actions.AcquireWinToken(set()).perform_action(board, None) + state_after = board.export() + self.assertEqual(state_after['wins'], state_before['wins'] + 1) + self.assertEqual(board.player.bits.bits, 0) + self.assert_state( + state_before, state_after, exclude=['wins'], + player_exclude=['bits'])