~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_registry.py

  • Committer: Martin Pool
  • Date: 2011-02-07 01:30:38 UTC
  • mto: This revision was merged to the branch mainline in revision 5650.
  • Revision ID: mbp@canonical.com-20110207013038-kbm3fx0pemehjnk2
Use cmdline.split on BZR_EDITOR so that you can pass names including spaces.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2008-2012, 2016 Canonical Ltd
 
1
# Copyright (C) 2006, 2008-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
39
39
        a_registry = registry.Registry()
40
40
        self.register_stuff(a_registry)
41
41
 
42
 
        self.assertTrue(a_registry.default_key is None)
 
42
        self.failUnless(a_registry.default_key is None)
43
43
 
44
44
        # test get() (self.default_key is None)
45
45
        self.assertRaises(KeyError, a_registry.get)
49
49
 
50
50
        # test _set_default_key
51
51
        a_registry.default_key = 'five'
52
 
        self.assertTrue(a_registry.default_key == 'five')
 
52
        self.failUnless(a_registry.default_key == 'five')
53
53
        self.assertEqual(5, a_registry.get())
54
54
        self.assertEqual(5, a_registry.get(None))
55
55
        # If they ask for a specific entry, they should get KeyError
64
64
        a_registry = registry.Registry()
65
65
        self.register_stuff(a_registry)
66
66
 
67
 
        self.assertTrue('one' in a_registry)
 
67
        self.failUnless('one' in a_registry)
68
68
        a_registry.remove('one')
69
 
        self.assertFalse('one' in a_registry)
 
69
        self.failIf('one' in a_registry)
70
70
        self.assertRaises(KeyError, a_registry.get, 'one')
71
71
 
72
72
        a_registry.register('one', 'one')
215
215
        # We create a registry with "official" objects and "hidden"
216
216
        # objects. The later represent the side effects that led to bug #277048
217
217
        # and #430510
218
 
        _registry = registry.Registry()
 
218
        self.registry =  registry.Registry()
219
219
 
220
220
        def register_more():
221
 
           _registry.register('hidden', None)
 
221
            self.registry.register('hidden', None)
222
222
 
223
 
        # Avoid closing over self by binding local variable
224
 
        self.registry = _registry
225
223
        self.registry.register('passive', None)
226
224
        self.registry.register('active', register_more)
227
225
        self.registry.register('passive-too', None)
231
229
            def get_obj(inner_self):
232
230
                # Surprise ! Getting a registered object (think lazy loaded
233
231
                # module) register yet another object !
234
 
                _registry.register('more hidden', None)
 
232
                self.registry.register('more hidden', None)
235
233
                return inner_self._obj
236
234
 
237
235
        self.registry.register('hacky', None)
272
270
        """
273
271
        os.mkdir('tmp')
274
272
        plugin_name = 'bzr_plugin_a_%s' % (osutils.rand_chars(4),)
275
 
        with open('tmp/'+plugin_name+'.py', 'wb') as f: f.write(contents)
 
273
        open('tmp/'+plugin_name+'.py', 'wb').write(contents)
276
274
        return plugin_name
277
275
 
278
276
    def create_simple_plugin(self):
306
304
        self.assertEqual(['function', 'klass', 'module', 'obj'],
307
305
                         sorted(a_registry.keys()))
308
306
        # The plugin should not be loaded until we grab the first object
309
 
        self.assertFalse(plugin_name in sys.modules)
 
307
        self.failIf(plugin_name in sys.modules)
310
308
 
311
309
        # By default the plugin won't be in the search path
312
310
        self.assertRaises(ImportError, a_registry.get, 'obj')
316
314
        try:
317
315
            obj = a_registry.get('obj')
318
316
            self.assertEqual('foo', obj)
319
 
            self.assertTrue(plugin_name in sys.modules)
 
317
            self.failUnless(plugin_name in sys.modules)
320
318
 
321
319
            # Now grab another object
322
320
            func = a_registry.get('function')
340
338
        finally:
341
339
            sys.path.remove(plugin_path)
342
340
 
343
 
    def test_lazy_import_get_module(self):
344
 
        a_registry = registry.Registry()
345
 
        a_registry.register_lazy('obj', "bzrlib.tests.test_registry",
346
 
            'object1')
347
 
        self.assertEqual("bzrlib.tests.test_registry",
348
 
            a_registry._get_module("obj"))
349
 
 
350
 
    def test_normal_get_module(self):
351
 
        class AThing(object):
352
 
            """Something"""
353
 
        a_registry = registry.Registry()
354
 
        a_registry.register("obj", AThing())
355
 
        self.assertEqual("bzrlib.tests.test_registry",
356
 
            a_registry._get_module("obj"))