1 from unittest import TestCase
3 from naja.constants import BITS, MOVES
4 from naja.player import PlayerBits, Player
5 from naja.gameboard import GameBoard
8 class TestPlayerBits(TestCase):
9 def test_range_limits(self):
10 bits = PlayerBits(0x00)
15 self.assertEqual(bits.bits, 0x00)
16 self.assertRaises(AssertionError, set_value, -1)
17 self.assertEqual(bits.bits, 0x00)
19 self.assertEqual(bits.bits, 0xff)
21 self.assertEqual(bits.bits, 0x00)
22 self.assertRaises(AssertionError, set_value, 256)
23 self.assertEqual(bits.bits, 0x00)
25 def test_check_bit(self):
26 bits = PlayerBits(0x01)
27 self.assertEqual(bits.check_bit(BITS.NORTH), True)
28 self.assertEqual(bits.check_bit(BITS.SOUTH), False)
30 def test_set_bit(self):
31 bits = PlayerBits(0x00)
32 self.assertEqual(bits._bits, 0x00)
33 bits.set_bit(BITS.NORTH)
34 self.assertEqual(bits._bits, 0x01)
35 bits.set_bit(BITS.NORTH)
36 self.assertEqual(bits._bits, 0x01)
38 def test_clear_bit(self):
39 bits = PlayerBits(0x01)
40 self.assertEqual(bits._bits, 0x01)
41 bits.clear_bit(BITS.NORTH)
42 self.assertEqual(bits._bits, 0x00)
43 bits.clear_bit(BITS.NORTH)
44 self.assertEqual(bits._bits, 0x00)
46 def test_toggle_bit(self):
47 bits = PlayerBits(0x00)
48 self.assertEqual(bits._bits, 0x00)
49 bits.toggle_bit(BITS.NORTH)
50 self.assertEqual(bits._bits, 0x01)
51 bits.toggle_bit(BITS.NORTH)
52 self.assertEqual(bits._bits, 0x00)
54 def test_check_bits(self):
55 bits = PlayerBits(0x03)
56 self.assertEqual(bits.check_bits([BITS.NORTH]), True)
57 self.assertEqual(bits.check_bits([BITS.NORTH, BITS.SOUTH]), True)
58 self.assertEqual(bits.check_bits([BITS.BLUE]), False)
59 self.assertEqual(bits.check_bits([BITS.BLUE, BITS.MSB]), False)
61 bits.check_bits([BITS.NORTH, BITS.SOUTH, BITS.BLUE]), False)
63 def test_set_bits(self):
64 bits = PlayerBits(0x03)
65 self.assertEqual(bits._bits, 0x03)
66 bits.set_bits([BITS.NORTH, BITS.BLUE])
67 self.assertEqual(bits._bits, 0x13)
68 bits.set_bits([BITS.NORTH, BITS.BLUE, BITS.MSB])
69 self.assertEqual(bits._bits, 0x93)
71 def test_clear_bits(self):
72 bits = PlayerBits(0x03)
73 self.assertEqual(bits._bits, 0x03)
74 bits.clear_bits([BITS.NORTH, BITS.BLUE])
75 self.assertEqual(bits._bits, 0x02)
76 bits.clear_bits([BITS.NORTH, BITS.BLUE, BITS.MSB])
77 self.assertEqual(bits._bits, 0x02)
79 def test_toggle_bits(self):
80 bits = PlayerBits(0x03)
81 self.assertEqual(bits._bits, 0x03)
82 bits.toggle_bits([BITS.NORTH, BITS.BLUE])
83 self.assertEqual(bits._bits, 0x12)
84 bits.toggle_bits([BITS.NORTH, BITS.BLUE])
85 self.assertEqual(bits._bits, 0x03)
86 bits.toggle_bits([BITS.NORTH, BITS.BLUE, BITS.MSB])
87 self.assertEqual(bits._bits, 0x92)
89 def test_shift_bits_left(self):
90 bits = PlayerBits(0x03)
91 self.assertEqual(bits._bits, 0x03)
92 bits.shift_bits_left(1)
93 self.assertEqual(bits._bits, 0x06)
94 bits.shift_bits_left(6)
95 self.assertEqual(bits._bits, 0x81)
97 def test_shift_bits_right(self):
98 bits = PlayerBits(0x06)
99 self.assertEqual(bits._bits, 0x06)
100 bits.shift_bits_right(1)
101 self.assertEqual(bits._bits, 0x03)
102 bits.shift_bits_right(1)
103 self.assertEqual(bits._bits, 0x81)
106 class TestPlayer(TestCase):
109 self.board = GameBoard.new_game({'cards': [
110 {'card_name': 'card1', 'actions': [
112 'action_class': 'LoseHealthOrMSB',
115 'action_class': 'GainHealth',
116 'required_bits': [BITS.RED],
121 def test_new_player(self):
122 player = Player(0x0f, (0, 1), self.board)
123 self.assertEqual(player.bits.bits, 0x0f)
124 self.assertEqual(player.position, (0, 1))
126 def test_import_player(self):
127 player = Player.import_player({
130 'movement_mode': MOVES.CASTLE,
132 self.assertEqual(player.bits.bits, 0xaa)
133 self.assertEqual(player.position, (1, 2))
134 self.assertEqual(player.movement_mode, MOVES.CASTLE)
136 def test_export_player(self):
137 player = Player(0xaa, (1, 2), MOVES.CASTLE)
138 self.assertEqual(player.export(), {
141 'movement_mode': MOVES.CASTLE,
144 def test_export_player_default_movement_mode(self):
145 player = Player(0xaa, (1, 2))
146 self.assertEqual(player.export(), {
149 'movement_mode': MOVES.ADJACENT,
152 def test_legal_moves_all_available(self):
153 player = Player(0x0f, (2, 2), gameboard=self.board)
155 player.legal_moves(), [(2, 2), (2, 1), (2, 3), (3, 2), (1, 2)])
157 def test_legal_moves_some_unavailable(self):
158 player = Player(0x0f, (0, 2), gameboard=self.board)
159 player.bits.clear_bit(BITS.NORTH)
160 self.assertEqual(player.legal_moves(), [(0, 2), (0, 3), (1, 2)])
162 def test_legal_moves_castle(self):
163 player = Player(0x0f, (1, 3), MOVES.CASTLE, gameboard=self.board)
164 self.assertEqual(player.legal_moves(), [
165 (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 3), (3, 3),
168 def test_legal_moves_bishop(self):
169 player = Player(0x0f, (1, 3), MOVES.BISHOP, gameboard=self.board)
170 self.assertEqual(player.legal_moves(), [
171 (0, 2), (0, 4), (1, 3), (2, 2), (2, 4), (3, 1), (4, 0)])
173 def test_legal_moves_knight(self):
174 player = Player(0x0f, (1, 3), MOVES.KNIGHT, gameboard=self.board)
175 self.assertEqual(player.legal_moves(), [
176 (0, 1), (1, 3), (2, 1), (3, 2), (3, 4)])
178 def test_set_position(self):
179 player = Player(0x0f, (3, 3), MOVES.BISHOP, gameboard=self.board)
180 player.set_position((4, 4))
181 self.assertEqual(player.movement_mode, MOVES.ADJACENT)