3d3a58ea63346c936ebfb2b1c8a619640dbb5d87
[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         return Player(sum(1 << bit for bit in bits), None)
12
13     def test_check_available(self):
14         def check_available(action_bits, player_bits, expected_result):
15             action = actions.LocationAction(action_bits)
16             player = self.make_player(*player_bits)
17             self.assertEqual(action.check_available(player), expected_result)
18
19         check_available(set(), [], True)
20         check_available(set(), [BITS.MSB], True)
21         check_available(set([BITS.MSB]), [], False)
22         check_available(set([BITS.MSB]), [BITS.MSB], True)
23
24     def test_DoNothing(self):
25         board = GameBoard.new_game([])
26         state_before = board.export()
27         actions.DoNothing(set()).perform_action(board, None)
28         state_after = board.export()
29         self.assertEqual(state_before, state_after)
30
31     def test_LoseHealthOrMSB_MSB_clear(self):
32         board = GameBoard.new_game([])
33         state_before = board.export()
34         actions.LoseHeathOrMSB(set()).perform_action(board, None)
35         state_after = board.export()
36         self.assertEqual(state_after['health'], state_before['health'] - 1)
37
38         state_before.pop('health')
39         state_after.pop('health')
40         self.assertEqual(state_before, state_after)
41
42     def test_LoseHealthOrMSB_MSB_set(self):
43         board = GameBoard.new_game([])
44         board.player.bits.set_bit(BITS.MSB)
45         state_before = board.export()
46         actions.LoseHeathOrMSB(set()).perform_action(board, None)
47         state_after = board.export()
48         self.assertEqual(board.player.bits.check_bit(BITS.MSB), False)
49
50         state_before['player'].pop('bits')
51         state_after['player'].pop('bits')
52         self.assertEqual(state_before, state_after)
53
54     def test_SetBits(self):
55         board = GameBoard.new_game([])
56         state_before = board.export()
57         location = LocationCard(set([BITS.MSB, BITS.NORTH]), [])
58         actions.SetBits(set()).perform_action(board, location)
59         state_after = board.export()
60         self.assertEqual(
61             board.player.bits.check_bits([BITS.MSB, BITS.NORTH]), True)
62
63         state_before['player'].pop('bits')
64         state_after['player'].pop('bits')
65         self.assertEqual(state_before, state_after)
66
67     def test_ToggleBits(self):
68         board = GameBoard.new_game([])
69         board.player.bits.set_bit(BITS.NORTH)
70         state_before = board.export()
71         location = LocationCard(set([BITS.MSB, BITS.NORTH]), [])
72         actions.ToggleBits(set()).perform_action(board, location)
73         state_after = board.export()
74         self.assertEqual(board.player.bits.check_bit(BITS.MSB), True)
75         self.assertEqual(board.player.bits.check_bit(BITS.NORTH), False)
76
77         state_before['player'].pop('bits')
78         state_after['player'].pop('bits')
79         self.assertEqual(state_before, state_after)