Ë
    oj‡  ã                  óv   — d Z ddlmZ ddlmZ ddlmZ ddlmZ  G d„ d«      Z	 G d„ d	ej                  «      Zy
)z%
Tests for L{twisted.python.monkey}.
é    )Úannotations)ÚNoReturn)ÚMonkeyPatcher)Úunittestc                  ó   — e Zd Zdd„Zy)ÚTestObjc                ó.   — d| _         d| _        d| _        y )Nz	foo valuez	bar valuez	baz value)ÚfooÚbarÚbaz©Úselfs    ú:/usr/lib/python3/dist-packages/twisted/test/test_monkey.pyÚ__init__zTestObj.__init__   s   € ØˆŒØˆŒØˆ�ó    N©ÚreturnÚNone)Ú__name__Ú
__module__Ú__qualname__r   © r   r   r   r      s   „ ôr   r   c                  óx   — e Zd ZdZdd„Zdd„Zdd„Zdd„Zdd„Zdd„Z	dd„Z
dd	„Zdd
„Zdd„Zdd„Zdd„Zdd„Zy)ÚMonkeyPatcherTestsz;
    Tests for L{MonkeyPatcher} monkey-patching class.
    c                ó^   — t        «       | _        t        «       | _        t        «       | _        y ©N)r   Ú
testObjectÚoriginalObjectr   ÚmonkeyPatcherr   s    r   ÚsetUpzMonkeyPatcherTests.setUp   s   € Ü!›)ˆŒÜ%›iˆÔÜ*›_ˆÕr   c                ó”  — | j                   j                  «        | j                  | j                  j                  | j
                  j                  «       | j                  | j                  j                  | j
                  j                  «       | j                  | j                  j                  | j
                  j                  «       y)zL
        A monkey patcher without patches shouldn't change a thing.
        N)r   ÚpatchÚassertEqualr   r
   r   r   r   r   s    r   Ú
test_emptyzMonkeyPatcherTests.test_empty    sŠ   € ð 	×Ñ× Ñ Ô"ð 	×Ñ˜×,Ñ,×0Ñ0°$·/±/×2EÑ2EÔFØ×Ñ˜×,Ñ,×0Ñ0°$·/±/×2EÑ2EÔFØ×Ñ˜×,Ñ,×0Ñ0°$·/±/×2EÑ2EÕFr   c                ó|  — t        | j                  ddf| j                  ddf«      }|j                  «        | j                  d| j                  j                  «       | j                  d| j                  j
                  «       | j                  | j                  j                  | j                  j                  «       y)z}
        Constructing a L{MonkeyPatcher} with patches should add all of the
        given patches to the patch list.
        r
   Úhahar   ÚheheN)r   r   r"   r#   r
   r   r   r   )r   Úpatchers     r   Útest_constructWithPatchesz,MonkeyPatcherTests.test_constructWithPatches,   sŽ   € ô
  Ø�_‰_˜e VÐ,¨t¯©ÀÀvÐ.Nó
ˆð 	�‰ŒØ×Ñ˜ §¡×!4Ñ!4Ô5Ø×Ñ˜ §¡×!4Ñ!4Ô5Ø×Ñ˜×,Ñ,×0Ñ0°$·/±/×2EÑ2EÕFr   c                óÒ   — | j                   j                  | j                  dd«       | j                   j                  «        | j	                  | j                  j
                  d«       y)zf
        Patching an attribute that exists sets it to the value defined in the
        patch.
        r
   r&   N)r   ÚaddPatchr   r"   r#   r
   r   s    r   Útest_patchExistingz%MonkeyPatcherTests.test_patchExisting9   sL   € ð
 	×Ñ×#Ñ# D§O¡O°U¸FÔCØ×Ñ× Ñ Ô"Ø×Ñ˜Ÿ™×,Ñ,¨fÕ5r   c                ó¦   — | j                   j                  | j                  dd«       | j                  t        | j                   j
                  «       y)zT
        Patching a non-existing attribute fails with an C{AttributeError}.
        Únowherezblow up pleaseN)r   r+   r   ÚassertRaisesÚAttributeErrorr"   r   s    r   Útest_patchNonExistingz(MonkeyPatcherTests.test_patchNonExistingB   s=   € ð 	×Ñ×#Ñ# D§O¡O°YÐ@PÔQØ×Ñœ.¨$×*<Ñ*<×*BÑ*BÕCr   c                óÈ  — | j                   j                  | j                  dd«       | j                   j                  | j                  dd«       | j                   j                  «        | j	                  | j                  j
                  d«       | j                   j                  «        | j	                  | j                  j
                  | j                  j
                  «       y)z|
        Adding a patch for an object and attribute that already have a patch
        overrides the existing patch.
        r
   ÚblahÚBLAHN)r   r+   r   r"   r#   r
   Úrestorer   r   s    r   Útest_patchAlreadyPatchedz+MonkeyPatcherTests.test_patchAlreadyPatchedI   s    € ð
 	×Ñ×#Ñ# D§O¡O°U¸FÔCØ×Ñ×#Ñ# D§O¡O°U¸FÔCØ×Ñ× Ñ Ô"Ø×Ñ˜Ÿ™×,Ñ,¨fÔ5Ø×Ñ×"Ñ"Ô$Ø×Ñ˜Ÿ™×,Ñ,¨d×.AÑ.A×.EÑ.EÕFr   c                óÖ  — | j                   j                  | j                  dd«       | j                   j                  «        | j                   j	                  «        | j                  | j                  j                  | j                  j                  «       | j                   j	                  «        | j                  | j                  j                  | j                  j                  «       y)zH
        Restoring an already-restored monkey patch is a no-op.
        r
   r3   N)r   r+   r   r"   r5   r#   r
   r   r   s    r   Útest_restoreTwiceIsANoOpz+MonkeyPatcherTests.test_restoreTwiceIsANoOpU   s¢   € ð 	×Ñ×#Ñ# D§O¡O°U¸FÔCØ×Ñ× Ñ Ô"Ø×Ñ×"Ñ"Ô$Ø×Ñ˜Ÿ™×,Ñ,¨d×.AÑ.A×.EÑ.EÔFØ×Ñ×"Ñ"Ô$Ø×Ñ˜Ÿ™×,Ñ,¨d×.AÑ.A×.EÑ.EÕFr   c                ó    ‡— g Šd	d
ˆfd„}| j                   j                  |ddd¬«      }| j                  d|«       | j                  dg‰«       y)z¤
        runWithPatches should run the given callable, passing in all arguments
        and keyword arguments, and return the return value of the callable.
        Nc                ó.   •— ‰j                  | ||f«       y)Nr
   )Úappend)ÚaÚbÚcÚlogs      €r   Úfz;MonkeyPatcherTests.test_runWithPatchesDecoration.<locals>.fg   s   ø€ Ø�J‰J˜˜1˜a�yÔ!Ør   é   é   é
   )r>   r
   )rA   rB   rC   r   )r<   Úintr=   rD   r>   z
int | Noner   Ústr)r   ÚrunWithPatchesr#   )r   r@   Úresultr?   s      @r   Útest_runWithPatchesDecorationz0MonkeyPatcherTests.test_runWithPatchesDecoration`   sR   ø€ ð
 24ˆö	ð ×#Ñ#×2Ñ2°1°a¸¸bÐ2ÓAˆØ×Ñ˜ Ô'Ø×Ñ˜*˜ sÕ+r   c                óÀ  ‡ — dˆ fd„}‰ j                   j                  ‰ j                  dd«       ‰ j                   j                  |«      }‰ j	                  d‰ j
                  j                  ‰ j
                  j                  f|«       ‰ j                   j                  |«      }‰ j	                  d‰ j
                  j                  ‰ j
                  j                  f|«       y)z‘
        We should be able to call the same function with runWithPatches more
        than once. All patches should apply for each call.
        c                 ó†   •— ‰ j                   j                  ‰ j                   j                  ‰ j                   j                  fS r   )r   r
   r   r   r   s   €r   r@   z9MonkeyPatcherTests.test_repeatedRunWithPatches.<locals>.fu   s.   ø€ Ø—O‘O×'Ñ'¨¯©×)<Ñ)<¸d¿o¹o×>QÑ>QÐRÐRr   r
   r&   N)r   ztuple[str, str, str])r   r+   r   rF   r#   r   r   r   )r   r@   rG   s   `  r   Útest_repeatedRunWithPatchesz.MonkeyPatcherTests.test_repeatedRunWithPatcheso   s¹   ø€ õ	Sð 	×Ñ×#Ñ# D§O¡O°U¸FÔCØ×#Ñ#×2Ñ2°1Ó5ˆØ×ÑØ�T×(Ñ(×,Ñ,¨d×.AÑ.A×.EÑ.EÐFÈô	
ð ×#Ñ#×2Ñ2°1Ó5ˆØ×ÑØ�T×(Ñ(×,Ñ,¨d×.AÑ.A×.EÑ.EÐFÈõ	
r   c                ór  — | j                   j                  | j                  dd«       | j                  | j                  j
                  | j                  j
                  «       | j                   j                  d„ «       | j                  | j                  j
                  | j                  j
                  «       y)zo
        C{runWithPatches} should restore the original values after the function
        has executed.
        r
   r&   c                  ó   — y r   r   r   r   r   ú<lambda>z@MonkeyPatcherTests.test_runWithPatchesRestores.<locals>.<lambda>‰   s   � r   N)r   r+   r   r#   r   r
   rF   r   s    r   Útest_runWithPatchesRestoresz.MonkeyPatcherTests.test_runWithPatchesRestores‚   s€   € ð
 	×Ñ×#Ñ# D§O¡O°U¸FÔCØ×Ñ˜×,Ñ,×0Ñ0°$·/±/×2EÑ2EÔFØ×Ñ×)Ñ)©,Ô7Ø×Ñ˜×,Ñ,×0Ñ0°$·/±/×2EÑ2EÕFr   c                óì  ‡ — dˆ fd„}‰ j                   j                  ‰ j                  dd«       ‰ j                   j                  ‰ j                  dd«       ‰ j                  t        ‰ j                   j
                  |«       ‰ j                  ‰ j                  j                  ‰ j                  j                  «       ‰ j                  ‰ j                  j                  ‰ j                  j                  «       y)zv
        Test runWithPatches restores the original values even when the function
        raises an exception.
        c                 ó²   •— ‰ j                  ‰ j                  j                  d«       ‰ j                  ‰ j                  j                  d«       t	        d«      ‚)Nr&   ÚblahblahzSomething went wrong!)r#   r   r
   r   ÚRuntimeErrorr   s   €r   Ú_zDMonkeyPatcherTests.test_runWithPatchesRestoresOnException.<locals>._’   sC   ø€ Ø×Ñ˜TŸ_™_×0Ñ0°&Ô9Ø×Ñ˜TŸ_™_×0Ñ0°*Ô=ÜÐ6Ó7Ð7r   r
   r&   r   rR   N)r   r   )
r   r+   r   r/   rS   rF   r#   r
   r   r   )r   rT   s   ` r   Ú&test_runWithPatchesRestoresOnExceptionz9MonkeyPatcherTests.test_runWithPatchesRestoresOnExceptionŒ   s¬   ø€ õ	8ð
 	×Ñ×#Ñ# D§O¡O°U¸FÔCØ×Ñ×#Ñ# D§O¡O°U¸JÔGà×Ñœ,¨×(:Ñ(:×(IÑ(IÈ1ÔMØ×Ñ˜Ÿ™×,Ñ,¨d×.AÑ.A×.EÑ.EÔFØ×Ñ˜Ÿ™×,Ñ,¨d×.AÑ.A×.EÑ.EÕFr   c                óT  — | j                   j                  | j                  dd«       | j                   5  | j                  | j                  j                  d«       ddd«       | j                  | j                  j                  | j
                  j                  «       y# 1 sw Y   ŒDxY w)z†
        L{MonkeyPatcher} is a context manager that applies its patches on
        entry and restore original values on exit.
        r
   zpatched valueN)r   r+   r   r#   r
   r   r   s    r   Útest_contextManagerz&MonkeyPatcherTests.test_contextManagerž   s‚   € ð
 	×Ñ×#Ñ# D§O¡O°U¸OÔLØ×Ññ 	CØ×Ñ˜TŸ_™_×0Ñ0°/ÔB÷	Cà×Ñ˜Ÿ™×,Ñ,¨d×.AÑ.A×.EÑ.EÕF÷	Cð 	Cús   ´'BÂB'c                ó¢   — | j                  t        «      5  | j                  5  t        d«      ‚# 1 sw Y   nxY w	 ddd«       y# 1 sw Y   yxY w)zh
        Exceptions propagate through the L{MonkeyPatcher} context-manager
        exit method.
        Ú	somethingN)r/   rS   r   r   s    r   Ú'test_contextManagerPropagatesExceptionsz:MonkeyPatcherTests.test_contextManagerPropagatesExceptions¨   sP   € ð
 ×Ñœ|Ó,ñ 	0Ø×#Ñ#ñ 0Ü" ;Ó/Ð/÷0ð 0úð 0÷	0÷ 	0ñ 	0ús   –A£/¯8	´AÁANr   )r   r   r   Ú__doc__r    r$   r)   r,   r1   r6   r8   rH   rK   rO   rU   rW   rZ   r   r   r   r   r      sP   „ ñó-ó

GóGó6óDó
Gó	Gó,ó
ó&GóGó$Gô0r   r   N)r[   Ú
__future__r   Útypingr   Útwisted.python.monkeyr   Útwisted.trialr   r   ÚSynchronousTestCaser   r   r   r   ú<module>ra      s6   ðñõ #å å /Ý "÷ñ ôY0˜×5Ñ5õ Y0r   