def testQUAD(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") targets = [tale.npc.NPC("max", "m")] # watch1 parsed = tale.soul.ParseResult("watch") who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You watch the surroundings carefully.", player_msg) self.assertEqual("Julie watches the surroundings carefully.", room_msg) # watch2 parsed.who_order = targets parsed.recalc_who_info() who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You watch max carefully.", player_msg) self.assertEqual("Julie watches max carefully.", room_msg) self.assertEqual("Julie watches you carefully.", target_msg) # ayt parsed.verb = "ayt" who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual(set(targets), who) self.assertEqual("You wave your hand in front of max's face, is he there?", player_msg) self.assertEqual("Julie waves her hand in front of max's face, is he there?", room_msg) self.assertEqual("Julie waves her hand in front of your face, are you there?", target_msg) # ayt targets2 = [tale.npc.NPC("max", "m"), player] parsed.who_order = targets2 parsed.recalc_who_info() who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertTrue(player_msg.startswith("You wave your hand in front of ")) self.assertTrue("max's" in player_msg and "your own" in player_msg) self.assertTrue(room_msg.startswith("Julie waves her hand in front of ")) self.assertTrue("max's" in room_msg and "her own" in room_msg) self.assertEqual("Julie waves her hand in front of your face, are you there?", target_msg)
def testMessageQuote(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") # babble parsed = tale.soul.ParseResult("babble") who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You babble something incoherently.", player_msg) self.assertEqual("Julie babbles something incoherently.", room_msg) # babble with message parsed.message = "blurp" who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You babble 'blurp' incoherently.", player_msg) self.assertEqual("Julie babbles 'blurp' incoherently.", room_msg)
def testSHRT(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") # faint parsed = tale.soul.ParseResult("faint", adverb="slowly") who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You faint slowly.", player_msg) self.assertEqual("Julie faints slowly.", room_msg) # cheer parsed = tale.soul.ParseResult("cheer") who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You cheer enthusiastically.", player_msg) self.assertEqual("Julie cheers enthusiastically.", room_msg)
def testGender(self): soul = tale.soul.Soul() with self.assertRaises(KeyError): tale.player.Player("player", "x") player = tale.player.Player("julie", "f") parsed = tale.soul.ParseResult("stomp") who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("Julie stomps her foot.", room_msg) player = tale.player.Player("fritz", "m") who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("Fritz stomps his foot.", room_msg) player = tale.player.Player("zyzzy", "n") who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("Zyzzy stomps its foot.", room_msg)
def testDEFA(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") targets = [tale.npc.NPC("max", "m")] # grin parsed = tale.soul.ParseResult("grin") who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You grin evilly.", player_msg) self.assertEqual("Julie grins evilly.", room_msg) # drool parsed = tale.soul.ParseResult("drool", who_order=targets) who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You drool on max.", player_msg) self.assertEqual("Julie drools on max.", room_msg) self.assertEqual("Julie drools on you.", target_msg)
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 testPERS(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") targets = [tale.npc.NPC("max", "m")] # fear1 parsed = tale.soul.ParseResult("fear") who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You shiver with fear.", player_msg) self.assertEqual("Julie shivers with fear.", room_msg) # fear2 parsed.who_order = targets parsed.recalc_who_info() who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You fear max.", player_msg) self.assertEqual("Julie fears max.", room_msg) self.assertEqual("Julie fears you.", target_msg)
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 testUnknownVerb(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") with self.assertRaises(tale.soul.UnknownVerbException) as ex: parsed = tale.soul.ParseResult("_unknown_verb_") soul.process_verb_parsed(player, parsed) self.assertEqual("_unknown_verb_", str(ex.exception)) self.assertEqual("_unknown_verb_", ex.exception.verb) self.assertEqual(None, ex.exception.words) self.assertEqual(None, ex.exception.qualifier) with self.assertRaises(tale.soul.UnknownVerbException) as ex: soul.process_verb(player, "fail _unknown_verb_ herp derp") self.assertEqual("_unknown_verb_", ex.exception.verb) self.assertEqual("fail", ex.exception.qualifier) self.assertEqual(["_unknown_verb_", "herp", "derp"], ex.exception.words) self.assertTrue(soul.is_verb("bounce")) self.assertFalse(soul.is_verb("_unknown_verb_"))
def testDEUX(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") # die parsed = tale.soul.ParseResult("die", adverb="suddenly") who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You suddenly fall down and play dead.", player_msg) self.assertEqual("Julie suddenly falls to the ground, dead.", room_msg) # ah parsed = tale.soul.ParseResult("ah", adverb="rudely") who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You go 'ah' rudely.", player_msg) self.assertEqual("Julie goes 'ah' rudely.", room_msg) # verb needs a person with self.assertRaises(tale.errors.ParseError) as x: soul.process_verb(player, "touch") self.assertEqual("The verb touch needs a person.", str(x.exception))
def testBodypart(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") targets = [tale.npc.NPC("max", "m")] parsed = tale.soul.ParseResult("beep", who_order=targets) who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You triumphantly beep max on the nose.", player_msg) self.assertEqual("Julie triumphantly beeps max on the nose.", room_msg) self.assertEqual("Julie triumphantly beeps you on the nose.", target_msg) parsed.bodypart = "arm" who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You triumphantly beep max on the arm.", player_msg) self.assertEqual("Julie triumphantly beeps max on the arm.", room_msg) self.assertEqual("Julie triumphantly beeps you on the arm.", target_msg) # check handling of more than one bodypart with self.assertRaises(tale.errors.ParseError) as ex: soul.process_verb(player, "kick max side knee") self.assertEqual("You can't do that both in the side and on the knee.", str(ex.exception))
def testPHYS(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") targets = [tale.npc.NPC("max", "m")] # require person with self.assertRaises(tale.errors.ParseError): parsed = tale.soul.ParseResult("bonk") soul.process_verb_parsed(player, parsed) # pounce parsed = tale.soul.ParseResult("pounce", who_order=targets) who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You pounce max playfully.", player_msg) self.assertEqual("Julie pounces max playfully.", room_msg) self.assertEqual("Julie pounces you playfully.", target_msg) # hold parsed = tale.soul.ParseResult("hold", who_order=targets) who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You hold max in your arms.", player_msg) self.assertEqual("Julie holds max in her arms.", room_msg) self.assertEqual("Julie holds you in her arms.", target_msg)
def testPREV(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") targets = [tale.npc.NPC("max", "m")] # peer parsed = tale.soul.ParseResult("peer", who_order=targets) who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You peer at max.", player_msg) self.assertEqual("Julie peers at max.", room_msg) self.assertEqual("Julie peers at you.", target_msg) # tease parsed = tale.soul.ParseResult("tease", who_order=targets) who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You tease max.", player_msg) self.assertEqual("Julie teases max.", room_msg) self.assertEqual("Julie teases you.", target_msg) # turn parsed = tale.soul.ParseResult("turn", who_order=targets) who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You turn your head towards max.", player_msg) self.assertEqual("Julie turns her head towards max.", room_msg) self.assertEqual("Julie turns her head towards you.", target_msg)
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 testQualifier(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") targets = [tale.npc.NPC("max", "m")] parsed = tale.soul.ParseResult("tickle", qualifier="fail", who_order=targets) who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You try to tickle max, but fail miserably.", player_msg) self.assertEqual("Julie tries to tickle max, but fails miserably.", room_msg) self.assertEqual("Julie tries to tickle you, but fails miserably.", target_msg) parsed.qualifier = "don't" who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You don't tickle max.", player_msg) self.assertEqual("Julie doesn't tickle max.", room_msg) self.assertEqual("Julie doesn't tickle you.", target_msg) parsed.qualifier = "suddenly" who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You suddenly tickle max.", player_msg) self.assertEqual("Julie suddenly tickles max.", room_msg) self.assertEqual("Julie suddenly tickles you.", target_msg) parsed = tale.soul.ParseResult("scream", qualifier="don't", message="I have no idea") who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You don't scream 'I have no idea' loudly.", player_msg) self.assertEqual("Julie doesn't scream 'I have no idea' loudly.", room_msg) self.assertEqual("Julie doesn't scream 'I have no idea' loudly.", target_msg)
def testSIMP(self): soul = tale.soul.Soul() player = tale.player.Player("julie", "f") targets = [tale.npc.NPC("max", "m")] # scream 1 parsed = tale.soul.ParseResult("scream") who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You scream loudly.", player_msg) self.assertEqual("Julie screams loudly.", room_msg) # scream 2 parsed.who_order = targets parsed.recalc_who_info() parsed.adverb = "angrily" parsed.message = "why" who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You scream 'why' angrily at max.", player_msg) self.assertEqual("Julie screams 'why' angrily at max.", room_msg) self.assertEqual("Julie screams 'why' angrily at you.", target_msg) # ask parsed = tale.soul.ParseResult("ask", message="are you happy", who_order=targets) who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You ask max: are you happy?", player_msg) self.assertEqual("Julie asks max: are you happy?", room_msg) self.assertEqual("Julie asks you: are you happy?", target_msg) # puzzle1 parsed = tale.soul.ParseResult("puzzle") who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You look puzzled.", player_msg) self.assertEqual("Julie looks puzzled.", room_msg) # puzzle2 parsed.who_order = targets parsed.recalc_who_info() who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You look puzzled at max.", player_msg) self.assertEqual("Julie looks puzzled at max.", room_msg) self.assertEqual("Julie looks puzzled at you.", target_msg) # chant1 parsed = tale.soul.ParseResult("chant", adverb="merrily", message="tralala") who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You merrily chant: tralala.", player_msg) self.assertEqual("Julie merrily chants: tralala.", room_msg) # chant2 parsed = tale.soul.ParseResult("chant") who, player_msg, room_msg, target_msg = soul.process_verb_parsed(player, parsed) self.assertEqual("You chant: Hare Krishna Krishna Hare Hare.", player_msg) self.assertEqual("Julie chants: Hare Krishna Krishna Hare Hare.", room_msg)