~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: 2006-09-12 19:07:27 UTC
  • mto: This revision was merged to the branch mainline in revision 2004.
  • Revision ID: john@arbash-meinel.com-20060912190727-35ae1dff7a95b789
Write a simple wrapper function to make lazy imports easy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
920
920
 
921
921
        text = 'import %s as root6' % (self.root_name,)
922
922
        proc = lazy_import.ImportProcessor(InstrumentedImportReplacer)
923
 
        proc.lazy_import(text=text, scope=globals())
 
923
        proc.lazy_import(scope=globals(), text=text)
924
924
 
925
925
        # So 'root6' should be a lazy import
926
926
        self.assertEqual(InstrumentedImportReplacer,
953
953
import %(root_name)s.%(sub_name)s.%(submoda_name)s as submoda7
954
954
""" % self.__dict__
955
955
        proc = lazy_import.ImportProcessor(InstrumentedImportReplacer)
956
 
        proc.lazy_import(text=text, scope=globals())
 
956
        proc.lazy_import(scope=globals(), text=text)
957
957
 
958
958
        # So 'submoda7' should be a lazy import
959
959
        self.assertEqual(InstrumentedImportReplacer,
970
970
                          ('_import', 'submoda7'),
971
971
                          ('import', submoda_path, []),
972
972
                         ], self.actions)
 
973
 
 
974
    def test_lazy_import(self):
 
975
        """Smoke test that lazy_import() does the right thing"""
 
976
        try:
 
977
            root8
 
978
        except NameError:
 
979
            pass # root8 should not be defined yet
 
980
        else:
 
981
            self.fail('root8 was not supposed to exist yet')
 
982
        lazy_import.lazy_import(globals(),
 
983
                                'import %s as root8' % (self.root_name,),
 
984
                                lazy_import_class=InstrumentedImportReplacer)
 
985
 
 
986
        self.assertEqual(InstrumentedImportReplacer,
 
987
                         object.__getattribute__(root8, '__class__'))
 
988
 
 
989
        self.assertEqual(1, root8.var1)
 
990
        self.assertEqual(1, root8.var1)
 
991
        self.assertEqual(1, root8.func1(1))
 
992
 
 
993
        self.assertEqual([('__getattribute__', 'var1'),
 
994
                          '_replace',
 
995
                          ('_import', 'root8'),
 
996
                          ('import', self.root_name, []),
 
997
                         ], self.actions)