/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 bzrlib/tests/test_lazy_import.py

First attempt to merge .dev and resolve the conflicts (but tests are 
failing)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 by Canonical Ltd
 
1
# Copyright (C) 2006 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
96
96
    get collisions.
97
97
    """
98
98
 
 
99
    def setUp(self):
 
100
        TestCase.setUp(self)
 
101
        # These tests assume we will not be proxying, so make sure proxying is
 
102
        # disabled.
 
103
        orig_proxy = lazy_import.ScopeReplacer._should_proxy
 
104
        def restore():
 
105
            lazy_import.ScopeReplacer._should_proxy = orig_proxy
 
106
        lazy_import.ScopeReplacer._should_proxy = False
 
107
 
99
108
    def test_object(self):
100
109
        """ScopeReplacer can create an instance in local scope.
101
110
        
141
150
                          ('foo', 2),
142
151
                         ], actions)
143
152
 
 
153
    def test_setattr_replaces(self):
 
154
        """ScopeReplacer can create an instance in local scope.
 
155
 
 
156
        An object should appear in globals() by constructing a ScopeReplacer,
 
157
        and it will be replaced with the real object upon the first request.
 
158
        """
 
159
        actions = []
 
160
        TestClass.use_actions(actions)
 
161
        def factory(replacer, scope, name):
 
162
            return TestClass()
 
163
        try:
 
164
            test_obj6
 
165
        except NameError:
 
166
            # test_obj6 shouldn't exist yet
 
167
            pass
 
168
        else:
 
169
            self.fail('test_obj6 was not supposed to exist yet')
 
170
 
 
171
        orig_globals = set(globals().keys())
 
172
 
 
173
        lazy_import.ScopeReplacer(scope=globals(), name='test_obj6',
 
174
                                  factory=factory)
 
175
 
 
176
        new_globals = set(globals().keys())
 
177
 
 
178
        # We can't use isinstance() because that uses test_obj6.__class__
 
179
        # and that goes through __getattribute__ which would activate
 
180
        # the replacement
 
181
        self.assertEqual(lazy_import.ScopeReplacer,
 
182
                         object.__getattribute__(test_obj6, '__class__'))
 
183
        test_obj6.bar = 'test'
 
184
        self.assertNotEqual(lazy_import.ScopeReplacer,
 
185
                            object.__getattribute__(test_obj6, '__class__'))
 
186
        self.assertEqual('test', test_obj6.bar)
 
187
 
144
188
    def test_replace_side_effects(self):
145
189
        """Creating a new object should only create one entry in globals.
146
190
 
326
370
        self.assertRaises(errors.IllegalUseOfScopeReplacer,
327
371
                          getattr, test_obj3, 'foo')
328
372
        
329
 
        # However, the 
330
 
        self.assertEqual([('__getattribute__', 'foo'),
331
 
                          '_replace',
332
 
                          'factory',
333
 
                          'init',
334
 
                          ('foo', 1),
335
 
                          ('foo', 2),
336
 
                          ('foo', 3),
337
 
                          ('__getattribute__', 'foo'),
338
 
                          '_replace',
 
373
        self.assertEqual([('__getattribute__', 'foo'),
 
374
                          '_replace',
 
375
                          'factory',
 
376
                          'init',
 
377
                          ('foo', 1),
 
378
                          ('foo', 2),
 
379
                          ('foo', 3),
 
380
                          ('__getattribute__', 'foo'),
 
381
                          '_replace',
 
382
                         ], actions)
 
383
 
 
384
    def test_enable_proxying(self):
 
385
        """Test that we can allow ScopeReplacer to proxy."""
 
386
        actions = []
 
387
        InstrumentedReplacer.use_actions(actions)
 
388
        TestClass.use_actions(actions)
 
389
 
 
390
        def factory(replacer, scope, name):
 
391
            actions.append('factory')
 
392
            return TestClass()
 
393
 
 
394
        try:
 
395
            test_obj4
 
396
        except NameError:
 
397
            # test_obj4 shouldn't exist yet
 
398
            pass
 
399
        else:
 
400
            self.fail('test_obj4 was not supposed to exist yet')
 
401
 
 
402
        lazy_import.ScopeReplacer._should_proxy = True
 
403
        InstrumentedReplacer(scope=globals(), name='test_obj4',
 
404
                             factory=factory)
 
405
 
 
406
        self.assertEqual(InstrumentedReplacer,
 
407
                         object.__getattribute__(test_obj4, '__class__'))
 
408
        test_obj5 = test_obj4
 
409
        self.assertEqual(InstrumentedReplacer,
 
410
                         object.__getattribute__(test_obj4, '__class__'))
 
411
        self.assertEqual(InstrumentedReplacer,
 
412
                         object.__getattribute__(test_obj5, '__class__'))
 
413
 
 
414
        # The first use of the alternate variable causes test_obj2 to
 
415
        # be replaced.
 
416
        self.assertEqual('foo', test_obj4.foo(1))
 
417
        self.assertEqual(TestClass,
 
418
                         object.__getattribute__(test_obj4, '__class__'))
 
419
        self.assertEqual(InstrumentedReplacer,
 
420
                         object.__getattribute__(test_obj5, '__class__'))
 
421
        # We should be able to access test_obj4 attributes normally
 
422
        self.assertEqual('foo', test_obj4.foo(2))
 
423
        # because we enabled proxying, test_obj5 can access its members as well
 
424
        self.assertEqual('foo', test_obj5.foo(3))
 
425
        self.assertEqual('foo', test_obj5.foo(4))
 
426
 
 
427
        # However, it cannot be replaced by the ScopeReplacer
 
428
        self.assertEqual(InstrumentedReplacer,
 
429
                         object.__getattribute__(test_obj5, '__class__'))
 
430
 
 
431
        self.assertEqual([('__getattribute__', 'foo'),
 
432
                          '_replace',
 
433
                          'factory',
 
434
                          'init',
 
435
                          ('foo', 1),
 
436
                          ('foo', 2),
 
437
                          ('__getattribute__', 'foo'),
 
438
                          ('foo', 3),
 
439
                          ('__getattribute__', 'foo'),
 
440
                          ('foo', 4),
339
441
                         ], actions)
340
442
 
341
443