
    hn<                        % S r SSKJr  SSKrSSKrSSKJrJr  SSKJ	r	  SSK
Jr  SSKJrJr  SSKJr  SS	KJr  SS
KJrJr  SSKJrJrJr  \" S5      r/ rS\S'   \	" \R:                  5       " S S\5      5       rg)z4
Things likely to be used by writers of unit tests.
    )annotationsN)CallableList)implementer)	ParamSpec)deferutils)_T)failure)itrialutil)FailTestSkipTestSynchronousTestCase_Pz
List[None]_wait_is_runningc                     ^  \ rS rSrSrSU 4S jjr      SS jr\rS rS r	S r
S rS	 rS
 r\R                  S 5       rS rS rS rS rS rS r        SU 4S jjrS rS r\4S jrSrU =r$ )TestCase!   a  
A unit test. The atom of the unit testing universe.

This class extends L{SynchronousTestCase} which extends C{unittest.TestCase}
from the standard library. The main feature is the ability to return
C{Deferred}s from tests and fixture methods and to have the suite wait for
those C{Deferred}s to fire.  Also provides new assertions such as
L{assertFailure}.

@ivar timeout: A real number of seconds. If set, the test will
raise an error if it takes longer than C{timeout} seconds.
If not set, util.DEFAULT_TIMEOUT_DURATION is used.
c                J   > [         TU ]  U5        SSKJn  X l        SU l        g)ag  
Construct an asynchronous test case for C{methodName}.

@param methodName: The name of a method on C{self}. This method should
be a unit test. That is, it should be a short method that calls some of
the assert* methods. If C{methodName} is unspecified,
L{SynchronousTestCase.runTest} will be used as the test method. This is
mostly useful for testing Trial.
r   reactorFN)super__init__twisted.internetr   _twistedPrivateScheduler_twistedPrivateNeedsTearDown)self
methodNamer   	__class__s      _/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/trial/_asynctest.pyr   TestCase.__init__1   s)     	$ 	- )0% -2)    c                B   ^ ^ U 4S jnUU 4S jnUR                  X45      $ )z
Fail if C{deferred} does not errback with one of C{expectedFailures}.
Returns the original Deferred with callbacks added. You will need
to return this Deferred from your test case.
c                .   > TR                  SU < 35      e)Nz$did not catch an error, instead got )failureException)ignorer   s    r!   _cb#TestCase.assertFailure.<locals>._cbR   s     ''6vjA r#   c                   > U R                   " T6 (       a  U R                  $ SR                  T[        U 5      5      nTR	                  U5      e)Nz
Expected: {!r}
Got:
{})checkvalueformatstrr&   )r   outputexpectedFailuresr   s     r!   _eb#TestCase.assertFailure.<locals>._ebW   sF    }}./}}$5<<$c'l ++F33r#   )addCallbacks)r   deferredr0   r(   r1   s   ` `  r!   assertFailureTestCase.assertFailureI   s     	
	4 $$S..r#   c                  ^ ^^^^ T R                  5       mUUU U4S jn[        R                  " U[        R                  " [
        S95      n[        R                  " U5      (       a0  [        SR                  U5      5      n[        R                  " U5      $ [        R                  " [        R                  T R                  5       U5      nT R                  R!                  TXF5      mUR#                  U4S j5        U$ )Nc                  > [         R                  " T< ST ST S35      n[        R                  " U5      n U R	                  U5        g ! [         R
                   as    TR                  R                  5         STl        TR                  5       nUb*  UR                  U5      (       a  TR                  TX#5         g TR                  TU5         g f = f)Nz (z) still running at z secsT)r   TimeoutErrorr   FailureerrbackAlreadyCalledErrorr   crash	_timedOutgetTodoexpectedaddExpectedFailureaddError)deftodofuncDescriptionresultr   timeouts       r!   	onTimeout TestCase._run.<locals>.onTimeoutg   s    ""("_--@	OA "A-		!++ 
- --335!%||~#a(8(8--dA<OOD!,
-s   A A0C=CCcategoryz9{!r} is a generator function and therefore will never runc                d   > TR                  5       =(       a    TR                  5       =(       d    U $ N)activecancel)xcalls    r!   <lambda>TestCase._run.<locals>.<lambda>   s    DKKM;dkkm@q@r#   )
getTimeoutr	   suppressWarningsr   suppressDeprecationWarninginspectisgeneratorfunction	TypeErrorr-   r   failmaybeDeferredrunWithWarningsSuppressed_getSuppressr   	callLateraddBoth)	r   funcrG   rH   rJ   excrC   rS   rI   s	   ` ``   @@r!   _runTestCase._rund   s    //#	- 	-, **t}}.@A
	 &&t,,KRRSWXC ::c?"++T->->-@$
 ,,66w	M			@Ar#   c                &    U R                   " U0 UD6$ rO   )run)r   argskwargss      r!   __call__TestCase.__call__   s    xx(((r#   c                x  #      U R                  U5      I S h  vN   U R                  U5      I S h  vN    U R                  (       a  U R                  U5      I S h  vN   g g  NJ N3! U R                  U5      I S h  vN    f = f N)! U R                  (       a  U R                  U5      I S h  vN    f f = f7frO   )_deferSetUpAndRun_deferRunCleanupsr   _deferTearDownr   rH   s     r!   _deferSetUpTestCase._deferSetUp   s     	25,,V444,,V44400))&111 1	 54d,,V444 2 00))&111 1sv   B:A( A$A( B	 A&B	 %B:BB:$A( &B	 (B=B >BB	 B:	&B7/B20B77B:c                H  #     U R                  U R                  SU5      I Sh  vN   SU l         U R                  [        X R                  5      U R                  U5      I Sh  vN   U R                  5       b   UR                  X R                  5       5        gSU l        g N}! [         a5  nUR                  X R	                  U R                  U5      5         SnAgSnAf[
         a@  nUR                  U [        R                  " U5      5        UR                  5          SnAgSnAf[         a0  nUR                  U [        R                  " U5      5         SnAgSnAff = f GN! [         a,  nU R#                  [        R                  " U5      U5        e SnAff = f7f)zS
Execute the setUp and run part of a test. Teardown and cleanups are not executed.
setUpNT)re   ru   r   addSkip_getSkipReasonKeyboardInterruptrB   r   r:   stopBaseExceptionr   getattr_testMethodNamer?   addUnexpectedSuccess_passed_ebDeferTestMethodr   rH   rD   s      r!   rn   TestCase._deferSetUpAndRun   s@    	))DJJ888 -1)
	))223T5I5I6   ||~)++D,,.A#+ 9 	NN4!4!4TZZ!CD  	OOD'//!"45KKM 	OOD'//!"45	  	##GOOA$6?	s   F" B$ B"B$ F"3E) $E&%4E) F"E) !F""B$ $
E#.+CF"E#+6D&!F"&E#3&EF"E##F"&E) )
F3'FFF"c           	     6   U R                  5       nUb)  UR                  U5      (       a  UR                  XU5        g UR                  U R                  [
        5      (       a  UR                  X5        g UR                  [        5      (       a"  UR                  X5        UR                  5         g UR                  [        5      (       a?  UR                  X R                  [        X R                  5      UR                  5      5        g UR                  X5        g rO   )r?   r@   rA   r+   r&   r   
addFailurerx   rB   ry   r   rv   rw   r{   r|   r,   )r   rE   rH   rF   s       r!   r   TestCase._ebDeferTestMethod   s    ||~a 0 0%%dt4WWT**H55d&WW&''OOD$KKMWWXNN))'$8L8L*MqwwW OOD$r#   c                  #     U R                  U R                  SU5      I S h  vN   g  N! [         aG  nUR                  U [        R
                  " U5      5        UR                  5         SU l         S nAg S nAf[         a7  nUR                  U [        R
                  " U5      5        SU l         S nAg S nAff = f7f)NtearDownF)	re   r   rx   rB   r   r:   ry   r~   rz   r   s      r!   rp   TestCase._deferTearDown   s     	!))DMM:v>>>  	!OOD'//!"45KKM DLL 	!OOD'//!"45 DLL	!sH   B? , *, B?, 
B<=A83B?8B<-B72B?7B<<B?c              #    ^^^#    / n[        U R                  5      S:  ab  U R                  R                  5       u  mmm U R                  UUU4S jSTR                   3U5      v   [        U R                  5      S:  a  Mb  U H  nUR                  X5        SU l
        M     g! [
         a'    UR                  [        R                  " 5       5         Nmf = f7f)zN
Run any scheduled cleanups and report errors (if any) to the result.
object.
r   c                    > T" T 0 TD6$ rO    )ri   rc   rj   s   r!   rT   ,TestCase._deferRunCleanups.<locals>.<lambda>   s    D$1&1r#   zcleanup function FN)len	_cleanupspopre   __name__	Exceptionappendr   r:   rB   r~   )r   rH   failuresrE   ri   rc   rj   s       @@@r!   ro   TestCase._deferRunCleanups   s      $..!A%!%!3!3!5D$3ii1'7  $..!A% AOOD$ DL   3 123s/   :C (B% (C"C%.CCCCc                    [         R                  " X5      R                  5       nU(       d  SU l        U R                  R                  5        H  nUR                  X5        SU l        M     U R                  5         U R                  5         U R                  (       a  UR                  U 5        g g ! [         a/    UR                  U [        R                  " 5       5        SU l         Nf = f)NF)r   _JanitorpostCaseCleanupr~   rz   rB   r   r:   	_observer	getErrorsflushLoggedErrors_removeObserver
addSuccess)r   rH   cleanerrors       r!   _cleanUpTestCase._cleanUp   s    	!MM$/??AE$ ^^--/EOOD( DL 0 	 <<d#   	!OOD'//"34 DL	!s   2B1 16C*)C*c                     [         R                  " X5      R                  5         g ! [         a(    UR	                  U [
        R                  " 5       5         g f = frO   )r   r   postClassCleanuprz   rB   r   r:   rq   s     r!   _classCleanUpTestCase._classCleanUp   s@    	5MM$'88: 	5OOD'//"34	5s   $' /AAc                   ^ ^ UU 4S jnU$ )z}
Create a method which wraps the reactor method C{name}. The new
method issues a deprecation warning and calls the original.
c                 t   > [         R                  " ST< ST< S3S[        S9  TR                  T   " U 0 UD6$ )Nzreactor.z8 cannot be used inside unit tests. In the future, using z7 will fail the test and may crash or hang the test run.   )
stacklevelrM   )warningswarnrY   _reactorMethods)akwnamer   s     r!   _&TestCase._makeReactorMethod.<locals>._  s@    MM 26t= + ''-q7B77r#   r   )r   r   r   s   `` r!   _makeReactorMethodTestCase._makeReactorMethod   s    	8 r#   c                    0 U l         S H6  n[        X5      U R                   U'   [        XU R                  U5      5        M8     g)z
Deprecate C{iterate}, C{crash} and C{stop} on C{reactor}. That is,
each method is wrapped in a function that issues a deprecation
warning, then calls the original.

@param reactor: The Twisted reactor.
)r=   iteratery   N)r   r{   setattrr   )r   r   r   s      r!   _deprecateReactorTestCase._deprecateReactor  s@      "0D)0)?D  &G4#:#:4#@A 1r#   c                p    U R                   R                  5        H  u  p#[        XU5        M     0 U l         g)zu
Restore the deprecated reactor methods. Undoes what
L{_deprecateReactor} did.

@param reactor: The Twisted reactor.
N)r   itemsr   )r   r   r   methods       r!   _undeprecateReactorTestCase._undeprecateReactor  s1     !00668LDG6* 9!r#   c                   SSK Jn  U R                  U5        SU l         [        R
                  R                  U R                  U5      5      n U R                  U5        U R                  U5        U R                  U5         U R                  U5        g! U R                  U5        U R                  U5        f = f! U R                  U5        f = f)z
Really run C{setUp}, the test method, and C{tearDown}.  Any of these may
return L{defer.Deferred}s. After they complete, do some reactor cleanup.

@param result: A L{TestResult} object.
r   r   FN)r   r   r   r>   r   DeferredfromCoroutinerr   _waitr   r   r   )r   rH   r   rC   s       r!   _runFixturesAndTestTestCase._runFixturesAndTest)  s     	-w'	.,,T-=-=f-EFA+

1f%""6*$$W- f%""6*$$W-s#   .B< B  "B< $B99B< <Cc                ,   > [         TU ]  " U/UQ70 UD6$ )a:  
Extend the base cleanup feature with support for cleanup functions which
return Deferreds.

If the function C{f} returns a Deferred, C{TestCase} will wait until the
Deferred has fired before proceeding to the next function.

If the function takes more than C{timeout} settings, then the test will
raise an error.
)r   
addCleanup)r   rE   ri   rj   r    s       r!   r   TestCase.addCleanup@  s     w!!5d5f55r#   c                "    U R                  5       $ rO   )r`   )r   s    r!   getSuppressTestCase.getSuppressO  s      ""r#   c                    [         R                  " U R                  S[         R                  5      n [	        U5      $ ! [
        [        4 a,    [        R                  " S[        S9  [         R                  s $ f = f)a5  
Returns the timeout value set on this test. Checks on the instance
first, then the class, then the module, then packages. As soon as it
finds something with a C{timeout} attribute, returns that. Returns
L{util.DEFAULT_TIMEOUT_DURATION} if it cannot find anything. See
L{TestCase} docstring for more details.
rI   z)'timeout' attribute needs to be a number.rL   )
r   acquireAttribute_parentsDEFAULT_TIMEOUT_DURATIONfloat
ValueErrorr\   r   r   rY   )r   rI   s     r!   rV   TestCase.getTimeoutR  sj     ''MM9d&C&C

	1>!I& 	1
 MM;FX 000	1s   
= 9A98A9c                  ^^ U(       a  [        S5      eSSKJm  / mU4S jnUU4S jn[        R                  " U[
        R                  " S[        S95      nU4S jn[        R                  " U[
        R                  " S[        S95      nUR                  S	5         UR                  U5        T(       a   S	mUR                  5         g	UR                  U5        UTl         TR                  5         T?T(       d  U R                  (       a   S	mUR                  5         g	[        5       e! T?f = f! S	mUR                  5         f = f)
zATake a Deferred that only ever callbacks. Block until it happens.z_wait is not reentrantr   r   c                0   > Tb  TR                  U 5        g g rO   )r   )anyresultss    r!   r   TestCase._wait.<locals>.appendr  s    "s# #r#   c                .   > Tb  TR                  5         g g rO   r=   )ignr   r   s    r!   r=   TestCase._wait.<locals>.crashv  s    " #r#   zreactor\.crash cannot be used.*)messagerM   c                 &   > T R                  5         g rO   r   r   s   r!   ry   TestCase._wait.<locals>.stop  s    MMOr#   N)RuntimeErrorr   r   r	   rW   r   rX   rY   r   rb   r   ry   rh   r>   rx   )r   rC   runningr   r=   ry   r   r   s         @@r!   r   TestCase._waiti  s   788,	$	  &&MM:EW
	 %%MM:EW
 	t$	IIf < GKKM= IIeGL!L $.." GKKM $%%- L0 GKKMs0   D2 
D2 #D+ 3D2 !
D2 +D//D2 2E)r~   r   r>   r   r   )runTest)r4   defer.Deferred[_T]r0   ztype[BaseException]returnr   )rE   zCallable[_P, object]ri   z_P.argsrj   z	_P.kwargsr   None)r   
__module____qualname____firstlineno____doc__r   r5   failUnlessFailurere   rk   rr   rn   r   rp   r   inlineCallbacksro   r   r   r   r   r   r   r   r   rV   r   r   __static_attributes____classcell__)r    s   @r!   r   r   !   s    20/*/?R/	/2 &&P)2>% 	! ! !*$ 5$B	"..6%6.56AJ6	6#1.  0 G Gr#   r   )r   
__future__r   rZ   r   typingr   r   zope.interfacer   typing_extensionsr   r   r   r	   twisted.internet.deferr
   twisted.pythonr   twisted.trialr   r   twisted.trial._synctestr   r   r   r   r   __annotations__	ITestCaser   r   r#   r!   <module>r      sp   
 #   ! & '
 * % " & K Kt_! * ! VN" N Nr#   