
    Yf8                         U d 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mZ dd	lmZmZmZ  e	d
          Zg Zed         ed<    eej                   G d de                      ZdS )zP
Things likely to be used by writers of unit tests.

Maintainer: Jonathan Lange
    N)CallableList)implementer)	ParamSpec)deferutils)failure)itrialutil)FailTestSkipTestSynchronousTestCase_P_wait_is_runningc                        e Zd ZdZd fd	Zd ZeZd Zd Zd 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eeef         dej        dej        ddf 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.
    runTestc                 J    t                                          |           dS )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.
        N)super__init__)self
methodName	__class__s     J/var/www/html/env/lib/python3.11/site-packages/twisted/trial/_asynctest.pyr   zTestCase.__init__1   s#     	$$$$$    c                 H      fd} fd}|                     ||          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                 4                         d|           )Nz$did not catch an error, instead got )failureException)ignorer   s    r   _cbz#TestCase.assertFailure.<locals>._cbD   s&    ''AvAA  r   c                      | j          r| j        S d                    t          |                     }                    |          )Nz
Expected: {!r}
Got:
{})checkvalueformatstrr   )r	   outputexpectedFailuresr   s     r   _ebz#TestCase.assertFailure.<locals>._ebI   sS    w}./ 4}$5<<$c'll  ++F333r   )addCallbacks)r   deferredr'   r    r(   s   ` `  r   assertFailurezTestCase.assertFailure=   sS    	 	 	 	 	
	4 	4 	4 	4 	4 	4 $$S#...r   c                 (   	 ddl m                                  	 	fd}t          j        |t          j        t                              }t                     }t          j
        |          r6t          d                    |                    }t          j        |          S t          j        t          j                                         |          }                    	||          |                    fd           |S )Nr   reactorc                    t          j        d d d          }t          j        |          }	 |                     |           d S # t           j        $ rz                                  d_                                        }|/|	                    |          r
                    ||           Y d S                     |           Y d S w xY w)Nz (z) still running at z secsT)r   TimeoutErrorr	   FailureerrbackAlreadyCalledErrorcrash	_timedOutgetTodoexpectedaddExpectedFailureaddError)	deftodor   r.   resultr   timeouts	       r   	onTimeoutz TestCase._run.<locals>.onTimeout[   s    "JJZJJGJJJ A ""A-		!+ 
- 
- 
- !%||~~#a(8(8#--dAt<<<<<<OOD!,,,,,,
-s   A A,C:CCcategoryz9{!r} is a generator function and therefore will never runc                 X                                     r                                p| S N)activecancel)xcalls    r   <lambda>zTestCase._run.<locals>.<lambda>   s!    DKKMM;dkkmm@q r   )twisted.internetr.   
getTimeoutr   suppressWarningsr   suppressDeprecationWarninggetattrinspectisgeneratorfunction	TypeErrorr$   r   failmaybeDeferredrunWithWarningsSuppressed_getSuppress	callLateraddBoth)
r   r   r>   r@   methodexcr:   rH   r.   r?   s
   ```    @@@r   _runzTestCase._runV   s8   ,,,,,,//##	- 	- 	- 	- 	- 	- 	- 	- 	-, *t}.@AAA
 
	 z**&v.. 	#KRR  C
 :c??"+T->->-@-@&
 
   )Q77			@@@@AAAr   c                      | j         |i |S rD   )run)r   argskwargss      r   __call__zTestCase.__call__   s    tx((((r   c                 |    |                      d|          }|                    | j        | j        |f|f           |S )NsetUpcallbackArgserrbackArgs)r[   r)   deferTestMethod_ebDeferSetUpr   ignoredr>   r:   s       r   
deferSetUpzTestCase.deferSetUp   sM    IIgv&&	  		 	 	
 	
 	
 r   c                 @    |j         t                    r5|                    | |                     | j        |j                             n?|                    | |            |j         t                    r|                                 | 	                    d |          S rD   )
r"   r   addSkip_getSkipReasonrb   r#   r9   KeyboardInterruptstopdeferRunCleanupsr   r	   r>   s      r   rg   zTestCase._ebDeferSetUp   s    7="" 	NN4!4!4TZ!O!OPPPPOOD'***w}.// $$T6222r   c                     |                      | j        |          }|                    | j        | j        |f|f           |                    | j        |           |                    | j        |           |S )Nrc   )r[   _testMethodNamer)   _cbDeferTestMethod_ebDeferTestMethodrX   rp   deferTearDownrh   s       r   rf   zTestCase.deferTestMethod   s~    IId*F33	## 		 	 	
 	
 	
 	
		$'000			$$f---r   c                     |                                  )|                    | |                                             nd| _        |S )NT)r6   addUnexpectedSuccess_passed)r   ri   r>   s      r   rt   zTestCase._cbDeferTestMethod   s=    <<>>%''dllnn====DLr   c           	      r   |                                  }|.|                    |          r|                    | ||           d S |                    | j        t
                    r|                    | |           d S |                    t                    r,|                    | |           |	                                 d S |                    t                    rD|                    | |                     t          | | j                  |j                             d S |                    | |           d S rD   )r6   r7   r8   r"   r   r   
addFailurern   r9   ro   r   rl   rm   rO   rs   r#   )r   r<   r>   r=   s       r   ru   zTestCase._ebDeferTestMethod   s'   ||~~a 0 0%%dAt44444WWT*H55 
	%dA&&&&&WW&'' 	%OOD!$$$KKMMMMMWWX 	%NNd))'$8L*M*MqwWW     OOD!$$$$$r   c                 h    |                      d|          }|                    | j        |           |S )NtearDown)r[   
addErrback_ebDeferTearDownrh   s       r   rv   zTestCase.deferTearDown   s1    IIj&))	T*F333r   c                     |                     | |            |j        t                    r|                                 d| _        d S NF)r9   r"   rn   ro   ry   rq   s      r   r   zTestCase._ebDeferTearDown   sC    g&&&7=*++ 	KKMMMr   c              #   p  K   g }t          | j                  dk    rw| j                                        \  }}}	  ||i |V  n6# t          $ r) |                    t          j                               Y nw xY wt          | j                  dk    w|D ]}|                    | |           d| _         dS )zf
        Run any scheduled cleanups and report errors (if any) to the result.
        object.
        r   FN)	len	_cleanupspop	Exceptionappendr	   r1   r9   ry   )r   ri   r>   failuresfuncr^   r_   r<   s           r   rp   zTestCase.deferRunCleanups   s       $.!!A%%!%!3!3!5!5D$3dD+F++++++ 3 3 3 1 1222223	 $.!!A%%  	! 	!AOOD!$$$ DLL	! 	!s   
A 0A98A9c                    	 t          j        | |                                          }|sd| _        n># t          $ r1 |                    | t          j                               d| _        Y nw xY w| j        	                                D ]}|                    | |           d| _         | 
                                 |                                  | j        r|                    |            d S d S r   )r   _JanitorpostCaseCleanupry   BaseExceptionr9   r	   r1   	_observer	getErrorsflushLoggedErrors_removeObserver
addSuccess)r   r>   cleanerrors       r   _cleanUpzTestCase._cleanUp   s   	!M$//??AAE %$ 	! 	! 	!OOD'/"3"3444 DLLL	! ^--// 	! 	!EOOD%((( DLL   < 	$d#####	$ 	$s   03 8A.-A.c                     	 t          j        | |                                           d S # t          $ r+ |                    | t          j                               Y d S w xY wrD   )r   r   postClassCleanupr   r9   r	   r1   )r   r>   s     r   _classCleanUpzTestCase._classCleanUp   sk    	5M$''88::::: 	5 	5 	5OOD'/"3"3444444	5s   '+ 1A 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                  p    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.   )
stacklevelrB   )warningswarnrN   _reactorMethods)akwnamer   s     r   _z&TestCase._makeReactorMethod.<locals>._   sY    MM 26ttt= +    .4'-q7B777r    )r   r   r   s   `` r   _makeReactorMethodzTestCase._makeReactorMethod   s)    	8 	8 	8 	8 	8 	8 r   c                     i | _         dD ]>}t          ||          | j         |<   t          |||                     |                     ?dS )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.
        )r4   iteratero   N)r   rO   setattrr   )r   r.   r   s      r   _deprecateReactorzTestCase._deprecateReactor  sc      "0 	B 	BD)0$)?)?D &GT4#:#:4#@#@AAAA	B 	Br   c                 t    | j                                         D ]\  }}t          |||           i | _         dS )z
        Restore the deprecated reactor methods. Undoes what
        L{_deprecateReactor} did.

        @param reactor: The Twisted reactor.
        N)r   itemsr   )r   r.   r   rY   s       r   _undeprecateReactorzTestCase._undeprecateReactor  sI     !06688 	+ 	+LD&GT6****!r   c                    ddl m} |                     |           d| _        	 |                     d|          }	 |                     |           |                     |           |                     |           n/# |                     |           |                     |           w xY w	 |                     |           dS # |                     |           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)	rJ   r.   r   r5   rj   _waitr   r   r   )r   r>   r.   r:   s       r   _runFixturesAndTestzTestCase._runFixturesAndTest  s     	-,,,,,w'''	.f--A+

1f%%%""6**** f%%%""6*****$$W-----D$$W----s"   C A; +C ;,B''C Cr<   r^   r_   returnNc                 >     t                      j        |g|R i |S )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.
        )r   
addCleanup)r   r<   r^   r_   r   s       r   r   zTestCase.addCleanup0  s,     "uww!!5d555f555r   c                 *    |                                  S rD   )rV   )r   s    r   getSuppresszTestCase.getSuppress<  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.
        r?   z)'timeout' attribute needs to be a number.rA   )
r   acquireAttribute_parentsDEFAULT_TIMEOUT_DURATIONfloat
ValueErrorrR   r   r   rN   )r   r?   s     r   rK   zTestCase.getTimeout?  s     'M9d&C
 

	1>>!I& 	1 	1 	1
 M;FX    0000	1s   6 8A10A1c                    |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                              }|                    d	           	 |	                    |           r	 d	|
                                 d	S |	                    |           |_        	                                  `n# `w xY ws| j        r	 d	|
                                 d	S t                      # d	|
                                 w xY w)
zATake a Deferred that only ever callbacks. Block until it happens.z_wait is not reentrantr   r-   c                 :                         |            d S d S rD   )r   )anyresultss    r   r   zTestCase._wait.<locals>.append_  s)    "s##### #"r   c                 8                                      d S d S rD   r4   )ignr.   r   s    r   r4   zTestCase._wait.<locals>.crashc  s"    " #"r   zreactor\.crash cannot be used.*)messagerB   c                  0                                       d S rD   r   r-   s   r   ro   zTestCase._wait.<locals>.stopn  s    MMOOOOOr   N)RuntimeErrorrJ   r.   r   rL   r   rM   rN   r   rX   r   ro   r]   r5   rn   )r   r:   runningr   r4   ro   r.   r   s         @@r   r   zTestCase._waitV  s    	97888,,,,,,	$ 	$ 	$ 	$ 	$	  	  	  	  	  	  &M:EW  
 
	 	 	 	 	 %M:EW  
 
 	t$	IIf  < GKKMMMMM= IIeGL!LLGL      $. " GKKMMMMM $%%%GKKMMMMs0   D9 D9 +D ?D9 DD9 +D9 9E)r   )$__name__
__module____qualname____doc__r   r+   failUnlessFailurer[   r`   rj   rg   rf   rt   ru   rv   r   r   inlineCallbacksrp   r   r   r   r   r   r   r   r   objectr^   r_   r   r   rK   r   r   __classcell__)r   s   @r   r   r   !   s        
% 
% 
% 
% 
% 
%/ / /. &+ + +Z) ) )  3 3 3
 
 
  % % %   
   ! ! !"$ $ $ 5 5 5  $B B B	" 	" 	". . ..
6"f*%
6.0g
6AC
6	
6 
6 
6 
6 
6 
6# # #1 1 1.  0 G G G G G G G Gr   r   )r   rP   r   typingr   r   zope.interfacer   typing_extensionsr   rJ   r   r   twisted.pythonr	   twisted.trialr
   r   twisted.trial._synctestr   r   r   r   r   __annotations__	ITestCaser   r   r   r   <module>r      sA  
     ! ! ! ! ! ! ! ! & & & & & & ' ' ' ' ' '
 * ) ) ) ) ) ) ) " " " " " " & & & & & & & & K K K K K K K K K KYt__! $t* ! ! ! V{ { { { {" { { { { {r   