from th import Ifc, Monster, g from th.data import Species, Items, Nodes import unittest class FormatterTest(unittest.TestCase): def test_color(self): self.assertEquals(Ifc.orig_color(), '\x1b[0m') self.assertEquals(Ifc.orig_color(Ifc.WHITE), '\x1b[37m') self.assertEquals(Ifc.orig_color(Ifc.BRIGHT_WHITE), '\x1b[1;37m') def test_format(self): mon = Monster.Monster() mon.fmt = {'spam': 'eggs'} self.assertEquals(Ifc.format('|spam| |eggs|', mon), 'eggs |eggs|') def test_you(self): Ifc.messages = [] Ifc.you('run|s| the regression tests.') mon = Species.hamster.make(rabid=False) Ifc.you('run|s| the regression tests.', mon) mon.rabid = True Ifc.you('run|s| the regression tests.', mon) mon = Species.coach.make(rabid=True) Ifc.you('run|s| the regression tests.', mon) mon.name = 'Spam' Ifc.you('run|s| the regression tests.', mon) self.assertEquals(Ifc.messages, [ 'You run the regression tests.', 'The Hamster runs the regression tests.', 'The rabid Hamster runs the regression tests.', 'The insane Coach runs the regression tests.', 'Spam runs the regression tests.' ]) def test_your(self): Ifc.messages = [] Ifc.your('regression tests are going |your| way!') mon = Species.hamster.make(rabid=False) Ifc.your('regression tests are going |your| way!', mon) mon.rabid = True Ifc.your('regression tests are going |your| way!', mon) mon = Species.coach.make(rabid=True, gender=Monster.MALE) Ifc.your('regression tests are going |your| way!', mon) mon.name = 'Spam' Ifc.your('regression tests are going |your| way!', mon) self.assertEquals(Ifc.messages, [ 'Your regression tests are going your way!', "The Hamster's regression tests are going its way!", "The rabid Hamster's regression tests are going its way!", "The insane Coach's regression tests are going his way!", "Spam's regression tests are going his way!", ]) def test_the_mon(self): self.assertEquals(Ifc.the_mon(), 'you') self.assertEquals(Ifc.the_mon(g.player), 'you') self.assertEquals(Ifc.the_mon(possessive=True), 'your') self.assertEquals(Ifc.the_mon(g.player, possessive=True), 'your') mon = Species.hamster.make(rabid=False) self.assertEquals(Ifc.the_mon(mon), 'the Hamster') self.assertEquals(Ifc.the_mon(mon, possessive=True), "the Hamster's") mon.ai = Monster.PET self.assertEquals(Ifc.the_mon(mon), 'the Hamster') self.assertEquals(Ifc.the_mon(mon, possessive=True), "the Hamster's") mon.owner = g.player self.assertEquals(Ifc.the_mon(mon), 'the allied Hamster') self.assertEquals(Ifc.the_mon(mon, possessive=True), "the allied Hamster's") self.assertEquals(Ifc.the_mon(mon, showallied=False), 'the Hamster') self.assertEquals(Ifc.the_mon(mon, showallied=False, possessive=True), "the Hamster's") mon.rabid, mon.owner = True, None self.assertEquals(Ifc.the_mon(mon), 'the rabid Hamster') self.assertEquals(Ifc.the_mon(mon, possessive=True), "the rabid Hamster's") mon.name = 'Spam' self.assertEquals(Ifc.the_mon(mon), 'Spam') self.assertEquals(Ifc.the_mon(mon, possessive=True), "Spam's") mon = Species.coach.make(rabid=True, gender=Monster.MALE) self.assertEquals(Ifc.the_mon(mon), 'the insane Coach') self.assertEquals(Ifc.the_mon(mon, possessive=True), "the insane Coach's") class InputTest(unittest.TestCase): def test_choice(self): mons = [Species.hamster.make()] Ifc.inputs = ['3', 'foo', '1', '0'] self.assertEquals(Ifc.choice('', mons), mons[0]) self.assertEquals(Ifc.choice('', mons), None) mons = [Species.hamster.make() for x in xrange(10)] Ifc.inputs = ['11', '01'] self.assertEquals(Ifc.choice('', mons), mons[0]) mons = [Species.hamster.make() for x in xrange(100)] Ifc.inputs = ['111', '001', '50'] self.assertEquals(Ifc.choice('', mons), mons[0]) self.assertEquals(Ifc.choice('', mons), mons[49]) items = [Items.pencil.make() for x in xrange(10)] Ifc.inputs = ['22', '02'] self.assertEquals(Ifc.choice('', items), items[1]) def test_prenumbered_choice(self): mons = [ (4, Species.hamster.make()), (11, Species.hamster.make()), (41, Species.hamster.make()) ] Ifc.inputs = ['1', '4', 'spam', '5', '12', '42', '0'] for i, mon in mons: self.assertEquals(Ifc.prenumbered_choice('', mons), mon) self.assertEquals(Ifc.prenumbered_choice('', mons), None) items = [ (9, Items.pencil.make()), (21, Items.pencil.make()), (56, Items.pencil.make()) ] Ifc.inputs = ['0', 'eggs', '57', '22', '10'] self.assertEquals(Ifc.prenumbered_choice('', items), None) for i, itm in reversed(items): self.assertEquals(Ifc.prenumbered_choice('', items), itm) def test_choice_str(self): strings = ['spam', 'eggs', 'more spam'] Ifc.inputs = ['0', '4', '2'] self.assertEquals(Ifc.choice_str('', strings), 1) Ifc.inputs = ['0'] self.assertEquals(Ifc.choice_str('', strings, cancel=True), -1) def test_get_spe(self): for string in ('hamster', 'Hamster', 'HAMSTER', 'HaMsTeR'): self.assert_(Ifc.get_spe(string) is Species.hamster) self.assertEquals(Ifc.get_spe('This should never work.'), None) def test_gnsew(self): node = Nodes.PlainNode() Ifc.inputs = ['n', 's', 'e', 'w', '0'] self.assertEquals(Ifc.gnsew('', node), 0) nodes = [Nodes.PlainNode() for x in xrange(4)] node.north, node.south, node.east, node.west = nodes Ifc.inputs = ['n', 's', 'e', 'w', '0'] for n in nodes: self.assert_(Ifc.gnsew('', node) is n) self.assertEquals(Ifc.gnsew('', node), 0) tests = ( FormatterTest, InputTest, )