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 testAdverbWithoutVerb(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") with self.assertRaises(tale.soul.UnknownVerbException) as ex: soul.parse(player, "forg") # forgetfully etc. self.assertEqual("forg", ex.exception.verb) with self.assertRaises(tale.errors.ParseError) as ex: soul.parse(player, "giggle forg") # forgetfully etc. self.assertEqual("What adverb did you mean: forgetfully or forgivingly?", str(ex.exception))
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 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 testUnparsed(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f", "human") parsed = soul.parse(player, "fart") self.assertEqual("", parsed.unparsed) parsed = soul.parse(player, "grin sadistically") self.assertEqual("sadistically", parsed.unparsed) parsed = soul.parse(player, "fail sit zen") self.assertEqual("zen", parsed.unparsed) parsed = soul.parse(player, "pat myself comfortingly on the shoulder") self.assertEqual("myself comfortingly on the shoulder", parsed.unparsed) parsed = soul.parse(player, "take the watch and the key from the box", external_verbs={"take"}) self.assertEqual("the watch and the key from the box", parsed.unparsed) parsed = soul.parse(player, "fail to _undefined_verb_ on the floor", external_verbs={"_undefined_verb_"}) self.assertEqual("on the floor", parsed.unparsed) parsed = soul.parse(player, "say 'red or blue'", external_verbs={"say"}) self.assertEqual("'red or blue'", parsed.unparsed) parsed = soul.parse(player, "say red or blue", external_verbs={"say"}) self.assertEqual("red or blue", parsed.unparsed) parsed = soul.parse(player, "say hastily red or blue", external_verbs={"say"}) self.assertEqual("hastily red or blue", parsed.unparsed) parsed = soul.parse(player, "fail say hastily red or blue on your head", external_verbs={"say"}) self.assertEqual("hastily red or blue on your head", parsed.unparsed)
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 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 testIgnorewords(self): soul = tale.soul.Soul() player = tale.player.Player("fritz", "m") with self.assertRaises(tale.errors.ParseError): soul.parse(player, "in") with self.assertRaises(tale.errors.ParseError): soul.parse(player, "and") with self.assertRaises(tale.errors.ParseError): soul.parse(player, "fail") with self.assertRaises(tale.errors.ParseError): soul.parse(player, "fail in") with self.assertRaises(tale.soul.UnknownVerbException) as x: soul.parse(player, "in fail") self.assertEqual("fail", x.exception.verb) parsed = soul.parse(player, "in sit") self.assertIsNone(parsed.qualifier) self.assertIsNone(parsed.adverb) self.assertEqual("sit", parsed.verb) parsed = soul.parse(player, "fail in sit") self.assertEqual("fail", parsed.qualifier) self.assertIsNone(parsed.adverb) self.assertEqual("sit", parsed.verb)
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
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)