def test_is_unconscious(self): player = players.Thief() self.assertFalse(player.is_dead) self.assertFalse(player.is_unconscious) #Ensure Save from death, but not max save with MockHelper.Method(players.GameRules, 'roll_death_save', return_value=19) as mock_death_save: self.assertTrue(player.wound(player.max_hit_points)) self.assertEqual(player.hit_points, 0) self.assertFalse(player.is_dead) self.assertTrue(player.is_unconscious) #Can't wound if no points self.assertFalse(player.wound(0)) mock_death_save.assert_called_once() #Massive damage, no save with MockHelper.Method(players.GameRules, 'roll_death_save', return_value=20) as mock_death_save: self.assertTrue(player.wound(player.max_hit_points)) self.assertEqual(player.hit_points, 0) self.assertTrue(player.is_dead) self.assertFalse(player.is_unconscious) #Dead can't wound anymore self.assertFalse(player.wound(player.max_hit_points)) mock_death_save.assert_not_called() #Massive damage, no save with MockHelper.Method(players.GameRules, 'roll_death_save', return_value=20) as mock_death_save: player = players.Thief() self.assertTrue(player.wound(player.max_hit_points * 2)) self.assertTrue(player.is_dead) self.assertFalse(player.is_unconscious) mock_death_save.assert_not_called() #Large damage, save matters with MockHelper.Method(players.GameRules, 'roll_death_save', return_value=20) as mock_death_save: player = players.Thief() self.assertTrue(player.wound(player.max_hit_points * 2 - 1)) self.assertFalse(player.is_dead) self.assertEqual(player.hit_points, 0) self.assertTrue(player.is_unconscious) mock_death_save.assert_called_once()
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_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_throw(self): player = players.Thief() rat = players.Rat() dagger = MockHelper.get_mock_weapon() not_a_dagger = MockHelper.get_mock_item() #Must be carrying item #Item must be a throwable weapon #Should decrease number of items when thrown #If none left then no longer carrying #Not carrying player.is_carrying = Mock(return_value=False) self.assertFalse(player.throw(dagger, rat)) player.is_carrying.assert_called_once() #Not a weapon #TODO: Allow food to be thrown? player.is_carrying = Mock(return_value=True) self.assertFalse(player.throw(not_a_dagger, rat)) #Not throwable #Mock class methods mock_is_throwable = PropertyMock(return_value=True) type(dagger).is_throwable = mock_is_throwable self.assertTrue(player.throw(dagger, rat)) mock_is_throwable.assert_called_once()
def test_harm_potion(self): #Decreases player health player = players.Thief() potion = potions.HarmPotion() self.assertEqual(player.hit_points, 100, 'Healthy') potion.drink(player) self.assertLess(player.hit_points, 100, 'Health decreased')
def test_mana_points(self): player = players.Thief() self.assertEqual(player.mana_points, 0) player = players.Mage() #TODO fix? intitial mana = 0? #Can't directly change try: player.mana_points = 10 self.assertTrue(False) except AttributeError: self.assertTrue(True) #Can't be negative self.assertGreater(player.max_mana_points, 0) player.add_mana(-10) self.assertEqual(player.mana_points, 0) #Can't exceed max mana points self.assertGreater(player.max_mana_points, 0) player.add_mana(player.max_mana_points + 10) self.assertEqual(player.mana_points, player.max_mana_points) #Just right player.add_mana(-player.max_mana_points + 2) self.assertEqual(player.mana_points, 2)
def test_healing_potion(self): #Increases players health player = players.Thief() potion = potions.HealingPotion() self.assertEqual(player.hit_points, 100, 'Healthy') potion.drink(player) self.assertGreater(player.hit_points, 100, 'Health increased')
def test_equip_weapon(self): #Check default armor player = players.Thief() self.assertEqual(player.get_equipped_weapon(), player.default_weapon) #Equip something that isn't armor fake_weapon = Mock() self.assertFalse(player.equip_weapon(fake_weapon)) #Not carrying good_weapon = MockHelper.get_mock_weapon() player.is_carrying = Mock(return_value=False) self.assertFalse(player.equip_weapon(good_weapon)) player.is_carrying.assert_called_once() #Equip Weapon player.is_carrying = Mock(return_value=True) self.assertTrue(player.equip_weapon(good_weapon)) #Equip None, should be default try: player.equip_weapon(None) except AttributeError: self.assertTrue(True, 'None is not valid') self.assertEqual(player.get_equipped_weapon(), good_weapon)
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_proficiency_bonus(self): #Attack bonus increases by level player = players.Thief() bonus = { 1: [0, 2], 2: [300, 2], 3: [900, 2], 4: [2700, 2], 5: [6500, 3], 6: [14000, 3], 7: [23000, 3], 8: [34000, 3], 9: [48000, 4], 10: [64000, 4], 11: [85000, 4], 12: [100000, 4], 13: [120000, 5], 14: [140000, 5], 15: [165000, 5], 16: [195000, 5], 17: [225000, 6], 18: [265000, 6], 19: [305000, 6], 20: [355000, 6] } last_exp = 0 for level in bonus.keys(): player.add_experience(bonus[level][0] - last_exp) last_exp = bonus[level][0] self.assertEqual(player.level, level) self.assertEqual(player.proficiency_bonus, bonus[level][1])
def test_default_weapon(self): import questgame.game_items.weapons as weapons player = players.Thief() self.assertTrue(isinstance(player.default_weapon, weapons.Fists)) player = players.Rat() self.assertTrue(isinstance(player.default_weapon, weapons.Claws))
def test_pickup(self): player = players.Thief() #If scroll learn it #If weapon equip it - if no weapon equipped and proficient #If armor equip it - if no armor equipped and proficient #Scroll scroll = MockHelper.get_mock_scroll() player.learn_spell = Mock(return_value=True) self.assertTrue(player.pickup(scroll)) player.learn_spell.assert_called_once() #Weapon weapon = MockHelper.get_mock_weapon() weapon.get_modifier_value = Mock(return_value=0) #Not proficient player.character_class.is_weapon_proficient = Mock(return_value=False) self.assertFalse(player.has_weapon_equipped()) self.assertTrue(player.pickup(weapon)) self.assertFalse(player.has_weapon_equipped()) self.assertNotEqual(player.get_equipped_weapon(), weapon) #Proficient player.character_class.is_weapon_proficient = Mock(return_value=True) self.assertFalse(player.has_weapon_equipped()) self.assertTrue(player.pickup(weapon)) self.assertTrue(player.has_weapon_equipped()) self.assertEqual(player.get_equipped_weapon(), weapon) #Already equipped another_weapon = MockHelper.get_mock_weapon() self.assertTrue(player.pickup(another_weapon)) self.assertTrue(player.has_weapon_equipped()) self.assertEqual(player.get_equipped_weapon(), weapon) #Armor myarmor = MockHelper.get_mock_armor() myarmor.get_modifier_value = Mock(return_value=0) #Not proficient player.character_class.is_armor_proficient = Mock(return_value=False) self.assertFalse(player.has_armor_equipped()) self.assertTrue(player.pickup(myarmor)) self.assertFalse(player.has_armor_equipped()) self.assertNotEqual(player.get_equipped_armor(), myarmor) #Proficient player.character_class.is_armor_proficient = Mock(return_value=True) self.assertFalse(player.has_armor_equipped()) self.assertTrue(player.pickup(myarmor)) self.assertTrue(player.has_armor_equipped()) self.assertEqual(player.get_equipped_armor(), myarmor) #Already equipped another_armor = MockHelper.get_mock_armor() self.assertTrue(player.pickup(another_armor)) self.assertTrue(player.has_armor_equipped()) self.assertEqual(player.get_equipped_armor(), myarmor)
def test_default_armor(self): import questgame.game_items.armor as armor player = players.Thief() self.assertTrue(isinstance(player.default_armor, armor.BodyArmor)) player = players.Rat() self.assertTrue(isinstance(player.default_armor, armor.FurArmor))
def test_loot_body(self): player = players.Thief() rat = players.Rat() zombie_rat = players.ZombieRat() another_rat = players.Rat() #Inventory is empty #Is looted #Inventory has 1 item #Inventory has 2 items #Empty self.assertEqual(player.loot_body(rat), []) #Already looted self.assertFalse(player.loot_body(rat)) penny = MockHelper.get_mock_item() quarter = MockHelper.get_mock_scroll() #1 item self.assertTrue(zombie_rat.pickup(penny)) self.assertEqual(player.loot_body(zombie_rat), [penny]) self.assertTrue(player.is_carrying(penny)) self.assertFalse(zombie_rat.is_carrying(penny)) #2 items self.assertTrue(another_rat.pickup(penny)) self.assertTrue(another_rat.pickup(quarter)) self.assertEqual(player.loot_body(another_rat), [penny, quarter]) self.assertTrue(player.is_carrying(penny)) self.assertTrue(player.is_carrying(quarter)) self.assertFalse(another_rat.is_carrying(penny)) self.assertFalse(another_rat.is_carrying(quarter))
def test_remove_item(self): player = players.Thief() #Not carrying item does nothing #Removing equipped armor resets to default #Removing equipped weapon resets to default #Armor myarmor = MockHelper.get_mock_armor() myarmor.get_modifier_value = Mock(return_value=0) #Nothing to remove self.assertFalse(player.remove_item(myarmor)) #Remove equipped armor player.character_class.is_armor_proficient = Mock(return_value=True) self.assertTrue(player.pickup(myarmor)) self.assertEqual(player.get_equipped_armor(), myarmor) self.assertTrue(player.has_armor_equipped()) self.assertTrue(player.remove_item(myarmor)) self.assertNotEqual(player.get_equipped_armor(), myarmor) self.assertFalse(player.has_armor_equipped()) #Remove equipped weapon weapon = MockHelper.get_mock_weapon() weapon.get_modifier_value = Mock(return_value=0) player.character_class.is_weapon_proficient = Mock(return_value=True) self.assertTrue(player.pickup(weapon)) self.assertTrue(player.has_weapon_equipped()) self.assertEqual(player.get_equipped_weapon(), weapon) self.assertTrue(player.remove_item(weapon)) self.assertNotEqual(player.get_equipped_weapon(), weapon) self.assertFalse(player.has_weapon_equipped())
def test_has_weapon_equipped(self): player = players.Thief() self.assertFalse(player.has_armor_equipped()) armor = MockHelper.get_mock_armor() player.is_carrying = Mock(return_value=True) self.assertTrue(player.equip_armor(armor)) self.assertTrue(player.has_armor_equipped())
def test_healing_potion_undead(self): #Decreases undead health player = players.Thief() player.is_undead = True potion = potions.HealingPotion() self.assertEqual(player.hit_points, 100, 'Healthy') potion.drink(player) self.assertLess(player.hit_points, 100, 'Health decreased')
def test_character_class(self): player = players.Thief() self.assertTrue(player.character_class, players.character_classes.Thief) player = players.Goblin self.assertTrue(player.character_class, players.character_classes.Goblin)
def test_is_armor_equipped(self): player = players.Thief() fur_armor = Mock() player.get_equipped_armor = Mock(return_value=fur_armor) self.assertTrue(player.is_armor_equipped(fur_armor)) leather_armor = Mock() self.assertFalse(player.is_weapon_equipped(leather_armor))
def test_is_weapon_equipped(self): player = players.Thief() fists = Mock() player.get_equipped_weapon = Mock(return_value=fists) self.assertTrue(player.is_weapon_equipped(fists)) mace = Mock() self.assertFalse(player.is_weapon_equipped(mace))
def test_mana_potion(self): #Increases players mana player = players.Thief() potion = potions.ManaPotion() self.assertEqual(player.mana_points, 0, 'No mana') potion.drink(player) self.assertGreater(player.mana_points, 0, 'Mana increased')
def test_other_attack_bonus(self): #Add an attack modifier player = players.Thief() self.assertEqual(player.level, 1) self.assertEqual(player.other_attack_bonus, 0) player.add_bonus('BECAUSE', players.PlayerAttributes.ATTACK, 3) self.assertEqual(player.other_attack_bonus, 3) player.remove_bonus('BECAUSE') self.assertEqual(player.other_attack_bonus, 0)
def test_get_equipped_weapon(self): player = players.Thief() rat = players.Rat() #Just returns currently assigned armor self.assertEqual(player.get_equipped_weapon(), player.default_weapon) #TODO: Probably shouldn't allow this.... player.is_carrying = Mock(return_value=True) self.assertTrue(player.equip_weapon(rat.default_weapon)) self.assertEqual(player.get_equipped_weapon(), rat.default_weapon)
def test_has_skill(self): player = players.Thief() skill = Mock() player.character_class.is_skill_proficient = Mock(return_value=True) self.assertTrue(player.has_skill(skill)) player.character_class.is_skill_proficient.assert_called_once() player.character_class.is_skill_proficient = Mock(return_value=False) self.assertFalse(player.has_skill(skill))
def test_strength_potion(self): #Increases players strength player = players.Thief() index = potions.PlayerAttributes.STRENGTH player.add_bonus('ATTR_BONUS', index, 0) self.assertEqual(player.get_bonuses()['ATTR_BONUS'][index], 0, 'No strength bonus') strength_potion = potions.StrengthPotion() strength_potion.drink(player) self.assertEqual(player.get_bonuses()['ATTR_BONUS'][index], 2, 'Strength bonus')
def test_get_item(self): player = players.Thief() penny = MockHelper.get_mock_item() #Get an item from inventory #Returns False if no such item self.assertFalse(player.get_item(penny)) self.assertTrue(player.pickup(penny)) self.assertEqual(player.get_item(penny), penny)
def test_cast(self): #Cast a spell player = players.Thief() rat = players.Rat() spell = Mock() spell.cast = Mock(return_value=True) #Calls spell.cast which returns true or false which is passed through self.assertTrue(player.cast(spell, rat)) spell.cast = Mock(return_value=False) self.assertFalse(player.cast(spell, rat))
def test_add_experience(self): player = players.Thief() # Level - [Experience, Proficiency] # 1: [300,2], # 2: [900,2], # 3: [2700,2], #Test adding experience and moving levels self.assertEqual(player.level, 1) player.add_experience(299) self.assertEqual(player.level, 1) player.add_experience(1) self.assertEqual(player.level, 2)
def test_drop(self): player = players.Thief() penny = MockHelper.get_mock_item() #Must be carrying item, then calls remove - which returns the item player.is_carrying = Mock(return_value=True) player.remove_item = Mock(return_value=penny) self.assertEqual(player.drop(penny), penny) #not carrying player.is_carrying = Mock(return_value=False) self.assertFalse(player.drop(penny))
def test_is_dead(self): player = players.Thief() self.assertFalse(player.is_dead) #Ensure save on death with MockHelper.Method(players.GameRules, 'roll_death_save', return_value=20) as mock_death_save: self.assertTrue(player.wound(player.max_hit_points)) self.assertEqual(player.hit_points, 0) self.assertFalse(player.is_dead) mock_death_save.assert_called() #Massive damage - save has no effect with MockHelper.Method(players.GameRules, 'roll_death_save', return_value=20) as mock_death_save: self.assertTrue(player.wound(player.max_hit_points)) self.assertEqual(player.hit_points, 0) self.assertTrue(player.is_dead) mock_death_save.assert_not_called() #Massive damage - save has no effect with MockHelper.Method(players.GameRules, 'roll_death_save', return_value=20) as mock_death_save: player = players.Thief() self.assertTrue(player.wound(player.max_hit_points * 2)) self.assertTrue(player.is_dead) mock_death_save.assert_not_called() #Large damage - save has an effect with MockHelper.Method(players.GameRules, 'roll_death_save', return_value=20) as mock_death_save: player = players.Thief() self.assertTrue(player.wound(player.max_hit_points * 2 - 1)) self.assertFalse(player.is_dead) mock_death_save.assert_called_once()