def test_can_act(self): #Player can act as long as not stunned, unconscious or dead #Use mage as mocks impact other tests player = players.Thief() self.assertTrue(player.can_act) with MockHelper.Property(player, 'is_dead', return_value=False) as is_dead_mock, \ MockHelper.Property(player, 'is_unconscious', return_value=False) as is_unconscious_mock, \ MockHelper.Property(player, 'is_stunned', return_value=False) as is_stunned_mock: self.assertTrue(player.can_act) #Check each one was called is_dead_mock.assert_called_once() is_stunned_mock.assert_called_once() is_unconscious_mock.assert_called_once() #Stunned is_stunned_mock.return_value = True self.assertFalse(player.can_act) is_stunned_mock.return_value = False #Unconscious is_unconscious_mock.return_value = True self.assertFalse(player.can_act) is_unconscious_mock.return_value = False #Dead is_dead_mock.return_value = True self.assertFalse(player.can_act) is_dead_mock.return_value = False self.assertTrue(player.can_act)
def test_heal(self): player = players.Thief() #Returns ture if any healing occurred #Hit points can exceed max_hit_points #Heal amount can't be <= 0 #No healing self.assertFalse(player.heal(-1)) self.assertFalse(player.heal(0)) #<0 self.assertGreater(player.max_hit_points, 0) self.assertEqual(player.hit_points, player.max_hit_points) #Ok full health self.assertFalse(player.heal(1)) self.assertEqual(player.hit_points, player.max_hit_points) #Wound self.assertTrue(player.wound(1)) self.assertLess(player.hit_points, player.max_hit_points) self.assertTrue(player.heal(20)) self.assertEqual(player.hit_points, player.max_hit_points) #Can't heal if dead self.assertTrue(player.wound(1)) #Mock class method with MockHelper.Property(player, 'is_dead', return_value=True) as mock_is_dead: self.assertFalse(player.heal(1)) mock_is_dead.assert_called_once() mock_is_dead.return_value = False self.assertTrue(player.heal(1))
def test_can_picklock(self): #Can act, has a lock pick and the lock picking skill player = players.Thief() player.is_carrying = Mock(return_value=True) player.has_skill = Mock(return_value=True) #Stash can_act with MockHelper.Property(player, 'can_act', return_value=True) as mock_can_act: self.assertTrue(player.can_picklock()) #No lockpick mock_is_carrying = Mock(return_value=False) player.is_carrying = mock_is_carrying self.assertFalse(player.can_picklock()) #Can't act mock_is_carrying.return_value = True self.assertTrue(player.can_picklock()) mock_can_act.return_value = False self.assertFalse(player.can_picklock()) #No skill mock_can_act.return_value = True self.assertTrue(player.can_picklock()) player.has_skill = Mock(return_value=False) self.assertFalse(player.can_picklock())
def test_is_encumbered(self): player = players.Thief() #Stash class methods with MockHelper.Property(player, 'carry_weight', return_value=0) as mock_carry_weight, \ MockHelper.Property(player, 'carry_capacity', return_value=10) as mock_carry_capacity: #weight < capacity self.assertFalse(player.is_encumbered) mock_carry_weight.assert_called_once() mock_carry_capacity.assert_called_once() #weight = capacity mock_carry_weight.return_value = 10 self.assertFalse(player.is_encumbered) #weight > capacity mock_carry_weight.return_value = 11 self.assertTrue(player.is_encumbered)
def test_strike(self): player = players.Thief() rat = players.Rat() #Can't act with MockHelper.Property(player, 'can_act', return_value=False) as mock_can_act: self.assertFalse(player.strike(rat)) mock_can_act.assert_called_once() mock_can_act.return_value = True strike = player.strike(rat) self.assertIsNotNone(strike.damage)
def test_can_cast_spell(self): #Knows spell #Has a sufficient level #Has enough mana #Can act smiteSpell = Mock() smiteSpell.level = 1 player = players.Thief() player.max_mana_points = 4 player.add_mana(4) with MockHelper.Property(player, 'can_act', return_value=True) as can_act_mock: player.knows_spell = Mock(return_value=True) self.assertTrue(player.can_cast_spell(smiteSpell)) #Ensure methods called as expected can_act_mock.assert_called_once() player.knows_spell.assert_called_once() #Not enough mana player.add_mana(-4) self.assertFalse(player.can_cast_spell(smiteSpell)) player.add_mana(4) self.assertTrue(player.can_cast_spell(smiteSpell)) #Not a high enough level smiteSpell.level = 2 self.assertFalse(player.can_cast_spell(smiteSpell)) smiteSpell.level = 1 self.assertTrue(player.can_cast_spell(smiteSpell)) #Can't act can_act_mock.return_value = False self.assertFalse(player.can_act) self.assertFalse(player.can_cast_spell(smiteSpell)) #Doesn't know spell player.knows_spell = Mock(return_value=False) self.assertFalse(player.knows_spell(smiteSpell)) self.assertFalse(player.can_cast_spell(smiteSpell))