~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_registry.py

  • Committer: John Arbash Meinel
  • Date: 2006-09-09 17:30:34 UTC
  • mto: This revision was merged to the branch mainline in revision 2074.
  • Revision ID: john@arbash-meinel.com-20060909173034-fd1e7849d827dcf8
Add help and info parameters, and tests for them

Show diffs side-by-side

added added

removed removed

Lines of Context:
69
69
        self.failIf('one' in a_registry)
70
70
        self.assertRaises(KeyError, a_registry.get, 'one')
71
71
 
72
 
        a_registry['one'] = 'one'
 
72
        # We intentionally don't implement __setitem__, because
 
73
        # register() is a much richer function, that doesn't translate
 
74
        # well into foo[x] = y
 
75
        def set_one():
 
76
            a_registry['one'] = 'one'
 
77
        self.assertRaises(AttributeError, set_one)
 
78
 
 
79
        a_registry.register('one', 'one')
73
80
        self.assertEqual('one', a_registry['one'])
74
81
        self.assertEqual(4, len(a_registry))
75
82
 
89
96
        self.assertEqual([2, 4, 5, 'one'],
90
97
                         sorted(a_registry.values()))
91
98
 
 
99
    def test_register_override(self):
 
100
        a_registry = registry.Registry()
 
101
        a_registry.register('one', 'one')
 
102
        self.assertRaises(KeyError, a_registry.register, 'one', 'two')
 
103
        self.assertRaises(KeyError, a_registry.register, 'one', 'two',
 
104
                                    override_existing=False)
 
105
 
 
106
        a_registry.register('one', 'two', override_existing=True)
 
107
        self.assertEqual('two', a_registry.get('one'))
 
108
 
 
109
        self.assertRaises(KeyError, a_registry.register_lazy,
 
110
                          'one', 'three', 'four')
 
111
 
 
112
        a_registry.register_lazy('one', 'module', 'member',
 
113
                                 override_existing=True)
 
114
 
 
115
    def test_registry_help(self):
 
116
        a_registry = registry.Registry()
 
117
        a_registry.register('one', 1, help='help text for one')
 
118
        # We should not have to import the module to return the help
 
119
        # information
 
120
        a_registry.register_lazy('two', 'nonexistent_module', 'member',
 
121
                                 help='help text for two')
 
122
 
 
123
        # We should be able to handle a callable to get information
 
124
        def generic_help(key):
 
125
            return 'generic help for %s' % (key,)
 
126
        a_registry.register('three', 3, help=generic_help)
 
127
        a_registry.register_lazy('four', 'nonexistent_module', 'member2',
 
128
                                 help=generic_help)
 
129
 
 
130
        self.assertEqual('help text for one', a_registry.get_help('one'))
 
131
        self.assertEqual('help text for two', a_registry.get_help('two'))
 
132
        self.assertEqual('generic help for three',
 
133
                         a_registry.get_help('three'))
 
134
        self.assertEqual('generic help for four',
 
135
                         a_registry.get_help('four'))
 
136
 
 
137
        self.assertRaises(KeyError, a_registry.get_help, None)
 
138
        self.assertRaises(KeyError, a_registry.get_help, 'five')
 
139
 
 
140
        a_registry.default_key = 'one'
 
141
        self.assertEqual('help text for one', a_registry.get_help(None))
 
142
        self.assertRaises(KeyError, a_registry.get_help, 'five')
 
143
 
 
144
    def test_registry_info(self):
 
145
        a_registry = registry.Registry()
 
146
        a_registry.register('one', 1, info='string info')
 
147
        # We should not have to import the module to return the info
 
148
        a_registry.register_lazy('two', 'nonexistent_module', 'member',
 
149
                                 info=2)
 
150
 
 
151
        # We should be able to handle a callable to get information
 
152
        a_registry.register('three', 3, info=['a', 'list'])
 
153
        obj = object()
 
154
        a_registry.register_lazy('four', 'nonexistent_module', 'member2',
 
155
                                 info=obj)
 
156
 
 
157
        self.assertEqual('string info', a_registry.get_info('one'))
 
158
        self.assertEqual(2, a_registry.get_info('two'))
 
159
        self.assertEqual(['a', 'list'], a_registry.get_info('three'))
 
160
        self.assertIs(obj, a_registry.get_info('four'))
 
161
 
 
162
        self.assertRaises(KeyError, a_registry.get_info, None)
 
163
        self.assertRaises(KeyError, a_registry.get_info, 'five')
 
164
 
 
165
        a_registry.default_key = 'one'
 
166
        self.assertEqual('string info', a_registry.get_info(None))
 
167
        self.assertRaises(KeyError, a_registry.get_info, 'five')
 
168
 
92
169
 
93
170
class TestRegistryWithDirs(TestCaseInTempDir):
94
171
    """Registry tests that require temporary dirs"""