
    Ϫf8                         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j0                         G d de             Zy)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j8                  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.
    c                 $    t         |   |       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.
        N)super__init__)self
methodName	__class__s     :/usr/lib/python3/dist-packages/twisted/trial/_asynctest.pyr   zTestCase.__init__1   s     	$    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>._cbD   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>._ebI   sH    w}}./}}$5<<$c'l ++F33r   )addCallbacks)r   deferredr&   r   r'   s   ` `  r   assertFailurezTestCase.assertFailure=   s"    	
	4 $$S#..r   c                     	 ddl m  j                         	 	fd}t        j                  |t        j                  t                    }t               }t        j                  |      r/t        dj                  |            }t        j                  |      S t        j                  t        j                    j#                         |      }j%                  	||      |j'                  fd       |S )Nr   reactorc                    t        j                  d d d      }t        j                  |      }	 | j	                  |       y # t         j
                  $ rd 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AlreadyCalledErrorcrash	_timedOutgetTodoexpectedaddExpectedFailureaddError)	deftodor   r-   resultr   timeouts	       r   	onTimeoutz TestCase._run.<locals>.onTimeout[   s    ""("ZL(;G9EJA "A-		!++ 
- !%||~#a(8--dAt<OOD!,
-s   A A B?*B?>B?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   )twisted.internetr-   
getTimeoutr   suppressWarningsr   suppressDeprecationWarninggetattrinspectisgeneratorfunction	TypeErrorr#   r   failmaybeDeferredrunWithWarningsSuppressed_getSuppress	callLateraddBoth)
r   r   r=   r?   methodexcr9   rG   r-   r>   s
   ```    @@@r   _runzTestCase._runV   s    ,//#	- 	-, **t}}.@A
	 z*&&v.KRRC
 ::c?"++T->->-@&
   )Q7			@Ar   c                 &     | j                   |i |S rC   )run)r   argskwargss      r   __call__zTestCase.__call__   s    txx(((r   c                     | j                  d|      }|j                  | j                  | j                  |f|f       |S )NsetUpcallbackArgserrbackArgs)rZ   r(   deferTestMethod_ebDeferSetUpr   ignoredr=   r9   s       r   
deferSetUpzTestCase.deferSetUp   sF    IIgv&	   		 	 	
 r   c                 0    |j                   t              r7|j                  | | j                  | j                  |j
                               n8|j                  | |        |j                   t              r|j                          | j                  d |      S rC   )
r!   r   addSkip_getSkipReasonra   r"   r8   KeyboardInterruptstopdeferRunCleanupsr   r	   r=   s      r   rf   zTestCase._ebDeferSetUp   sl    7=="NN4!4!4TZZ!OPOOD'*w}}./$$T622r   c                    | j                  | j                  |      }|j                  | j                  | j                  |f|f       |j                  | j                  |       |j                  | j                  |       |S )Nrb   )rZ   _testMethodNamer(   _cbDeferTestMethod_ebDeferTestMethodrW   ro   deferTearDownrg   s       r   re   zTestCase.deferTestMethod   st    IId**F3	#### 		 	 	
 	
		$''0			$$$f-r   c                 x    | j                         "|j                  | | j                                |S d| _        |S )NT)r5   addUnexpectedSuccess_passed)r   rh   r=   s      r   rs   zTestCase._cbDeferTestMethod   s9    <<>%''dlln=   DLr   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 rC   )r5   r6   r7   r!   r   r   
addFailurerm   r8   rn   r   rk   rl   rN   rr   r"   )r   r;   r=   r<   s       r   rt   zTestCase._ebDeferTestMethod   s    ||~a 0%%dAt4WWT**H5dA&WW&'OOD!$KKMWWXNNd))'$8L8L*MqwwW OOD!$r   c                 b    | j                  d|      }|j                  | j                  |       |S )NtearDown)rZ   
addErrback_ebDeferTearDownrg   s       r   ru   zTestCase.deferTearDown   s+    IIj&)	T**F3r   c                     |j                  | |        |j                  t              r|j                          d| _        y NF)r8   r!   rm   rn   rx   rp   s      r   r~   zTestCase._ebDeferTearDown   s/    g&7==*+KKMr   c              #   h  K   g }t        | j                        dkD  rB| j                  j                         \  }}}	  ||i | t        | j                        dkD  rB|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   FN)	len	_cleanupspop	Exceptionappendr	   r0   r8   rx   )r   rh   r=   failuresfuncr]   r^   r;   s           r   ro   zTestCase.deferRunCleanups   s      $..!A%!%!3!3!5D$3D+F++ $..!A%  	!AOOD!$ DL	!  3 123s.   9B2
B  B2!B2 ,B/,B2.B//B2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r   )r   _JanitorpostCaseCleanuprx   BaseExceptionr8   r	   r0   	_observer	getErrorsflushLoggedErrors_removeObserver
addSuccess)r   r=   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rC   )r   r   postClassCleanupr   r8   r	   r0   )r   r=   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.   )
stacklevelrA   )warningswarnrM   _reactorMethods)akwnamer   s     r   _z&TestCase._makeReactorMethod.<locals>._   sB    MM 26t= + .4''-q7B77r    )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.
        )r3   iteratern   N)r   rN   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   rX   s       r   _undeprecateReactorzTestCase._undeprecateReactor  s:     !00668 	+LD&GT6*	+!r   c                 l   ddl m} | j                  |       d| _        	 | j	                  d|      }	 | 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)	rI   r-   r   r4   ri   _waitr   r   r   )r   r=   r-   r9   s       r   _runFixturesAndTestzTestCase._runFixturesAndTest  s     	-w'	.f-A+

1f%""6*$$W- f%""6*$$W-s"   B  A9 "B  9$BB   B3r;   r]   r^   returnNc                 *    t        |   |g|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     w!!5d5f55r   c                 "    | j                         S rC   )rU   )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.r@   )
r   acquireAttribute_parentsDEFAULT_TIMEOUT_DURATIONfloat
ValueErrorrQ   r   r   rM   )r   r>   s     r   rJ   zTestCase.getTimeout?  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 rC   )r   )anyresultss    r   r   zTestCase._wait.<locals>.append_  s    "s# #r   c                 ,    j                          y y rC   r3   )ignr-   r   s    r   r3   zTestCase._wait.<locals>.crashc  s    " #r   zreactor\.crash cannot be used.*)messagerA   c                  &     j                          y rC   r   r,   s   r   rn   zTestCase._wait.<locals>.stopn  s    MMOr   N)RuntimeErrorrI   r-   r   rK   r   rL   rM   r   rW   r   rn   r\   r4   rm   )r   r9   runningr   r3   rn   r-   r   s         @@r   r   zTestCase._waitV  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)$__name__
__module____qualname____doc__r   r*   failUnlessFailurerZ   r_   ri   rf   re   rs   rt   ru   r~   r   inlineCallbacksro   r   r   r   r   r   r   r   r   objectr]   r^   r   r   rJ   r   r   __classcell__)r   s   @r   r   r   !   s    
%/. &+Z)3
% 
 ! !"$ 5$B	"..
6"f*%
6.0gg
6AC
6	
6#1.  0 Gr   r   )r   rO   r   typingr   r   zope.interfacer   r   rI   r   r   twisted.pythonr	   twisted.trialr
   r   twisted.trial._synctestr   r   r   r   r   __annotations__	ITestCaser   r   r   r   <module>r      sn   
   ! & 
 * " & K Kt_! $t* ! V{" { {r   