
    Vhn<                        U d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZmZmZ  ed      Zg Zded<    e	ej:                         G d de             Zy)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                       e Zd ZdZd fd	Z	 	 	 	 	 	 ddZeZd Zd Zd Z	d Z
d Zd	 Zej                  d
        Zd Zd Zd Zd Zd Zd Z	 	 	 	 	 	 	 	 d fdZd Zd ZefdZ xZS )TestCasea=  
    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                L    t         |   |       ddlm} || _        d| _        y)a  
        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      H/home/dcms/DCMS/lib/python3.12/site-packages/twisted/trial/_asynctest.pyr   zTestCase.__init__1   s+     	$ 	- )0% -2)    c                @      fd} fd}|j                  ||      S )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                ,    j                  d|       )Nz$did not catch an error, instead got )failureException)ignorer   s    r    _cbz#TestCase.assertFailure.<locals>._cbR   s     ''6vjA r!   c                     | j                    r| j                  S dj                  t        |             }j	                  |      )Nz
Expected: {!r}
Got:
{})checkvalueformatstrr$   )r   outputexpectedFailuresr   s     r    _ebz#TestCase.assertFailure.<locals>._ebW   sH    w}}./}}$5<<$c'l ++F33r!   )addCallbacks)r   deferredr-   r&   r.   s   ` `  r    assertFailurezTestCase.assertFailureI   s"    	
	4 $$S#..r!   c                     j                          fd}t        j                  |t        j                  t
                    }t        j                  |      r/t        dj                  |            }t        j                  |      S t        j                  t        j                   j                         |      } j                  j!                  ||      |j#                  fd       |S )Nc                   t        j                  d d d      }t        j                  |      }	 | j	                  |       y # t         j
                  $ rn j                  j                          d_        j                         }|&|j                  |      rj                  ||       Y y j                  |       Y y w xY w)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    	onTimeoutz TestCase._run.<locals>.onTimeoutg   s    ""("_--@	OA "A-		!++ 
- --335!%||~#a(8--dAt<OOD!,
-s   A A*C	4C	C	categoryz9{!r} is a generator function and therefore will never runc                P    j                         xr j                         xs | S N)activecancel)xcalls    r    <lambda>zTestCase._run.<locals>.<lambda>   s    DKKM;dkkm@q r!   )
getTimeoutr	   suppressWarningsr   suppressDeprecationWarninginspectisgeneratorfunction	TypeErrorr*   r   failmaybeDeferredrunWithWarningsSuppressed_getSuppressr   	callLateraddBoth)	r   funcrB   rC   rE   excr>   rM   rD   s	   ` ``   @@r    _runzTestCase._rund   s    //#	-, **t}}.@A
	 &&t,KRRSWXC ::c?"++T->->-@$
 ,,66w	1M			@Ar!   c                &     | j                   |i |S rI   )run)r   argskwargss      r    __call__zTestCase.__call__   s    txx(((r!   c                d  K   	 	 | j                  |       d {    | j                  |       d {    	 | j                  r| j                  |       d {    y y 7 E7 .# | j                  |       d {  7   w xY w7 )# | j                  r| j                  |       d {  7   w w xY wwrI   )_deferSetUpAndRun_deferRunCleanupsr   _deferTearDownr   rC   s     r    _deferSetUpz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   B0A# AA# B A!B  B0BB0A# !B #A?8A;9A??B B0!B-%B(&B--B0c                <  K   	 | j                  | j                  d|       d{    d| _        	 | j                  t        | | j                        | j                  |       d{    | j                         !|j                  | | j                                yd| _        y7 ~# t        $ r6}|j                  | | j	                  | j                  |             Y d}~yd}~wt
        $ r?}|j                  | t        j                  |             |j                          Y d}~yd}~wt        $ r/}|j                  | t        j                  |             Y d}~yd}~ww xY w7 # t        $ r+}| j#                  t        j                  |      |        d}~ww xY ww)zc
        Execute the setUp and run part of a test. Teardown and cleanups are not executed.
        setUpNT)r^   rk   r   addSkip_getSkipReasonKeyboardInterruptr=   r   r5   stopBaseExceptionr   getattr_testMethodNamer:   addUnexpectedSuccess_passed_ebDeferTestMethodr   rC   r?   s      r    re   zTestCase._deferSetUpAndRun   sG    	))DJJ888 -1)
	))d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4E% %E#&4E% FE% "F#B% %	E .,CFE +5D% F%E 1%EFE  F#E% %	F.&FFFc           	        | j                         }|%|j                  |      r|j                  | ||       y |j                  | j                  t
              r|j                  | |       y |j                  t              r#|j                  | |       |j                          y |j                  t              rA|j                  | | j                  t        | | j                        |j                               y |j                  | |       y rI   )r:   r;   r<   r(   r$   r   
addFailurern   r=   ro   r   rl   rm   rq   rr   r)   )r   r@   rC   rA   s       r    ru   zTestCase._ebDeferTestMethod   s    ||~a 0%%dAt4WWT**H5dA&WW&'OOD!$KKMWWXNNd))'$8L8L*MqwwW OOD!$r!   c                z  K   	 | j                  | j                  d|       d {    y 7 # t        $ rF}|j                  | t	        j
                  |             |j                          d| _        Y d }~y d }~wt        $ r6}|j                  | t	        j
                  |             d| _        Y d }~y d }~ww xY ww)NtearDownF)	r^   rz   rn   r=   r   r5   ro   rt   rp   rv   s      r    rg   zTestCase._deferTearDown   s     	!))DMM:v>>>  	!OOD'//!"45KKM DLL 	!OOD'//!"45 DLL	!sH   B; , *, B;, 	B8<A61B;6B8,B3.B;3B88B;c              #    K   g }t        | j                        dkD  r_| j                  j                         \  	 | j                  fddj                   |       t        | j                        dkD  r_|D ]  }|j                  | |       d| _
         y# t
        $ r& |j                  t        j                                Y hw xY ww)zf
        Run any scheduled cleanups and report errors (if any) to the result.
        object.
        r   c                       i S rI    )ra   r\   rb   s   r    rN   z,TestCase._deferRunCleanups.<locals>.<lambda>   s    D$1&1 r!   zcleanup function FN)len	_cleanupspopr^   __name__	Exceptionappendr   r5   r=   rt   )r   rC   failuresr@   ra   r\   rb   s       @@@r    rf   zTestCase._deferRunCleanups   s      $..!A%!%!3!3!5D$3ii1'7  $..!A%  	!AOOD!$ DL	!  3 123s.   9C'B  &C?!C ,CCCCc                   	 t        j                  | |      j                         }|sd| _        | j                  j                         D ]  }|j                  | |       d| _         | j                          | j                          | j                  r|j                  |        y y # t        $ r. |j                  | t        j                                d| _        Y w xY w)NF)r   _JanitorpostCaseCleanuprt   rp   r=   r   r5   	_observer	getErrorsflushLoggedErrors_removeObserver
addSuccess)r   rC   cleanerrors       r    _cleanUpzTestCase._cleanUp   s    	!MM$/??AE$ ^^--/ 	!EOOD%( DL	! 	 <<d#   	!OOD'//"34 DL	!s   -B& &4CCc                    	 t        j                  | |      j                          y # t        $ r' |j	                  | t        j                                Y y w xY wrI   )r   r   postClassCleanuprp   r=   r   r5   rh   s     r    _classCleanUpzTestCase._classCleanUp   sB    	5MM$'88: 	5OOD'//"34	5s   $' -AAc                      fd}|S )z
        Create a method which wraps the reactor method C{name}. The new
        method issues a deprecation warning and calls the original.
        c                 t    t        j                  ddddt                j                     | i |S )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.   )
stacklevelrG   )warningswarnrR   _reactorMethods)akwnamer   s     r    _z&TestCase._makeReactorMethod.<locals>._  sB    MM 26t= + .4''-q7B77r!   r}   )r   r   r   s   `` r    _makeReactorMethodzTestCase._makeReactorMethod   s    	8 r!   c                    i | _         dD ]7  }t        ||      | j                   |<   t        ||| j                  |             9 y)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.
        )r8   iteratero   N)r   rq   setattrr   )r   r   r   s      r    _deprecateReactorzTestCase._deprecateReactor  sK      "0 	BD)0$)?D  &GT4#:#:4#@A	Br!   c                p    | j                   j                         D ]  \  }}t        |||        i | _         y)z
        Restore the deprecated reactor methods. Undoes what
        L{_deprecateReactor} did.

        @param reactor: The Twisted reactor.
        N)r   itemsr   )r   r   r   methods       r    _undeprecateReactorzTestCase._undeprecateReactor  s:     !00668 	+LD&GT6*	+!r!   c                   ddl m} | j                  |       d| _        	 t        j
                  j                  | j                  |            }	 | j                  |       | j                  |       | j                  |       	 | j                  |       y# | j                  |       | j                  |       w xY w# | j                  |       w xY w)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   r9   r   DeferredfromCoroutineri   _waitr   r   r   )r   rC   r   r>   s       r    _runFixturesAndTestz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        |   |g|i |S )ar  
        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   r@   ra   rb   r   s       r    r   zTestCase.addCleanup@  s     w!!5d5f55r!   c                "    | j                         S rI   )rY   )r   s    r    getSuppresszTestCase.getSuppressO  s      ""r!   c                    t        j                  | j                  dt         j                        }	 t	        |      S # t
        t        f$ r. t        j                  dt               t         j                  cY S w xY w)ae  
        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.
        rD   z)'timeout' attribute needs to be a number.rF   )
r   acquireAttribute_parentsDEFAULT_TIMEOUT_DURATIONfloat
ValueErrorrU   r   r   rR   )r   rD   s     r    rO   zTestCase.getTimeoutR  sj     ''MM9d&C&C

	1>!I& 	1
 MM;FX 000	1s   
< :A98A9c                h   |rt        d      ddlm g fd}fd}t        j                  |t        j                  dt                    }fd}t        j                  |t        j                  dt                    }|j                  d	       	 |j                  |       r	 d	|j                          y	|j                  |       |_        	 j                          `s| j                  r	 d	|j                          y	t               # `w xY w# d	|j                          w xY w)
zATake a Deferred that only ever callbacks. Block until it happens.z_wait is not reentrantr   r   c                .    j                  |        y y rI   )r   )anyresultss    r    r   zTestCase._wait.<locals>.appendr  s    "s# #r!   c                ,    j                          y y rI   r8   )ignr   r   s    r    r8   zTestCase._wait.<locals>.crashv  s    " #r!   zreactor\.crash cannot be used.*)messagerG   c                 &     j                          y rI   r   r   s   r    ro   zTestCase._wait.<locals>.stop  s    MMOr!   N)RuntimeErrorr   r   r	   rP   r   rQ   rR   r   r[   r   ro   r`   r9   rn   )r   r>   runningr   r8   ro   r   r   s         @@r    r   zTestCase._waiti  s   788,	$	  &&MM:EW
	 %%MM:EW
 	t$	IIf < GKKM= IIeGL!L $.." GKKM $%%- L0 GKKMs0   D ?D D (D 
D DD D1)runTest)r0   defer.Deferred[_T]r-   ztype[BaseException]returnr   )r@   zCallable[_P, object]ra   z_P.argsrb   z	_P.kwargsr   None)r   
__module____qualname____doc__r   r1   failUnlessFailurer^   rc   ri   re   ru   rg   r   inlineCallbacksrf   r   r   r   r   r   r   r   r   rO   r   r   __classcell__)r   s   @r    r   r   !   s    20/*/?R/	/2 &&P)2>% 	! ! !*$ 5$B	"..6%6.56AJ6	6#1.  0 Gr!   r   )r   
__future__r   rS   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!   