コード例 #1
0
ファイル: test_actions.py プロジェクト: saltire/advengine-tdd
    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)
コード例 #2
0
ファイル: test_actions.py プロジェクト: saltire/advengine-tdd
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)