o
    ¼>h’  ã                   @  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„ZdS )ÚTestObjÚreturnÚNonec                 C  s   d| _ d| _d| _d S )Nz	foo valuez	bar valuez	baz value)ÚfooÚbarÚbaz©Úself© r   úx/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/test/test_monkey.pyÚ__init__   s   
zTestObj.__init__N©r   r   )Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s    r   c                   @  s’   e Zd ZdZddd„Zddd„Zddd	„Zdd
d„Zddd„Zddd„Z	ddd„Z
ddd„Zddd„Zddd„Zddd„Zddd„Z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
   Ú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_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}.
        Ú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	   ÚblahÚ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                   sF   g ‰ 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ÚaÚintÚbÚcú
int | Noner   Ústrc                   s   ˆ   | ||f¡ dS )Nr	   )Úappend)r-   r/   r0   ©Úlogr   r   Úfg   s   z;MonkeyPatcherTests.test_runWithPatchesDecoration.<locals>.fé   é   é
   )r0   r	   )r7   r8   r9   r   )r-   r.   r/   r.   r0   r1   r   r2   )r   ÚrunWithPatchesr   ©r   r6   Úresultr   r4   r   Útest_runWithPatchesDecoration`   s
   z0MonkeyPatcherTests.test_runWithPatchesDecorationc                   sp   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.
        r   útuple[str, str, str]c                     s   ˆ j jˆ j jˆ j jfS r   )r   r	   r
   r   r   r   r   r   r6   u   s   z9MonkeyPatcherTests.test_repeatedRunWithPatches.<locals>.fr	   r   N)r   r>   )r   r"   r   r:   r   r   r
   r   r;   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>‰   s    z@MonkeyPatcherTests.test_runWithPatchesRestores.<locals>.<lambda>N)r   r"   r   r   r   r	   r:   r   r   r   r   Útest_runWithPatchesRestores‚   s   z.MonkeyPatcherTests.test_runWithPatchesRestoresc                   sp   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ƒ‚)Nr   ÚblahblahzSomething went wrong!)r   r   r	   r
   ÚRuntimeErrorr   r   r   r   Ú_’   s   zDMonkeyPatcherTests.test_runWithPatchesRestoresOnException.<locals>._r	   r   r
   rB   N)r   r   )
r   r"   r   r%   rC   r:   r   r	   r   r
   )r   rD   r   r   r   Ú&test_runWithPatchesRestoresOnExceptionŒ   s   z9MonkeyPatcherTests.test_runWithPatchesRestoresOnExceptionc                 C  s`   | j  | jdd¡ | j  |  | jjd¡ W d  ƒ n1 sw   Y  |  | 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  sP   |   t¡ | j tdƒ‚1 sw   Y  W d  ƒ dS 1 s!w   Y  dS )zh
        Exceptions propagate through the L{MonkeyPatcher} context-manager
        exit method.
        Ú	somethingN)r%   rC   r   r   r   r   r   Ú'test_contextManagerPropagatesExceptions¨   s
   ÿ"ÿz:MonkeyPatcherTests.test_contextManagerPropagatesExceptionsNr   )r   r   r   Ú__doc__r   r   r!   r#   r'   r+   r,   r=   r?   rA   rE   rF   rH   r   r   r   r   r      s    




	








r   N)rI   Ú
__future__r   Útyping_extensionsr   Útwisted.python.monkeyr   Útwisted.trialr   r   ÚSynchronousTestCaser   r   r   r   r   Ú<module>   s   