<s> """<STR_LIT>""" <EOL> import unittest <EOL> import sys <EOL> import os . path <EOL> from pyamf . util import imports <EOL> class InstalledTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_installed ( self ) : <EOL> f = imports . finder <EOL> self . assertTrue ( f in sys . meta_path ) <EOL> self . assertIdentical ( sys . meta_path [ <NUM_LIT:0> ] , f ) <EOL> class ImportsTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . finder = imports . finder <EOL> self . _state = self . finder . __getstate__ ( ) <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> sys . path . insert ( <NUM_LIT:0> , path ) <EOL> def tearDown ( self ) : <EOL> self . finder . __setstate__ ( self . _state ) <EOL> del sys . path [ <NUM_LIT:0> ] <EOL> self . _clearModules ( '<STR_LIT>' ) <EOL> def _clearModules ( self , * args ) : <EOL> for mod in args : <EOL> for k , v in sys . modules . copy ( ) . iteritems ( ) : <EOL> if k . startswith ( mod ) or k == '<STR_LIT>' % ( mod , ) : <EOL> del sys . modules [ k ] <EOL> class WhenImportedTestCase ( ImportsTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> ImportsTestCase . setUp ( self ) <EOL> self . executed = False <EOL> def _hook ( self , module ) : <EOL> self . executed = True <EOL> def _check_module ( self , mod ) : <EOL> name = mod . __name__ <EOL> self . assertTrue ( name in sys . modules ) <EOL> self . assertIdentical ( sys . modules [ name ] , mod ) <EOL> def test_import ( self ) : <EOL> imports . when_imported ( '<STR_LIT>' , self . _hook ) <EOL> self . assertFalse ( self . executed ) <EOL> import spam <EOL> self . _check_module ( spam ) <EOL> self . assertTrue ( self . executed ) <EOL> def test_already_imported ( self ) : <EOL> import spam <EOL> self . assertFalse ( self . executed ) <EOL> imports . when_imported ( '<STR_LIT>' , self . _hook ) <EOL> self . _check_module ( spam ) <EOL> self . assertTrue ( self . executed ) <EOL> def test_failed_hook ( self ) : <EOL> def h ( mod ) : <EOL> raise RuntimeError <EOL> imports . when_imported ( '<STR_LIT>' , h ) <EOL> try : <EOL> import spam <EOL> except Exception , e : <EOL> pass <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> self . assertFalse ( '<STR_LIT>' in self . finder . loaded_modules ) <EOL> self . assertEqual ( e . __class__ , RuntimeError ) </s>
