Ë
    oj‚8  ã                   óæ   — 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    ø€ Ø×'Ñ'Ø6°v°jÐ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­   ø€ Ü×"Ñ"Ø�(˜"˜Z˜LÐ(;¸G¸9ÀEÐJóˆAô —‘ Ó"ˆAð-Ø—	‘	˜!•øÜ×+Ñ+ò 
-ð —‘”Ø!%�”Ø—|‘|“~�ØÐ#¨¯©°aÔ(8Ø×-Ñ-¨d°A°tÖ<à—O‘O D¨!Ö,ð
-ú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   ø€ ˜DŸK™K›MÒ;¨d¯k©k«mÒ@¸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   € Øˆt�x‰x˜Ð( Ñ(Ð(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   € Ø�I‰I�g˜vÓ&ˆØ	�‰Ø× Ñ Ø×ÑØ ˜Ø˜	ð	 	ô 	
ð ˆ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�=‰=œÔ"Ø�N‰N˜4 ×!4Ñ!4°T·Z±ZÀÇÁÓ!OÕPà�O‰O˜D 'Ô*Øˆw�}‰}Ô.Ô/Ø—‘”Ø×$Ñ$ T¨6Ó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   € Ø�I‰I�d×*Ñ*¨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   € Ø�<‰<‹>Ð%Ø×'Ñ'¨¨d¯l©l«nÔ=ð ˆð  ˆDŒLØˆ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Ø×%Ñ% d¨A¨tÕ4Ø�W‰W�T×*Ñ*¬HÔ5Ø×Ñ˜d AÕ&Ø�W‰WÔ&Ô'Ø�O‰O˜D !Ô$Ø�K‰K�MØ�W‰W”XÔØ�N‰NØ�d×)Ñ)¬'°$¸×8LÑ8LÓ*MÈqÏwÉwÓWõð �O‰O˜D !Õ$r   c                 ób   — | j                  d|«      }|j                  | j                  |«       |S )NÚtearDown)rZ   Ú
addErrbackÚ_ebDeferTearDownrg   s       r   ru   zTestCase.deferTearDown¼   s+   € Ø�I‰I�j &Ó)ˆØ	�‰�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�=‰=Ô*Ô+Ø�K‰KŒMØˆ�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Ø�O‰O˜D !Ô$Ø ˆD�Lñ	!øô ò 3Ø—‘¤§¡Ó 1Ö2ð3üs.   ‚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¾   € ð	!Ü—M‘M $¨Ó/×?Ñ?ÓAˆEÙØ$�”ð —^‘^×-Ñ-Ó/ò 	!ˆEØ�O‰O˜D %Ô(Ø ˆD�Lð	!ð 	×ÑÔ Ø×ÑÔØ�<Š<Ø×Ñ˜dÕ#ð øô ò 	!Ø�O‰O˜D¤'§/¡/Ó"3Ô4Ø ˆDŽLð	!ú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Ü�M‰M˜$ Ó'×8Ñ8Õ:øÜò 	5Ø�O‰O˜D¤'§/¡/Ó"3Ö4ð	5ús   ‚$' §-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   ø€ Ü�MŠMò 26²tð=ð Ü+õð .�4×'Ñ'¨Ñ-¨qÐ7°BÑ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× Ñ  Ñ&Ü�G˜T 4×#:Ñ#:¸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ò 	+‰LˆD�&Ü�G˜T 6Õ*ð	+à!ˆÕ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Ñ! !Ð5 dÒ5¨fÑ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   € ô ×'Ñ'Ø�M‰M˜9¤d×&CÑ&Có
ˆð
	1Ü˜“>Ð!øÜœIÐ&ò 	1ô
 �M‰MØ;ÔFXõô ×0Ñ0Ò0ð	1ús   ±
< ¼: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   ø€ Ø�M‰M�Or   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å,àˆô	$õ	 ô ×&Ñ&ØÜ�M‰MØ:ÔEWôó
ˆô	ô ×%Ñ%ØÜ�M‰MØ:ÔEWôó
ˆð 	�‰�tÔð$	Ø�I‰I�fÔÙð ð< ˆGØ�K‰K�Mð= �I‰I�eÔØˆGŒLð!Ø—‘”à�Lñ ˜$Ÿ.š.Øð" ˆGØ�K‰K�Mô $Ó%Ð%øð- ‘Lûð0 ˆGØ�K‰K�Mús0   Â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Ø.0¯g©gð
6ØACÇÁð
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   