ϪfvdZddlmZddlmZddlmZddlmZGddZ Gdd ejZ y ) z% Tests for L{twisted.python.monkey}. ) annotations)NoReturn) MonkeyPatcher)unittestceZdZddZy)TestObjc.d|_d|_d|_y)Nz foo valuez bar valuez baz value)foobarbazselfs :/usr/lib/python3/dist-packages/twisted/test/test_monkey.py__init__zTestObj.__init__sNreturnNone)__name__ __module__ __qualname__rrrrrsrrcxeZdZdZddZddZddZddZddZddZ ddZ dd Z dd Z dd Z dd Zdd ZddZy)MonkeyPatcherTestsz; Tests for L{MonkeyPatcher} monkey-patching class. c^t|_t|_t|_yN)r testObjectoriginalObjectr monkeyPatcherr s rsetUpzMonkeyPatcherTests.setUps!)%i*_rc|jj|j|jj|j j|j|jj |j j |j|jj|j jy)zL A monkey patcher without patches shouldn't change a thing. N)rpatch assertEqualrr rr r r s r test_emptyzMonkeyPatcherTests.test_empty s   " ,,00$//2E2EF ,,00$//2E2EF ,,00$//2E2EFrc|t|jddf|jddf}|j|jd|jj|jd|jj |j|j j|jjy)z} Constructing a L{MonkeyPatcher} with patches should add all of the given patches to the patch list. r hahar heheN)rrr"r#r r rr )rpatchers rtest_constructWithPatchesz,MonkeyPatcherTests.test_constructWithPatches,s  __eV ,tv.N    !4!45 !4!45 ,,00$//2E2EFrc|jj|jdd|jj|j |jj dy)zf Patching an attribute that exists sets it to the value defined in the patch. r r&N)raddPatchrr"r#r r s rtest_patchExistingz%MonkeyPatcherTests.test_patchExisting9sL ##DOOUFC   " ,,f5rc|jj|jdd|jt|jj y)zT Patching a non-existing attribute fails with an C{AttributeError}. nowherezblow up pleaseN)rr+r assertRaisesAttributeErrorr"r s rtest_patchNonExistingz(MonkeyPatcherTests.test_patchNonExistingBs= ##DOOY@PQ .$*<*<*B*BCrc|jj|jdd|jj|jdd|jj|j |jj d|jj |j |jj |jj y)z| Adding a patch for an object and attribute that already have a patch overrides the existing patch. r blahBLAHN)rr+rr"r#r restorerr s rtest_patchAlreadyPatchedz+MonkeyPatcherTests.test_patchAlreadyPatchedIs ##DOOUFC ##DOOUFC   " ,,f5 ""$ ,,d.A.A.E.EFrc|jj|jdd|jj|jj |j |jj |jj |jj |j |jj |jj y)zH Restoring an already-restored monkey patch is a no-op. r r3N)rr+rr"r5r#r rr s rtest_restoreTwiceIsANoOpz+MonkeyPatcherTests.test_restoreTwiceIsANoOpUs ##DOOUFC   " ""$ ,,d.A.A.E.EF ""$ ,,d.A.A.E.EFrcgd d fd }|jj|ddd}|jd||jdgy) z runWithPatches should run the given callable, passing in all arguments and keyword arguments, and return the return value of the callable. Nc.j|||fy)Nr )append)abclogs rfz;MonkeyPatcherTests.test_runWithPatchesDecoration..fgs JJ1ay !r )r>r )rArBrCr)r<intr=rDr>z int | Nonerstr)rrunWithPatchesr#)rr@resultr?s @rtest_runWithPatchesDecorationz0MonkeyPatcherTests.test_runWithPatchesDecoration`sR 24 ##221ab2A ' *s+rcdfd }jjjddjj|}j dj j j jf|jj|}j dj j j jf|y)z We should be able to call the same function with runWithPatches more than once. All patches should apply for each call. cjjjjjjfSr)rr r r r srr@z9MonkeyPatcherTests.test_repeatedRunWithPatches..fus.OO'')<)Q>QR Rrr r&N)rztuple[str, str, str])rr+rrFr#rr r )rr@rGs` rtest_repeatedRunWithPatchesz.MonkeyPatcherTests.test_repeatedRunWithPatchesos  S ##DOOUFC##2215  T((,,d.A.A.E.E F ##2215  T((,,d.A.A.E.E F rcr|jj|jdd|j|jj |jj |jj d|j|jj |jj y)zo C{runWithPatches} should restore the original values after the function has executed. r r&cyrrrrrz@MonkeyPatcherTests.test_runWithPatchesRestores..srN)rr+rr#rr rFr s rtest_runWithPatchesRestoresz.MonkeyPatcherTests.test_runWithPatchesRestoress ##DOOUFC ,,00$//2E2EF )),7 ,,00$//2E2EFrcdfd }jjjddjjjddjtjj |j jjjjj jjjjy)zv Test runWithPatches restores the original values even when the function raises an exception. cjjjdjjjdt d)Nr&blahblahzSomething went wrong!)r#rr r RuntimeErrorr sr_zDMonkeyPatcherTests.test_runWithPatchesRestoresOnException.._sC   T__00& 9   T__00* =67 7rr r&r rRN)rr) rr+rr/rSrFr#r rr )rrTs` r&test_runWithPatchesRestoresOnExceptionz9MonkeyPatcherTests.test_runWithPatchesRestoresOnExceptions  8 ##DOOUFC ##DOOUJG ,(:(:(I(I1M ,,d.A.A.E.EF ,,d.A.A.E.EFrcT|jj|jdd|j5|j|jjdddd|j|jj|j jy#1swYDxYw)z L{MonkeyPatcher} is a context manager that applies its patches on entry and restore original values on exit. r z patched valueN)rr+rr#r rr s rtest_contextManagerz&MonkeyPatcherTests.test_contextManagers ##DOOUOL    C   T__00/ B C ,,d.A.A.E.EF C Cs 'BB'c|jt5|j5td#1swYnxYw dddy#1swYyxYw)zh Exceptions propagate through the L{MonkeyPatcher} context-manager exit method. somethingN)r/rSrr s r'test_contextManagerPropagatesExceptionsz:MonkeyPatcherTests.test_contextManagerPropagatesExceptionssP   | , 0## 0";// 0 0 0 0 0 0s A /8 AANr)rrr__doc__r r$r)r,r1r6r8rHrKrOrUrWrZrrrrrsP- G G6D G G , &GG$G0rrN) r[ __future__rtypingrtwisted.python.monkeyr twisted.trialrrSynchronousTestCaserrrrras6#/"Y055Y0r