Ϫf8UdZddlZddlZddlmZmZddlmZddlmZddl m Z m Z ddl m Z ddlmZmZdd lmZmZmZed ZgZeded <eej0Gd d eZy)zP Things likely to be used by writers of unit tests. Maintainer: Jonathan Lange N)CallableList) implementer) ParamSpec)deferutils)failure)itrialutil)FailTestSkipTestSynchronousTestCase_P_wait_is_runningceZdZdZdfd ZdZeZdZdZdZ dZ dZ d Z d Z d Zd Zej"d ZdZdZdZdZdZdZdeeefdej8dej:ddffd ZdZdZ e!fdZ"xZ#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.pyrzTestCase.__init__1s $c@fd}fd}|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,jd|)Nz$did not catch an error, instead got )failureException)ignorers r_cbz#TestCase.assertFailure.._cbDs ''6vjA rc|jr |jSdjt|}j |)Nz Expected: {!r} Got: {})checkvalueformatstrr)r outputexpectedFailuresrs r_ebz#TestCase.assertFailure.._ebIsHw}}./}}$5<<$c'l++F33r) addCallbacks)rdeferredr&rr's` ` r assertFailurezTestCase.assertFailure=s"   4$$S#..rc ddlmj  fd}tj|t j t}t}tj|r/tdj|}tj|Stjtj j#|}j% |||j'fd|S)Nrreactorctjddd}tj|} |j |y#tj $rdj d_j}|&|j|rj||Yyj|YywxYw)Nz (z) still running at z secsT) r TimeoutErrorr FailureerrbackAlreadyCalledErrorcrash _timedOutgetTodoexpectedaddExpectedFailureaddError) deftodorr-resultrtimeouts r onTimeoutz TestCase._run..onTimeout[s""("ZL(;G9EJA"A - ! ++ - !%||~# a(8--dAt<OOD!, -sAA B?*B?>B?categoryz9{!r} is a generator function and therefore will never runcPjxrjxs|SN)activecancel)xcalls rzTestCase._run..sDKKM;dkkm@qr)twisted.internetr- getTimeoutrsuppressWarningsr suppressDeprecationWarninggetattrinspectisgeneratorfunction TypeErrorr#rfail maybeDeferredrunWithWarningsSuppressed _getSuppress callLateraddBoth) rrr=r?methodexcr9rGr-r>s ``` @@@r_runz TestCase._runVs,//# - -,** t}}.@A z*  & &v .KRRC ::c? "     + +T->->-@&   )Q7 @Arc&|j|i|SrC)run)rargskwargss r__call__zTestCase.__call__stxx(((rc|jd|}|j|j|j|f|f|S)NsetUp callbackArgs errbackArgs)rZr(deferTestMethod _ebDeferSetUprignoredr=r9s r deferSetUpzTestCase.deferSetUpsF IIgv &        rc0|jtr7|j||j|j|j n8|j |||jtr|j|jd|SrC) r!r addSkip_getSkipReasonrar"r8KeyboardInterruptstopdeferRunCleanupsrr r=s rrfzTestCase._ebDeferSetUpsl 7== " NN4!4!4TZZ!O P OOD' *w}}./ $$T622rc|j|j|}|j|j|j|f|f|j |j ||j |j||S)Nrb)rZ_testMethodNamer(_cbDeferTestMethod_ebDeferTestMethodrWro deferTearDownrgs rrezTestCase.deferTestMethodst IId**F 3   # #  # #   $''0 $$$f-rcx|j"|j||j|Sd|_|S)NT)r5addUnexpectedSuccess_passed)rrhr=s rrszTestCase._cbDeferTestMethods9 <<> %  ' 'dlln = DLrc |j}|%|j|r|j|||y|j|jt r|j ||y|jtr#|j|||jy|jtrA|j||jt||j|jy|j||yrC)r5r6r7r!rr addFailurermr8rnr rkrlrNrrr")rr;r=r<s rrtzTestCase._ebDeferTestMethods||~   a 0  % %dAt 4 WWT**H 5   dA & WW& ' OOD! $ KKM WWX  NNd))'$8L8L*MqwwW  OOD! $rcb|jd|}|j|j||S)NtearDown)rZ addErrback_ebDeferTearDownrgs rruzTestCase.deferTearDowns+ IIj& ) T**F3rc|j|||jtr|jd|_yNF)r8r!rmrnrxrps rr~zTestCase._ebDeferTearDowns/g& 7==* + KKM rc#hKg}t|jdkDrB|jj\}}} ||i|t|jdkDrB|D]}|j||d|_y#t$r&|j t j YhwxYww)zf Run any scheduled cleanups and report errors (if any) to the result. object. rFN) len _cleanupspop Exceptionappendr r0r8rx)rrhr=failuresfuncr]r^r;s rrozTestCase.deferRunCleanupss $..!A%!%!3!3!5 D$ 3D+F++$..!A% !A OOD! $ DL ! 3 12 3s.9B2 BB2!B2,B/,B2.B//B2c tj||j}|sd|_|jjD]}|j ||d|_|j|j|jr|j|yy#t$r.|j |t jd|_YwxYwr) r _JanitorpostCaseCleanuprx BaseExceptionr8r r0 _observer getErrorsflushLoggedErrors_removeObserver addSuccess)rr=cleanerrors r_cleanUpzTestCase._cleanUps !MM$/??AE$ ^^--/ !E OOD% ( DL !    <<   d #  ! OOD'//"3 4 DL !s-B&&4CCc tj||jy#t$r'|j |t j YywxYwrC)r rpostClassCleanuprr8r r0)rr=s r _classCleanUpzTestCase._classCleanUpsB 5 MM$ ' 8 8 : 5 OOD'//"3 4 5s$'-AAcfd}|S)z Create a method which wraps the reactor method C{name}. The new method issues a deprecation warning and calls the original. cttjddddtj|i|S)Nzreactor.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)warningswarnrM_reactorMethods)akwnamers r_z&TestCase._makeReactorMethod.._sB MM26t=+  .4''-q7B7 7r)rrrs`` r_makeReactorMethodzTestCase._makeReactorMethods  8rci|_dD]7}t|||j|<t|||j|9y)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. )r3iteraternN)rrNsetattrr)rr-rs r_deprecateReactorzTestCase._deprecateReactorsK "0 BD)0$)?D  & GT4#:#:4#@ A Brcp|jjD]\}}t|||i|_y)z Restore the deprecated reactor methods. Undoes what L{_deprecateReactor} did. @param reactor: The Twisted reactor. N)ritemsr)rr-rrXs r_undeprecateReactorzTestCase._undeprecateReactors:!00668 +LD& GT6 * +!rclddlm}|j|d|_ |j d|} |j ||j ||j| |j|y#|j ||j|wxYw#|j|wxYw)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. rr,FN) rIr-rr4ri_waitrrr)rr=r-r9s r_runFixturesAndTestzTestCase._runFixturesAndTests - w' .f-A + 1  f%""6*  $ $W - f%""6*  $ $W -s"B A9"B 9$BB B3r;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)rr;r]r^rs rrzTestCase.addCleanup0sw!!5d5f55rc"|jSrC)rU)rs r getSuppresszTestCase.getSuppress<s  ""rctj|jdtj} t |S#t t f$r.tjdttjcYSwxYw)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_parentsDEFAULT_TIMEOUT_DURATIONfloat ValueErrorrQrrrM)rr>s rrJzTestCase.getTimeout?sj'' MM9d&C&C  1> !I& 1 MM;FX 00 0 1s <:A98A9ch|r tdddlmgfd}fd}tj|t j dt}fd}tj|t j dt}|jd  |j|r d |jy |j||_ j` s |jr d |jy t#` wxYw#d |jwxYw) zATake a Deferred that only ever callbacks. Block until it happens.z_wait is not reentrantrr,c.j|yyrC)r)anyresultss rrzTestCase._wait..append_s"s##rc,jyyrCr3)ignr-rs rr3zTestCase._wait..crashcs" #rzreactor\.crash cannot be used.*)messagerAc&jyrCrr,srrnzTestCase._wait..stopns MMOrN) RuntimeErrorrIr-rrKr rLrMrrWrrnr\r4rm)rr9runningrr3rnr-rs @@rrzTestCase._waitVs 78 8, $ &&  MM:EW   %%  MM:EW   t$  IIf <G KKM= IIe GL ! L $.."G KKM$% %-L0G KKMs0D?DD(D DDDD1)runTest)$__name__ __module__ __qualname____doc__rr*failUnlessFailurerZr_rirfrersrtrur~rinlineCallbacksrorrrrrrrrobjectr]r^rrrJrr __classcell__)rs@rrr!s  %/.&+Z)3 %    !!"$ 5 $ B ".. 6"f*% 6.0gg 6AC 6  6#1. 0Grr)rrOrtypingrrzope.interfacerrrIrrtwisted.pythonr twisted.trialr r twisted.trial._synctestr r rrr__annotations__ ITestCaserrrrrsn !& *"&KKt_!$t*! V  {"{{r