69ed8819ed54c80be1d87e89fe6eef8f931f931b
[naja.git] / naja / tests / test_actions.py
1 from unittest import TestCase
2
3 from naja.constants import BITS
4 from naja.gameboard import GameBoard, LocationCard
5 from naja.player import Player
6 from naja import actions
7
8
9 class TestActions(TestCase):
10     def make_player(self, *bits):
11         player_bits = 0
12         for bit in bits:
13             player_bits |= bit
14         return Player(player_bits, None)
15
16     def make_board(self, player_bits=None, locations=None):
17         if locations is None:
18             locations = [{'actions': []}]
19         board = GameBoard.new_game(locations)
20         if player_bits is not None:
21             board.player.bits.set_bits(player_bits)
22         return board
23
24     def test_check_available(self):
25         def check_available(action_bits, player_bits, expected_result):
26             action = actions.LocationAction(action_bits)
27             player = self.make_player(*player_bits)
28             self.assertEqual(action.check_available(player), expected_result)
29
30         check_available(set(), [], True)
31         check_available(set(), [BITS.MSB], True)
32         check_available(set([BITS.MSB]), [], False)
33         check_available(set([BITS.MSB]), [BITS.MSB], True)
34
35     def test_DoNothing(self):
36         board = self.make_board()
37         state_before = board.export()
38         actions.DoNothing(set()).perform_action(board, None)
39         state_after = board.export()
40         self.assertEqual(state_before, state_after)
41
42     def test_LoseHealthOrMSB_MSB_clear(self):
43         board = self.make_board()
44         state_before = board.export()
45         actions.LoseHeathOrMSB(set()).perform_action(board, None)
46         state_after = board.export()
47         self.assertEqual(state_after['health'], state_before['health'] - 1)
48
49         state_before.pop('health')
50         state_after.pop('health')
51         self.assertEqual(state_before, state_after)
52
53     def test_LoseHealthOrMSB_MSB_set(self):
54         board = self.make_board(player_bits=[BITS.MSB])
55         state_before = board.export()
56         actions.LoseHeathOrMSB(set()).perform_action(board, None)
57         state_after = board.export()
58         self.assertEqual(board.player.bits.check_bit(BITS.MSB), False)
59
60         state_before['player'].pop('bits')
61         state_after['player'].pop('bits')
62         self.assertEqual(state_before, state_after)
63
64     def test_SetBits(self):
65         board = self.make_board()
66         state_before = board.export()
67         location = LocationCard(set([BITS.MSB, BITS.NORTH]), [])
68         actions.SetBits(set()).perform_action(board, location)
69         state_after = board.export()
70         self.assertEqual(
71             board.player.bits.check_bits([BITS.MSB, BITS.NORTH]), True)
72
73         state_before['player'].pop('bits')
74         state_after['player'].pop('bits')
75         self.assertEqual(state_before, state_after)
76
77     def test_ToggleBits(self):
78         board = self.make_board(player_bits=[BITS.NORTH])
79         state_before = board.export()
80         location = LocationCard(set([BITS.MSB, BITS.NORTH]), [])
81         actions.ToggleBits(set()).perform_action(board, location)
82         state_after = board.export()
83         self.assertEqual(board.player.bits.check_bit(BITS.MSB), True)
84         self.assertEqual(board.player.bits.check_bit(BITS.NORTH), False)
85
86         state_before['player'].pop('bits')
87         state_after['player'].pop('bits')
88         self.assertEqual(state_before, state_after)