99
99
def test_object(self):
100
"""ScopeReplacer can create an instance in local scope.
102
An object should appear in globals() by constructing a ScopeReplacer,
103
and it will be replaced with the real object upon the first request.
102
106
InstrumentedReplacer.use_actions(actions)
103
107
TestClass.use_actions(actions)
115
119
self.fail('test_obj1 was not supposed to exist yet')
121
orig_globals = set(globals().keys())
117
123
InstrumentedReplacer(scope=globals(), name='test_obj1',
126
new_globals = set(globals().keys())
120
128
# We can't use isinstance() because that uses test_obj1.__class__
121
129
# and that goes through __getattribute__ which would activate
122
130
# the replacement
144
def test_replace_side_effects(self):
145
"""Creating a new object should only create one entry in globals.
147
And only that entry even after replacement.
152
# test_scope1 shouldn't exist yet
155
self.fail('test_scope1 was not supposed to exist yet')
157
# ignore the logged actions
158
TestClass.use_actions([])
160
def factory(replacer, scope, name):
163
orig_globals = set(globals().keys())
165
lazy_import.ScopeReplacer(scope=globals(), name='test_scope1',
168
new_globals = set(globals().keys())
170
self.assertEqual(lazy_import.ScopeReplacer,
171
object.__getattribute__(test_scope1, '__class__'))
172
self.assertEqual('foo', test_scope1.foo(1))
173
self.assertIsInstance(test_scope1, TestClass)
175
final_globals = set(globals().keys())
177
self.assertEqual(set(['test_scope1']), new_globals - orig_globals)
178
self.assertEqual(set(), orig_globals - new_globals)
179
self.assertEqual(set(), final_globals - new_globals)
180
self.assertEqual(set(), new_globals - final_globals)
136
182
def test_class(self):
138
184
InstrumentedReplacer.use_actions(actions)