X-Git-Url: https://git.ctpug.org.za/?a=blobdiff_plain;f=naja%2Ftests%2Ftest_actions.py;h=fb2c370b4cf11662543479a048564c1066a5951d;hb=558ff08be6951579d134aca37232af1f7a532d6c;hp=e4a4fa523a2ea3b9620ab79de83f6ef46dd44588;hpb=56157eeadd045772b6bfab1ce80e87c1f9bb8719;p=naja.git diff --git a/naja/tests/test_actions.py b/naja/tests/test_actions.py index e4a4fa5..fb2c370 100644 --- a/naja/tests/test_actions.py +++ b/naja/tests/test_actions.py @@ -1,14 +1,25 @@ from unittest import TestCase from naja.constants import BITS -from naja.gameboard import GameBoard +from naja.gameboard import GameBoard, LocationCard from naja.player import Player from naja import actions class TestActions(TestCase): def make_player(self, *bits): - return Player(sum(1 << bit for bit in bits), None) + player_bits = 0 + for bit in bits: + player_bits |= bit + return Player(player_bits, None) + + def make_board(self, player_bits=None, locations=None): + if locations is None: + locations = [{'actions': []}] + board = GameBoard.new_game(locations) + if player_bits is not None: + board.player.bits.set_bits(player_bits) + return board def test_check_available(self): def check_available(action_bits, player_bits, expected_result): @@ -21,20 +32,61 @@ class TestActions(TestCase): check_available(set([BITS.MSB]), [], False) check_available(set([BITS.MSB]), [BITS.MSB], True) + def test_bits_translation(self): + action = actions.LocationAction(set([BITS.NORTH, 'MSB'])) + self.assertEqual(action.required_bits, set([BITS.NORTH, BITS.MSB])) + def test_DoNothing(self): - board = GameBoard.new_game([]) + board = self.make_board() state_before = board.export() - actions.DoNothing(set()).perform_action(board) + actions.DoNothing(set()).perform_action(board, None) state_after = board.export() self.assertEqual(state_before, state_after) def test_LoseHealthOrMSB_MSB_clear(self): - board = GameBoard.new_game([]) + board = self.make_board() state_before = board.export() - actions.LoseHeathOrMSB(set()).perform_action(board) + 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) + + def test_LoseHealthOrMSB_MSB_set(self): + board = self.make_board(player_bits=[BITS.MSB]) + state_before = board.export() + 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) + + 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) + 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) + + 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) + state_after = board.export() + self.assertEqual(board.player.bits.check_bit(BITS.MSB), True) + self.assertEqual(board.player.bits.check_bit(BITS.NORTH), False) + + state_before['player'].pop('bits') + state_after['player'].pop('bits') + self.assertEqual(state_before, state_after)