def setUp(self): data = {'rooms': {'bedroom': {'start': True, 'exits': {'south': 'kitchen'}, }, 'kitchen': {'exits': {'north': 'bedroom'}}, }, 'nouns': {'wallet': {'locs': ['INVENTORY'], 'words': ['wallet'], 'tags': ['movable'], }, 'money': {'locs': ['wallet'], 'desc': "You're rich!", 'notes': ['pass'], 'tags': ['movable'], }, 'hat': {'locs': ['WORN'], 'words': ['wallet'], 'tags': ['movable', 'wearable'], }, 'blender': {'locs': ['kitchen'], 'words': ['blender'], }, 'window': {'locs': ['bedroom', 'kitchen']}, 'nothing': {}, }, 'vars': {'one': 1, 'two': 2}, 'messages': {'pass': '******'}, 'words': [['north', 'n'], ['south', 's']] } self.state = State(GameData(data)) self.tests = Tests(self.state)
def setUp(self): data = GameData({'rooms': {'start': {'start': True, 'desc': 'The starting room.', 'notes': ['pass', 'fail'], 'exits': {'south': 'finish'}, }, 'finish': {}, }, 'nouns': {'window': {'name': 'A window.', 'shortname': 'window', 'shortdesc': 'You see a window.', 'desc': 'Made of glass.', 'notes': ['pass', 'fail'], 'locs': ['start', 'finish'], 'tags': ['visible'], }, 'bowl': {'name': 'A bowl.', 'shortname': 'bowl', 'shortdesc': 'A bowl is here.', 'desc': 'The bowl is red.', 'locs': ['finish'], 'tags': ['visible'], }, 'apple': {'name': 'An apple.', 'shortname': 'apple', 'locs': ['bowl'], 'tags': ['visible'], }, 'worm': {'name': 'A worm.', 'locs': ['apple'], 'tags': ['visible'], }, 'money': {'name': 'Some money.', 'locs': ['INVENTORY'], 'tags': ['visible'], }, 'hat': {'name': 'A hat.', 'locs': ['WORN'], 'tags': ['visible'], }, 'unicorn': {}, 'invisible': {'name': 'An invisible item.', 'locs': ['start'], }, }, 'vars': {'one': 1, 'two': 2}, 'messages': {'pass': '******', 'fail': 'Fail', 'subword': 'Second word is %2', 'inside': ' (in the %NOUN)', 'worn': ' (worn)', }, }) self.state = State(data) self.actions = Actions(self.state) for rid, room in self.state.rooms.iteritems(): setattr(self, rid, room) for nid, noun in self.state.nouns.iteritems(): setattr(self, nid, noun)
class Test_Tests(unittest.TestCase): def setUp(self): data = {'rooms': {'bedroom': {'start': True, 'exits': {'south': 'kitchen'}, }, 'kitchen': {'exits': {'north': 'bedroom'}}, }, 'nouns': {'wallet': {'locs': ['INVENTORY'], 'words': ['wallet'], 'tags': ['movable'], }, 'money': {'locs': ['wallet'], 'desc': "You're rich!", 'notes': ['pass'], 'tags': ['movable'], }, 'hat': {'locs': ['WORN'], 'words': ['wallet'], 'tags': ['movable', 'wearable'], }, 'blender': {'locs': ['kitchen'], 'words': ['blender'], }, 'window': {'locs': ['bedroom', 'kitchen']}, 'nothing': {}, }, 'vars': {'one': 1, 'two': 2}, 'messages': {'pass': '******'}, 'words': [['north', 'n'], ['south', 's']] } self.state = State(GameData(data)) self.tests = Tests(self.state) def test_any_returns_true_for_any_selector_with_at_least_one_result(self): self.assertTrue(self.tests.any('')) self.assertTrue(self.tests.any('hat')) self.assertTrue(self.tests.any('hat|bedroom')) self.assertTrue(self.tests.any('fake|bedroom')) self.assertTrue(self.tests.any('fake|hat')) self.assertFalse(self.tests.any('fake')) def test_any_noun(self): self.assertTrue(self.tests.anynoun('')) self.assertTrue(self.tests.anynoun('hat')) self.assertTrue(self.tests.anynoun('hat|bedroom')) self.assertFalse(self.tests.anynoun('kitchen|bedroom')) self.assertFalse(self.tests.anynoun('fake')) def test_any_room(self): self.assertTrue(self.tests.anyroom('')) self.assertTrue(self.tests.anyroom('kitchen')) self.assertTrue(self.tests.anyroom('hat|bedroom')) self.assertFalse(self.tests.anyroom('hat|blender')) self.assertFalse(self.tests.anyroom('INVENTORY')) self.assertFalse(self.tests.anyroom('fake')) def test_any_location(self): self.assertTrue(self.tests.anyloc('')) self.assertTrue(self.tests.anyloc('hat')) self.assertTrue(self.tests.anyloc('hat|bedroom')) self.assertTrue(self.tests.anyloc('fake|bedroom')) self.assertTrue(self.tests.anyloc('INVENTORY')) self.assertFalse(self.tests.anyloc('fake')) def test_variable_equals(self): self.assertTrue(self.tests.var('one', 1)) self.assertTrue(self.tests.var('one', '1')) self.assertTrue(self.tests.var('one', '=1')) self.assertFalse(self.tests.var('one', 2)) self.assertFalse(self.tests.var('one', '2')) def test_variable_less_than_or_greater_than(self): self.assertTrue(self.tests.var('one', '<2')) self.assertFalse(self.tests.var('one', '<0')) self.assertTrue(self.tests.var('one', '>0')) self.assertFalse(self.tests.var('one', '>2')) self.assertTrue(self.tests.var('one', '>=0')) self.assertTrue(self.tests.var('one', '>=1')) self.assertFalse(self.tests.var('one', '>=2')) self.assertFalse(self.tests.var('one', '<=0')) self.assertTrue(self.tests.var('one', '<=1')) self.assertTrue(self.tests.var('one', '<=2')) def test_room(self): self.assertTrue(self.tests.room('bedroom')) def test_visited(self): self.assertTrue(self.tests.visited('bedroom')) def test_exitexists(self): self.assertTrue(self.tests.exitexists('south')) self.assertFalse(self.tests.exitexists('north')) def test_exitexists_with_synonym(self): self.assertTrue(self.tests.exitexists('s')) self.assertFalse(self.tests.exitexists('n')) def test_exitexists_with_numerical_wildcard(self): self.state.start_turn('go south') self.assertTrue(self.tests.exitexists('%2')) self.state.start_turn('go east') self.assertFalse(self.tests.exitexists('%2')) def test_carrying_with_no_arguments_returns_true_if_carrying_anything(self): self.assertTrue(self.tests.carrying()) self.state.remove_noun(self.state.nouns['wallet'], 'INVENTORY') self.assertFalse(self.tests.carrying()) def test_wearing_with_no_arguments_returns_true_if_wearing_anything(self): self.assertTrue(self.tests.wearing()) self.state.remove_noun(self.state.nouns['hat'], 'WORN') self.assertFalse(self.tests.wearing()) def test_carrying(self): self.assertTrue(self.tests.carrying('wallet')) self.assertFalse(self.tests.carrying('blender')) def test_carrying_with_piped_filter(self): self.assertTrue(self.tests.carrying('blender|wallet')) def test_carrying_with_numerical_wildcard(self): self.state.start_turn('examine wallet') self.assertTrue(self.tests.carrying('%2')) self.state.start_turn('examine blender') self.assertFalse(self.tests.carrying('%2')) def test_nounloc(self): self.assertTrue(self.tests.nounloc('blender', 'kitchen')) self.assertFalse(self.tests.nounloc('blender', 'bedroom')) self.assertTrue(self.tests.nounloc('blender|wallet', 'kitchen|bedroom')) self.assertTrue(self.tests.nounloc('money', 'wallet')) def test_wearing(self): self.assertTrue(self.tests.wearing('hat')) self.assertFalse(self.tests.wearing('blender')) def test_inroom(self): self.assertTrue(self.tests.inroom('window')) self.assertFalse(self.tests.inroom('hat')) def test_present(self): self.assertTrue(self.tests.present('window')) self.assertTrue(self.tests.present('hat')) self.assertTrue(self.tests.present('wallet')) self.assertTrue(self.tests.present('money')) self.assertFalse(self.tests.present('blender')) def test_contained(self): self.assertTrue(self.tests.contained('money')) self.assertFalse(self.tests.contained('window')) self.assertFalse(self.tests.contained('hat')) self.assertFalse(self.tests.contained('wallet')) def test_somewhere(self): self.assertTrue(self.tests.somewhere('money')) self.assertTrue(self.tests.somewhere('window')) self.assertTrue(self.tests.somewhere('hat')) self.assertTrue(self.tests.somewhere('wallet')) self.assertFalse(self.tests.somewhere('nothing')) def test_tagged(self): self.assertTrue(self.tests.tagged('wallet', 'movable')) self.assertFalse(self.tests.tagged('window', 'movable')) self.assertTrue(self.tests.tagged('money', 'movable|wearable')) self.assertFalse(self.tests.tagged('blender', 'movable|wearable')) self.assertTrue(self.tests.tagged('money|blender', 'movable')) self.assertFalse(self.tests.tagged('money|blender', 'wearable')) def test_hasdesc(self): self.assertTrue(self.tests.hasdesc('money')) self.assertFalse(self.tests.hasdesc('wallet')) def test_hasnotes(self): self.assertTrue(self.tests.hasnotes('money')) self.assertFalse(self.tests.hasnotes('wallet')) def test_hascontents(self): self.assertTrue(self.tests.hascontents('wallet')) self.assertFalse(self.tests.hascontents('money')) def test_random(self): self.assertIn(self.tests.random(50), (True, False))
class Test_Actions(unittest.TestCase): def setUp(self): data = GameData({'rooms': {'start': {'start': True, 'desc': 'The starting room.', 'notes': ['pass', 'fail'], 'exits': {'south': 'finish'}, }, 'finish': {}, }, 'nouns': {'window': {'name': 'A window.', 'shortname': 'window', 'shortdesc': 'You see a window.', 'desc': 'Made of glass.', 'notes': ['pass', 'fail'], 'locs': ['start', 'finish'], 'tags': ['visible'], }, 'bowl': {'name': 'A bowl.', 'shortname': 'bowl', 'shortdesc': 'A bowl is here.', 'desc': 'The bowl is red.', 'locs': ['finish'], 'tags': ['visible'], }, 'apple': {'name': 'An apple.', 'shortname': 'apple', 'locs': ['bowl'], 'tags': ['visible'], }, 'worm': {'name': 'A worm.', 'locs': ['apple'], 'tags': ['visible'], }, 'money': {'name': 'Some money.', 'locs': ['INVENTORY'], 'tags': ['visible'], }, 'hat': {'name': 'A hat.', 'locs': ['WORN'], 'tags': ['visible'], }, 'unicorn': {}, 'invisible': {'name': 'An invisible item.', 'locs': ['start'], }, }, 'vars': {'one': 1, 'two': 2}, 'messages': {'pass': '******', 'fail': 'Fail', 'subword': 'Second word is %2', 'inside': ' (in the %NOUN)', 'worn': ' (worn)', }, }) self.state = State(data) self.actions = Actions(self.state) for rid, room in self.state.rooms.iteritems(): setattr(self, rid, room) for nid, noun in self.state.nouns.iteritems(): setattr(self, nid, noun) def test_message(self): self.assertEqual(self.actions.message('pass'), ['Pass']) def test_message_replaces_numerical_wildcard(self): self.state.start_turn('testing numbered words') self.assertEqual(self.actions.message('subword'), ['Second word is numbered']) def test_pause(self): self.assertEqual(self.actions.pause(), 'PAUSE') def test_showdesc(self): self.assertEqual(self.actions.showdesc('start'), ['The starting room.']) self.assertEqual(self.actions.showdesc('window'), ['Made of glass.']) self.assertEqual(self.actions.showdesc('unicorn'), []) def test_showdesc_defaults_to_current_room(self): self.assertEqual(self.actions.showdesc(), self.actions.showdesc('start')) def test_shownotes(self): self.assertEqual(self.actions.shownotes('start|window'), ['Pass', 'Fail', 'Pass', 'Fail']) def test_shownotes_defaults_to_current_room(self): self.assertEqual(self.actions.shownotes(), self.actions.shownotes('start')) def test_showcontents_lists_names(self): self.assertItemsEqual(self.actions.showcontents('finish').split('\n'), ('A window.', 'A bowl.')) def test_showcontents_filters_by_tag(self): self.assertNotIn('An invisible item.', self.actions.showcontents(tags='visible').split('\n')) def test_showcontents_defaults_to_current_room(self): self.assertEqual(self.actions.showcontents(), self.actions.showcontents('start')) def test_showcontents_lists_shortdescs(self): self.assertItemsEqual(self.actions.showcontents('finish', text='shortdesc').split('\n'), ('You see a window.', 'A bowl is here.')) def test_showcontents_lists_recursively(self): self.assertItemsEqual(self.actions.showcontents('finish', recursive=True).split('\n'), ('A window.', 'A bowl.', 'An apple.', 'A worm.')) def test_showcontents_lists_recursively_with_indent(self): self.assertItemsEqual(self.actions.showcontents('finish', recursive=True, indent=True) .split('\n'), ('A window.', 'A bowl.', '\tAn apple.', '\t\tA worm.')) def test_showcontents_lists_recursively_with_message(self): self.assertItemsEqual(self.actions.showcontents('finish', recursive=True, in_msg='inside') .split('\n'), ('A window.', 'A bowl.', 'An apple. (in the bowl)', 'A worm. (in the apple)')) def test_inv(self): self.assertItemsEqual(self.actions.inv().split('\n'), ('Some money.', 'A hat.')) def test_inv_with_worn_message(self): self.assertItemsEqual(self.actions.inv(worn_msg='worn').split('\n'), ('Some money.', 'A hat. (worn)')) def test_move(self): self.actions.move('south') self.assertEqual(self.state.current_room, self.finish) def test_move_with_numerical_wildcard(self): self.state.start_turn('go south') self.actions.move('%2') self.assertEqual(self.state.current_room, self.finish) def test_move_does_not_happen_without_matching_exit(self): self.actions.move('north') self.assertEqual(self.state.current_room, self.start) def test_destroy(self): self.actions.destroy('window') self.assertItemsEqual(self.state.noun_locs(self.window), []) def test_sendnoun(self): self.actions.sendnoun('bowl', 'start') self.assertItemsEqual(self.state.noun_locs(self.bowl), [self.start]) def test_sendtoroom(self): self.actions.sendtoroom('bowl') self.assertItemsEqual(self.state.noun_locs(self.bowl), [self.start]) def test_sendtoinv(self): self.actions.sendtoinv('bowl') self.assertItemsEqual(self.state.noun_locs(self.bowl), ['INVENTORY']) def test_wear(self): self.actions.wear('bowl') self.assertItemsEqual(self.state.noun_locs(self.bowl), ['WORN']) def test_sendtonounloc(self): self.actions.sendtonounloc('unicorn', 'bowl') self.assertItemsEqual(self.state.noun_locs(self.unicorn), [self.finish]) def test_sendtonounloc_works_for_dest_nouns_in_multiple_locations(self): self.actions.sendtonounloc('unicorn', 'window') self.assertItemsEqual(self.state.noun_locs(self.unicorn), [self.start, self.finish]) def test_sendtonoun(self): self.actions.sendtonoun('unicorn', 'bowl') self.assertItemsEqual(self.state.noun_locs(self.unicorn), [self.bowl]) def test_swapnouns(self): self.actions.swapnouns('bowl', 'window') self.assertItemsEqual(self.state.noun_locs(self.bowl), [self.start, self.finish]) self.assertItemsEqual(self.state.noun_locs(self.window), [self.finish]) def test_setnoundesc(self): self.actions.setnoundesc('bowl', 'pass') self.assertEqual(self.bowl.description, 'Pass') def test_addnounnote(self): self.actions.addnounnote('bowl', 'pass') self.assertEqual(self.bowl.notes, ['pass']) def test_addnounnote_with_multiple_notes(self): self.actions.addnounnote('bowl', 'pass', 'fail') self.assertEqual(self.bowl.notes, ['pass', 'fail']) def test_removenounnote(self): self.actions.removenounnote('window', 'pass') self.assertEqual(self.window.notes, ['fail']) def test_clearnounnotes(self): self.actions.clearnounnotes('window') self.assertEqual(self.window.notes, []) def test_setroomdesc(self): self.actions.setroomdesc('start', 'pass') self.assertEqual(self.start.description, 'Pass') def test_addroomnote(self): self.actions.addroomnote('start', 'pass') self.assertEqual(self.start.notes, ['pass', 'fail', 'pass']) def test_addroomnote_with_multiple_notes(self): self.actions.addroomnote('start', 'pass', 'fail') self.assertEqual(self.start.notes, ['pass', 'fail', 'pass', 'fail']) def test_removeroomnote(self): self.actions.removeroomnote('start', 'pass') self.assertEqual(self.start.notes, ['fail']) def test_clearroomnotes(self): self.actions.clearroomnotes('start') self.assertEqual(self.start.notes, []) def test_setvar_on_existing_variable(self): self.actions.setvar('one', 100) self.assertEqual(self.state.vars['one'], 100) def test_setvar_on_nonexisting_variable(self): self.actions.setvar('ten', 10) self.assertEqual(self.state.vars['ten'], 10) def test_setvar_casts_string_value_to_int(self): self.actions.setvar('one', '100') self.assertEqual(self.state.vars['one'], 100) def test_adjustvar_with_integer_adds(self): self.actions.adjustvar('one', 2) self.assertEqual(self.state.vars['one'], 3) def test_adjustvar_with_string_and_no_operator_adds(self): self.actions.adjustvar('two', '2') self.assertEqual(self.state.vars['two'], 4) def test_adjustvar_with_plus_adds(self): self.actions.adjustvar('one', '+2') self.assertEqual(self.state.vars['one'], 3) def test_adjustvar_with_minus_subtracts(self): self.actions.adjustvar('one', '-2') self.assertEqual(self.state.vars['one'], -1) def test_adjustvar_with_x_multiplies(self): self.actions.adjustvar('two', 'x2') self.assertEqual(self.state.vars['two'], 4) def test_adjustvar_with_slash_divides(self): self.actions.adjustvar('two', '/2') self.assertEqual(self.state.vars['two'], 1)