
    \de7                         U d Z ddlZddl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 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)List)implementer)deferutils)failure)itrialutil)FailTestSkipTestSynchronousTestCase_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 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     8lib/python3.11/site-packages/twisted/trial/_asynctest.pyr   zTestCase.__init__-   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>._cb@   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>._ebE   sS    w}./ 4}$5<<$c'll  ++F333r   )addCallbacks)r   deferredr$   r   r%   s   ` `  r   assertFailurezTestCase.assertFailure9   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>.onTimeoutW   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excr7   rE   r+   r<   s
   ```    @@@r   _runzTestCase._runR   s8   ,,,,,,//##	- 	- 	- 	- 	- 	- 	- 	- 	-, *t}.@AAA
 
	 z**&v.. 	#KRR  C
 :c??"+T->->-@-@&
 
   )Q77			@@@@AAAr   c                      | j         |i |S rA   )run)r   argskwargss      r   __call__zTestCase.__call__   s    tx((((r   c                 |    |                      d|          }|                    | j        | j        |f|f           |S )NsetUpcallbackArgserrbackArgs)rX   r&   deferTestMethod_ebDeferSetUpr   ignoredr;   r7   s       r   
deferSetUpzTestCase.deferSetUp   sM    IIgv&&	  		 	 	
 	
 	
 r   c                 @    |j         t                    r5|                    | |                     | j        |j                             n?|                    | |            |j         t                    r|                                 | 	                    d |          S rA   )
r   r   addSkip_getSkipReasonr_   r    r6   KeyboardInterruptstopdeferRunCleanupsr   r   r;   s      r   rd   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 )Nr`   )rX   _testMethodNamer&   _cbDeferTestMethod_ebDeferTestMethodrU   rm   deferTearDownre   s       r   rc   zTestCase.deferTestMethod   s~    IId*F33	## 		 	 	
 	
 	
 	
		$'000			$$f---r   c                     |                                  )|                    | |                                             nd| _        |S )NT)r3   addUnexpectedSuccess_passed)r   rf   r;   s      r   rq   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 rA   )r3   r4   r5   r   r   r
   
addFailurerk   r6   rl   r   ri   rj   rL   rp   r    )r   r9   r;   r:   s       r   rr   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)rX   
addErrback_ebDeferTearDownre   s       r   rs   zTestCase.deferTearDown   s1    IIj&))	T*F333r   c                     |                     | |            |j        t                    r|                                 d| _        d S NF)r6   r   rk   rl   rv   rn   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   r.   r6   rv   )r   rf   r;   failuresfuncr[   r\   r9   s           r   rm   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postCaseCleanuprv   BaseExceptionr6   r   r.   	_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rA   )r	   r   postClassCleanupr   r6   r   r.   )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.   )
stacklevelr?   )warningswarnrK   _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.
        )r1   iteraterl   N)r   rL   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   rV   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)	rG   r+   r   r2   rg   _waitr   r   r   )r   r;   r+   r7   s       r   _runFixturesAndTestzTestCase._runFixturesAndTest  s     	-,,,,,w'''	.f--A+

1f%%%""6**** f%%%""6*****$$W-----D$$W----s"   C A; +C ;,B''C Cc                 >     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   r9   r[   r\   r   s       r   r   zTestCase.addCleanup*  s,     "uww!!5d555f555r   c                 *    |                                  S rA   )rS   )r   s    r   getSuppresszTestCase.getSuppress4  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.r>   )
r	   acquireAttribute_parentsDEFAULT_TIMEOUT_DURATIONfloat
ValueErrorrO   r   r   rK   )r   r<   s     r   rH   zTestCase.getTimeout7  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 rA   )r   )anyresultss    r   r   zTestCase._wait.<locals>.appendW  s)    "s##### #"r   c                 8                                      d S d S rA   r1   )ignr+   r   s    r   r1   zTestCase._wait.<locals>.crash[  s"    " #"r   zreactor\.crash cannot be used.*)messager?   c                  0                                       d S rA   r   r*   s   r   rl   zTestCase._wait.<locals>.stopf  s    MMOOOOOr   N)RuntimeErrorrG   r+   r   rI   r	   rJ   rK   r   rU   r   rl   rZ   r2   rk   )r   r7   runningr   r1   rl   r+   r   s         @@r   r   zTestCase._waitN  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(   failUnlessFailurerX   r]   rg   rd   rc   rq   rr   rs   r|   r   inlineCallbacksrm   r   r   r   r   r   r   r   r   rH   r   r   __classcell__)r   s   @r   r   r      s        
% 
% 
% 
% 
% 
%/ / /. &+ + +Z) ) )  3 3 3
 
 
  % % %   
   ! ! !"$ $ $ 5 5 5  $B B B	" 	" 	". . .*6 6 6 6 6# # #1 1 1.  0 G G G G G G G Gr   r   )r   rM   r   typingr   zope.interfacer   rG   r   r   twisted.pythonr   twisted.trialr   r	   twisted.trial._synctestr
   r   r   r   __annotations__	ITestCaser   r   r   r   <module>r      s  
           & & & & & &
 * ) ) ) ) ) ) ) " " " " " " & & & & & & & & K K K K K K K K K K! $t* ! ! ! Vw w w w w" w w w w wr   