69
69
self.failIf('one' in a_registry)
70
70
self.assertRaises(KeyError, a_registry.get, 'one')
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
76
a_registry['one'] = 'one'
77
self.assertRaises(AttributeError, set_one)
79
a_registry.register('one', 'one')
73
80
self.assertEqual('one', a_registry['one'])
74
81
self.assertEqual(4, len(a_registry))
89
96
self.assertEqual([2, 4, 5, 'one'],
90
97
sorted(a_registry.values()))
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)
106
a_registry.register('one', 'two', override_existing=True)
107
self.assertEqual('two', a_registry.get('one'))
109
self.assertRaises(KeyError, a_registry.register_lazy,
110
'one', 'three', 'four')
112
a_registry.register_lazy('one', 'module', 'member',
113
override_existing=True)
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
120
a_registry.register_lazy('two', 'nonexistent_module', 'member',
121
help='help text for two')
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',
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'))
137
self.assertRaises(KeyError, a_registry.get_help, None)
138
self.assertRaises(KeyError, a_registry.get_help, 'five')
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')
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',
151
# We should be able to handle a callable to get information
152
a_registry.register('three', 3, info=['a', 'list'])
154
a_registry.register_lazy('four', 'nonexistent_module', 'member2',
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'))
162
self.assertRaises(KeyError, a_registry.get_info, None)
163
self.assertRaises(KeyError, a_registry.get_info, 'five')
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')
93
170
class TestRegistryWithDirs(TestCaseInTempDir):
94
171
"""Registry tests that require temporary dirs"""