227
def test_other_variable(self):
228
"""Test when a ScopeReplacer is assigned to another variable.
230
This test could be updated if we find a way to trap '=' rather
231
than just giving a belated exception.
232
ScopeReplacer only knows about the variable it was created as,
233
so until the object is replaced, it is illegal to pass it to
234
another variable. (Though discovering this may take a while)
237
InstrumentedReplacer.use_actions(actions)
238
TestClass.use_actions(actions)
240
def factory(replacer, scope, name):
241
actions.append('factory')
247
# test_obj2 shouldn't exist yet
250
self.fail('test_obj2 was not supposed to exist yet')
252
InstrumentedReplacer(scope=globals(), name='test_obj2',
255
self.assertEqual(InstrumentedReplacer,
256
object.__getattribute__(test_obj2, '__class__'))
257
# This is technically not allowed, but we don't have a way to
258
# test it until later.
259
test_obj3 = test_obj2
260
self.assertEqual(InstrumentedReplacer,
261
object.__getattribute__(test_obj2, '__class__'))
262
self.assertEqual(InstrumentedReplacer,
263
object.__getattribute__(test_obj3, '__class__'))
265
# The first use of the alternate variable causes test_obj2 to
267
self.assertEqual('foo', test_obj3.foo(1))
268
# test_obj2 has been replaced, but the ScopeReplacer has no
270
self.assertEqual(TestClass,
271
object.__getattribute__(test_obj2, '__class__'))
272
self.assertEqual(InstrumentedReplacer,
273
object.__getattribute__(test_obj3, '__class__'))
274
# We should be able to access test_obj2 attributes normally
275
self.assertEqual('foo', test_obj2.foo(2))
276
self.assertEqual('foo', test_obj2.foo(3))
278
# However, the next access on test_obj3 should raise an error
279
# because only now are we able to detect the problem.
280
self.assertRaises(errors.IllegalUseOfScopeReplacer,
281
getattr, test_obj3, 'foo')
284
self.assertEqual([('__getattribute__', 'foo'),
291
('__getattribute__', 'foo'),
227
296
class ImportReplacerHelper(TestCaseInTempDir):
228
297
"""Test the ability to have a lazily imported module or object"""