/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_registry.py

  • Committer: Jelmer Vernooij
  • Date: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
    )
28
28
 
29
29
 
 
30
 
30
31
class TestRegistry(tests.TestCase):
31
32
 
32
33
    def register_stuff(self, a_registry):
82
83
        a_registry.register('one', 'one')
83
84
        self.assertRaises(KeyError, a_registry.register, 'one', 'two')
84
85
        self.assertRaises(KeyError, a_registry.register, 'one', 'two',
85
 
                                    override_existing=False)
 
86
                          override_existing=False)
86
87
 
87
88
        a_registry.register('one', 'two', override_existing=True)
88
89
        self.assertEqual('two', a_registry.get('one'))
103
104
 
104
105
        # We should be able to handle a callable to get information
105
106
        help_calls = []
 
107
 
106
108
        def generic_help(reg, key):
107
109
            help_calls.append(key)
108
110
            return 'generic help for %s' % (key,)
144
146
                          ('six', 'this is my help'),
145
147
                          ('three', 'generic help for three'),
146
148
                          ('two', 'help text for two'),
147
 
                         ], sorted((key, a_registry.get_help(key))
 
149
                          ], sorted((key, a_registry.get_help(key))
148
150
                                    for key in a_registry.keys()))
149
151
 
150
152
        # We don't know what order it was called in, but we should get
184
186
                          ('one', 'string info'),
185
187
                          ('three', ['a', 'list']),
186
188
                          ('two', 2),
187
 
                         ], sorted((key, a_registry.get_info(key))
 
189
                          ], sorted((key, a_registry.get_info(key))
188
190
                                    for key in a_registry.keys()))
189
191
 
190
192
    def test_get_prefix(self):
201
203
        self.assertEqual('//baz/qux', suffix)
202
204
        self.assertIs(sftp_object, found_object)
203
205
 
 
206
    def test_registry_alias(self):
 
207
        a_registry = registry.Registry()
 
208
        a_registry.register('one', 1, info='string info')
 
209
        a_registry.register_alias('two', 'one')
 
210
        a_registry.register_alias('three', 'one', info='own info')
 
211
        self.assertEqual(a_registry.get('one'), a_registry.get('two'))
 
212
        self.assertEqual(a_registry.get_help('one'),
 
213
                         a_registry.get_help('two'))
 
214
        self.assertEqual(a_registry.get_info('one'),
 
215
                         a_registry.get_info('two'))
 
216
        self.assertEqual('own info', a_registry.get_info('three'))
 
217
        self.assertEqual({'two': 'one', 'three': 'one'}, a_registry.aliases())
 
218
        self.assertEqual(
 
219
            {'one': ['three', 'two']},
 
220
            {k: sorted(v) for (k, v) in a_registry.alias_map().items()})
 
221
 
 
222
    def test_registry_alias_exists(self):
 
223
        a_registry = registry.Registry()
 
224
        a_registry.register('one', 1, info='string info')
 
225
        a_registry.register('two', 2)
 
226
        self.assertRaises(KeyError, a_registry.register_alias, 'one', 'one')
 
227
 
 
228
    def test_registry_alias_targetmissing(self):
 
229
        a_registry = registry.Registry()
 
230
        self.assertRaises(KeyError, a_registry.register_alias, 'one', 'two')
 
231
 
204
232
 
205
233
class TestRegistryIter(tests.TestCase):
206
234
    """Test registry iteration behaviors.
218
246
        _registry = registry.Registry()
219
247
 
220
248
        def register_more():
221
 
           _registry.register('hidden', None)
 
249
            _registry.register('hidden', None)
222
250
 
223
251
        # Avoid closing over self by binding local variable
224
252
        self.registry = _registry
272
300
        """
273
301
        os.mkdir('tmp')
274
302
        plugin_name = 'bzr_plugin_a_%s' % (osutils.rand_chars(4),)
275
 
        with open('tmp/'+plugin_name+'.py', 'wb') as f: f.write(contents)
 
303
        with open('tmp/' + plugin_name + '.py', 'wb') as f:
 
304
            f.write(contents)
276
305
        return plugin_name
277
306
 
278
307
    def create_simple_plugin(self):
279
308
        return self.create_plugin_file(
280
 
            'object1 = "foo"\n'
281
 
            '\n\n'
282
 
            'def function(a,b,c):\n'
283
 
            '    return a,b,c\n'
284
 
            '\n\n'
285
 
            'class MyClass(object):\n'
286
 
            '    def __init__(self, a):\n'
287
 
            '      self.a = a\n'
288
 
            '\n\n'
 
309
            b'object1 = "foo"\n'
 
310
            b'\n\n'
 
311
            b'def function(a,b,c):\n'
 
312
            b'    return a,b,c\n'
 
313
            b'\n\n'
 
314
            b'class MyClass(object):\n'
 
315
            b'    def __init__(self, a):\n'
 
316
            b'      self.a = a\n'
 
317
            b'\n\n'
289
318
        )
290
319
 
291
320
    def test_lazy_import_registry_foo(self):
343
372
    def test_lazy_import_get_module(self):
344
373
        a_registry = registry.Registry()
345
374
        a_registry.register_lazy('obj', "breezy.tests.test_registry",
346
 
            'object1')
 
375
                                 'object1')
347
376
        self.assertEqual("breezy.tests.test_registry",
348
 
            a_registry._get_module("obj"))
 
377
                         a_registry._get_module("obj"))
349
378
 
350
379
    def test_normal_get_module(self):
351
380
        class AThing(object):
353
382
        a_registry = registry.Registry()
354
383
        a_registry.register("obj", AThing())
355
384
        self.assertEqual("breezy.tests.test_registry",
356
 
            a_registry._get_module("obj"))
 
385
                         a_registry._get_module("obj"))