1
# Copyright (C) 2006 by Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Test the lazy_import functionality."""
23
from bzrlib.tests import TestCase, TestCaseInTempDir
26
class InstrumentedReplacer(lazy_import.ScopeReplacer):
27
"""Track what actions are done"""
30
def use_actions(actions):
31
InstrumentedReplacer.actions = actions
34
InstrumentedReplacer.actions.append('_replace')
35
return lazy_import.ScopeReplacer._replace(self)
37
def __getattribute__(self, attr):
38
InstrumentedReplacer.actions.append(('__getattribute__', attr))
39
return lazy_import.ScopeReplacer.__getattribute__(self, attr)
41
def __call__(self, *args, **kwargs):
42
InstrumentedReplacer.actions.append(('__call__', args, kwargs))
43
return lazy_import.ScopeReplacer.__call__(self, *args, **kwargs)
46
class TestClass(object):
47
"""Just a simple test class instrumented for the test cases"""
51
class_member = 'class_member'
54
def use_actions(actions):
55
TestClass.actions = actions
58
TestClass.actions.append('init')
61
TestClass.actions.append(('foo', x))
65
class TestScopeReplacer(TestCase):
66
"""Test the ability of the replacer to put itself into the correct scope.
68
In these tests we use the global scope, because we cannot replace
69
variables in the local scope. This means that we need to be careful
70
and not have the replacing objects use the same name, or we would
74
def test_object(self):
77
InstrumentedReplacer.use_actions(actions)
78
TestClass.use_actions(actions)
80
def factory(replacer, scope, name):
81
actions.append('factory')
87
# test_obj1 shouldn't exist yet
90
self.fail('test_obj1 was not supposed to exist yet')
92
InstrumentedReplacer(scope=globals(), name='test_obj1',
95
# We can't use isinstance() because that uses test_obj1.__class__
96
# and that goes through __getattribute__ which would activate
98
self.assertEqual(InstrumentedReplacer,
99
object.__getattribute__(test_obj1, '__class__'))
100
self.assertEqual('foo', test_obj1.foo(1))
101
self.assertIsInstance(test_obj1, TestClass)
102
self.assertEqual('foo', test_obj1.foo(2))
103
self.assertEqual([('__getattribute__', 'foo'),
111
def test_class(self):
113
InstrumentedReplacer.use_actions(actions)
114
TestClass.use_actions(actions)
116
def factory(replacer, scope, name):
117
actions.append('factory')
123
# test_class2 shouldn't exist yet
126
self.fail('test_class1 was not supposed to exist yet')
128
InstrumentedReplacer(scope=globals(), name='test_class1',
131
self.assertEqual('class_member', test_class1.class_member)
132
self.assertEqual(test_class1, TestClass)
133
self.assertEqual([('__getattribute__', 'class_member'),
138
def test_call_class(self):
140
InstrumentedReplacer.use_actions(actions)
141
TestClass.use_actions(actions)
143
def factory(replacer, scope, name):
144
actions.append('factory')
150
# test_class2 shouldn't exist yet
153
self.fail('test_class2 was not supposed to exist yet')
155
InstrumentedReplacer(scope=globals(), name='test_class2',
158
self.failIf(test_class2 is TestClass)
160
self.assertIs(test_class2, TestClass)
161
self.assertIsInstance(obj, TestClass)
162
self.assertEqual('class_member', obj.class_member)
163
self.assertEqual([('__call__', (), {}),
169
def test_call_func(self):
171
InstrumentedReplacer.use_actions(actions)
173
def func(a, b, c=None):
174
actions.append('func')
177
def factory(replacer, scope, name):
178
actions.append('factory')
184
# test_func1 shouldn't exist yet
187
self.fail('test_func1 was not supposed to exist yet')
188
InstrumentedReplacer(scope=globals(), name='test_func1',
191
self.failIf(test_func1 is func)
192
val = test_func1(1, 2, c='3')
193
self.assertIs(test_func1, func)
195
self.assertEqual((1,2,'3'), val)
196
self.assertEqual([('__call__', (1,2), {'c':'3'}),
202
class TestImportReplacer(TestCaseInTempDir):
203
"""Test the ability to have a lazily imported module or object"""