
    Vh                    v    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      Z	 G d d	ej                        Zy
)z%
Tests for L{twisted.python.monkey}.
    )annotations)NoReturn)MonkeyPatcher)unittestc                      e Zd ZddZy)TestObjc                .    d| _         d| _        d| _        y )Nz	foo valuez	bar valuez	baz value)foobarbazselfs    H/home/dcms/DCMS/lib/python3.12/site-packages/twisted/test/test_monkey.py__init__zTestObj.__init__   s        NreturnNone)__name__
__module____qualname__r    r   r   r   r      s    r   r   c                  x    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dZddZy)MonkeyPatcherTestsz;
    Tests for L{MonkeyPatcher} monkey-patching class.
    c                ^    t               | _        t               | _        t               | _        y N)r   
testObjectoriginalObjectr   monkeyPatcherr   s    r   setUpzMonkeyPatcherTests.setUp   s    !)%i*_r   c                   | j                   j                          | j                  | j                  j                  | j
                  j                         | j                  | j                  j                  | j
                  j                         | j                  | j                  j                  | j
                  j                         y)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0$//2E2EF,,00$//2E2EF,,00$//2E2EFr   c                |   t        | j                  ddf| j                  ddf      }|j                          | j                  d| j                  j                         | j                  d| j                  j
                         | j                  | j                  j                  | j                  j                         y)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!45!4!45,,00$//2E2EFr   c                    | j                   j                  | j                  dd       | j                   j                          | j	                  | j                  j
                  d       y)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_patchExisting9   sL    
 	##DOOUFC  ",,f5r   c                    | j                   j                  | j                  dd       | j                  t        | j                   j
                         y)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_patchNonExistingB   s=     	##DOOY@PQ.$*<*<*B*BCr   c                   | j                   j                  | j                  dd       | j                   j                  | j                  dd       | j                   j                          | j	                  | j                  j
                  d       | j                   j                          | j	                  | j                  j
                  | j                  j
                         y)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_patchAlreadyPatchedI   s    
 	##DOOUFC##DOOUFC  ",,f5""$,,d.A.A.E.EFr   c                   | j                   j                  | j                  dd       | j                   j                          | j                   j	                          | j                  | j                  j                  | j                  j                         | j                   j	                          | j                  | j                  j                  | j                  j                         y)zH
        Restoring an already-restored monkey patch is a no-op.
        r
   r3   N)r   r+   r   r"   r5   r#   r
   r   r   s    r   test_restoreTwiceIsANoOpz+MonkeyPatcherTests.test_restoreTwiceIsANoOpU   s     	##DOOUFC  """$,,d.A.A.E.EF""$,,d.A.A.E.EFr   c                    g d	d
fd}| j                   j                  |ddd      }| j                  d|       | j                  dg       y)z
        runWithPatches should run the given callable, passing in all arguments
        and keyword arguments, and return the return value of the callable.
        Nc                .    j                  | ||f       y)Nr
   )append)abclogs      r   fz;MonkeyPatcherTests.test_runWithPatchesDecoration.<locals>.fg   s    JJ1ay!r         
   )r>   r
   )rA   rB   rC   r   )r<   intr=   rD   r>   z
int | Noner   str)r   runWithPatchesr#   )r   r@   resultr?   s      @r   test_runWithPatchesDecorationz0MonkeyPatcherTests.test_runWithPatchesDecoration`   sR    
 24	 ##221ab2A'*s+r   c                    d fd} j                   j                   j                  dd        j                   j                  |      } j	                  d j
                  j                   j
                  j                  f|        j                   j                  |      } j	                  d j
                  j                   j
                  j                  f|       y)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                  fS r   )r   r
   r   r   r   s   r   r@   z9MonkeyPatcherTests.test_repeatedRunWithPatches.<locals>.fu   s.    OO'')<)<doo>Q>QRRr   r
   r&   N)r   ztuple[str, str, str])r   r+   r   rF   r#   r   r   r   )r   r@   rG   s   `  r   test_repeatedRunWithPatchesz.MonkeyPatcherTests.test_repeatedRunWithPatcheso   s    	S 	##DOOUFC##2215T((,,d.A.A.E.EF	
 ##2215T((,,d.A.A.E.EF	
r   c                r   | j                   j                  | j                  dd       | j                  | j                  j
                  | j                  j
                         | j                   j                  d        | j                  | j                  j
                  | j                  j
                         y)zo
        C{runWithPatches} should restore the original values after the function
        has executed.
        r
   r&   c                      y r   r   r   r   r   <lambda>z@MonkeyPatcherTests.test_runWithPatchesRestores.<locals>.<lambda>   s    r   N)r   r+   r   r#   r   r
   rF   r   s    r   test_runWithPatchesRestoresz.MonkeyPatcherTests.test_runWithPatchesRestores   s    
 	##DOOUFC,,00$//2E2EF)),7,,00$//2E2EFr   c                    d fd} j                   j                   j                  dd        j                   j                   j                  dd        j                  t         j                   j
                  |        j                   j                  j                   j                  j                          j                   j                  j                   j                  j                         y)zv
        Test runWithPatches restores the original values even when the function
        raises an exception.
        c                      j                   j                  j                  d        j                   j                  j                  d       t	        d      )Nr&   blahblahzSomething went wrong!)r#   r   r
   r   RuntimeErrorr   s   r   _zDMonkeyPatcherTests.test_runWithPatchesRestoresOnException.<locals>._   sC    T__00&9T__00*=677r   r
   r&   r   rR   N)r   r   )
r   r+   r   r/   rS   rF   r#   r
   r   r   )r   rT   s   ` r   &test_runWithPatchesRestoresOnExceptionz9MonkeyPatcherTests.test_runWithPatchesRestoresOnException   s    	8
 	##DOOUFC##DOOUJG,(:(:(I(I1M,,d.A.A.E.EF,,d.A.A.E.EFr   c                T   | j                   j                  | j                  dd       | j                   5  | j                  | j                  j                  d       ddd       | j                  | j                  j                  | j
                  j                         y# 1 sw Y   DxY w)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    
 	##DOOUOL 	CT__00/B	C,,d.A.A.E.EF	C 	Cs   'BB'c                    | j                  t              5  | j                  5  t        d      # 1 sw Y   nxY w	 ddd       y# 1 sw Y   yxY w)zh
        Exceptions propagate through the L{MonkeyPatcher} context-manager
        exit method.
        	somethingN)r/   rS   r   r   s    r   'test_contextManagerPropagatesExceptionsz:MonkeyPatcherTests.test_contextManagerPropagatesExceptions   sP    
 |, 	0## 0";//0 0 0	0 	0 	0s   A/8	AANr   )r   r   r   __doc__r    r$   r)   r,   r1   r6   r8   rH   rK   rO   rU   rW   rZ   r   r   r   r   r      sP    -

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