3 « f]ãã@s¶dZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m Z ddlmZmZmZmZmZdZeƒZdZGdd „d eƒZGd d „d eƒZGd d „d eƒZGdd„deƒZdd„Zdd„Zdd„Zdd„Z dd„Z!dd„Z"Gdd„dƒZ#dd„Z$d d!„Z%Gd"d#„d#e#ƒZ&Gd$d%„d%e&ƒZ'Gd&d'„d'e&ƒZ(e j)d(d)d*gƒZ*Gd+d,„d,ej+ƒZ,Gd-d.„d.e#ƒZ-Gd/d0„d0eƒZ.Gd1d2„d2e.ƒZ/Gd3d4„d4e.ƒZ0dS)5zTest case implementationéNé)Úresult)ÚstrclassÚ safe_reprÚ_count_diff_all_purposeÚ_count_diff_hashableÚ_common_shorten_reprTz@ Diff is %s characters long. Set self.maxDiff to None to see it.c@seZdZdZdS)ÚSkipTestz© Raise this exception in a test to skip it. Usually you can use TestCase.skipTest() or one of the skipping decorators instead of raising this directly. N)Ú__name__Ú __module__Ú __qualname__Ú__doc__©rrú2/opt/alt/python36/lib64/python3.6/unittest/case.pyr sr c@seZdZdZdS)Ú _ShouldStopz The test should stop. N)r r r r rrrrr"src@seZdZdZdS)Ú_UnexpectedSuccessz7 The test was supposed to fail, but it didn't! N)r r r r rrrrr'src@s&eZdZddd„Zejddd„ƒZdS) Ú_OutcomeNcCs4d|_||_t|dƒ|_d|_g|_d|_g|_dS)NFÚ addSubTestT)Úexpecting_failurerÚhasattrÚresult_supports_subtestsÚsuccessÚskippedÚexpectedFailureÚerrors)ÚselfrrrrÚ__init__.s z_Outcome.__init__Fccsè|j}d|_zÈy dVWnœtk r.‚Yn¤tk rh}z d|_|jj|t|ƒfƒWYdd}~Xnjtk rzYnXtjƒ}|j r–||_ nd|_|j j||fƒd}YnX|j rÒ|jrÒ|j j|dfƒWd|joÞ||_XdS)NTF) rÚKeyboardInterruptr rÚappendÚstrrÚsysÚexc_inforrrr)rÚ test_caseÚisTestZ old_successÚer!rrrÚtestPartExecutor7s* $  z_Outcome.testPartExecutor)N)F)r r r rÚ contextlibÚcontextmanagerr%rrrrr-s rcCs|S)Nr)ÚobjrrrÚ_idUsr)cs‡fdd„}|S)z& Unconditionally skip a test. cs4t|tƒs$tj|ƒ‡fdd„ƒ}|}d|_ˆ|_|S)Ncs tˆƒ‚dS)N)r )ÚargsÚkwargs)ÚreasonrrÚ skip_wrapper^sz-skip..decorator..skip_wrapperT)Ú isinstanceÚtypeÚ functoolsÚwrapsÚ__unittest_skip__Ú__unittest_skip_why__)Ú test_itemr-)r,rrÚ decorator\s  zskip..decoratorr)r,r5r)r,rÚskipXs r6cCs|r t|ƒStS)z/ Skip a test if the condition is true. )r6r))Ú conditionr,rrrÚskipIfhsr8cCs|s t|ƒStS)z3 Skip a test unless the condition is true. )r6r))r7r,rrrÚ skipUnlesspsr9cCs d|_|S)NT)Ú__unittest_expecting_failure__)r4rrrrxsrcs4t|tƒr t‡fdd„|DƒƒSt|tƒo2t|ˆƒS)Nc3s|]}t|ˆƒVqdS)N)Ú _is_subtype)Ú.0r$)Úbasetyperrú ~sz_is_subtype..)r.ÚtupleÚallr/Ú issubclass)Úexpectedr=r)r=rr;|s r;c@seZdZdd„Zdd„ZdS)Ú_BaseTestCaseContextcCs ||_dS)N)r")rr"rrrrƒsz_BaseTestCaseContext.__init__cCs |jj|j|ƒ}|jj|ƒ‚dS)N)r"Ú_formatMessageÚmsgÚfailureException)rÚ standardMsgrErrrÚ _raiseFailure†sz"_BaseTestCaseContext._raiseFailureN)r r r rrHrrrrrC�srCcCsdtjkrt|ƒStSdS)a Non-standard/downstream-only decorator for marking a specific unit test to be skipped when run within the %check of an rpmbuild. Specifically, this takes effect when WITHIN_PYTHON_RPM_BUILD is set within the environment, and has no effect otherwise. ÚWITHIN_PYTHON_RPM_BUILDN)ÚosÚenvironr6r))r,rrrÚ_skipInRpmBuild�s rLcstjˆƒ‡fdd„ƒ}|S)a  Non-standard/downstream-only decorator for marking a specific unit test as expected to fail within the %check of an rpmbuild. Specifically, this takes effect when WITHIN_PYTHON_RPM_BUILD is set within the environment, and has no effect otherwise. c sNdtjkr@yˆ||ŽWn tk r8ttjƒƒ‚YnXt‚n ˆ||ŽdS)NrI)rJrKÚ ExceptionZ_ExpectedFailurer r!r)r*r+)ÚfuncrrÚwrapper¢s z+_expectedFailureInRpmBuild..wrapper)r0r1)rNrOr)rNrÚ_expectedFailureInRpmBuildšs rPc@seZdZddd„Zdd„ZdS)Ú_AssertRaisesBaseContextNcCs@tj||ƒ||_||_|dk r*tj|ƒ}||_d|_d|_dS)N) rCrrBr"ÚreÚcompileÚexpected_regexÚobj_namerE)rrBr"rTrrrr±s  z!_AssertRaisesBaseContext.__init__cCsÔzÈt|j|jƒs"td||jfƒ‚|rD|ddkrDtjdtdƒf}|sx|jddƒ|_ |rttjdt t |ƒƒtdƒ|S|^}}y |j |_ Wntk rªt|ƒ|_ YnX|�|||ŽWdQRXWdd}XdS)zî If args is empty, assertRaises/Warns is being used as a context manager, so check for a 'msg' kwarg and return self. If args is not empty, call a callable passing positional and keyword arguments. z%s() arg 1 must be %srNzcallable is NoneérEz3%r is an invalid keyword argument for this function)r;rBÚ _base_typeÚ TypeErrorÚ_base_type_strÚwarningsÚwarnÚDeprecationWarningÚpoprEÚnextÚiterr rUÚAttributeErrorr)rÚnamer*r+Z callable_objrrrÚhandle»s.  z_AssertRaisesBaseContext.handle)N)r r r rrbrrrrrQ¯s rQc@s(eZdZdZeZdZdd„Zdd„ZdS)Ú_AssertRaisesContextzCA context manager used to implement TestCase.assertRaises* methods.z-an exception type or tuple of exception typescCs|S)Nr)rrrrÚ __enter__äsz_AssertRaisesContext.__enter__c CsÆ|dkrby |jj}Wntk r2t|jƒ}YnX|jrP|jdj||jƒƒql|jdj|ƒƒn tj|ƒt ||jƒs|dS|j dƒ|_ |j dkr–dS|j }|j t|ƒƒsÂ|jdj|jt|ƒƒƒdS)Nz{} not raised by {}z {} not raisedFTz"{}" does not match "{}")rBr r`rrUrHÚformatÚ tracebackÚ clear_framesrAÚwith_tracebackZ exceptionrTÚsearchÚpattern)rÚexc_typeÚ exc_valueÚtbÚexc_namerTrrrÚ__exit__çs(       z_AssertRaisesContext.__exit__N) r r r r Ú BaseExceptionrWrYrdrorrrrrcÞs rcc@s(eZdZdZeZdZdd„Zdd„ZdS)Ú_AssertWarnsContextzBA context manager used to implement TestCase.assertWarns* methods.z(a warning type or tuple of warning typescCsRx$tjjƒD]}t|ddƒr i|_q Wtjdd�|_|jjƒ|_tj d|j ƒ|S)NÚ__warningregistry__T)ÚrecordÚalways) r ÚmodulesÚvaluesÚgetattrrrrZÚcatch_warningsÚwarnings_managerrdÚ simplefilterrB)rÚvrrrrd s   z_AssertWarnsContext.__enter__c Cs|jj|||ƒ|dk rdSy |jj}Wntk rFt|jƒ}YnXd}xd|jD]Z}|j}t||jƒslqT|dkrx|}|j dk r–|j j t|ƒƒ r–qT||_ |j |_ |j |_ dSW|dk rÔ|jdj|j jt|ƒƒƒ|jrð|jdj||jƒƒn|jdj|ƒƒdS)Nz"{}" does not match "{}"z{} not triggered by {}z{} not triggered)ryrorBr r`rrZÚmessager.rTriZwarningÚfilenameÚlinenorHrerjrU)rrkrlrmrnZfirst_matchingÚmÚwrrrros8      z_AssertWarnsContext.__exit__N) r r r r ÚWarningrWrYrdrorrrrrqs  rqÚ_LoggingWatcherÚrecordsÚoutputc@s(eZdZdZdd„Zdd„Zdd„ZdS) Ú_CapturingHandlerzM A logging handler capturing all (raw and formatted) logging output. cCstjj|ƒtggƒ|_dS)N)ÚloggingÚHandlerrr‚Úwatcher)rrrrrAs z_CapturingHandler.__init__cCsdS)Nr)rrrrÚflushEsz_CapturingHandler.flushcCs*|jjj|ƒ|j|ƒ}|jjj|ƒdS)N)rˆrƒrrer„)rrsrErrrÚemitHs z_CapturingHandler.emitN)r r r r rr‰rŠrrrrr…<sr…c@s,eZdZdZdZdd„Zdd„Zdd„Zd S) Ú_AssertLogsContextz:A context manager used to implement TestCase.assertLogs().z"%(levelname)s:%(name)s:%(message)scCs:tj||ƒ||_|r(tjj||ƒ|_ntj|_d|_dS)N) rCrÚ logger_namer†Z _nameToLevelÚgetÚlevelÚINFOrE)rr"rŒrŽrrrrTs  z_AssertLogsContext.__init__cCs’t|jtjƒr|j}|_ntj|jƒ}|_tj|jƒ}tƒ}|j |ƒ|j |_ |j dd…|_ |j |_|j|_|g|_ |j|j ƒd|_|j S)NF)r.rŒr†ZLoggerÚloggerZ getLoggerZ FormatterÚLOGGING_FORMATr…Z setFormatterrˆÚhandlersÚ old_handlersrŽÚ old_levelÚ propagateÚ old_propagateÚsetLevel)rr�Z formatterZhandlerrrrrd]s   z_AssertLogsContext.__enter__cCs`|j|j_|j|j_|jj|jƒ|dk r.dSt|jj ƒdkr\|j dj t j |jƒ|jjƒƒdS)NFrz-no logs of level {} or higher triggered on {})r“r�r’r–r•r—r”ÚlenrˆrƒrHrer†Z getLevelNamerŽra)rrkrlrmrrrrons  z_AssertLogsContext.__exit__N)r r r r r‘rrdrorrrrr‹Os  r‹c@sÜeZdZdZeZdZdŠZd‹ZdZ dŒd d „Z d d „Z d d„Z dd„Z dd„Zedd„ƒZedd„ƒZdd„Zdd„Zdd„Zdd„Zdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zejefd)d*„ƒZd+d,„Zd-d.„Z d/d0„Z!d�d2d3„Z"d4d5„Z#d6d7„Z$d8d9„Z%d:d;„Z&dŽdd?„Z(d�d@dA„Z)dBdC„Z*dDdE„Z+dFdG„Z,d‘dHdI„Z-dJdK„Z.d’dLdM„Z/d“dNdO„Z0d”dPdQ„Z1d•dRdS„Z2d–dTdU„Z3d—dVdW„Z4dXdY„Z5d˜dZd[„Z6d™d\d]„Z7dšd^d_„Z8d›d`da„Z9dœdbdc„Z:d�ddde„Z;dždfdg„Zd¡dldm„Z?d¢dndo„Z@d£dpdq„ZAd¤drds„ZBd¥dtdu„ZCd¦dvdw„ZDd§dxdy„ZEd¨dzd{„ZFd©d|d}„ZGdªd~d„ZHd€d�„ZId‚dƒ„ZJd«d„d…„ZKd¬d†d‡„ZLdˆd‰„ZMeMe0ƒZNZOeMe1ƒZPZQeMe2ƒZRZSeMe3ƒZTZUeMe)ƒZVZWeMe+ƒZXeMe(ƒZYeMeIƒZZeMeKƒZ[eMeLƒZ\d1S)­ÚTestCaseaWA class whose instances are single test cases. By default, the test code itself should be placed in a method named 'runTest'. If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute. Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively. If it is necessary to override the __init__ method, the base class __init__ method must always be called. It is important that subclasses should not change the signature of their __init__ method, since instances of the classes are instantiated automatically by parts of the framework in order to be run. When subclassing TestCase, you can set these attributes: * failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'. * longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in *addition* to any explicit message passed. * maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required. TéPéééFÚrunTestc Cs¶||_d|_d|_yt||ƒ}Wn.tk rN|dkrJtd|j|fƒ‚Yn X|j|_g|_d|_ i|_ |j t dƒ|j t dƒ|j tdƒ|j tdƒ|j tdƒ|j td ƒdS) zÅCreate an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name. NzNo testržzno such test method in %s: %sÚassertDictEqualÚassertListEqualÚassertTupleEqualÚassertSetEqualÚassertMultiLineEqual)Ú_testMethodNameÚ_outcomeÚ_testMethodDocrwr`Ú ValueErrorÚ __class__r Ú _cleanupsÚ_subtestÚ_type_equality_funcsÚaddTypeEqualityFuncÚdictÚlistr?ÚsetÚ frozensetr)rZ methodNameÚ testMethodrrrrªs&     zTestCase.__init__cCs||j|<dS)a[Add a type specific assertEqual style function to compare a type. This method is for use by TestCase subclasses that need to register their own type equality functions to provide nicer error messages. Args: typeobj: The data type to call this function on when both values are of the same type in assertEqual(). function: The callable taking two arguments and an optional msg= argument that raises self.failureException with a useful error message when the two arguments are not equal. N)r«)rZtypeobjÚfunctionrrrr¬Ês zTestCase.addTypeEqualityFunccOs|jj|||fƒdS)aAdd a function, with arguments, to be called when the test is completed. Functions added are called on a LIFO basis and are called after tearDown on test failure or success. Cleanup items are called even if setUp fails (unlike tearDown).N)r©r)rr²r*r+rrrÚ addCleanupÙszTestCase.addCleanupcCsdS)zAHook method for setting up the test fixture before exercising it.Nr)rrrrÚsetUpászTestCase.setUpcCsdS)zAHook method for deconstructing the test fixture after testing it.Nr)rrrrÚtearDownåszTestCase.tearDowncCsdS)zKHook method for setting up class fixture before running tests in the class.Nr)ÚclsrrrÚ setUpClassészTestCase.setUpClasscCsdS)zVHook method for deconstructing the class fixture after running all tests in the class.Nr)r¶rrrÚ tearDownClassíszTestCase.tearDownClasscCsdS)Nrr)rrrrÚcountTestCasesñszTestCase.countTestCasescCstjƒS)N)rZ TestResult)rrrrÚdefaultTestResultôszTestCase.defaultTestResultcCs |j}|r|jdƒdjƒpdS)zåReturns a one-line description of the test, or None if no description has been provided. The default implementation of this method returns the first line of the specified test method's docstring. Ú rN)r¦ÚsplitÚstrip)rÚdocrrrÚshortDescription÷szTestCase.shortDescriptioncCsdt|jƒ|jfS)Nz%s.%s)rr¨r¤)rrrrÚidsz TestCase.idcCs t|ƒt|ƒk rtS|j|jkS)N)r/ÚNotImplementedr¤)rÚotherrrrÚ__eq__szTestCase.__eq__cCstt|ƒ|jfƒS)N)Úhashr/r¤)rrrrÚ__hash__ szTestCase.__hash__cCsd|jt|jƒfS)Nz%s (%s))r¤rr¨)rrrrÚ__str__szTestCase.__str__cCsdt|jƒ|jfS)Nz<%s testMethod=%s>)rr¨r¤)rrrrÚ__repr__szTestCase.__repr__cCs<t|ddƒ}|dk r |||ƒntjdtdƒ|j|ƒdS)NÚaddSkipz4TestResult has no addSkip method, skips not reportedrœ)rwrZr[ÚRuntimeWarningÚ addSuccess)rrr"r,rÈrrrÚ_addSkips   zTestCase._addSkipcks¸|jdks|jj rdVdS|j}|dkr8tj|ƒ}n |jj|ƒ}t|||ƒ|_zX|jj|jdd�� dVWdQRX|jj sœ|jj }|dk r¨|j r¨t ‚n |jj r¨t ‚Wd||_XdS)aPReturn a context manager that will return the enclosed block of code in a subtest identified by the optional message and keyword parameters. A failure in the subtest marks the test case as failed but resumes execution at the end of the enclosed block, allowing further test code to be executed. NT)r#)r¥rrªÚ collectionsÚChainMapÚparamsÚ new_childÚ_SubTestr%rrZfailfastrr)rrErÎÚparentZ params_maprrrrÚsubTests$  zTestCase.subTestcCsdx^|D]V\}}t|tƒr*|j|j||ƒq|dk rt|d|jƒrP|j||ƒq|j||ƒqWdS)Nr)r.rÐrr"rArFÚ addFailureZaddError)rrrÚtestr!rrrÚ_feedErrorsToResult=s zTestCase._feedErrorsToResultc CsDy |j}Wn*tk r4tjdtƒ|j|ƒYn X|||ƒdS)Nz@TestResult has no addExpectedFailure method, reporting as passes)ÚaddExpectedFailurer`rZr[rÉrÊ)rrr!rÖrrrÚ_addExpectedFailureGs zTestCase._addExpectedFailurecCshy |j}WnPtk rZtjdtƒy td‚Wn$tk rT|j|tjƒƒYnXYn X||ƒdS)NzCTestResult has no addUnexpectedSuccess method, reporting as failure) ÚaddUnexpectedSuccessr`rZr[rÉrrÓr r!)rrrØrrrÚ_addUnexpectedSuccessQs  zTestCase._addUnexpectedSuccessNc (Cs|}|dkr.|jƒ}t|ddƒ}|dk r.|ƒ|j|ƒt||jƒ}t|jddƒs^t|ddƒrœz,t|jddƒpxt|ddƒ}|j|||ƒWd|j|ƒXdSt|ddƒ}t|ddƒ}|pº|}t|ƒ} zð| |_| j |ƒ�|j ƒWdQRX| j �r<|| _ | j |dd�� |ƒWdQRXd| _ | j |ƒ�|j ƒWdQRX|jƒx"| jD]\} } |j|| | ƒ�qLW|j|| jƒ| j �r²|�r¨| j�rœ|j|| jƒn |j|ƒn |j|ƒ|S|j|ƒ|dk�ræt|d dƒ} | dk �ræ| ƒ| jjƒd| _d|_XdS) NÚ startTestRunr2Fr3Úr:T)r#Ú stopTestRun)rºrwZ startTestr¤r¨rËZstopTestrr¥r%r´rrrµÚ doCleanupsrrÕrrr×rÙrÊÚclear) rrZ orig_resultrÚr±Zskip_whyZexpecting_failure_methodZexpecting_failure_classrÚoutcomerÔr,rÜrrrÚrun`sh               z TestCase.runc CsN|jp tƒ}x:|jrF|jjƒ\}}}|j|ƒ�|||ŽWdQRXqW|jS)zNExecute all cleanup functions. Normally called for you after tearDown.N)r¥rr©r]r%r)rrßr²r*r+rrrrÝ¥s   zTestCase.doCleanupscOs |j||ŽS)N)rà)rr*ÚkwdsrrrÚ__call__²szTestCase.__call__cCsJ|jƒt||jƒƒ|jƒx&|jrD|jjdƒ\}}}|||Žq WdS)z6Run the test without collecting errors in a TestResultrNéÿÿÿÿ)r´rwr¤rµr©r])rr²r*r+rrrÚdebugµs zTestCase.debugcCs t|ƒ‚dS)zSkip this test.N)r )rr,rrrÚskipTest¾szTestCase.skipTestcCs|j|ƒ‚dS)z)Fail immediately, with the given message.N)rF)rrErrrÚfailÂsz TestCase.failcCs&|r"|j|dt|ƒƒ}|j|ƒ‚dS)z#Check that the expression is false.z%s is not falseN)rDrrF)rÚexprrErrrÚ assertFalseÆszTestCase.assertFalsecCs&|s"|j|dt|ƒƒ}|j|ƒ‚dS)z"Check that the expression is true.z%s is not trueN)rDrrF)rrçrErrrÚ assertTrueÌszTestCase.assertTruec CsP|js|p |S|dkr|Sy d||fStk rJdt|ƒt|ƒfSXdS)a‚Honour the longMessage attribute when generating failure messages. If longMessage is False this means: * Use only an explicit message if it is provided * Otherwise use the standard message for the assert If longMessage is True: * Use the standard message * If an explicit message is provided, plus ' : ' and the explicit message Nz%s : %s)Ú longMessageÚUnicodeDecodeErrorr)rrErGrrrrDÒs  zTestCase._formatMessagec Os$t||ƒ}z|jd||ƒSd}XdS)a=Fail unless an exception of class expected_exception is raised by the callable when invoked with specified positional and keyword arguments. If a different type of exception is raised, it will not be caught, and the test case will be deemed to have suffered an error, exactly as for an unexpected exception. If called with the callable and arguments omitted, will return a context object used like this:: with self.assertRaises(SomeException): do_something() An optional keyword argument 'msg' can be provided when assertRaises is used as a context object. The context manager keeps a reference to the exception as the 'exception' attribute. This allows you to inspect the exception after the assertion:: with self.assertRaises(SomeException) as cm: do_something() the_exception = cm.exception self.assertEqual(the_exception.error_code, 3) Ú assertRaisesN)rcrb)rÚexpected_exceptionr*r+Úcontextrrrrìçs zTestCase.assertRaisescOst||ƒ}|jd||ƒS)aîFail unless a warning of class warnClass is triggered by the callable when invoked with specified positional and keyword arguments. If a different type of warning is triggered, it will not be handled: depending on the other warning filtering rules in effect, it might be silenced, printed out, or raised as an exception. If called with the callable and arguments omitted, will return a context object used like this:: with self.assertWarns(SomeWarning): do_something() An optional keyword argument 'msg' can be provided when assertWarns is used as a context object. The context manager keeps a reference to the first matching warning as the 'warning' attribute; similarly, the 'filename' and 'lineno' attributes give you information about the line of Python code from which the warning was triggered. This allows you to inspect the warning after the assertion:: with self.assertWarns(SomeWarning) as cm: do_something() the_warning = cm.warning self.assertEqual(the_warning.some_attribute, 147) Ú assertWarns)rqrb)rÚexpected_warningr*r+rîrrrrïs zTestCase.assertWarnscCs t|||ƒS)ažFail unless a log message of level *level* or higher is emitted on *logger_name* or its children. If omitted, *level* defaults to INFO and *logger* defaults to the root logger. This method must be used as a context manager, and will yield a recording object with two attributes: `output` and `records`. At the end of the context manager, the `output` attribute will be a list of the matching formatted log messages and the `records` attribute will be a list of the corresponding LogRecord objects. Example:: with self.assertLogs('foo', level='INFO') as cm: logging.getLogger('foo').info('first message') logging.getLogger('foo.bar').error('second message') self.assertEqual(cm.output, ['INFO:foo:first message', 'ERROR:foo.bar:second message']) )r‹)rr�rŽrrrÚ assertLogs'szTestCase.assertLogscCsFt|ƒt|ƒkr@|jjt|ƒƒ}|dk r@t|tƒrt||ƒ|kr>dSdt|ƒt|ƒt|ƒf}nF|dkrfd}||k rŠtt||ƒ|ƒdkrŠdSdt|ƒt|ƒ|f}|j||ƒ}|j|ƒ‚dS)aæFail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the difference between the two objects is less than the given delta. Note that decimal places (from zero) are usually not the same as significant digits (measured from the most significant digit). Objects that are equal automatically fail. Nz specify delta or places not bothz%s == %s within %s deltarørz%s == %s within %r places)rXrùrrúrDrF)rrórôrûrErürGrrrÚassertNotAlmostEqual–s"   zTestCase.assertNotAlmostEqualc'Csì|dk rP|j}t||ƒs.|jd|t|ƒfƒ‚t||ƒsT|jd|t|ƒfƒ‚nd}d}y t|ƒ}Wn ttfk r„d|}YnX|dkr¼y t|ƒ}Wn ttfk rºd|}YnX|dk�r˜||krÒdSd|jƒft||ƒ}xÚt t ||ƒƒD]ž} y || } Wn.tt tfk �r8|d| |f7}PYnXy || } Wn.tt tfk �rt|d | |f7}PYnX| | krú|d | ft| | ƒ7}PqúW||k�rÆ|dk�rÆt |ƒt |ƒk�rÆdS||k�r0|d |||f7}y|d |t||ƒf7}Wn,tt tfk �r,|d ||f7}YnXnh||k�r˜|d|||f7}y|d |t||ƒf7}Wn,tt tfk �r–|d||f7}YnX|} ddj tjtj|ƒjƒtj|ƒjƒƒƒ} |j| | ƒ} |j|| ƒ}|j|ƒdS)aAAn equality assertion for ordered sequences (like lists and tuples). For the purposes of this function, a valid ordered sequence type is one which can be indexed, has a length, and has an equality operator. Args: seq1: The first sequence to compare. seq2: The second sequence to compare. seq_type: The expected datatype of the sequences, or None if no datatype should be enforced. msg: Optional message to use on failure instead of a list of differences. NzFirst sequence is not a %s: %szSecond sequence is not a %s: %sZsequencez(First %s has no length. Non-sequence?z)Second %s has no length. Non-sequence?z%ss differ: %s != %s z( Unable to index element %d of first %s z) Unable to index element %d of second %s z# First differing element %d: %s %s z+ First %s contains %d additional elements. zFirst extra element %d: %s z'Unable to index element %d of first %s z, Second %s contains %d additional elements. z(Unable to index element %d of second %s r»)r r.rFrr˜rXÚNotImplementedErrorÚ capitalizerÚrangeÚminÚ IndexErrorr/ÚjoinÚdifflibÚndiffÚpprintÚpformatÚ splitlinesÚ_truncateMessagerDræ)rZseq1Zseq2rEÚseq_typeZ seq_type_nameZ differingZlen1Zlen2ÚiZitem1Zitem2rGÚdiffMsgrrrÚassertSequenceEqual·s�               zTestCase.assertSequenceEqualcCs2|j}|dkst|ƒ|kr"||S|tt|ƒS)N)ÚmaxDiffr˜Ú DIFF_OMITTED)rr|ÚdiffZmax_diffrrrr szTestCase._truncateMessagecCs|j|||td�dS)aA list-specific equality assertion. Args: list1: The first list to compare. list2: The second list to compare. msg: Optional message to use on failure instead of a list of differences. )r N)rr®)rZlist1Zlist2rErrrr  s zTestCase.assertListEqualcCs|j|||td�dS)aA tuple-specific equality assertion. Args: tuple1: The first tuple to compare. tuple2: The second tuple to compare. msg: Optional message to use on failure instead of a list of differences. )r N)rr?)rZtuple1Ztuple2rErrrr¡,s zTestCase.assertTupleEqualc Cshy|j|ƒ}Wn^tk r>}z|jd|ƒWYdd}~Xn0tk rl}z|jd|ƒWYdd}~XnXy|j|ƒ}Wn^tk r¬}z|jd|ƒWYdd}~Xn0tk rÚ}z|jd|ƒWYdd}~XnX|pâ|sèdSg}|�r|jdƒx|D]}|jt|ƒƒ�qW|�rH|jdƒx|D]}|jt|ƒƒ�q0Wdj|ƒ} |j|j|| ƒƒdS)a¯A set-specific equality assertion. Args: set1: The first set to compare. set2: The second set to compare. msg: Optional message to use on failure instead of a list of differences. assertSetEqual uses ducktyping to support different types of sets, and is optimized for sets specifically (parameters must support a difference method). z/invalid type when attempting set difference: %sNz2first argument does not support set difference: %sz3second argument does not support set difference: %sz*Items in the first set but not the second:z*Items in the second set but not the first:r»)Ú differencerXrær`rÚreprrrD) rZset1Zset2rEZ difference1r$Z difference2ÚlinesÚitemrGrrrr¢7s2        zTestCase.assertSetEqualcCs2||kr.dt|ƒt|ƒf}|j|j||ƒƒdS)zDJust like self.assertTrue(a in b), but with a nicer default message.z%s not found in %sN)rrærD)rÚmemberÚ containerrErGrrrÚassertInbs zTestCase.assertIncCs2||kr.dt|ƒt|ƒf}|j|j||ƒƒdS)zHJust like self.assertTrue(a not in b), but with a nicer default message.z%s unexpectedly found in %sN)rrærD)rrrrErGrrrÚ assertNotInis zTestCase.assertNotIncCs2||k r.dt|ƒt|ƒf}|j|j||ƒƒdS)zDJust like self.assertTrue(a is b), but with a nicer default message.z %s is not %sN)rrærD)rÚexpr1Úexpr2rErGrrrÚassertIsps zTestCase.assertIscCs,||kr(dt|ƒf}|j|j||ƒƒdS)zHJust like self.assertTrue(a is not b), but with a nicer default message.zunexpectedly identical: %sN)rrærD)rrrrErGrrrÚ assertIsNotwszTestCase.assertIsNotcCs~|j|tdƒ|j|tdƒ||krzdt||ƒ}ddjtjtj|ƒjƒtj|ƒjƒƒƒ}|j ||ƒ}|j |j ||ƒƒdS)Nz"First argument is not a dictionaryz#Second argument is not a dictionaryz%s != %sr») ÚassertIsInstancer­rrrrrrr r rærD)rZd1Zd2rErGrrrrrŸ}s   zTestCase.assertDictEqualc CsÒtjdtƒg}g}xX|jƒD]L\}}||kr:|j|ƒq|||kr|jdt|ƒt|ƒt||ƒfƒqW|pt|szdSd}|ršddjdd„|Dƒƒ}|r¼|rª|d 7}|d dj|ƒ7}|j|j||ƒƒdS) z2Checks whether dictionary is a superset of subset.z&assertDictContainsSubset is deprecatedz%s, expected: %s, actual: %sNrÛz Missing: %sú,css|]}t|ƒVqdS)N)r)r<rrrrr>œsz4TestCase.assertDictContainsSubset..z; zMismatched values: %s) rZr[r\ÚitemsrrrrærD) rZsubsetZ dictionaryrEZmissingZ mismatchedÚkeyÚvaluerGrrrÚassertDictContainsSubset‰s,     z!TestCase.assertDictContainsSubsetc Cs¦t|ƒt|ƒ}}ytj|ƒ}tj|ƒ}Wntk rHt||ƒ}YnX||krVdSt||ƒ}|r¢d}dd„|Dƒ}dj|ƒ} |j|| ƒ}|j||ƒ}|j |ƒdS)a¿An unordered sequence comparison asserting that the same elements, regardless of order. If the same element occurs more than once, it verifies that the elements occur the same number of times. self.assertEqual(Counter(list(first)), Counter(list(second))) Example: - [0, 1, 1] and [1, 0, 1] compare equal. - [0, 0, 1] and [0, 1] compare unequal. NzElement counts were not equal: cSsg|] }d|‘qS)z First has %d, Second has %d: %rr)r<rrrrú Ász-TestCase.assertCountEqual..r») r®rÌÚCounterrXrrrr rDræ) rrórôrEZ first_seqZ second_seqZ differencesrGrr rrrÚassertCountEqual¦s      zTestCase.assertCountEqualcCsÚ|j|tdƒ|j|tdƒ||krÖt|ƒ|jks@t|ƒ|jkrN|j|||ƒ|jdd�}|jdd�}t|ƒdkr”|jdƒ|kr”|dg}|dg}dt||ƒ}dd jt j ||ƒƒ}|j ||ƒ}|j |j ||ƒƒd S) z-Assert that two multi-line strings are equal.zFirst argument is not a stringzSecond argument is not a stringT)Úkeependsrz r»z%s != %srÛN)rrr˜Ú_diffThresholdròr r½rrrrr rærD)rrórôrEZ firstlinesZ secondlinesrGrrrrr£Çs     zTestCase.assertMultiLineEqualcCs2||ks.dt|ƒt|ƒf}|j|j||ƒƒdS)zCJust like self.assertTrue(a < b), but with a nicer default message.z%s not less than %sN)rrærD)rÚaÚbrErGrrrÚ assertLessÛszTestCase.assertLesscCs2||ks.dt|ƒt|ƒf}|j|j||ƒƒdS)zDJust like self.assertTrue(a <= b), but with a nicer default message.z%s not less than or equal to %sN)rrærD)rr)r*rErGrrrÚassertLessEqualászTestCase.assertLessEqualcCs2||ks.dt|ƒt|ƒf}|j|j||ƒƒdS)zCJust like self.assertTrue(a > b), but with a nicer default message.z%s not greater than %sN)rrærD)rr)r*rErGrrrÚ assertGreaterçszTestCase.assertGreatercCs2||ks.dt|ƒt|ƒf}|j|j||ƒƒdS)zDJust like self.assertTrue(a >= b), but with a nicer default message.z"%s not greater than or equal to %sN)rrærD)rr)r*rErGrrrÚassertGreaterEqualíszTestCase.assertGreaterEqualcCs,|dk r(dt|ƒf}|j|j||ƒƒdS)zCSame as self.assertTrue(obj is None), with a nicer default message.Nz%s is not None)rrærD)rr(rErGrrrÚ assertIsNoneószTestCase.assertIsNonecCs"|dkrd}|j|j||ƒƒdS)z(Included for symmetry with assertIsNone.Nzunexpectedly None)rærD)rr(rErGrrrÚassertIsNotNoneùszTestCase.assertIsNotNonecCs0t||ƒs,dt|ƒ|f}|j|j||ƒƒdS)zTSame as self.assertTrue(isinstance(obj, cls)), with a nicer default message.z%s is not an instance of %rN)r.rrærD)rr(r¶rErGrrrrÿs zTestCase.assertIsInstancecCs0t||ƒr,dt|ƒ|f}|j|j||ƒƒdS)z,Included for symmetry with assertIsInstance.z%s is an instance of %rN)r.rrærD)rr(r¶rErGrrrÚassertNotIsInstances zTestCase.assertNotIsInstancecOst|||ƒ}|jd||ƒS)aAsserts that the message in a raised exception matches a regex. Args: expected_exception: Exception class expected to be raised. expected_regex: Regex (re pattern object or string) expected to be found in error message. args: Function to be called and extra positional args. kwargs: Extra kwargs. msg: Optional message used in case of failure. Can only be used when assertRaisesRegex is used as a context manager. ÚassertRaisesRegex)rcrb)rrírTr*r+rîrrrr2 s zTestCase.assertRaisesRegexcOst|||ƒ}|jd||ƒS)aÒAsserts that the message in a triggered warning matches a regexp. Basic functioning is similar to assertWarns() with the addition that only warnings whose messages also match the regular expression are considered successful matches. Args: expected_warning: Warning class expected to be triggered. expected_regex: Regex (re pattern object or string) expected to be found in error message. args: Function to be called and extra positional args. kwargs: Extra kwargs. msg: Optional message used in case of failure. Can only be used when assertWarnsRegex is used as a context manager. ÚassertWarnsRegex)rqrb)rrðrTr*r+rîrrrr3s zTestCase.assertWarnsRegexcCsJt|ttfƒrtj|ƒ}|j|ƒsFd|j|f}|j||ƒ}|j|ƒ‚dS)z=Fail the test unless the text matches the regular expression.z&Regex didn't match: %r not found in %rN) r.rÚbytesrRrSrirjrDrF)rÚtextrTrErGrrrÚ assertRegex/s    zTestCase.assertRegexcCs`t|ttfƒrtj|ƒ}|j|ƒ}|r\d||jƒ|jƒ…|j|f}|j ||ƒ}|j |ƒ‚dS)z9Fail the test if the text matches the regular expression.z"Regex matched: %r matches %r in %rN) r.rr4rRrSriÚstartÚendrjrDrF)rr5Zunexpected_regexrEÚmatchrGrrrÚassertNotRegex;s   zTestCase.assertNotRegexcs‡fdd„}|S)Ncs tjdjˆjƒtdƒˆ||ŽS)NzPlease use {0} instead.rœ)rZr[rer r\)r*r+)Ú original_funcrrÚdeprecated_funcKs z,TestCase._deprecate..deprecated_funcr)r;r<r)r;rÚ _deprecateJs zTestCase._deprecatei€i)rž)N)N)N)N)NN)N)N)N)NNN)NNN)NN)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)]r r r r ÚAssertionErrorrFrêrr(Z_classSetupFailedrr¬r³r´rµÚ classmethodr·r¸r¹rºr¿rÀrÃrÅrÆrÇrËr&r'Ú_subtest_msg_sentinelrÒrÕr×rÙràrÝrârärårærèrérDrìrïrñrõròrör÷rýrþrr r r¡r¢rrrrrŸr#r&r£r+r,r-r.r/r0rr1r2r3r6r:r=ZfailUnlessEqualZ assertEqualsZ failIfEqualZassertNotEqualsZfailUnlessAlmostEqualZassertAlmostEqualsZfailIfAlmostEqualZassertNotAlmostEqualsZ failUnlessZassert_ZfailUnlessRaisesZfailIfZassertRaisesRegexpZassertRegexpMatchesZassertNotRegexpMatchesrrrrr™{s¦        E     !     ' c +      !               r™csjeZdZdZd‡fdd„ Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z dd„Z ‡ZS)ÚFunctionTestCaseaIA test case that wraps a test function. This is useful for slipping pre-existing test functions into the unittest framework. Optionally, set-up and tidy-up functions can be supplied. As with TestCase, the tidy-up ('tearDown') function will always be called if the set-up ('setUp') function ran successfully. Ncs*tt|ƒjƒ||_||_||_||_dS)N)ÚsuperrArÚ _setUpFuncÚ _tearDownFuncÚ _testFuncÚ _description)rZtestFuncr´rµZ description)r¨rrris zFunctionTestCase.__init__cCs|jdk r|jƒdS)N)rC)rrrrr´ps zFunctionTestCase.setUpcCs|jdk r|jƒdS)N)rD)rrrrrµts zFunctionTestCase.tearDowncCs |jƒdS)N)rE)rrrrržxszFunctionTestCase.runTestcCs|jjS)N)rEr )rrrrrÀ{szFunctionTestCase.idcCs@t||jƒstS|j|jko>|j|jko>|j|jko>|j|jkS)N)r.r¨rÁrCrDrErF)rrÂrrrrÃ~s     zFunctionTestCase.__eq__cCstt|ƒ|j|j|j|jfƒS)N)rÄr/rCrDrErF)rrrrrŇszFunctionTestCase.__hash__cCsdt|jƒ|jjfS)Nz%s (%s))rr¨rEr )rrrrrÆ‹s zFunctionTestCase.__str__cCsdt|jƒ|jfS)Nz <%s tec=%s>)rr¨rE)rrrrrÇ�s zFunctionTestCase.__repr__cCs2|jdk r|jS|jj}|r.|jdƒdjƒp0dS)Nr»r)rFrEr r¼r½)rr¾rrrr¿“s z!FunctionTestCase.shortDescription)NNN)r r r r rr´rµržrÀrÃrÅrÆrÇr¿Ú __classcell__rr)r¨rrA`s rAcsDeZdZ‡fdd„Zdd„Zdd„Zdd„Zd d „Zd d „Z‡Z S) rÐcs(tƒjƒ||_||_||_|j|_dS)N)rBrÚ_messager"rÎrF)rr"r|rÎ)r¨rrrœs  z_SubTest.__init__cCs tdƒ‚dS)Nzsubtests cannot be run directly)rÿ)rrrrrž£sz_SubTest.runTestcCsbg}|jtk r |jdj|jƒƒ|jrTdjdd„t|jjƒƒDƒƒ}|jdj|ƒƒdj|ƒp`dS)Nz[{}]z, css|]\}}dj||ƒVqdS)z{}={!r}N)re)r<Úkr{rrrr>¬sz+_SubTest._subDescription..z({})ú z ())rHr@rrerÎrÚsortedr )rÚpartsZ params_descrrrÚ_subDescription¦s z_SubTest._subDescriptioncCsdj|jjƒ|jƒƒS)Nz{} {})rer"rÀrM)rrrrrÀ±sz _SubTest.idcCs |jjƒS)zlReturns a one-line description of the subtest, or None if no description has been provided. )r"r¿)rrrrr¿´sz_SubTest.shortDescriptioncCsdj|j|jƒƒS)Nz{} {})rer"rM)rrrrrƺsz_SubTest.__str__) r r r rržrMrÀr¿rÆrGrr)r¨rrКs   rÐ)1r r r0rrJr†rrRrZrÌr&rfrÛrÚutilrrrrrZ __unittestÚobjectr@rrMr rrrr)r6r8r9rr;rCrLrPrQrcrqÚ namedtupler‚r‡r…r‹r™rArÐrrrrÚs\ (  /%5 ,l: