コード例 #1
0
    def test_travel_in_out(self):
        room1 = Room(self.game, "A place", "Description of a place. ")
        room2 = Room(
            self.game, "A different place", "Description of a different place. "
        )
        room1.entrance = room2
        room2.exit = room1
        self.me.location.removeThing(self.me)
        room1.addThing(self.me)
        self.assertIs(
            self.me.location, room1, "This test needs the user to start in room1"
        )

        self.game.turnMain("enter")

        self.assertEqual(self.app.print_stack[-3], room1.in_msg)

        self.assertIs(
            self.me.location,
            room2,
            f"Tried to travel in to {room2}, '{room2.name}', but player in "
            f"{self.me.location}",
        )

        self.game.turnMain("exit")

        self.assertEqual(self.app.print_stack[-3], room1.out_msg)

        self.assertIs(
            self.me.location,
            room1,
            f"Tried to travel out to {room1}, '{room1.name}', but player in "
            f"{self.me.location}",
        )
コード例 #2
0
    def test_travel_north_south(self):
        room1 = Room(self.game, "A place", "Description of a place. ")
        room2 = Room(
            self.game, "A different place", "Description of a different place. "
        )
        room1.north = room2
        room2.south = room1
        self.me.location.removeThing(self.me)
        room1.addThing(self.me)
        self.assertIs(
            self.me.location, room1, "This test needs the user to start in room1"
        )

        self.game.turnMain("go n")
        self.assertEqual(self.app.print_stack[-3], room1.n_msg)

        self.assertIs(
            self.me.location,
            room2,
            f"Tried to travel north to {room2}, '{room2.name}', but player in "
            f"{self.me.location}",
        )

        self.game.turnMain("s")

        self.assertEqual(self.app.print_stack[-3], room1.s_msg)

        self.assertIs(
            self.me.location,
            room1,
            f"Tried to travel south to {room1}, '{room1.name}', but player in "
            f"{self.me.location}",
        )
コード例 #3
0
    def test_travel_up_down(self):
        room1 = Room(self.game, "A place", "Description of a place. ")
        room2 = Room(
            self.game, "A different place", "Description of a different place. "
        )
        room1.up = room2
        room2.down = room1
        self.me.location.removeThing(self.me)
        room1.addThing(self.me)
        self.assertIs(
            self.me.location, room1, "This test needs the user to start in room1"
        )

        self.game.turnMain("u")

        self.assertEqual(self.app.print_stack[-3], room1.u_msg)

        self.assertIs(
            self.me.location,
            room2,
            f"Tried to travel up to {room2}, '{room2.name}', but player in "
            f"{self.me.location}",
        )

        self.game.turnMain("go d")
        self.assertEqual(self.app.print_stack[-3], room1.d_msg)

        self.assertIs(
            self.me.location,
            room1,
            f"Tried to travel down to {room1}, '{room1.name}', but player in "
            f"{self.me.location}",
        )
コード例 #4
0
    def test_travel_east_west(self):
        room1 = Room(self.game, "A place", "Description of a place. ")
        room2 = Room(
            self.game, "A different place", "Description of a different place. "
        )
        room1.east = room2
        room2.west = room1
        self.me.location.removeThing(self.me)
        room1.addThing(self.me)
        self.assertIs(
            self.me.location, room1, "This test needs the user to start in room1"
        )

        self.game.turnMain("go e")

        self.assertEqual(self.app.print_stack[-3], room1.e_msg)

        self.assertIs(
            self.me.location,
            room2,
            f"Tried to travel east to {room2}, '{room2.name}', but player in "
            f"{self.me.location}",
        )

        self.game.turnMain("w")

        self.assertEqual(self.app.print_stack[-3], room1.w_msg)

        self.assertIs(
            self.me.location,
            room1,
            f"Tried to travel west to {room1}, '{room1.name}', but player in "
            f"{self.me.location}",
        )
コード例 #5
0
    def test_cannot_travel_if_not_connection(self):
        room1 = Room(self.game, "A place", "Description of a place. ")
        self.assertTrue(
            (not room1.north and not room1.northeast and not room1.east
             and not room1.southeast and not room1.south
             and not room1.southwest and not room1.west and not room1.northwest
             and not room1.up and not room1.down and not room1.entrance
             and not room1.exit),
            "This test needs room1 to have no directional connections.",
        )
        self.me.location.removeThing(self.me)
        room1.addThing(self.me)

        self.game.turnMain("n")
        self.assertIs(self.me.location, room1)
        self.assertEqual(self.app.print_stack.pop(), room1.n_false_msg)

        self.game.turnMain("ne")
        self.assertIs(self.me.location, room1)
        self.assertEqual(self.app.print_stack.pop(), room1.ne_false_msg)

        self.game.turnMain("e")
        self.assertIs(self.me.location, room1)
        self.assertEqual(self.app.print_stack.pop(), room1.e_false_msg)

        self.game.turnMain("se")
        self.assertIs(self.me.location, room1)
        self.assertEqual(self.app.print_stack.pop(), room1.se_false_msg)

        self.game.turnMain("s")
        self.assertIs(self.me.location, room1)
        self.assertEqual(self.app.print_stack.pop(), room1.s_false_msg)

        self.game.turnMain("sw")
        self.assertIs(self.me.location, room1)
        self.assertEqual(self.app.print_stack.pop(), room1.sw_false_msg)

        self.game.turnMain("w")
        self.assertIs(self.me.location, room1)
        self.assertEqual(self.app.print_stack.pop(), room1.w_false_msg)

        self.game.turnMain("nw")
        self.assertIs(self.me.location, room1)
        self.assertEqual(self.app.print_stack.pop(), room1.nw_false_msg)

        self.game.turnMain("u")
        self.assertIs(self.me.location, room1)
        self.assertEqual(self.app.print_stack.pop(), room1.u_false_msg)

        self.game.turnMain("d")
        self.assertIs(self.me.location, room1)
        self.assertEqual(self.app.print_stack.pop(), room1.d_false_msg)

        self.game.turnMain("in")
        self.assertIs(self.me.location, room1)
        self.assertEqual(self.app.print_stack.pop(), room1.entrance_false_msg)

        self.game.turnMain("out")
        self.assertIs(self.me.location, room1)
        self.assertEqual(self.app.print_stack.pop(), room1.exit_false_msg)
コード例 #6
0
    def test_move_to_removes_item_from_old_location_and_adds_to_new_location(self):
        old = Room(self.game, "old", "It is old")
        child = Thing(self.game, "child")
        old.addThing(child)

        new = Container(self.game, "new")
        child.moveTo(new)

        self.assertItemIn(child, new.contains, "Item not added to new location")
        self.assertItemNotIn(child, old.contains, "Item not removed from old location")
        self.assertIs(child.location, new, "Item not added to new location")
コード例 #7
0
class IFPTestCase(TestCase):
    def setUp(self):
        self.app = TestApp()
        self.game = IFPGame(self.app, main=__name__)
        self.me = Player(self.game)
        self.start_room = Room(self.game, "room", "desc")
        self.start_room.addThing(self.me)
        self.game.setPlayer(self.me)
        self.game.initGame()

    def _insert_dobj_into_phrase(self, phrase, dobj):
        ix = phrase.index("<dobj>")
        phrase = phrase[:ix] + dobj + phrase[ix + 1 :]
        return phrase

    def _insert_iobj_into_phrase(self, phrase, iobj):
        ix = phrase.index("<iobj>")
        phrase = phrase[:ix] + iobj + phrase[ix + 1 :]
        return phrase

    def _insert_objects_into_phrase(self, phrase, dobj, iobj):
        phrase = self._insert_dobj_into_phrase(phrase, dobj)
        phrase = self._insert_iobj_into_phrase(phrase, iobj)
        return phrase

    def _get_unique_noun(self):
        noun = str(random.getrandbits(128))
        if noun in self.game.nouns:
            noun = self._get_unique_noun()
        return noun

    def assertItemIn(self, item, contains_dict, msg):
        self.assertIn(item.ix, contains_dict, f"Index not in dictionary: {msg}")
        self.assertIn(
            item,
            contains_dict[item.ix],
            f"Index in dictionary, but item not found under index: {msg}",
        )

    def assertItemNotIn(self, item, contains_dict, msg):
        if item.ix in contains_dict:
            self.assertNotIn(
                item,
                contains_dict[item.ix],
                f"Item unexpectedly found in dictionary: {msg}",
            )

    def assertItemExactlyOnceIn(self, item, contains_dict, msg):
        self.assertIn(item.ix, contains_dict, f"Index not in dictionary: {msg}")
        n = len(contains_dict[item.ix])
        self.assertEqual(
            n, 1, f"Expected a single occurrence of item {item}, found {n}: {msg}"
        )
コード例 #8
0
ファイル: test_look.py プロジェクト: JSMaika/intficpy
    def test_look(self):
        room = Room(self.game, "Strange Room", "It's strange in here. ")
        item = Thing(self.game, "hullabaloo")
        item.describeThing("All around is a hullabaloo. ")
        room.addThing(item)

        self.me.location.removeThing(self.me)
        room.addThing(self.me)

        self.game.turnMain("look")
        look_desc = self.app.print_stack.pop()
        look_title = self.app.print_stack.pop()

        self.assertIn(room.name, look_title, f"Room title printed incorrectly")
        self.assertIn(room.desc, look_desc, "Room description not printed by look")
        self.assertIn(item.desc, look_desc, "Item description not printed by look")
コード例 #9
0
    def test_move_to_removes_item_from_old_superlocation_subcontains(self):
        room = Room(self.game, "old", "It is old")
        old = Container(self.game, "box")
        room.addThing(old)
        child = Thing(self.game, "child")
        old.addThing(child)

        new = Container(self.game, "new")

        self.assertItemIn(
            child, room.sub_contains, "Item not removed from old location"
        )

        child.moveTo(new)

        self.assertItemNotIn(
            child, room.sub_contains, "Item not removed from old location"
        )
        self.assertIs(child.location, new, "Item not added to new location")