
    h                    v    S r SSKJr  SSKJr  SSKJr  SSKJr   " S S5      r	 " S S	\R                  5      rg
)z%
Tests for L{twisted.python.monkey}.
    )annotations)NoReturn)MonkeyPatcher)unittestc                      \ rS rSrSS jrSrg)TestObj   c                .    SU l         SU l        SU l        g )Nz	foo valuez	bar valuez	baz value)foobarbazselfs    _/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/test/test_monkey.py__init__TestObj.__init__   s        )r   r   r   NreturnNone)__name__
__module____qualname____firstlineno__r   __static_attributes__ r   r   r   r      s    r   r   c                      \ rS rSrSrSS jrSS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrSS jrSS jrSS jrSS jrSS jrSrg)MonkeyPatcherTests   z3
Tests for L{MonkeyPatcher} monkey-patching class.
c                ^    [        5       U l        [        5       U l        [        5       U l        g N)r   
testObjectoriginalObjectr   monkeyPatcherr   s    r   setUpMonkeyPatcherTests.setUp   s    !)%i*_r   c                   U R                   R                  5         U R                  U R                  R                  U R
                  R                  5        U R                  U R                  R                  U R
                  R                  5        U R                  U R                  R                  U R
                  R                  5        g)z<
A monkey patcher without patches shouldn't change a thing.
N)r$   patchassertEqualr#   r   r"   r   r   r   s    r   
test_emptyMonkeyPatcherTests.test_empty    s     	  " 	,,00$//2E2EF,,00$//2E2EF,,00$//2E2EFr   c                |   [        U R                  SS4U R                  SS45      nUR                  5         U R                  SU R                  R                  5        U R                  SU R                  R
                  5        U R                  U R                  R                  U R                  R                  5        g)ze
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_constructWithPatches,MonkeyPatcherTests.test_constructWithPatches,   s    
  __eV,tv.N
 	!4!45!4!45,,00$//2E2EFr   c                    U R                   R                  U R                  SS5        U R                   R                  5         U R	                  U R                  R
                  S5        g)zN
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_patchExisting%MonkeyPatcherTests.test_patchExisting9   sL    
 	##DOOUFC  ",,f5r   c                    U R                   R                  U R                  SS5        U R                  [        U R                   R
                  5        g)zD
Patching a non-existing attribute fails with an C{AttributeError}.
nowherezblow up pleaseN)r$   r3   r"   assertRaisesAttributeErrorr(   r   s    r   test_patchNonExisting(MonkeyPatcherTests.test_patchNonExistingB   s=     	##DOOY@PQ.$*<*<*B*BCr   c                   U R                   R                  U R                  SS5        U R                   R                  U R                  SS5        U R                   R                  5         U R	                  U R                  R
                  S5        U R                   R                  5         U R	                  U R                  R
                  U R                  R
                  5        g)zd
Adding a patch for an object and attribute that already have a patch
overrides the existing patch.
r   blahBLAHN)r$   r3   r"   r(   r)   r   restorer#   r   s    r   test_patchAlreadyPatched+MonkeyPatcherTests.test_patchAlreadyPatchedI   s    
 	##DOOUFC##DOOUFC  ",,f5""$,,d.A.A.E.EFr   c                   U R                   R                  U R                  SS5        U R                   R                  5         U R                   R	                  5         U R                  U R                  R                  U R                  R                  5        U R                   R	                  5         U R                  U R                  R                  U R                  R                  5        g)z8
Restoring an already-restored monkey patch is a no-op.
r   r=   N)r$   r3   r"   r(   r?   r)   r   r#   r   s    r   test_restoreTwiceIsANoOp+MonkeyPatcherTests.test_restoreTwiceIsANoOpU   s     	##DOOUFC  """$,,d.A.A.E.EF""$,,d.A.A.E.EFr   c                   ^ / mS	S
U4S jjjnU R                   R                  USSSS9nU R                  SU5        U R                  S/T5        g)z
runWithPatches should run the given callable, passing in all arguments
and keyword arguments, and return the return value of the callable.
Nc                ,   > TR                  XU45        g)Nr   )append)abclogs      r   f;MonkeyPatcherTests.test_runWithPatchesDecoration.<locals>.fg   s    JJay!r         
   )rJ   r   )rN   rO   rP   r!   )rH   intrI   rQ   rJ   z
int | Noner   str)r$   runWithPatchesr)   )r   rL   resultrK   s      @r   test_runWithPatchesDecoration0MonkeyPatcherTests.test_runWithPatchesDecoration`   sW    
 24	 	 ##221ab2A'*s+r   c                  ^  SU 4S jjnT R                   R                  T R                  SS5        T R                   R                  U5      nT R	                  ST R
                  R                  T R
                  R                  4U5        T R                   R                  U5      nT R	                  ST R
                  R                  T R
                  R                  4U5        g)zy
We should be able to call the same function with runWithPatches more
than once. All patches should apply for each call.
c                    > T R                   R                  T R                   R                  T R                   R                  4$ r!   )r"   r   r   r   r   s   r   rL   9MonkeyPatcherTests.test_repeatedRunWithPatches.<locals>.fu   s.    OO'')<)<doo>Q>QRRr   r   r-   N)r   ztuple[str, str, str])r$   r3   r"   rS   r)   r#   r   r   )r   rL   rT   s   `  r   test_repeatedRunWithPatches.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   U R                   R                  U R                  SS5        U R                  U R                  R
                  U R                  R
                  5        U R                   R                  S 5        U R                  U R                  R
                  U R                  R
                  5        g)zW
C{runWithPatches} should restore the original values after the function
has executed.
r   r-   c                     g r!   r   r   r   r   <lambda>@MonkeyPatcherTests.test_runWithPatchesRestores.<locals>.<lambda>   s    $r   N)r$   r3   r"   r)   r#   r   rS   r   s    r   test_runWithPatchesRestores.MonkeyPatcherTests.test_runWithPatchesRestores   s    
 	##DOOUFC,,00$//2E2EF)),7,,00$//2E2EFr   c                  ^  SU 4S jjnT R                   R                  T R                  SS5        T R                   R                  T R                  SS5        T R                  [        T R                   R
                  U5        T R                  T R                  R                  T R                  R                  5        T R                  T R                  R                  T R                  R                  5        g)z^
Test runWithPatches restores the original values even when the function
raises an exception.
c                    > T R                  T R                  R                  S5        T R                  T R                  R                  S5        [	        S5      e)Nr-   blahblahzSomething went wrong!)r)   r"   r   r   RuntimeErrorr   s   r   _DMonkeyPatcherTests.test_runWithPatchesRestoresOnException.<locals>._   sC    T__00&9T__00*=677r   r   r-   r   rd   N)r   r   )
r$   r3   r"   r8   re   rS   r)   r   r#   r   )r   rf   s   ` r   &test_runWithPatchesRestoresOnException9MonkeyPatcherTests.test_runWithPatchesRestoresOnException   s    	8
 	##DOOUFC##DOOUJG,(:(:(I(I1M,,d.A.A.E.EF,,d.A.A.E.EFr   c                ^   U R                   R                  U R                  SS5        U R                      U R                  U R                  R                  S5        SSS5        U R                  U R                  R                  U R
                  R                  5        g! , (       d  f       NI= f)zn
L{MonkeyPatcher} is a context manager that applies its patches on
entry and restore original values on exit.
r   zpatched valueN)r$   r3   r"   r)   r   r#   r   s    r   test_contextManager&MonkeyPatcherTests.test_contextManager   sw    
 	##DOOUOLT__00/B  ,,d.A.A.E.EF  s   'B
B,c                    U R                  [        5         U R                     [        S5      e! , (       d  f       O= f SSS5        g! , (       d  f       g= f)zP
Exceptions propagate through the L{MonkeyPatcher} context-manager
exit method.
	somethingN)r8   re   r$   r   s    r   'test_contextManagerPropagatesExceptions:MonkeyPatcherTests.test_contextManagerPropagatesExceptions   s>    
 |,##";// $## -,,s   A
/
=	A


A)r$   r#   r"   Nr   )r   r   r   r   __doc__r%   r*   r0   r4   r:   r@   rC   rU   rZ   r`   rh   rk   ro   r   r   r   r   r   r      sP    -

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