Esempio n. 1
0
class TestItems(object):
    """
    Tests for items
    """
    def __init__(self):
        """
        Default constructor
        """
        super(TestItems, self).__init__()
        self.item = None
        self.level = None
        self.dungeon = None
        self.character = None

    def setup(self):
        """
        Setup for this test case
        """
        self.item = (ItemBuilder()
                        .with_name('banana')
                        .build())

        self.level = Level([20, 20])

        self.character = (CharacterBuilder()
                            .with_action_factory(ActionFactoryBuilder()
                                                    .with_move_factory())
                            .with_level(self.level)
                            .with_location((5, 5))
                            .build())

        self.level.add_item(self.item, (5, 5))

    def test_wield_weapon(self):
        """
        Test that character can wield a weapon (dagger)
        """
        item = ItemBuilder().build()

        assert_that(item, is_not(is_in(self.character.weapons)))

        pyherc.rules.items.wield(mock(), self.character, item)

        assert_that(item, is_in(self.character.weapons))

    def test_unwielding_item(self):
        """
        Test that wielded item can be unwielded
        """
        item = ItemBuilder().build()
        pyherc.rules.items.wield(mock(), self.character, item)

        assert_that(item, is_in(self.character.weapons))

        pyherc.rules.items.unwield(mock(), self.character, item)

        assert_that(item, is_not(is_in(self.character.weapons)))

    def test_dual_wielding(self):
        """
        Test that character can wield two weapons
        """
        item1 = (ItemBuilder()
                        .with_name('dagger')
                        .with_tag('light weapon')
                        .with_damage(1)
                        .build())

        item2 = (ItemBuilder()
                        .with_name('sickle')
                        .with_tag('light weapon')
                        .with_damage(2)
                        .build())

        assert_that(item1, is_not(is_in(self.character.weapons)))
        assert_that(item2, is_not(is_in(self.character.weapons)))

        pyherc.rules.items.wield(mock(), self.character, item1)
        pyherc.rules.items.wield(mock(),
                                 self.character,
                                 item2,
                                 dual_wield = True)

        assert_that(item1, is_in(self.character.weapons))
        assert_that(item2, is_in(self.character.weapons))

    def test_dual_wielding_two_handed_weapons(self): #pylint: disable=C0103
        """
        Test that character can not dual wield two-handed weapon
        """
        item1 = (ItemBuilder()
                        .with_name('longspear')
                        .with_tag('two-handed weapon')
                        .with_tag('weapon')
                        .build())

        item2 = (ItemBuilder()
                        .with_name('sickle')
                        .with_tag('light weapon')
                        .with_tag('weapon')
                        .build())

        assert_that(item1, is_not(is_in(self.character.weapons)))
        assert_that(item2, is_not(is_in(self.character.weapons)))

        pyherc.rules.items.wield(mock(), self.character, item2)
        pyherc.rules.items.wield(mock(), self.character,
                                 item1,
                                 dual_wield = True)

        assert_that(item1, is_not(is_in(self.character.weapons)))
        assert_that(item2, is_in(self.character.weapons))

    def test_can_dual_wield(self):
        """
        Test that system can determine if two items can be dual-wielded
        """
        item1 = (ItemBuilder()
                    .with_name('longspear')
                    .with_tag('weapon')
                    .with_damage(2)
                    .build())

        item2 = (ItemBuilder()
                    .with_name('sickle')
                    .with_tag('light weapon')
                    .with_damage(1)
                    .build())

        assert(not pyherc.rules.items.can_dual_wield(
                                                     mock(),
                                                     self.character,
                                                     item1,
                                                     item2))

    def test_dual_wieldable(self):
        """
        Test that system can determine if item is dual-wieldable
        """
        item1 = (ItemBuilder()
                    .with_name('longspear')
                    .with_tag('weapon')
                    .with_damage(3)
                    .build())

        item2 = (ItemBuilder()
                    .with_name('sickle')
                    .with_tag('light weapon')
                    .with_damage(1)
                    .build())

        assert(not pyherc.rules.items.is_dual_wieldable(
                                                        mock(),
                                                        self.character,
                                                        item1))
        assert(pyherc.rules.items.is_dual_wieldable(
                                                    mock(),
                                                    self.character,
                                                    item2))

    def test_dual_wieldable_apples(self):
        """
        Test determing if item is dual-wieldable when using mundane items
        """
        item = (ItemBuilder()
                    .with_name('apple')
                    .with_tag('food')
                    .build())

        assert(not pyherc.rules.items.is_dual_wieldable(
                                                        mock(),
                                                        self.character,
                                                        item))

    def test_tags(self):
        """
        Test that different types of items have tags
        """
        item = ItemBuilder().build()

        assert(item.get_tags() is not None)

    def test_main_type_basic(self):
        """
        Test that main type can be retrieved
        """
        self.item = (ItemBuilder()
                    .with_tag('weapon')
                    .build())

        main_type = self.item.get_main_type()

        assert(main_type == 'weapon')

        self.item = (ItemBuilder()
                        .with_tag('food')
                        .build())

        main_type = self.item.get_main_type()

        assert(main_type == 'food')
Esempio n. 2
0
class TestItemsInLevel:
    """
    Tests performed with items that are placed on levels
    """
    def __init__(self):
        """
        Default constructor
        """
        self.item = None
        self.level = None
        self.dungeon = None
        self.model = None
        self.character = None
        self.rng = None

    def setup(self):
        """
        Setup this test case
        """
        self.rng = Random()

        self.item = (ItemBuilder()
                        .build())

        self.level = Level([20, 20])

        self.character = (CharacterBuilder()
                            .with_location((5, 5))
                            .with_level(self.level)
                            .with_action_factory(
                                ActionFactoryBuilder()
                                    .with_inventory_factory())
                            .build())

        self.level.add_item(self.item, (5, 5))

        self.dungeon = pyherc.data.dungeon.Dungeon()
        self.dungeon.levels = self.level

        self.model = pyherc.data.model.Model()
        self.model.dungeon = self.dungeon
        self.model.player = self.character

    def test_picking_up(self):
        """
        Test that item can be picked up
        """
        assert(self.character.location == (5, 5))
        assert(self.item.location == (5, 5))

        self.character.pick_up(self.item)

        assert(self.item in self.character.inventory)
        assert(not self.item in self.level.items)
        assert(self.item.location == ())

    def test_picking_up_not_correct_location(self): #pylint: disable=C0103
        """
        Test that item is not picked up from wrong location
        """
        self.character.location = (6, 6)

        assert(self.character.location == (6, 6))
        assert(self.item.location == (5, 5))

        self.character.pick_up(self.item)

        assert(not self.item in self.character.inventory)
        assert(self.item in self.level.items)

    def test_dropping_item(self):
        """
        Test that an item can be dropped from inventory
        """
        self.character.pick_up(self.item)

        assert(self.item in self.character.inventory)
        assert(not self.item in self.level.items)

        self.character.location = (8, 8)
        pyherc.rules.items.drop(self.model, self.character, self.item)

        assert(not self.item in self.character.inventory)
        assert(self.item in self.level.items)
        assert(self.item.location == (8, 8))

    def test_dropping_wielded_item(self):
        """
        Test that wielded item is dropped correctly
        """
        self.character.pick_up(self.item)
        pyherc.rules.items.wield(self.model, self.character, self.item)

        assert(self.item in self.character.inventory)
        assert(not self.item in self.level.items)
        assert(self.item in self.character.weapons)

        self.character.location = (8, 8)
        pyherc.rules.items.drop(self.model, self.character, self.item)

        assert(not self.item in self.character.inventory)
        assert(self.item in self.level.items)
        assert(self.item.location == (8, 8))
        assert(not self.item in self.character.weapons)

    def test_finding_items(self):
        """
        Test that level can be queried for items on a certain location
        """
        item = (ItemBuilder()
                    .with_name('apple')
                    .build())
        self.level.add_item(item, (5, 5))

        item = (ItemBuilder()
                    .with_name('kiwi')
                    .build())
        self.level.add_item(item, (3, 3))

        items = self.level.get_items_at((5, 5))
        assert(len(items) == 2)

        items = self.level.get_items_at((3, 3))
        assert(len(items) == 1)

        items = self.level.get_items_at((12, 0))
        assert(len(items) == 0)