U
    
W[  ã                   @   sP   d Z ddlmZ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}.
é    )ÚdivisionÚabsolute_import)Úunittest)ÚMonkeyPatcherc                   @   s   e Zd Zdd„ ZdS )ÚTestObjc                 C   s   d| _ d| _d| _d S )Nz	foo valuez	bar valuez	baz value)ÚfooÚbarÚbaz©Úself© r   ú:/usr/lib/python3/dist-packages/twisted/test/test_monkey.pyÚ__init__   s    zTestObj.__init__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s   r   c                   @   sh   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S )ÚMonkeyPatcherTestsz;
    Tests for L{MonkeyPatcher} monkey-patching class.
    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_patchExisting;   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_patchNonExistingE   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_patchAlreadyPatchedN   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_restoreTwiceIsANoOp[   s    


z+MonkeyPatcherTests.test_restoreTwiceIsANoOpc                    sD   g ‰ 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.
        Nc                    s   ˆ   | ||f¡ dS )Nr   )Úappend)ÚaÚbÚc©Úlogr   r   Úfn   s    z;MonkeyPatcherTests.test_runWithPatchesDecoration.<locals>.fé   é   é
   )r)   r   )r-   r.   r/   )N)r   ÚrunWithPatchesr   ©r   r,   Úresultr   r*   r   Útest_runWithPatchesDecorationg   s
    z0MonkeyPatcherTests.test_runWithPatchesDecorationc                    sn   ‡ 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.
        c                      s   ˆ j jˆ j jˆ j jfS r   )r   r   r   r	   r   r
   r   r   r,   |   s    ÿz9MonkeyPatcherTests.test_repeatedRunWithPatches.<locals>.fr   r   N)r   r   r   r0   r   r   r   r	   r1   r   r
   r   Útest_repeatedRunWithPatchesw   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   r0   r
   r   r   r   Útest_runWithPatchesRestoresŠ   s    z.MonkeyPatcherTests.test_runWithPatchesRestoresc                    sn   ‡ 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.
        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   r8   N)
r   r   r   r   r9   r0   r   r   r   r   )r   r:   r   r
   r   Ú&test_runWithPatchesRestoresOnException•   s    z9MonkeyPatcherTests.test_runWithPatchesRestoresOnExceptionN)r   r   r   Ú__doc__r   r   r   r   r!   r$   r%   r3   r4   r7   r;   r   r   r   r   r      s   
	r   N)r<   Z
__future__r   r   Ztwisted.trialr   Ztwisted.python.monkeyr   r   ZSynchronousTestCaser   r   r   r   r   Ú<module>   s
   