/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

merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Test the lazy_import functionality."""
18
18
 
107
107
 
108
108
    def test_object(self):
109
109
        """ScopeReplacer can create an instance in local scope.
110
 
        
 
110
 
111
111
        An object should appear in globals() by constructing a ScopeReplacer,
112
112
        and it will be replaced with the real object upon the first request.
113
113
        """
150
150
                          ('foo', 2),
151
151
                         ], actions)
152
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
 
153
188
    def test_replace_side_effects(self):
154
189
        """Creating a new object should only create one entry in globals.
155
190
 
316
351
                         object.__getattribute__(test_obj2, '__class__'))
317
352
        self.assertEqual(InstrumentedReplacer,
318
353
                         object.__getattribute__(test_obj3, '__class__'))
319
 
        
 
354
 
320
355
        # The first use of the alternate variable causes test_obj2 to
321
356
        # be replaced.
322
357
        self.assertEqual('foo', test_obj3.foo(1))
334
369
        # because only now are we able to detect the problem.
335
370
        self.assertRaises(errors.IllegalUseOfScopeReplacer,
336
371
                          getattr, test_obj3, 'foo')
337
 
        
 
372
 
338
373
        self.assertEqual([('__getattribute__', 'foo'),
339
374
                          '_replace',
340
375
                          'factory',