def testMultiTarget(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") philip = tale.npc.NPC("philip", "m") kate = tale.npc.NPC("kate", "f", title="Kate") cat = tale.npc.NPC("cat", "n", title="hairy cat") targets = [philip, kate, cat] # peer parsed = tale.soul.ParseResult("peer", who_order=targets) who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual(set(targets), who) self.assertTrue(player_msg.startswith("You peer at ")) self.assertTrue("philip" in player_msg and "hairy cat" in player_msg and "Kate" in player_msg) self.assertTrue(room_msg.startswith("Julie peers at ")) self.assertTrue("philip" in room_msg and "hairy cat" in room_msg and "Kate" in room_msg) self.assertEqual("Julie peers at you.", target_msg) # all/everyone player.move(tale.base.Location("somewhere")) livings = set(targets) livings.add(player) player.location.livings = livings verb, (who, player_msg, room_msg, target_msg) = soul.process_verb(player, "smile confusedly at everyone") self.assertEqual("smile", verb) self.assertEqual(3, len(who)) self.assertEqual(set(targets), set(who), "player should not be in targets") self.assertTrue("philip" in player_msg and "hairy cat" in player_msg and "Kate" in player_msg and not "yourself" in player_msg) self.assertTrue("philip" in room_msg and "hairy cat" in room_msg and "Kate" in room_msg and not "herself" in room_msg) self.assertEqual("Julie smiles confusedly at you.", target_msg)
def testEnterExits(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") room = tale.base.Location("somewhere") gate = tale.base.Exit("gate", room, "gate") east = tale.base.Exit("east", room, "east") door1 = tale.base.Exit("door one", room, "door number one") room.add_exits([gate, door1, east]) player.move(room) # known actions: enter/go/climb/crawl with self.assertRaises(tale.soul.NonSoulVerb) as x: soul.parse(player, "enter door one") parsed = x.exception.parsed self.assertEqual("door one", parsed.verb) self.assertEqual([door1], parsed.who_order) with self.assertRaises(tale.soul.NonSoulVerb) as x: soul.parse(player, "climb gate") parsed = x.exception.parsed self.assertEqual("gate", parsed.verb) with self.assertRaises(tale.soul.NonSoulVerb) as x: soul.parse(player, "go east") parsed = x.exception.parsed self.assertEqual("east", parsed.verb) with self.assertRaises(tale.soul.NonSoulVerb) as x: soul.parse(player, "crawl east") parsed = x.exception.parsed self.assertEqual("east", parsed.verb) parsed = soul.parse(player, "jump west") self.assertEqual("jump", parsed.verb) self.assertEqual("westwards", parsed.adverb)
def testWhoInfo(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") kate = tale.npc.NPC("kate", "f", title="Kate") cat = tale.npc.NPC("cat", "n", title="hairy cat") player.move(tale.base.Location("somewhere")) cat.move(player.location) kate.move(player.location) parsed = soul.parse(player, "smile at cat and kate and myself") self.assertEqual(["cat", "kate", "myself"], parsed.args) self.assertEqual(3, len(parsed.who_order)) self.assertEqual(3, len(parsed.who_info)) self.assertTrue(cat in parsed.who_info and kate in parsed.who_info and player in parsed.who_info) self.assertEqual(0, parsed.who_info[cat].sequence) self.assertEqual(1, parsed.who_info[kate].sequence) self.assertEqual(2, parsed.who_info[player].sequence) self.assertEqual("at", parsed.who_info[cat].previous_word) self.assertEqual("and", parsed.who_info[kate].previous_word) self.assertEqual("and", parsed.who_info[player].previous_word) self.assertEqual([cat, kate, player], parsed.who_order) parsed = soul.parse(player, "smile at myself and kate and cat") self.assertEqual(["myself", "kate", "cat"], parsed.args) self.assertEqual([player, kate, cat], parsed.who_order) parsed = soul.parse(player, "smile at kate, cat and cat") self.assertEqual(["kate", "cat", "cat"], parsed.args, "deal with multiple occurences") self.assertEqual([kate, cat, cat], parsed.who_order, "deal with multiple occurrences") parsed = soul.parse(player, "smile at kate cat myself") self.assertEqual("at", parsed.who_info[kate].previous_word, "ony kate has a previous word") self.assertEqual(None, parsed.who_info[cat].previous_word, "cat doesn't have a previous word") self.assertEqual(None, parsed.who_info[player].previous_word, "player doesn't have a previous word")
def testMultiTarget(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") philip = tale.npc.NPC("philip", "m") kate = tale.npc.NPC("kate", "f", title="Kate") cat = tale.npc.NPC("cat", "n", title="hairy cat") targets = [philip, kate, cat] # peer parsed = tale.soul.ParseResult("peer", who_order=targets) who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual(set(targets), who) self.assertTrue(player_msg.startswith("You peer at ")) self.assertTrue("philip" in player_msg and "hairy cat" in player_msg and "Kate" in player_msg) self.assertTrue(room_msg.startswith("Julie peers at ")) self.assertTrue("philip" in room_msg and "hairy cat" in room_msg and "Kate" in room_msg) self.assertEqual("Julie peers at you.", target_msg) # all/everyone player.move(tale.base.Location("somewhere")) livings = set(targets) livings.add(player) player.location.livings = livings verb, (who, player_msg, room_msg, target_msg) = soul.process_verb(player, "smile confusedly at everyone") self.assertEqual("smile", verb) self.assertEqual(3, len(who)) self.assertEqual(set(targets), set(who), "player should not be in targets") self.assertTrue("philip" in player_msg and "hairy cat" in player_msg and "Kate" in player_msg and "yourself" not in player_msg) self.assertTrue("philip" in room_msg and "hairy cat" in room_msg and "Kate" in room_msg and "herself" not in room_msg) self.assertEqual("Julie smiles confusedly at you.", target_msg)
def testMessageQuoteParse(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") player.move(tale.base.Location("somewhere")) player.location.livings = {tale.npc.NPC("max", "m"), player} # whisper verb, (who, player_msg, room_msg, target_msg) = soul.process_verb(player, "whisper \"hello there\"") self.assertEqual("You whisper 'hello there'.", player_msg) self.assertEqual("Julie whispers 'hello there'.", room_msg) # whisper to a person verb, (who, player_msg, room_msg, target_msg) = soul.process_verb(player, "whisper to max \"hello there\"") self.assertEqual("You whisper 'hello there' to max.", player_msg) self.assertEqual("Julie whispers 'hello there' to max.", room_msg) # whisper to a person with adverb verb, (who, player_msg, room_msg, target_msg) = soul.process_verb(player, "whisper softly to max \"hello there\"") self.assertEqual("You whisper 'hello there' softly to max.", player_msg) self.assertEqual("Julie whispers 'hello there' softly to max.", room_msg)
def testPronounReferences(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f", "human") room = tale.base.Location("somewhere") room2 = tale.base.Location("somewhere else") player.move(room) max_npc = tale.npc.NPC("Max", "m") kate_npc = tale.npc.NPC("Kate", "f") dino_npc = tale.npc.NPC("dinosaur", "n") targets = [max_npc, kate_npc, dino_npc] player.location.livings = targets newspaper = tale.base.Item("newspaper") player.location.insert(newspaper, player) # her parsed = soul.parse(player, "hug kate") soul.previously_parsed = parsed parsed = soul.parse(player, "kiss her") self.assertEqual(["(By 'her', it is assumed you mean Kate.)\n"], player.test_get_output_paragraphs()) self.assertEqual(kate_npc, parsed.who_order[0]) # it parsed = soul.parse(player, "hug dinosaur") soul.previously_parsed = parsed parsed = soul.parse(player, "kiss it") self.assertEqual(["(By 'it', it is assumed you mean dinosaur.)\n"], player.test_get_output_paragraphs()) self.assertEqual(dino_npc, parsed.who_order[0]) with self.assertRaises(tale.errors.ParseError) as x: parsed = soul.parse(player, "kiss her") self.assertEqual("It is not clear who you're referring to.", str(x.exception)) # them parsed = soul.parse(player, "hug kate and dinosaur") soul.previously_parsed = parsed parsed = soul.parse(player, "kiss them") self.assertEqual( ["(By 'them', it is assumed you mean: Kate and dinosaur.)\n"], player.test_get_output_paragraphs() ) self.assertEqual([kate_npc, dino_npc], parsed.who_order) # when no longer around parsed = soul.parse(player, "hug kate") soul.previously_parsed = parsed player.move(room2) with self.assertRaises(tale.errors.ParseError) as x: parsed = soul.parse(player, "kiss her") self.assertEqual("She is no longer around.", str(x.exception))
def testCheckNamesWithSpacesParsing(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") bird = tale.npc.NPC("brown bird", "f") room = tale.base.Location("somewhere") gate = tale.base.Exit("gate", room, "the gate") door1 = tale.base.Exit("door one", room, "door number one") door2 = tale.base.Exit("door two", room, "door number two") room.add_exits([gate, door1, door2]) bird.move(room) player.move(room) with self.assertRaises(tale.errors.ParseError) as x: soul.parse(player, "hug bird") self.assertEqual("It's not clear what you mean by 'bird'.", str(x.exception)) parsed = soul.parse(player, "hug brown bird affection") self.assertEqual("hug", parsed.verb) self.assertEqual("affectionately", parsed.adverb) self.assertEqual([bird], parsed.who_order) # check spaces in exit names parsed = soul.parse(player, "gate", external_verbs=frozenset(room.exits)) self.assertEqual("gate", parsed.verb) parsed = soul.parse(player, "frobnizz gate", external_verbs={"frobnizz"}) self.assertEqual("frobnizz", parsed.verb) self.assertEqual(["gate"], parsed.args) self.assertEqual([gate], parsed.who_order) with self.assertRaises(tale.soul.UnknownVerbException): soul.parse(player, "door") parsed = soul.parse(player, "enter door two", external_verbs={"enter"}) self.assertEqual("enter", parsed.verb) self.assertEqual(["door two"], parsed.args) self.assertEqual([door2], parsed.who_order) with self.assertRaises(tale.soul.NonSoulVerb) as x: soul.parse(player, "door one") parsed = x.exception.parsed self.assertEqual("door one", parsed.verb) self.assertEqual([door1], parsed.who_order) with self.assertRaises(tale.soul.NonSoulVerb) as x: soul.parse(player, "door two") parsed = x.exception.parsed self.assertEqual("door two", parsed.verb) self.assertEqual([door2], parsed.who_order)
def testPronounReferences(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f", "human") room = tale.base.Location("somewhere") room2 = tale.base.Location("somewhere else") player.move(room) max_npc = tale.npc.NPC("Max", "m") kate_npc = tale.npc.NPC("Kate", "f") dino_npc = tale.npc.NPC("dinosaur", "n") targets = [max_npc, kate_npc, dino_npc] player.location.livings = targets newspaper = tale.base.Item("newspaper") player.location.insert(newspaper, player) # her parsed = soul.parse(player, "hug kate") soul.previously_parsed = parsed parsed = soul.parse(player, "kiss her") self.assertEqual(["(By 'her', it is assumed you mean Kate.)\n"], player.test_get_output_paragraphs()) self.assertEqual(kate_npc, parsed.who_order[0]) # it parsed = soul.parse(player, "hug dinosaur") soul.previously_parsed = parsed parsed = soul.parse(player, "kiss it") self.assertEqual(["(By 'it', it is assumed you mean dinosaur.)\n"], player.test_get_output_paragraphs()) self.assertEqual(dino_npc, parsed.who_order[0]) with self.assertRaises(tale.errors.ParseError) as x: parsed = soul.parse(player, "kiss her") self.assertEqual("It is not clear who you're referring to.", str(x.exception)) # them parsed = soul.parse(player, "hug kate and dinosaur") soul.previously_parsed = parsed parsed = soul.parse(player, "kiss them") self.assertEqual(["(By 'them', it is assumed you mean: Kate and dinosaur.)\n"], player.test_get_output_paragraphs()) self.assertEqual([kate_npc, dino_npc], parsed.who_order) # when no longer around parsed = soul.parse(player, "hug kate") soul.previously_parsed = parsed player.move(room2) with self.assertRaises(tale.errors.ParseError) as x: parsed = soul.parse(player, "kiss her") self.assertEqual("She is no longer around.", str(x.exception))
def testVerbTarget(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") player.title = "the great Julie, destroyer of worlds" player.move(tale.base.Location("somewhere")) npc_max = tale.npc.NPC("max", "m") player.location.livings = {npc_max, player} verb, (who, player_msg, room_msg, target_msg) = soul.process_verb(player, "grin") self.assertEqual("grin", verb) self.assertTrue(len(who) == 0) self.assertIsInstance(who, (set, frozenset), "targets must be a set for O(1) lookups") self.assertEqual("You grin evilly.", player_msg) self.assertEqual("The great Julie, destroyer of worlds grins evilly.", room_msg) verb, (who, player_msg, room_msg, target_msg) = soul.process_verb(player, "grin at max") self.assertEqual("grin", verb) self.assertTrue(len(who) == 1) self.assertIsInstance(who, (set, frozenset), "targets must be a set for O(1) lookups") self.assertEqual("max", list(who)[0].name) self.assertEqual("You grin evilly at max.", player_msg) self.assertEqual("The great Julie, destroyer of worlds grins evilly at max.", room_msg) self.assertEqual("The great Julie, destroyer of worlds grins evilly at you.", target_msg) # parsed results parsed = soul.parse(player, "grin at all") self.assertEqual("grin", parsed.verb) self.assertEqual([npc_max], parsed.who_order, "parse('all') must result in only the npc, not the player") who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertTrue(len(who) == 1) self.assertIsInstance(who, (set, frozenset), "targets must be a set for O(1) lookups") self.assertEqual("max", list(who)[0].name) self.assertEqual("You grin evilly at max.", player_msg) parsed = soul.parse(player, "grin at all and me") self.assertEqual("grin", parsed.verb) self.assertEqual([npc_max, player], parsed.who_order, "parse('all and me') must include npc and the player") who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual({npc_max}, who, "player should no longer be part of the remaining targets") self.assertTrue("yourself" in player_msg and "max" in player_msg)
def testParse(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f", "human") room = tale.base.Location("somewhere") south_exit = tale.base.Exit("south", room, "a door to the south") east_exit = tale.base.Exit("east", room, "a door to the east") room.add_exits([east_exit, south_exit]) player.move(room) max_npc = tale.npc.NPC("max", "m") kate_npc = tale.npc.NPC("kate", "f") dino_npc = tale.npc.NPC("dinosaur", "n") targets = [max_npc, kate_npc, dino_npc] targets_with_player = targets + [player] player.location.livings = targets newspaper = tale.base.Item("newspaper") player.location.insert(newspaper, player) parsed = soul.parse(player, "fail grin sickly at everyone head") self.assertEqual("fail", parsed.qualifier) self.assertEqual("grin", parsed.verb) self.assertEqual("sickly", parsed.adverb) self.assertEqual("head", parsed.bodypart) self.assertEqual("", parsed.message) self.assertTrue(len(parsed.who_order) == 3) self.assertTrue(all(isinstance(x, tale.base.Living) for x in parsed.who_order), "parse must return Livings in 'who'") self.assertEqual(targets, parsed.who_order) parsed = soul.parse(player, "slap myself") self.assertEqual(None, parsed.qualifier) self.assertEqual("slap", parsed.verb) self.assertEqual(None, parsed.adverb) self.assertEqual(None, parsed.bodypart) self.assertEqual("", parsed.message) self.assertEqual([player], parsed.who_order, "myself should be player") parsed = soul.parse(player, "slap all") self.assertEqual(None, parsed.qualifier) self.assertEqual("slap", parsed.verb) self.assertEqual(None, parsed.adverb) self.assertEqual(None, parsed.bodypart) self.assertEqual("", parsed.message) self.assertEqual(3, len(parsed.who_info), "all should not include player") self.assertEqual(targets, parsed.who_order, "all should not include player") parsed = soul.parse(player, "slap all but kate") self.assertEqual(2, len(parsed.who_info), "all but kate should only be max and the dino") self.assertEqual([max_npc, dino_npc], parsed.who_order, "all but kate should only be max and the dino") parsed = soul.parse(player, "slap all and myself") self.assertEqual(targets_with_player, parsed.who_order, "all and myself should include player") parsed = soul.parse(player, "slap newspaper") self.assertEqual([newspaper], parsed.who_order, "must be able to perform soul verb on item") with self.assertRaises(tale.errors.ParseError) as x: soul.parse(player, "slap dino") self.assertEqual("Perhaps you meant dinosaur?", str(x.exception), "must suggest living with prefix") with self.assertRaises(tale.errors.ParseError) as x: soul.parse(player, "slap news") self.assertEqual("Perhaps you meant newspaper?", str(x.exception), "must suggest item with prefix") with self.assertRaises(tale.errors.ParseError) as x: soul.parse(player, "slap undefined") self.assertEqual("It's not clear what you mean by 'undefined'.", str(x.exception)) parsed = soul.parse(player, "smile west") self.assertEqual("westwards", parsed.adverb) with self.assertRaises(tale.errors.ParseError) as x: soul.parse(player, "smile north") self.assertEqual("What adverb did you mean: northeastwards, northwards, or northwestwards?", str(x.exception)) parsed = soul.parse(player, "smile south") self.assertEqual(["south"], parsed.args, "south must be parsed as a normal arg because it's an exit in the room") parsed = soul.parse(player, "smile kate dinosaur and max") self.assertEqual(["kate", "dinosaur", "max"], parsed.args, "must be able to skip comma") self.assertEqual(3, len(parsed.who_order), "must be able to skip comma") parsed = soul.parse(player, "reply kate ofcourse, darling.") self.assertEqual(["kate", "ofcourse,", "darling."], parsed.args, "must be able to skip comma") self.assertEqual(1, len(parsed.who_order)) # check movement parsing for room exits with self.assertRaises(tale.soul.NonSoulVerb) as x: soul.parse(player, "crawl south") self.assertEqual("south", x.exception.parsed.verb, "just the exit is the verb, not the movement action") self.assertEqual([south_exit], x.exception.parsed.who_order, "exit must be in the who set") parsed_str = str(x.exception.parsed) self.assertTrue("verb=south" in parsed_str) with self.assertRaises(tale.errors.ParseError) as x: soul.parse(player, "crawl somewherenotexisting") self.assertEqual("You can't crawl there.", str(x.exception)) with self.assertRaises(tale.errors.ParseError) as x: soul.parse(player, "crawl") self.assertEqual("Crawl where?", str(x.exception)) room = tale.base.Location("somewhere") # no exits in this new room player.move(room) with self.assertRaises(tale.soul.UnknownVerbException): soul.parse(player, "crawl") # must raise unknownverb if there are no exits in the room