
    \dr                     b    d Z ddlmZ ddlmZ  G d d          Z G d dej                  ZdS )	z%
Tests for L{twisted.python.monkey}.
    )MonkeyPatcher)unittestc                       e Zd Zd ZdS )TestObjc                 0    d| _         d| _        d| _        d S )Nz	foo valuez	bar valuez	baz value)foobarbazselfs    8lib/python3.11/site-packages/twisted/test/test_monkey.py__init__zTestObj.__init__   s        N)__name__
__module____qualname__r    r   r   r   r      s#            r   r   c                   `    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd ZdS )MonkeyPatcherTestsz;
    Tests for L{MonkeyPatcher} monkey-patching class.
    c                 x    t                      | _        t                      | _        t                      | _        d S N)r   
testObjectoriginalObjectr   monkeyPatcherr   s    r   setUpzMonkeyPatcherTests.setUp   s+    !))%ii*__r   c                 4   | 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   s    r   
test_emptyzMonkeyPatcherTests.test_empty   s     	  """ 	,0$/2EFFF,0$/2EFFF,0$/2EFFFFFr   c                 B   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   patchers     r   test_constructWithPatchesz,MonkeyPatcherTests.test_constructWithPatches*   s    
  _eV,tv.N
 
 	!4555!4555,0$/2EFFFFFr   c                     | 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   s    r   test_patchExistingz%MonkeyPatcherTests.test_patchExisting7   sW    
 	##DOUFCCC  """,f55555r   c                     | 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   s    r   test_patchNonExistingz(MonkeyPatcherTests.test_patchNonExisting@   sD     	##DOY@PQQQ.$*<*BCCCCCr   c                    | 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   s    r   test_patchAlreadyPatchedz+MonkeyPatcherTests.test_patchAlreadyPatchedG   s    
 	##DOUFCCC##DOUFCCC  """,f555""$$$,d.A.EFFFFFr   c                    | 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   r0   r   r   r   r   s    r   test_restoreTwiceIsANoOpz+MonkeyPatcherTests.test_restoreTwiceIsANoOpS   s     	##DOUFCCC  """""$$$,d.A.EFFF""$$$,d.A.EFFFFFr   c                     g d	fd	}| j                             |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                 8                         | ||f           dS )Nr   )append)abclogs      r   fz;MonkeyPatcherTests.test_runWithPatchesDecoration.<locals>.fe   s     JJ1ay!!!5r         
   )r9   r   )r<   r=   r>   r   )r   runWithPatchesr   )r   r;   resultr:   s      @r   test_runWithPatchesDecorationz0MonkeyPatcherTests.test_runWithPatchesDecoration^   s}    
 	 	 	 	 	 	 #221ab2AA'''*s+++++r   c                 p     f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                  J     j         j         j         j         j         j        fS r   )r   r   r	   r
   r   s   r   r;   z9MonkeyPatcherTests.test_repeatedRunWithPatches.<locals>.fs   s    O')<do>QRRr   r   r!   N)r   r&   r   r?   r   r   r	   r
   )r   r;   r@   s   `  r   test_repeatedRunWithPatchesz.MonkeyPatcherTests.test_repeatedRunWithPatchesm   s    	S 	S 	S 	S 	S 	##DOUFCCC#22155T(,d.A.EF	
 	
 	
 #22155T(,d.A.EF	
 	
 	
 	
 	
r   c                 &   | j                             | j        dd           |                     | j        j        | j        j                   | j                             d            |                     | j        j        | j        j                   dS )zo
        C{runWithPatches} should restore the original values after the function
        has executed.
        r   r!   c                      d S r   r   r   r   r   <lambda>z@MonkeyPatcherTests.test_runWithPatchesRestores.<locals>.<lambda>   s    $ r   N)r   r&   r   r   r   r   r?   r   s    r   test_runWithPatchesRestoresz.MonkeyPatcherTests.test_runWithPatchesRestores   s    
 	##DOUFCCC,0$/2EFFF)),,777,0$/2EFFFFFr   c                      f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                                             j        j        d                                  j        j        d           t	          d          )Nr!   blahblahzSomething went wrong!)r   r   r   r	   RuntimeErrorr   s   r   _zDMonkeyPatcherTests.test_runWithPatchesRestoresOnException.<locals>._   sH    T_0&999T_0*===6777r   r   r!   r	   rK   N)
r   r&   r   r*   rL   r?   r   r   r   r	   )r   rM   s   ` r   &test_runWithPatchesRestoresOnExceptionz9MonkeyPatcherTests.test_runWithPatchesRestoresOnException   s    	8 	8 	8 	8 	8
 	##DOUFCCC##DOUJGGG,(:(I1MMM,d.A.EFFF,d.A.EFFFFFr   c                    | j                             | j        dd           | j         5  |                     | j        j        d           ddd           n# 1 swxY 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   s    r   test_contextManagerz&MonkeyPatcherTests.test_contextManager   s    
 	##DOUOLLL 	C 	CT_0/BBB	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C,d.A.EFFFFFs   !AAAc                     |                      t                    5  | j        5  t          d          # 1 swxY w Y   	 ddd           dS # 1 swxY w Y   dS )zh
        Exceptions propagate through the L{MonkeyPatcher} context-manager
        exit method.
        	somethingN)r*   rL   r   r   s    r   'test_contextManagerPropagatesExceptionsz:MonkeyPatcherTests.test_contextManagerPropagatesExceptions   s    
 |,, 	0 	0# 0 0";///0 0 0 0 0 0 0 0 0	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0s'   A37	A7	AAAN)r   r   r   __doc__r   r   r$   r'   r,   r1   r3   rA   rD   rH   rN   rP   rS   r   r   r   r   r      s         - - -

G 
G 
GG G G6 6 6D D D
G 
G 
G	G 	G 	G, , ,
 
 
&G G GG G G$G G G0 0 0 0 0r   r   N)rT   twisted.python.monkeyr   twisted.trialr   r   SynchronousTestCaser   r   r   r   <module>rX      s    
 0 / / / / / " " " " " "       Y0 Y0 Y0 Y0 Y05 Y0 Y0 Y0 Y0 Y0r   