<s> import io <EOL> import os <EOL> import sys <EOL> import pickle <EOL> import subprocess <EOL> import unittest <EOL> from . support import LoggingResult , ResultWithNoStartTestRunStopTestRun <EOL> class TestCleanUp ( unittest . TestCase ) : <EOL> def testCleanUp ( self ) : <EOL> class TestableTest ( unittest . TestCase ) : <EOL> def testNothing ( self ) : <EOL> pass <EOL> test = TestableTest ( '<STR_LIT>' ) <EOL> self . assertEqual ( test . _cleanups , [ ] ) <EOL> cleanups = [ ] <EOL> def cleanup1 ( * args , ** kwargs ) : <EOL> cleanups . append ( ( <NUM_LIT:1> , args , kwargs ) ) <EOL> def cleanup2 ( * args , ** kwargs ) : <EOL> cleanups . append ( ( <NUM_LIT:2> , args , kwargs ) ) <EOL> test . addCleanup ( cleanup1 , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , four = '<STR_LIT:hello>' , five = '<STR_LIT>' ) <EOL> test . addCleanup ( cleanup2 ) <EOL> self . assertEqual ( test . _cleanups , <EOL> [ ( cleanup1 , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) , dict ( four = '<STR_LIT:hello>' , five = '<STR_LIT>' ) ) , <EOL> ( cleanup2 , ( ) , { } ) ] ) <EOL> self . assertTrue ( test . doCleanups ( ) ) <EOL> self . assertEqual ( cleanups , [ ( <NUM_LIT:2> , ( ) , { } ) , ( <NUM_LIT:1> , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) , dict ( four = '<STR_LIT:hello>' , five = '<STR_LIT>' ) ) ] ) <EOL> def testCleanUpWithErrors ( self ) : <EOL> class TestableTest ( unittest . TestCase ) : <EOL> def testNothing ( self ) : <EOL> pass <EOL> class MockOutcome ( object ) : <EOL> success = True <EOL> errors = [ ] <EOL> test = TestableTest ( '<STR_LIT>' ) <EOL> test . _outcomeForDoCleanups = MockOutcome <EOL> exc1 = Exception ( '<STR_LIT:foo>' ) <EOL> exc2 = Exception ( '<STR_LIT:bar>' ) <EOL> def cleanup1 ( ) : <EOL> raise exc1 <EOL> def cleanup2 ( ) : <EOL> raise exc2 <EOL> test . addCleanup ( cleanup1 ) <EOL> test . addCleanup ( cleanup2 ) <EOL> self . assertFalse ( test . doCleanups ( ) ) <EOL> self . assertFalse ( MockOutcome . success ) <EOL> ( Type1 , instance1 , _ ) , ( Type2 , instance2 , _ ) = reversed ( MockOutcome . errors ) <EOL> self . assertEqual ( ( Type1 , instance1 ) , ( Exception , exc1 ) ) <EOL> self . assertEqual ( ( Type2 , instance2 ) , ( Exception , exc2 ) ) <EOL> def testCleanupInRun ( self ) : <EOL> blowUp = False <EOL> ordering = [ ] <EOL> class TestableTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> ordering . append ( '<STR_LIT>' ) <EOL> if blowUp : <EOL> raise Exception ( '<STR_LIT:foo>' ) <EOL> def testNothing ( self ) : <EOL> ordering . append ( '<STR_LIT:test>' ) <EOL> def tearDown ( self ) : <EOL> ordering . append ( '<STR_LIT>' ) <EOL> test = TestableTest ( '<STR_LIT>' ) <EOL> def cleanup1 ( ) : <EOL> ordering . append ( '<STR_LIT>' ) <EOL> def cleanup2 ( ) : <EOL> ordering . append ( '<STR_LIT>' ) <EOL> test . addCleanup ( cleanup1 ) <EOL> test . addCleanup ( cleanup2 ) <EOL> def success ( some_test ) : <EOL> self . assertEqual ( some_test , test ) <EOL> ordering . append ( '<STR_LIT:success>' ) <EOL> result = unittest . TestResult ( ) <EOL> result . addSuccess = success <EOL> test . run ( result ) <EOL> self . assertEqual ( ordering , [ '<STR_LIT>' , '<STR_LIT:test>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:success>' ] ) <EOL> blowUp = True <EOL> ordering = [ ] <EOL> test = TestableTest ( '<STR_LIT>' ) <EOL> test . addCleanup ( cleanup1 ) <EOL> test . run ( result ) <EOL> self . assertEqual ( ordering , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def testTestCaseDebugExecutesCleanups ( self ) : <EOL> ordering = [ ] <EOL> class TestableTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> ordering . append ( '<STR_LIT>' ) <EOL> self . addCleanup ( cleanup1 ) <EOL> def testNothing ( self ) : <EOL> ordering . append ( '<STR_LIT:test>' ) <EOL> def tearDown ( self ) : <EOL> ordering . append ( '<STR_LIT>' ) <EOL> test = TestableTest ( '<STR_LIT>' ) <EOL> def cleanup1 ( ) : <EOL> ordering . append ( '<STR_LIT>' ) <EOL> test . addCleanup ( cleanup2 ) <EOL> def cleanup2 ( ) : <EOL> ordering . append ( '<STR_LIT>' ) <EOL> test . debug ( ) <EOL> self . assertEqual ( ordering , [ '<STR_LIT>' , '<STR_LIT:test>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> class Test_TextTestRunner ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_init ( self ) : <EOL> runner = unittest . TextTestRunner ( ) <EOL> self . assertFalse ( runner . failfast ) <EOL> self . assertFalse ( runner . buffer ) <EOL> self . assertEqual ( runner . verbosity , <NUM_LIT:1> ) <EOL> self . assertEqual ( runner . warnings , None ) <EOL> self . assertTrue ( runner . descriptions ) <EOL> self . assertEqual ( runner . resultclass , unittest . TextTestResult ) <EOL> def testBufferAndFailfast ( self ) : <EOL> class Test ( unittest . TestCase ) : <EOL> def testFoo ( self ) : <EOL> pass <EOL> result = unittest . TestResult ( ) <EOL> runner = unittest . TextTestRunner ( stream = io . StringIO ( ) , failfast = True , <EOL> buffer = True ) <EOL> runner . _makeResult = lambda : result <EOL> runner . run ( Test ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( result . failfast ) <EOL> self . assertTrue ( result . buffer ) <EOL> def testRunnerRegistersResult ( self ) : <EOL> class Test ( unittest . TestCase ) : <EOL> def testFoo ( self ) : <EOL> pass <EOL> originalRegisterResult = unittest . runner . registerResult <EOL> def cleanup ( ) : <EOL> unittest . runner . registerResult = originalRegisterResult <EOL> self . addCleanup ( cleanup ) <EOL> result = unittest . TestResult ( ) <EOL> runner = unittest . TextTestRunner ( stream = io . StringIO ( ) ) <EOL> runner . _makeResult = lambda : result <EOL> self . wasRegistered = <NUM_LIT:0> <EOL> def fakeRegisterResult ( thisResult ) : <EOL> self . wasRegistered += <NUM_LIT:1> <EOL> self . assertEqual ( thisResult , result ) <EOL> unittest . runner . registerResult = fakeRegisterResult <EOL> runner . run ( unittest . TestSuite ( ) ) <EOL> self . assertEqual ( self . wasRegistered , <NUM_LIT:1> ) <EOL> def test_works_with_result_without_startTestRun_stopTestRun ( self ) : <EOL> class OldTextResult ( ResultWithNoStartTestRunStopTestRun ) : <EOL> separator2 = '<STR_LIT>' <EOL> def printErrors ( self ) : <EOL> pass <EOL> class Runner ( unittest . TextTestRunner ) : <EOL> def __init__ ( self ) : <EOL> super ( Runner , self ) . __init__ ( io . StringIO ( ) ) <EOL> def _makeResult ( self ) : <EOL> return OldTextResult ( ) <EOL> runner = Runner ( ) <EOL> runner . run ( unittest . TestSuite ( ) ) <EOL> def test_startTestRun_stopTestRun_called ( self ) : <EOL> class LoggingTextResult ( LoggingResult ) : <EOL> separator2 = '<STR_LIT>' <EOL> def printErrors ( self ) : <EOL> pass <EOL> class LoggingRunner ( unittest . TextTestRunner ) : <EOL> def __init__ ( self , events ) : <EOL> super ( LoggingRunner , self ) . __init__ ( io . StringIO ( ) ) <EOL> self . _events = events <EOL> def _makeResult ( self ) : <EOL> return LoggingTextResult ( self . _events ) <EOL> events = [ ] <EOL> runner = LoggingRunner ( events ) <EOL> runner . run ( unittest . TestSuite ( ) ) <EOL> expected = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertEqual ( events , expected ) <EOL> def test_pickle_unpickle ( self ) : <EOL> stream = io . StringIO ( "<STR_LIT:foo>" ) <EOL> runner = unittest . TextTestRunner ( stream ) <EOL> for protocol in range ( <NUM_LIT:2> , pickle . HIGHEST_PROTOCOL + <NUM_LIT:1> ) : <EOL> s = pickle . dumps ( runner , protocol ) <EOL> obj = pickle . loads ( s ) <EOL> self . assertEqual ( obj . stream . getvalue ( ) , stream . getvalue ( ) ) <EOL> def test_resultclass ( self ) : <EOL> def MockResultClass ( * args ) : <EOL> return args <EOL> STREAM = object ( ) <EOL> DESCRIPTIONS = object ( ) <EOL> VERBOSITY = object ( ) <EOL> runner = unittest . TextTestRunner ( STREAM , DESCRIPTIONS , VERBOSITY , <EOL> resultclass = MockResultClass ) <EOL> self . assertEqual ( runner . resultclass , MockResultClass ) <EOL> expectedresult = ( runner . stream , DESCRIPTIONS , VERBOSITY ) <EOL> self . assertEqual ( runner . _makeResult ( ) , expectedresult ) <EOL> def test_warnings ( self ) : <EOL> """<STR_LIT>""" <EOL> def get_parse_out_err ( p ) : <EOL> return [ b . splitlines ( ) for b in p . communicate ( ) ] <EOL> opts = dict ( stdout = subprocess . PIPE , stderr = subprocess . PIPE , <EOL> cwd = os . path . dirname ( __file__ ) ) <EOL> ae_msg = b'<STR_LIT>' <EOL> at_msg = b'<STR_LIT>' <EOL> p = subprocess . Popen ( [ sys . executable , '<STR_LIT>' ] , ** opts ) <EOL> out , err = get_parse_out_err ( p ) <EOL> self . assertIn ( b'<STR_LIT:OK>' , err ) <EOL> self . assertEqual ( len ( out ) , <NUM_LIT:12> ) <EOL> for msg in [ b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' ] : <EOL> self . assertEqual ( out . count ( msg ) , <NUM_LIT:3> ) <EOL> for msg in [ ae_msg , at_msg , b'<STR_LIT>' ] : <EOL> self . assertEqual ( out . count ( msg ) , <NUM_LIT:1> ) <EOL> args_list = ( <EOL> [ sys . executable , '<STR_LIT>' , '<STR_LIT:ignore>' ] , <EOL> [ sys . executable , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:ignore>' ] , <EOL> [ sys . executable , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ) <EOL> for args in args_list : <EOL> p = subprocess . Popen ( args , ** opts ) <EOL> out , err = get_parse_out_err ( p ) <EOL> self . assertIn ( b'<STR_LIT:OK>' , err ) <EOL> self . assertEqual ( len ( out ) , <NUM_LIT:0> ) <EOL> p = subprocess . Popen ( [ sys . executable , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> ** opts ) <EOL> out , err = get_parse_out_err ( p ) <EOL> self . assertIn ( b'<STR_LIT:OK>' , err ) <EOL> self . assertEqual ( len ( out ) , <NUM_LIT> ) <EOL> for msg in [ b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' ] : <EOL> self . assertEqual ( out . count ( msg ) , <NUM_LIT:3> ) <EOL> for msg in [ ae_msg , at_msg ] : <EOL> self . assertEqual ( out . count ( msg ) , <NUM_LIT:1> ) <EOL> def testStdErrLookedUpAtInstantiationTime ( self ) : <EOL> old_stderr = sys . stderr <EOL> f = io . StringIO ( ) <EOL> sys . stderr = f <EOL> try : <EOL> runner = unittest . TextTestRunner ( ) <EOL> self . assertTrue ( runner . stream . stream is f ) <EOL> finally : <EOL> sys . stderr = old_stderr <EOL> def testSpecifiedStreamUsed ( self ) : <EOL> f = io . StringIO ( ) <EOL> runner = unittest . TextTestRunner ( f ) <EOL> self . assertTrue ( runner . stream . stream is f ) </s>
