<s> """<STR_LIT>""" <EOL> from __future__ import division , absolute_import <EOL> from twisted . trial import unittest <EOL> from twisted . python . monkey import MonkeyPatcher <EOL> class TestObj : <EOL> def __init__ ( self ) : <EOL> self . foo = '<STR_LIT>' <EOL> self . bar = '<STR_LIT>' <EOL> self . baz = '<STR_LIT>' <EOL> class MonkeyPatcherTests ( unittest . SynchronousTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . testObject = TestObj ( ) <EOL> self . originalObject = TestObj ( ) <EOL> self . monkeyPatcher = MonkeyPatcher ( ) <EOL> def test_empty ( self ) : <EOL> """<STR_LIT>""" <EOL> self . monkeyPatcher . patch ( ) <EOL> self . assertEqual ( self . originalObject . foo , self . testObject . foo ) <EOL> self . assertEqual ( self . originalObject . bar , self . testObject . bar ) <EOL> self . assertEqual ( self . originalObject . baz , self . testObject . baz ) <EOL> def test_constructWithPatches ( self ) : <EOL> """<STR_LIT>""" <EOL> patcher = MonkeyPatcher ( ( self . testObject , '<STR_LIT:foo>' , '<STR_LIT>' ) , <EOL> ( self . testObject , '<STR_LIT:bar>' , '<STR_LIT>' ) ) <EOL> patcher . patch ( ) <EOL> self . assertEqual ( '<STR_LIT>' , self . testObject . foo ) <EOL> self . assertEqual ( '<STR_LIT>' , self . testObject . bar ) <EOL> self . assertEqual ( self . originalObject . baz , self . testObject . baz ) <EOL> def test_patchExisting ( self ) : <EOL> """<STR_LIT>""" <EOL> self . monkeyPatcher . addPatch ( self . testObject , '<STR_LIT:foo>' , '<STR_LIT>' ) <EOL> self . monkeyPatcher . patch ( ) <EOL> self . assertEqual ( self . testObject . foo , '<STR_LIT>' ) <EOL> def test_patchNonExisting ( self ) : <EOL> """<STR_LIT>""" <EOL> self . monkeyPatcher . addPatch ( self . testObject , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> self . assertRaises ( AttributeError , self . monkeyPatcher . patch ) <EOL> def test_patchAlreadyPatched ( self ) : <EOL> """<STR_LIT>""" <EOL> self . monkeyPatcher . addPatch ( self . testObject , '<STR_LIT:foo>' , '<STR_LIT>' ) <EOL> self . monkeyPatcher . addPatch ( self . testObject , '<STR_LIT:foo>' , '<STR_LIT>' ) <EOL> self . monkeyPatcher . patch ( ) <EOL> self . assertEqual ( self . testObject . foo , '<STR_LIT>' ) <EOL> self . monkeyPatcher . restore ( ) <EOL> self . assertEqual ( self . testObject . foo , self . originalObject . foo ) <EOL> def test_restoreTwiceIsANoOp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . monkeyPatcher . addPatch ( self . testObject , '<STR_LIT:foo>' , '<STR_LIT>' ) <EOL> self . monkeyPatcher . patch ( ) <EOL> self . monkeyPatcher . restore ( ) <EOL> self . assertEqual ( self . testObject . foo , self . originalObject . foo ) <EOL> self . monkeyPatcher . restore ( ) <EOL> self . assertEqual ( self . testObject . foo , self . originalObject . foo ) <EOL> def test_runWithPatchesDecoration ( self ) : <EOL> """<STR_LIT>""" <EOL> log = [ ] <EOL> def f ( a , b , c = None ) : <EOL> log . append ( ( a , b , c ) ) <EOL> return '<STR_LIT:foo>' <EOL> result = self . monkeyPatcher . runWithPatches ( f , <NUM_LIT:1> , <NUM_LIT:2> , c = <NUM_LIT:10> ) <EOL> self . assertEqual ( '<STR_LIT:foo>' , result ) <EOL> self . assertEqual ( [ ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:10> ) ] , log ) <EOL> def test_repeatedRunWithPatches ( self ) : <EOL> """<STR_LIT>""" <EOL> def f ( ) : <EOL> return ( self . testObject . foo , self . testObject . bar , <EOL> self . testObject . baz ) <EOL> self . monkeyPatcher . addPatch ( self . testObject , '<STR_LIT:foo>' , '<STR_LIT>' ) <EOL> result = self . monkeyPatcher . runWithPatches ( f ) <EOL> self . assertEqual ( <EOL> ( '<STR_LIT>' , self . originalObject . bar , self . originalObject . baz ) , result ) <EOL> result = self . monkeyPatcher . runWithPatches ( f ) <EOL> self . assertEqual ( <EOL> ( '<STR_LIT>' , self . originalObject . bar , self . originalObject . baz ) , <EOL> result ) <EOL> def test_runWithPatchesRestores ( self ) : <EOL> """<STR_LIT>""" <EOL> self . monkeyPatcher . addPatch ( self . testObject , '<STR_LIT:foo>' , '<STR_LIT>' ) <EOL> self . assertEqual ( self . originalObject . foo , self . testObject . foo ) <EOL> self . monkeyPatcher . runWithPatches ( lambda : None ) <EOL> self . assertEqual ( self . originalObject . foo , self . testObject . foo ) <EOL> def test_runWithPatchesRestoresOnException ( self ) : <EOL> """<STR_LIT>""" <EOL> def _ ( ) : <EOL> self . assertEqual ( self . testObject . foo , '<STR_LIT>' ) <EOL> self . assertEqual ( self . testObject . bar , '<STR_LIT>' ) <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> self . monkeyPatcher . addPatch ( self . testObject , '<STR_LIT:foo>' , '<STR_LIT>' ) <EOL> self . monkeyPatcher . addPatch ( self . testObject , '<STR_LIT:bar>' , '<STR_LIT>' ) <EOL> self . assertRaises ( RuntimeError , self . monkeyPatcher . runWithPatches , _ ) <EOL> self . assertEqual ( self . testObject . foo , self . originalObject . foo ) <EOL> self . assertEqual ( self . testObject . bar , self . originalObject . bar ) </s>
