<s> import gc <EOL> import StringIO , sys , types <EOL> from twisted . trial import unittest , runner <EOL> from twisted . scripts import trial <EOL> from twisted . python import util <EOL> from twisted . python . compat import set <EOL> from twisted . python . filepath import FilePath <EOL> from twisted . trial . test . test_loader import testNames <EOL> pyunit = __import__ ( '<STR_LIT>' ) <EOL> def sibpath ( filename ) : <EOL> """<STR_LIT>""" <EOL> return util . sibpath ( __file__ , filename ) <EOL> class ForceGarbageCollection ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . config = trial . Options ( ) <EOL> self . log = [ ] <EOL> self . patch ( gc , '<STR_LIT>' , self . collect ) <EOL> test = pyunit . FunctionTestCase ( self . simpleTest ) <EOL> self . test = runner . TestSuite ( [ test , test ] ) <EOL> def simpleTest ( self ) : <EOL> """<STR_LIT>""" <EOL> self . log . append ( '<STR_LIT:test>' ) <EOL> def collect ( self ) : <EOL> """<STR_LIT>""" <EOL> self . log . append ( '<STR_LIT>' ) <EOL> def makeRunner ( self ) : <EOL> """<STR_LIT>""" <EOL> runner = trial . _makeRunner ( self . config ) <EOL> runner . stream = StringIO . StringIO ( ) <EOL> return runner <EOL> def test_forceGc ( self ) : <EOL> """<STR_LIT>""" <EOL> self . config [ '<STR_LIT>' ] = True <EOL> self . config . postOptions ( ) <EOL> runner = self . makeRunner ( ) <EOL> runner . run ( self . test ) <EOL> self . assertEqual ( self . log , [ '<STR_LIT>' , '<STR_LIT:test>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:test>' , '<STR_LIT>' ] ) <EOL> def test_unforceGc ( self ) : <EOL> """<STR_LIT>""" <EOL> self . config . postOptions ( ) <EOL> runner = self . makeRunner ( ) <EOL> runner . run ( self . test ) <EOL> self . assertEqual ( self . log , [ '<STR_LIT:test>' , '<STR_LIT:test>' ] ) <EOL> class TestSuiteUsed ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . config = trial . Options ( ) <EOL> def test_defaultSuite ( self ) : <EOL> """<STR_LIT>""" <EOL> loader = trial . _getLoader ( self . config ) <EOL> self . assertEquals ( loader . suiteFactory , runner . DestructiveTestSuite ) <EOL> def test_untilFailureSuite ( self ) : <EOL> """<STR_LIT>""" <EOL> self . config [ '<STR_LIT>' ] = True <EOL> loader = trial . _getLoader ( self . config ) <EOL> self . assertEquals ( loader . suiteFactory , runner . TestSuite ) <EOL> class TestModuleTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . config = trial . Options ( ) <EOL> def tearDown ( self ) : <EOL> self . config = None <EOL> def test_testNames ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( testNames ( self ) , [ self . id ( ) ] ) <EOL> def assertSuitesEqual ( self , test1 , names ) : <EOL> loader = runner . TestLoader ( ) <EOL> names1 = testNames ( test1 ) <EOL> names2 = testNames ( runner . TestSuite ( map ( loader . loadByName , names ) ) ) <EOL> names1 . sort ( ) <EOL> names2 . sort ( ) <EOL> self . assertEqual ( names1 , names2 ) <EOL> def test_baseState ( self ) : <EOL> self . failUnlessEqual ( <NUM_LIT:0> , len ( self . config [ '<STR_LIT>' ] ) ) <EOL> def test_testmoduleOnModule ( self ) : <EOL> """<STR_LIT>""" <EOL> self . config . opt_testmodule ( sibpath ( '<STR_LIT>' ) ) <EOL> self . assertSuitesEqual ( trial . _getSuite ( self . config ) , <EOL> [ '<STR_LIT>' ] ) <EOL> def test_testmoduleTwice ( self ) : <EOL> """<STR_LIT>""" <EOL> self . config . opt_testmodule ( sibpath ( '<STR_LIT>' ) ) <EOL> self . config . opt_testmodule ( sibpath ( '<STR_LIT>' ) ) <EOL> self . assertSuitesEqual ( trial . _getSuite ( self . config ) , <EOL> [ '<STR_LIT>' ] ) <EOL> def test_testmoduleOnSourceAndTarget ( self ) : <EOL> """<STR_LIT>""" <EOL> self . config . opt_testmodule ( sibpath ( '<STR_LIT>' ) ) <EOL> self . config . opt_testmodule ( sibpath ( '<STR_LIT>' ) ) <EOL> self . assertSuitesEqual ( trial . _getSuite ( self . config ) , <EOL> [ '<STR_LIT>' ] ) <EOL> def test_testmoduleOnSelfModule ( self ) : <EOL> """<STR_LIT>""" <EOL> self . config . opt_testmodule ( sibpath ( '<STR_LIT>' ) ) <EOL> self . assertSuitesEqual ( trial . _getSuite ( self . config ) , <EOL> [ '<STR_LIT>' ] ) <EOL> def test_testmoduleOnScript ( self ) : <EOL> """<STR_LIT>""" <EOL> self . config . opt_testmodule ( sibpath ( '<STR_LIT>' ) ) <EOL> self . assertSuitesEqual ( trial . _getSuite ( self . config ) , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> def test_testmoduleOnNonexistentFile ( self ) : <EOL> """<STR_LIT>""" <EOL> buffy = StringIO . StringIO ( ) <EOL> stderr , sys . stderr = sys . stderr , buffy <EOL> filename = '<STR_LIT>' <EOL> try : <EOL> self . config . opt_testmodule ( filename ) <EOL> self . failUnlessEqual ( <NUM_LIT:0> , len ( self . config [ '<STR_LIT>' ] ) ) <EOL> self . failUnlessEqual ( "<STR_LIT>" % ( filename , ) , <EOL> buffy . getvalue ( ) ) <EOL> finally : <EOL> sys . stderr = stderr <EOL> def test_testmoduleOnEmptyVars ( self ) : <EOL> """<STR_LIT>""" <EOL> self . config . opt_testmodule ( sibpath ( '<STR_LIT>' ) ) <EOL> self . failUnlessEqual ( <NUM_LIT:0> , len ( self . config [ '<STR_LIT>' ] ) ) <EOL> def test_testmoduleOnModuleName ( self ) : <EOL> """<STR_LIT>""" <EOL> buffy = StringIO . StringIO ( ) <EOL> stderr , sys . stderr = sys . stderr , buffy <EOL> moduleName = '<STR_LIT>' <EOL> try : <EOL> self . config . opt_testmodule ( moduleName ) <EOL> self . failUnlessEqual ( <NUM_LIT:0> , len ( self . config [ '<STR_LIT>' ] ) ) <EOL> self . failUnlessEqual ( "<STR_LIT>" % ( moduleName , ) , <EOL> buffy . getvalue ( ) ) <EOL> finally : <EOL> sys . stderr = stderr <EOL> def test_parseLocalVariable ( self ) : <EOL> declaration = '<STR_LIT>' <EOL> localVars = trial . _parseLocalVariables ( declaration ) <EOL> self . failUnlessEqual ( { '<STR_LIT>' : <EOL> '<STR_LIT>' } , <EOL> localVars ) <EOL> def test_trailingSemicolon ( self ) : <EOL> declaration = '<STR_LIT>' <EOL> localVars = trial . _parseLocalVariables ( declaration ) <EOL> self . failUnlessEqual ( { '<STR_LIT>' : <EOL> '<STR_LIT>' } , <EOL> localVars ) <EOL> def test_parseLocalVariables ( self ) : <EOL> declaration = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> localVars = trial . _parseLocalVariables ( declaration ) <EOL> self . failUnlessEqual ( { '<STR_LIT>' : <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:foo>' : '<STR_LIT:bar>' } , <EOL> localVars ) <EOL> def test_surroundingGuff ( self ) : <EOL> declaration = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> localVars = trial . _parseLocalVariables ( declaration ) <EOL> self . failUnlessEqual ( { '<STR_LIT>' : <EOL> '<STR_LIT>' } , <EOL> localVars ) <EOL> def test_invalidLine ( self ) : <EOL> self . failUnlessRaises ( ValueError , trial . _parseLocalVariables , <EOL> '<STR_LIT:foo>' ) <EOL> def test_invalidDeclaration ( self ) : <EOL> self . failUnlessRaises ( ValueError , trial . _parseLocalVariables , <EOL> '<STR_LIT>' ) <EOL> self . failUnlessRaises ( ValueError , trial . _parseLocalVariables , <EOL> '<STR_LIT>' ) <EOL> self . failUnlessRaises ( ValueError , trial . _parseLocalVariables , <EOL> '<STR_LIT>' ) <EOL> def test_variablesFromFile ( self ) : <EOL> localVars = trial . loadLocalVariables ( sibpath ( '<STR_LIT>' ) ) <EOL> self . failUnlessEqual ( { '<STR_LIT>' : <EOL> '<STR_LIT>' } , <EOL> localVars ) <EOL> def test_noVariablesInFile ( self ) : <EOL> localVars = trial . loadLocalVariables ( sibpath ( '<STR_LIT>' ) ) <EOL> self . failUnlessEqual ( { } , localVars ) <EOL> def test_variablesFromScript ( self ) : <EOL> localVars = trial . loadLocalVariables ( sibpath ( '<STR_LIT>' ) ) <EOL> self . failUnlessEqual ( <EOL> { '<STR_LIT>' : ( '<STR_LIT>' <EOL> '<STR_LIT>' ) } , <EOL> localVars ) <EOL> def test_getTestModules ( self ) : <EOL> modules = trial . getTestModules ( sibpath ( '<STR_LIT>' ) ) <EOL> self . failUnlessEqual ( modules , [ '<STR_LIT>' ] ) <EOL> def test_getTestModules_noVars ( self ) : <EOL> modules = trial . getTestModules ( sibpath ( '<STR_LIT>' ) ) <EOL> self . failUnlessEqual ( len ( modules ) , <NUM_LIT:0> ) <EOL> def test_getTestModules_multiple ( self ) : <EOL> modules = trial . getTestModules ( sibpath ( '<STR_LIT>' ) ) <EOL> self . failUnlessEqual ( set ( modules ) , <EOL> set ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) <EOL> def test_looksLikeTestModule ( self ) : <EOL> for filename in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . failUnless ( trial . isTestFile ( filename ) , <EOL> "<STR_LIT>" % ( filename , ) ) <EOL> for filename in [ '<STR_LIT>' , <EOL> sibpath ( '<STR_LIT>' ) , sibpath ( '<STR_LIT>' ) ] : <EOL> self . failIf ( trial . isTestFile ( filename ) , <EOL> "<STR_LIT>" % ( filename , ) ) <EOL> class WithoutModuleTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . config = trial . Options ( ) <EOL> self . savedModules = dict ( sys . modules ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> for module in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if module in self . savedModules : <EOL> sys . modules [ module ] = self . savedModules [ module ] <EOL> else : <EOL> sys . modules . pop ( module , None ) <EOL> def _checkSMTP ( self ) : <EOL> """<STR_LIT>""" <EOL> import smtplib <EOL> return smtplib <EOL> def _checkIMAP ( self ) : <EOL> """<STR_LIT>""" <EOL> import imaplib <EOL> return imaplib <EOL> def test_disableOneModule ( self ) : <EOL> """<STR_LIT>""" <EOL> self . config . parseOptions ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> self . assertRaises ( ImportError , self . _checkSMTP ) <EOL> del sys . modules [ "<STR_LIT>" ] <EOL> self . assertIsInstance ( self . _checkSMTP ( ) , types . ModuleType ) <EOL> def test_disableMultipleModules ( self ) : <EOL> """<STR_LIT>""" <EOL> self . config . parseOptions ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> self . assertRaises ( ImportError , self . _checkSMTP ) <EOL> self . assertRaises ( ImportError , self . _checkIMAP ) <EOL> del sys . modules [ "<STR_LIT>" ] <EOL> del sys . modules [ "<STR_LIT>" ] <EOL> self . assertIsInstance ( self . _checkSMTP ( ) , types . ModuleType ) <EOL> self . assertIsInstance ( self . _checkIMAP ( ) , types . ModuleType ) <EOL> def test_disableAlreadyImportedModule ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertIsInstance ( self . _checkSMTP ( ) , types . ModuleType ) <EOL> self . assertWarns ( RuntimeWarning , <EOL> "<STR_LIT>" , <EOL> trial . __file__ , <EOL> self . config . parseOptions , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> self . assertRaises ( ImportError , self . _checkSMTP ) <EOL> class CoverageTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> if getattr ( sys , '<STR_LIT>' , None ) is None : <EOL> skip = ( <EOL> "<STR_LIT>" ) <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . addCleanup ( sys . settrace , sys . gettrace ( ) ) <EOL> def test_tracerInstalled ( self ) : <EOL> """<STR_LIT>""" <EOL> options = trial . Options ( ) <EOL> options . parseOptions ( [ "<STR_LIT>" ] ) <EOL> self . assertEquals ( sys . gettrace ( ) , options . tracer . globaltrace ) <EOL> def test_coverdirDefault ( self ) : <EOL> """<STR_LIT>""" <EOL> options = trial . Options ( ) <EOL> self . assertEquals ( <EOL> options . coverdir ( ) , <EOL> FilePath ( "<STR_LIT:.>" ) . descendant ( [ options [ "<STR_LIT>" ] , "<STR_LIT>" ] ) ) <EOL> def test_coverdirOverridden ( self ) : <EOL> """<STR_LIT>""" <EOL> path = self . mktemp ( ) <EOL> options = trial . Options ( ) <EOL> options . parseOptions ( [ "<STR_LIT>" , path ] ) <EOL> self . assertEquals ( <EOL> options . coverdir ( ) , FilePath ( path ) . child ( "<STR_LIT>" ) ) </s>
