U
    Â“«j’  ã                   @  sX   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„ dƒZ	G dd	„ d	ej
ƒZd
S )z%
Tests for L{twisted.python.monkey}.
é    )Úannotations)ÚNoReturn)ÚMonkeyPatcher)Úunittestc                   @  s   e Zd Zddœdd„ZdS )ÚTestObjÚNone©Úreturnc                 C  s   d| _ d| _d| _d S )Nz	foo valuez	bar valuez	baz value)ÚfooÚbarÚbaz©Úself© r   ú</tmp/pip-unpacked-wheel-59kc99xd/twisted/test/test_monkey.pyÚ__init__   s    zTestObj.__init__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s   r   c                   @  sÆ   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zddœdd„Zddœdd„Z	ddœdd„Z
ddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„ZdS )ÚMonkeyPatcherTestsz;
    Tests for L{MonkeyPatcher} monkey-patching class.
    r   r   c                 C  s   t ƒ | _t ƒ | _tƒ | _d S ©N)r   Ú
testObjectÚoriginalObjectr   ÚmonkeyPatcherr   r   r   r   ÚsetUp   s    zMonkeyPatcherTests.setUpc                 C  sJ   | j  ¡  |  | jj| jj¡ |  | jj| jj¡ |  | jj| jj¡ dS )zL
        A monkey patcher without patches shouldn't change a thing.
        N)r   ÚpatchÚassertEqualr   r
   r   r   r   r   r   r   r   Ú
test_empty    s    
zMonkeyPatcherTests.test_emptyc                 C  sZ   t | jddf| jddfƒ}| ¡  |  d| jj¡ |  d| jj¡ |  | jj| jj¡ dS )z}
        Constructing a L{MonkeyPatcher} with patches should add all of the
        given patches to the patch list.
        r
   Úhahar   ZheheN)r   r   r   r   r
   r   r   r   )r   Zpatcherr   r   r   Útest_constructWithPatches,   s    
 
ÿz,MonkeyPatcherTests.test_constructWithPatchesc                 C  s0   | j  | jdd¡ | j  ¡  |  | jjd¡ dS )zf
        Patching an attribute that exists sets it to the value defined in the
        patch.
        r
   r   N)r   ÚaddPatchr   r   r   r
   r   r   r   r   Útest_patchExisting9   s    
z%MonkeyPatcherTests.test_patchExistingc                 C  s&   | j  | jdd¡ |  t| j j¡ dS )zT
        Patching a non-existing attribute fails with an C{AttributeError}.
        Znowherezblow up pleaseN)r   r    r   ÚassertRaisesÚAttributeErrorr   r   r   r   r   Útest_patchNonExistingB   s    z(MonkeyPatcherTests.test_patchNonExistingc                 C  s`   | j  | jdd¡ | j  | jdd¡ | j  ¡  |  | jjd¡ | j  ¡  |  | jj| jj¡ dS )z|
        Adding a patch for an object and attribute that already have a patch
        overrides the existing patch.
        r
   ÚblahZBLAHN)r   r    r   r   r   r
   Úrestorer   r   r   r   r   Útest_patchAlreadyPatchedI   s    

z+MonkeyPatcherTests.test_patchAlreadyPatchedc                 C  s\   | j  | jdd¡ | j  ¡  | j  ¡  |  | jj| jj¡ | j  ¡  |  | jj| jj¡ dS )zH
        Restoring an already-restored monkey patch is a no-op.
        r
   r%   N)r   r    r   r   r&   r   r
   r   r   r   r   r   Útest_restoreTwiceIsANoOpU   s    


z+MonkeyPatcherTests.test_restoreTwiceIsANoOpc                   sP   g ‰ ddddddœ‡ fdd„}| j j|dd	d
d�}|  d|¡ |  dgˆ ¡ dS )z¤
        runWithPatches should run the given callable, passing in all arguments
        and keyword arguments, and return the return value of the callable.
        NÚintz
int | NoneÚstr)ÚaÚbÚcr	   c                   s   ˆ   | ||f¡ dS )Nr
   )Úappend)r+   r,   r-   ©Úlogr   r   Úfg   s    z;MonkeyPatcherTests.test_runWithPatchesDecoration.<locals>.fé   é   é
   )r-   r
   )r2   r3   r4   )N)r   ÚrunWithPatchesr   ©r   r1   Úresultr   r/   r   Útest_runWithPatchesDecoration`   s
    z0MonkeyPatcherTests.test_runWithPatchesDecorationc                   st   ddœ‡ fdd„}ˆ j  ˆ jdd¡ ˆ j  |¡}ˆ  dˆ jjˆ jjf|¡ ˆ j  |¡}ˆ  dˆ jjˆ jjf|¡ dS )z‘
        We should be able to call the same function with runWithPatches more
        than once. All patches should apply for each call.
        ztuple[str, str, str]r   c                     s   ˆ j jˆ j jˆ j jfS r   )r   r
   r   r   r   r   r   r   r1   u   s    z9MonkeyPatcherTests.test_repeatedRunWithPatches.<locals>.fr
   r   N)r   r    r   r5   r   r   r   r   r6   r   r   r   Útest_repeatedRunWithPatcheso   s     ÿ ÿz.MonkeyPatcherTests.test_repeatedRunWithPatchesc                 C  sN   | j  | jdd¡ |  | jj| jj¡ | j  dd„ ¡ |  | jj| jj¡ dS )zo
        C{runWithPatches} should restore the original values after the function
        has executed.
        r
   r   c                   S  s   d S r   r   r   r   r   r   Ú<lambda>‰   ó    z@MonkeyPatcherTests.test_runWithPatchesRestores.<locals>.<lambda>N)r   r    r   r   r   r
   r5   r   r   r   r   Útest_runWithPatchesRestores‚   s    z.MonkeyPatcherTests.test_runWithPatchesRestoresc                   st   ddœ‡ fdd„}ˆ j  ˆ jdd¡ ˆ j  ˆ jdd¡ ˆ  tˆ j j|¡ ˆ  ˆ jjˆ jj¡ ˆ  ˆ jj	ˆ jj	¡ d	S )
zv
        Test runWithPatches restores the original values even when the function
        raises an exception.
        r   r   c                     s,   ˆ   ˆ jjd¡ ˆ   ˆ jjd¡ tdƒ‚d S )Nr   ÚblahblahzSomething went wrong!)r   r   r
   r   ÚRuntimeErrorr   r   r   r   Ú_’   s    zDMonkeyPatcherTests.test_runWithPatchesRestoresOnException.<locals>._r
   r   r   r=   N)
r   r    r   r"   r>   r5   r   r
   r   r   )r   r?   r   r   r   Ú&test_runWithPatchesRestoresOnExceptionŒ   s    z9MonkeyPatcherTests.test_runWithPatchesRestoresOnExceptionc              	   C  sL   | j  | jdd¡ | j � |  | jjd¡ W 5 Q R X |  | jj| jj¡ dS )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   r   r   r   Útest_contextManagerž   s    z&MonkeyPatcherTests.test_contextManagerc              
   C  s4   |   t¡�  | j� tdƒ‚W 5 Q R X W 5 Q R X dS )zh
        Exceptions propagate through the L{MonkeyPatcher} context-manager
        exit method.
        Z	somethingN)r"   r>   r   r   r   r   r   Ú'test_contextManagerPropagatesExceptions¨   s    z:MonkeyPatcherTests.test_contextManagerPropagatesExceptionsN)r   r   r   Ú__doc__r   r   r   r!   r$   r'   r(   r8   r9   r<   r@   rA   rB   r   r   r   r   r      s   	

r   N)rC   Ú
__future__r   Ztyping_extensionsr   Ztwisted.python.monkeyr   Ztwisted.trialr   r   ZSynchronousTestCaser   r   r   r   r   Ú<module>   s   