o
    ¯b  ã                   @   s@   d 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}.
é    )ÚMonkeyPatcher)Úunittestc                   @   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   ÚheheN)r   r   r   r   r   r   r   r   )r	   Ú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_patchExisting7   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}.
        Únowherezblow up pleaseN)r   r   r   ÚassertRaisesÚAttributeErrorr   r   r
   r
   r   Útest_patchNonExisting@   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   ÚblahÚBLAHN)r   r   r   r   r   r   Úrestorer   r   r
   r
   r   Útest_patchAlreadyPatchedG   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_restoreTwiceIsANoOpS   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   Úfe   s   z;MonkeyPatcherTests.test_runWithPatchesDecoration.<locals>.fé   é   é
   )r+   r   )r/   r0   r1   r   )r   ÚrunWithPatchesr   ©r	   r.   Úresultr
   r,   r   Útest_runWithPatchesDecoration^   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   s   z9MonkeyPatcherTests.test_repeatedRunWithPatches.<locals>.fr   r   N)r   r   r   r2   r   r   r   r   r3   r
   r   r   Útest_repeatedRunWithPatchesm   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>‡   s    z@MonkeyPatcherTests.test_runWithPatchesRestores.<locals>.<lambda>N)r   r   r   r   r   r   r2   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ƒ‚)Nr   ÚblahblahzSomething went wrong!)r   r   r   r   ÚRuntimeErrorr
   r   r
   r   Ú_   s   zDMonkeyPatcherTests.test_runWithPatchesRestoresOnException.<locals>._r   r   r   r9   N)
r   r   r   r    r:   r2   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'   r5   r6   r8   r<   r
   r
   r
   r   r      s    	
r   N)r=   Útwisted.python.monkeyr   Útwisted.trialr   r   ÚSynchronousTestCaser   r
   r
   r
   r   Ú<module>   s
   