Пример #1
0
    def test_door_pair(self):
        loc1 = Location("room1", "room one")
        loc2 = Location("room2", "room two")
        key = Key("key")
        door_one_two = Door("two", loc2, "door to room two", locked=True, opened=False)
        door_two_one = door_one_two.reverse_door("one", loc1, "door to room one", reverse_open_msg="door one open", reverse_close_msg="door one close",
                                                 this_open_msg="door two open", this_close_msg="door two close")
        loc1.add_exits([door_one_two])
        loc2.add_exits([door_two_one])
        door_one_two.key_code = 555
        key.key_for(door_one_two)
        pubsub1 = PubsubCollector()
        pubsub2 = PubsubCollector()
        loc1.get_wiretap().subscribe(pubsub1)
        loc2.get_wiretap().subscribe(pubsub2)
        self.assertTrue(door_two_one.locked)
        self.assertFalse(door_two_one.opened)
        lucy = Living("lucy", "f")

        door_two_one.unlock(lucy, item=key)
        self.assertFalse(door_one_two.locked)
        door_two_one.open(lucy)
        self.assertTrue(door_one_two.opened)
        pubsub.sync()
        self.assertEqual(["door one open"], pubsub1.messages)
        self.assertEqual([], pubsub2.messages)
        door_one_two.close(lucy)
        door_one_two.lock(lucy, item=key)
        self.assertTrue(door_two_one.locked)
        self.assertFalse(door_two_one.opened)
        pubsub1.clear()
        pubsub2.clear()
        pubsub.sync()
        self.assertEqual([], pubsub1.messages)
        self.assertEqual(["door two close"], pubsub2.messages)
Пример #2
0
    def test_move_notify(self):
        class LocationNotify(Location):
            def notify_npc_left(self, npc, target_location):
                self.npc_left = npc
                self.npc_left_target = target_location

            def notify_npc_arrived(self, npc, previous_location):
                self.npc_arrived = npc
                self.npc_arrived_from = previous_location

            def notify_player_left(self, player, target_location):
                self.player_left = player
                self.player_left_target = target_location

            def notify_player_arrived(self, player, previous_location):
                self.player_arrived = player
                self.player_arrived_from = previous_location

        npc = NPC("rat", "m", race="rodent")
        room1 = LocationNotify("room1")
        room2 = LocationNotify("room2")
        room1.insert(npc, None)
        npc.move(room2)
        pubsub.sync()
        self.assertEqual(room2, npc.location)
        self.assertEqual(npc, room1.npc_left)
        self.assertEqual(room2, room1.npc_left_target)
        self.assertEqual(npc, room2.npc_arrived)
        self.assertEqual(room1, room2.npc_arrived_from)
Пример #3
0
 def test_message_nearby_location(self):
     plaza = Location("plaza")
     road = Location("road")
     house = Location("house")
     attic = Location("attic")
     plaza.add_exits([Exit("north", road, "road leads north"), Exit("door", house, "door to a house")])
     road.add_exits([Exit("south", plaza, "plaza to the south")])
     house.add_exits([Exit("door", plaza, "door to the plaza"), Exit("ladder", attic, "dusty attic")])
     attic.add_exits([Exit("ladder", house, "the house")])
     wiretap_plaza = Wiretap(plaza)
     wiretap_road = Wiretap(road)
     wiretap_house = Wiretap(house)
     wiretap_attic = Wiretap(attic)
     util.message_nearby_locations(plaza, "boing")
     pubsub.sync()
     self.assertEqual([], wiretap_plaza.msgs, "the plaza doesnt receive tells")
     self.assertEqual([], wiretap_attic.msgs, "the attic is too far away to receive msgs")
     self.assertTrue(("road", "boing") in wiretap_road.msgs)
     self.assertTrue(
         ("road", "The sound is coming from the south.") in wiretap_road.msgs, "road should give sound direction"
     )
     self.assertTrue(("house", "boing") in wiretap_house.msgs)
     self.assertTrue(
         ("house", "You can't hear where the sound is coming from.") in wiretap_house.msgs,
         "in the house you can't locate the sound direction",
     )
Пример #4
0
    def test_move_notify(self):
        class LocationNotify(Location):
            def notify_npc_left(self, npc, target_location):
                self.npc_left = npc
                self.npc_left_target = target_location

            def notify_npc_arrived(self, npc, previous_location):
                self.npc_arrived = npc
                self.npc_arrived_from = previous_location

            def notify_player_left(self, player, target_location):
                self.player_left = player
                self.player_left_target = target_location

            def notify_player_arrived(self, player, previous_location):
                self.player_arrived = player
                self.player_arrived_from = previous_location

        player = Player("julie", "f")
        room1 = LocationNotify("room1")
        room2 = LocationNotify("room2")
        room1.insert(player, player)
        player.move(room2)
        pubsub.sync()
        self.assertEqual(room2, player.location)
        self.assertEqual(player, room1.player_left)
        self.assertEqual(room2, room1.player_left_target)
        self.assertEqual(player, room2.player_arrived)
        self.assertEqual(room1, room2.player_arrived_from)
Пример #5
0
    def test_show_inventory(self):
        class Ctx(object):
            class Config(object):
                pass

            config = Config()

        class MoneyDriverDummy(object):
            pass

        ctx = Ctx()
        ctx.config.money_type = "modern"
        ctx.driver = MoneyDriverDummy()
        ctx.driver.moneyfmt = MoneyFormatter(ctx.config.money_type)
        julie = Living("julie", "f", race="human")
        tap = julie.get_wiretap()
        collector = PubsubCollector()
        tap.subscribe(collector)
        item1 = Item("key")
        julie.init_inventory([item1])
        julie.money = 9.23
        julie.show_inventory(julie, ctx)
        pubsub.sync()
        text = " ".join(msg.strip() for msg in collector.messages)
        self.assertEqual(
            "Julie is carrying: key Money in possession: 9 dollars and 23 cents.",
            text)
        ctx.config.money_type = None
        ctx.driver.moneyfmt = None
        collector.clear()
        julie.show_inventory(julie, ctx)
        pubsub.sync()
        text = " ".join(msg.strip() for msg in collector.messages)
        self.assertEqual("Julie is carrying: key", text)
Пример #6
0
 def test_pubsub_async(self):
     sync()
     s = topic("test1async")
     subber = Subber("sub1")
     subber2 = Subber("sub2")
     s.subscribe(subber)
     s.subscribe(subber2)
     s2 = topic("test1async")
     result = s2.send("event1")
     self.assertIsNone(result)
     self.assertEqual([], subber.messages)
     self.assertEqual([], subber2.messages)
     events, idle, subbers = pending()["test1async"]
     self.assertEqual(1, events)
     result = sync()
     events, idle, subbers = pending()["test1async"]
     self.assertEqual(0, events)
     self.assertIsNone(result)
     self.assertEqual([("test1async", "event1")], subber.messages)
     self.assertEqual([("test1async", "event1")], subber2.messages)
     subber.clear()
     subber2.clear()
     s2.send("event2")
     result = sync("test1async")
     self.assertEqual(2, len(result))
     self.assertTrue("sub1" in result)
     self.assertTrue("sub2" in result)
     self.assertEqual([("test1async", "event2")], subber.messages)
     self.assertEqual([("test1async", "event2")], subber2.messages)
Пример #7
0
 def test_move(self):
     hall = Location("hall")
     attic = Location("attic")
     rat = Living("rat", "n", race="rodent")
     hall.init_inventory([rat])
     wiretap_hall = Wiretap(hall)
     wiretap_attic = Wiretap(attic)
     self.assertTrue(rat in hall.livings)
     self.assertFalse(rat in attic.livings)
     self.assertEqual(hall, rat.location)
     rat.move(attic)
     self.assertTrue(rat in attic.livings)
     self.assertFalse(rat in hall.livings)
     self.assertEqual(attic, rat.location)
     pubsub.sync()
     self.assertEqual([("hall", "Rat leaves.")], wiretap_hall.msgs)
     self.assertEqual([("attic", "Rat arrives.")], wiretap_attic.msgs)
     # now try silent
     wiretap_hall.clear()
     wiretap_attic.clear()
     rat.move(hall, silent=True)
     pubsub.sync()
     self.assertTrue(rat in hall.livings)
     self.assertFalse(rat in attic.livings)
     self.assertEqual(hall, rat.location)
     self.assertEqual([], wiretap_hall.msgs)
     self.assertEqual([], wiretap_attic.msgs)
Пример #8
0
    def test_move_notify(self):
        class LocationNotify(Location):
            def notify_npc_left(self, npc, target_location):
                self.npc_left = npc
                self.npc_left_target = target_location

            def notify_npc_arrived(self, npc, previous_location):
                self.npc_arrived = npc
                self.npc_arrived_from = previous_location

            def notify_player_left(self, player, target_location):
                self.player_left = player
                self.player_left_target = target_location

            def notify_player_arrived(self, player, previous_location):
                self.player_arrived = player
                self.player_arrived_from = previous_location

        npc = NPC("rat", "m", race="rodent")
        room1 = LocationNotify("room1")
        room2 = LocationNotify("room2")
        room1.insert(npc, None)
        npc.move(room2)
        pubsub.sync()
        self.assertEqual(room2, npc.location)
        self.assertEqual(npc, room1.npc_left)
        self.assertEqual(room2, room1.npc_left_target)
        self.assertEqual(npc, room2.npc_arrived)
        self.assertEqual(room1, room2.npc_arrived_from)
Пример #9
0
 def test_message_nearby_location(self):
     plaza = Location("plaza")
     road = Location("road")
     house = Location("house")
     attic = Location("attic")
     plaza.add_exits([
         Exit("north", road, "road leads north"),
         Exit("door", house, "door to a house")
     ])
     road.add_exits([Exit("south", plaza, "plaza to the south")])
     house.add_exits([
         Exit("door", plaza, "door to the plaza"),
         Exit("ladder", attic, "dusty attic")
     ])
     attic.add_exits([Exit("ladder", house, "the house")])
     wiretap_plaza = Wiretap(plaza)
     wiretap_road = Wiretap(road)
     wiretap_house = Wiretap(house)
     wiretap_attic = Wiretap(attic)
     plaza.message_nearby_locations("boing")
     pubsub.sync()
     self.assertEqual([], wiretap_plaza.msgs,
                      "the plaza doesnt receive tells")
     self.assertEqual([], wiretap_attic.msgs,
                      "the attic is too far away to receive msgs")
     self.assertTrue(("road", "boing") in wiretap_road.msgs)
     self.assertTrue(("road", "The sound is coming from the south.")
                     in wiretap_road.msgs,
                     "road should give sound direction")
     self.assertTrue(("house", "boing") in wiretap_house.msgs)
     self.assertTrue(
         ("house", "You can't hear where the sound is coming from.")
         in wiretap_house.msgs,
         "in the house you can't locate the sound direction")
Пример #10
0
    def test_show_inventory(self):
        class Ctx(object):
            class Config(object):
                pass
            config = Config()

        class MoneyDriverDummy(object):
            pass

        ctx = Ctx()
        ctx.config.money_type = "modern"
        ctx.driver = MoneyDriverDummy()
        ctx.driver.moneyfmt = MoneyFormatter(ctx.config.money_type)
        julie = Living("julie", "f", race="human")
        tap = julie.get_wiretap()
        collector = PubsubCollector()
        tap.subscribe(collector)
        item1 = Item("key")
        julie.init_inventory([item1])
        julie.money = 9.23
        julie.show_inventory(julie, ctx)
        pubsub.sync()
        text = " ".join(msg.strip() for msg in collector.messages)
        self.assertEqual("Julie is carrying: key Money in possession: 9 dollars and 23 cents.", text)
        ctx.config.money_type = None
        ctx.driver.moneyfmt = None
        collector.clear()
        julie.show_inventory(julie, ctx)
        pubsub.sync()
        text = " ".join(msg.strip() for msg in collector.messages)
        self.assertEqual("Julie is carrying: key", text)
Пример #11
0
 def test_wiretap(self):
     attic = Location("Attic", "A dark attic.")
     player = Player("fritz", "m")
     io = ConsoleIo(None)
     io.supports_smartquotes = False
     pc = PlayerConnection(player, io)
     player.set_screen_sizes(0, 100)
     julie = NPC("julie", "f")
     julie.move(attic)
     player.move(attic)
     julie.tell("message for julie")
     attic.tell("message for room")
     self.assertEqual(["message for room\n"], player.test_get_output_paragraphs())
     with self.assertRaises(ActionRefused):
         player.create_wiretap(julie)
     player.privileges = {"wizard"}
     player.create_wiretap(julie)
     player.create_wiretap(attic)
     julie.tell("message for julie")
     attic.tell("message for room")
     pubsub.sync()
     output = pc.get_output()
     self.assertTrue("[wiretapped from 'Attic': message for room]" in output)
     self.assertTrue("[wiretapped from 'julie': message for julie]" in output)
     self.assertTrue("[wiretapped from 'julie': message for room]" in output)
     self.assertTrue("message for room " in output)
     # test removing the wiretaps
     player.clear_wiretaps()
     import gc
     gc.collect()
     julie.tell("message for julie")
     attic.tell("message for room")
     self.assertEqual(["message for room\n"], player.test_get_output_paragraphs())
Пример #12
0
 def test_move(self):
     hall = Location("hall")
     attic = Location("attic")
     rat = Living("rat", "n", race="rodent")
     hall.init_inventory([rat])
     wiretap_hall = Wiretap(hall)
     wiretap_attic = Wiretap(attic)
     self.assertTrue(rat in hall.livings)
     self.assertFalse(rat in attic.livings)
     self.assertEqual(hall, rat.location)
     rat.move(attic)
     self.assertTrue(rat in attic.livings)
     self.assertFalse(rat in hall.livings)
     self.assertEqual(attic, rat.location)
     pubsub.sync()
     self.assertEqual([("hall", "Rat leaves.")], wiretap_hall.msgs)
     self.assertEqual([("attic", "Rat arrives.")], wiretap_attic.msgs)
     # now try silent
     wiretap_hall.clear()
     wiretap_attic.clear()
     rat.move(hall, silent=True)
     pubsub.sync()
     self.assertTrue(rat in hall.livings)
     self.assertFalse(rat in attic.livings)
     self.assertEqual(hall, rat.location)
     self.assertEqual([], wiretap_hall.msgs)
     self.assertEqual([], wiretap_attic.msgs)
Пример #13
0
 def test_pubsub_async(self):
     sync()
     s = topic("test1async")
     subber = Subber("sub1")
     subber2 = Subber("sub2")
     s.subscribe(subber)
     s.subscribe(subber2)
     s2 = topic("test1async")
     result = s2.send("event1")
     self.assertIsNone(result)
     self.assertEqual([], subber.messages)
     self.assertEqual([], subber2.messages)
     events, idle, subbers = pending()["test1async"]
     self.assertEqual(1, events)
     result = sync()
     events, idle, subbers = pending()["test1async"]
     self.assertEqual(0, events)
     self.assertEqual([], result)
     self.assertEqual([("test1async", "event1")], subber.messages)
     self.assertEqual([("test1async", "event1")], subber2.messages)
     subber.clear()
     subber2.clear()
     s2.send("event2")
     result = sync("test1async")
     self.assertEqual(2, len(result))
     self.assertTrue("sub1" in result)
     self.assertTrue("sub2" in result)
     self.assertEqual([("test1async", "event2")], subber.messages)
     self.assertEqual([("test1async", "event2")], subber2.messages)
Пример #14
0
    def test_move_notify(self):
        class LocationNotify(Location):
            def notify_npc_left(self, npc, target_location):
                self.npc_left = npc
                self.npc_left_target = target_location

            def notify_npc_arrived(self, npc, previous_location):
                self.npc_arrived = npc
                self.npc_arrived_from = previous_location

            def notify_player_left(self, player, target_location):
                self.player_left = player
                self.player_left_target = target_location

            def notify_player_arrived(self, player, previous_location):
                self.player_arrived = player
                self.player_arrived_from = previous_location

        player = Player("julie", "f")
        room1 = LocationNotify("room1")
        room2 = LocationNotify("room2")
        room1.insert(player, player)
        player.move(room2)
        pubsub.sync()
        self.assertEqual(room2, player.location)
        self.assertEqual(player, room1.player_left)
        self.assertEqual(room2, room1.player_left_target)
        self.assertEqual(player, room2.player_arrived)
        self.assertEqual(room1, room2.player_arrived_from)
Пример #15
0
 def test_idletime(self):
     sync()
     s = topic("testA")
     self.assertLess(s.idle_time, 0.1)
     time.sleep(0.2)
     self.assertGreater(s.idle_time, 0.1)
     s.send("event")
     self.assertLess(s.idle_time, 0.1)
Пример #16
0
 def test_idletime(self):
     sync()
     s = topic("testA")
     self.assertLess(s.idle_time, 0.1)
     time.sleep(0.2)
     self.assertGreater(s.idle_time, 0.1)
     s.send("event")
     self.assertLess(s.idle_time, 0.1)
Пример #17
0
 def test_tell(self):
     julie = Living("julie", "f", race="human")
     tap = julie.get_wiretap()
     collector = PubsubCollector()
     tap.subscribe(collector)
     julie.tell("msg1", "msg2")
     julie.tell("msg3", "msg4", ignored_arg=42)
     pubsub.sync()
     self.assertEqual(["msg1 msg2", "msg3 msg4"], collector.messages)
Пример #18
0
 def test_tell(self):
     julie = Living("julie", "f", race="human")
     tap = julie.get_wiretap()
     collector = PubsubCollector()
     tap.subscribe(collector)
     julie.tell("msg1", "msg2")
     julie.tell("msg3", "msg4", ignored_arg=42)
     pubsub.sync()
     self.assertEqual(["msg1 msg2", "msg3 msg4"], collector.messages)
Пример #19
0
 def test_destroy(self):
     sync()
     s1 = topic("testA")
     s2 = topic("testB")
     s1.send("123")
     p = pending()
     self.assertIn("testA", p)
     self.assertIn("testB", p)
     s1.destroy()
     self.assertEqual("<defunct>", s1.name)
     p = pending()
     self.assertNotIn("testA", p)
     self.assertIn("testB", p)
     s2.destroy()
     p = pending()
     self.assertNotIn("testA", p)
     self.assertNotIn("testB", p)
Пример #20
0
 def test_destroy(self):
     sync()
     s1 = topic("testA")
     s2 = topic("testB")
     s1.send("123")
     p = pending()
     self.assertIn("testA", p)
     self.assertIn("testB", p)
     s1.destroy()
     self.assertEqual("<defunct>", s1.name)
     p = pending()
     self.assertNotIn("testA", p)
     self.assertIn("testB", p)
     s2.destroy()
     p = pending()
     self.assertNotIn("testA", p)
     self.assertNotIn("testB", p)
Пример #21
0
    def test_door_pair(self):
        loc1 = Location("room1", "room one")
        loc2 = Location("room2", "room two")
        key = Key("key")
        door_one_two = Door("two",
                            loc2,
                            "door to room two",
                            locked=True,
                            opened=False)
        door_two_one = door_one_two.reverse_door(
            "one",
            loc1,
            "door to room one",
            reverse_open_msg="door one open",
            reverse_close_msg="door one close",
            this_open_msg="door two open",
            this_close_msg="door two close")
        loc1.add_exits([door_one_two])
        loc2.add_exits([door_two_one])
        door_one_two.key_code = 555
        key.key_for(door_one_two)
        pubsub1 = PubsubCollector()
        pubsub2 = PubsubCollector()
        loc1.get_wiretap().subscribe(pubsub1)
        loc2.get_wiretap().subscribe(pubsub2)
        self.assertTrue(door_two_one.locked)
        self.assertFalse(door_two_one.opened)
        lucy = Living("lucy", "f")

        door_two_one.unlock(lucy, item=key)
        self.assertFalse(door_one_two.locked)
        door_two_one.open(lucy)
        self.assertTrue(door_one_two.opened)
        pubsub.sync()
        self.assertEqual(["door one open"], pubsub1.messages)
        self.assertEqual([], pubsub2.messages)
        door_one_two.close(lucy)
        door_one_two.lock(lucy, item=key)
        self.assertTrue(door_two_one.locked)
        self.assertFalse(door_two_one.opened)
        pubsub1.clear()
        pubsub2.clear()
        pubsub.sync()
        self.assertEqual([], pubsub1.messages)
        self.assertEqual(["door two close"], pubsub2.messages)
Пример #22
0
 def test_unsubscribe_all(self):
     s1 = topic("testA")
     s2 = topic("testB")
     s3 = topic("testC")
     subber = Subber("sub1")
     s1.subscribe(subber)
     s2.subscribe(subber)
     s3.subscribe(subber)
     s1.send("one")
     s2.send("two")
     s3.send("three")
     sync()
     self.assertEqual({('testA', 'one'), ('testB', 'two'), ('testC', 'three')}, set(subber.messages))
     subber.clear()
     unsubscribe_all(subber)
     unsubscribe_all(subber)
     s1.send("one")
     s2.send("two")
     s3.send("three")
     sync()
     self.assertEqual([], subber.messages)
Пример #23
0
 def test_pubsub_sync(self):
     sync()
     s = topic("testsync")
     subber = Subber("sub1")
     subber2 = Subber("sub2")
     s.subscribe(subber)
     s.subscribe(subber)
     s.subscribe(subber2)
     s.subscribe(subber2)
     result = s.send([1, 2, 3], True)
     self.assertEqual([("testsync", [1, 2, 3])], subber.messages)
     self.assertEqual([("testsync", [1, 2, 3])], subber2.messages)
     self.assertEqual(2, len(result))
     self.assertTrue("sub1" in result)
     self.assertTrue("sub2" in result)
     # check explicit unsubscribe
     s.unsubscribe(subber)
     s.unsubscribe(subber)
     s.unsubscribe(subber2)
     result = s.send("after unsubscribing", True)
     self.assertEqual(0, len(result))
Пример #24
0
 def test_unsubscribe_all(self):
     s1 = topic("testA")
     s2 = topic("testB")
     s3 = topic("testC")
     subber = Subber("sub1")
     s1.subscribe(subber)
     s2.subscribe(subber)
     s3.subscribe(subber)
     s1.send("one")
     s2.send("two")
     s3.send("three")
     sync()
     self.assertEqual(set([('testA', 'one'), ('testB', 'two'), ('testC', 'three')]), set(subber.messages))
     subber.clear()
     unsubscribe_all(subber)
     unsubscribe_all(subber)
     s1.send("one")
     s2.send("two")
     s3.send("three")
     sync()
     self.assertEqual([], subber.messages)
Пример #25
0
 def test_pubsub_sync(self):
     sync()
     s = topic("testsync")
     subber = Subber("sub1")
     subber2 = Subber("sub2")
     s.subscribe(subber)
     s.subscribe(subber)
     s.subscribe(subber2)
     s.subscribe(subber2)
     result = s.send([1, 2, 3], True)
     self.assertEqual([("testsync", [1, 2, 3])], subber.messages)
     self.assertEqual([("testsync", [1, 2, 3])], subber2.messages)
     self.assertEqual(2, len(result))
     self.assertTrue("sub1" in result)
     self.assertTrue("sub2" in result)
     # check explicit unsubscribe
     s.unsubscribe(subber)
     s.unsubscribe(subber)
     s.unsubscribe(subber2)
     result = s.send("after unsubscribing", True)
     self.assertEqual(0, len(result))
Пример #26
0
 def test_wiretap(self):
     attic = Location("Attic", "A dark attic.")
     player = Player("fritz", "m")
     io = ConsoleIo(None)
     io.supports_smartquotes = False
     pc = PlayerConnection(player, io)
     player.set_screen_sizes(0, 100)
     julie = NPC("julie", "f")
     julie.move(attic)
     player.move(attic)
     julie.tell("message for julie")
     attic.tell("message for room")
     self.assertEqual(["message for room\n"],
                      player.test_get_output_paragraphs())
     with self.assertRaises(ActionRefused):
         player.create_wiretap(julie)
     player.privileges = {"wizard"}
     player.create_wiretap(julie)
     player.create_wiretap(attic)
     julie.tell("message for julie")
     attic.tell("message for room")
     pubsub.sync()
     output = pc.get_output()
     self.assertTrue(
         "[wiretapped from 'Attic': message for room]" in output)
     self.assertTrue(
         "[wiretapped from 'julie': message for julie]" in output)
     self.assertTrue(
         "[wiretapped from 'julie': message for room]" in output)
     self.assertTrue("message for room " in output)
     # test removing the wiretaps
     player.clear_wiretaps()
     import gc
     gc.collect()
     julie.tell("message for julie")
     attic.tell("message for room")
     self.assertEqual(["message for room\n"],
                      player.test_get_output_paragraphs())