~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lazy_import.py

  • Committer: John Arbash Meinel
  • Date: 2007-04-12 19:02:36 UTC
  • mto: This revision was merged to the branch mainline in revision 2566.
  • Revision ID: john@arbash-meinel.com-20070412190236-9izxi311ng5zofvh
Update lazy_import with tests for the new '_should_proxy' variable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
        
326
335
        self.assertRaises(errors.IllegalUseOfScopeReplacer,
327
336
                          getattr, test_obj3, 'foo')
328
337
        
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',
 
338
        self.assertEqual([('__getattribute__', 'foo'),
 
339
                          '_replace',
 
340
                          'factory',
 
341
                          'init',
 
342
                          ('foo', 1),
 
343
                          ('foo', 2),
 
344
                          ('foo', 3),
 
345
                          ('__getattribute__', 'foo'),
 
346
                          '_replace',
 
347
                         ], actions)
 
348
 
 
349
    def test_enable_proxying(self):
 
350
        """Test that we can allow ScopeReplacer to proxy."""
 
351
        actions = []
 
352
        InstrumentedReplacer.use_actions(actions)
 
353
        TestClass.use_actions(actions)
 
354
 
 
355
        def factory(replacer, scope, name):
 
356
            actions.append('factory')
 
357
            return TestClass()
 
358
 
 
359
        try:
 
360
            test_obj4
 
361
        except NameError:
 
362
            # test_obj4 shouldn't exist yet
 
363
            pass
 
364
        else:
 
365
            self.fail('test_obj4 was not supposed to exist yet')
 
366
 
 
367
        lazy_import.ScopeReplacer._should_proxy = True
 
368
        InstrumentedReplacer(scope=globals(), name='test_obj4',
 
369
                             factory=factory)
 
370
 
 
371
        self.assertEqual(InstrumentedReplacer,
 
372
                         object.__getattribute__(test_obj4, '__class__'))
 
373
        test_obj5 = test_obj4
 
374
        self.assertEqual(InstrumentedReplacer,
 
375
                         object.__getattribute__(test_obj4, '__class__'))
 
376
        self.assertEqual(InstrumentedReplacer,
 
377
                         object.__getattribute__(test_obj5, '__class__'))
 
378
 
 
379
        # The first use of the alternate variable causes test_obj2 to
 
380
        # be replaced.
 
381
        self.assertEqual('foo', test_obj4.foo(1))
 
382
        self.assertEqual(TestClass,
 
383
                         object.__getattribute__(test_obj4, '__class__'))
 
384
        self.assertEqual(InstrumentedReplacer,
 
385
                         object.__getattribute__(test_obj5, '__class__'))
 
386
        # We should be able to access test_obj4 attributes normally
 
387
        self.assertEqual('foo', test_obj4.foo(2))
 
388
        # because we enabled proxying, test_obj5 can access its members as well
 
389
        self.assertEqual('foo', test_obj5.foo(3))
 
390
        self.assertEqual('foo', test_obj5.foo(4))
 
391
 
 
392
        # However, it cannot be replaced by the ScopeReplacer
 
393
        self.assertEqual(InstrumentedReplacer,
 
394
                         object.__getattribute__(test_obj5, '__class__'))
 
395
 
 
396
        self.assertEqual([('__getattribute__', 'foo'),
 
397
                          '_replace',
 
398
                          'factory',
 
399
                          'init',
 
400
                          ('foo', 1),
 
401
                          ('foo', 2),
 
402
                          ('__getattribute__', 'foo'),
 
403
                          ('foo', 3),
 
404
                          ('__getattribute__', 'foo'),
 
405
                          ('foo', 4),
339
406
                         ], actions)
340
407
 
341
408