Tweak actions.
authorJeremy Thurgood <firxen@gmail.com>
Sun, 11 May 2014 18:31:04 +0000 (20:31 +0200)
committerJeremy Thurgood <firxen@gmail.com>
Sun, 11 May 2014 18:31:04 +0000 (20:31 +0200)
naja/actions.py
naja/tests/test_actions.py

index c49f7c61b909ba01f27778b5265b742cfab63821..b0371a7ca987235e0b5ffce32168f020eea64ceb 100644 (file)
@@ -15,7 +15,7 @@ class LocationAction(object):
     def check_available(self, player):
         return player.bits.check_bits(self.required_bits)
 
-    def perform_action(self, player, board):
+    def perform_action(self, board):
         raise NotImplementedError("TODO")
 
     def check_and_clear_MSB(self, player):
@@ -29,13 +29,13 @@ class LocationAction(object):
 class DoNothing(LocationAction):
     TEXT = "No effect."
 
-    def perform_action(self, player, board):
+    def perform_action(self, board):
         pass
 
 
 class LoseHeathOrMSB(LocationAction):
     TEXT = "Lose health. If MSB is set, it will be cleared instead."
 
-    def perform_action(self, player, board):
-        if not self.check_and_clear_MSB(player):
+    def perform_action(self, board):
+        if not self.check_and_clear_MSB(board.player):
             board.lose_health()
index 8896b8d2607e72a6bc0ac034fba8b7f2a2a75f66..e4a4fa523a2ea3b9620ab79de83f6ef46dd44588 100644 (file)
@@ -1,5 +1,7 @@
 from unittest import TestCase
 
+from naja.constants import BITS
+from naja.gameboard import GameBoard
 from naja.player import Player
 from naja import actions
 
@@ -8,12 +10,31 @@ class TestActions(TestCase):
     def make_player(self, *bits):
         return Player(sum(1 << bit for bit in bits), None)
 
-    def test_DoNothing_check_available(self):
-        player = self.make_player()
-        action = actions.DoNothing(set())
-        self.assertEqual(action.check_available(player), True)
+    def test_check_available(self):
+        def check_available(action_bits, player_bits, expected_result):
+            action = actions.LocationAction(action_bits)
+            player = self.make_player(*player_bits)
+            self.assertEqual(action.check_available(player), expected_result)
 
-    def test_LoseHealthOrMSB_check_available(self):
-        player = self.make_player()
-        action = actions.LoseHeathOrMSB(set())
-        self.assertEqual(action.check_available(player), True)
+        check_available(set(), [], True)
+        check_available(set(), [BITS.MSB], True)
+        check_available(set([BITS.MSB]), [], False)
+        check_available(set([BITS.MSB]), [BITS.MSB], True)
+
+    def test_DoNothing(self):
+        board = GameBoard.new_game([])
+        state_before = board.export()
+        actions.DoNothing(set()).perform_action(board)
+        state_after = board.export()
+        self.assertEqual(state_before, state_after)
+
+    def test_LoseHealthOrMSB_MSB_clear(self):
+        board = GameBoard.new_game([])
+        state_before = board.export()
+        actions.LoseHeathOrMSB(set()).perform_action(board)
+        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)