U eFD @s\dZddlZddlZddlZddlZddlmZddlmZddl m Z ddl m Z m Z mZmZmZmZmZzddlmZWn ek rddlmZYnXddlZddlZeeZzddlZejWn*eefk rddlmm ZYnXej!Z"Gdd d e"Z#e#e_!Gd d d ej$Z%Gd d d eZ&GdddejZGdddej'Z'dS)aUse the Doctest plugin with ``--with-doctest`` or the NOSE_WITH_DOCTEST environment variable to enable collection and execution of :mod:`doctests `. Because doctests are usually included in the tested package (instead of being grouped into packages or modules of their own), nose only looks for them in the non-test packages it discovers in the working directory. Doctests may also be placed into files other than python modules, in which case they can be collected and executed by using the ``--doctest-extension`` switch or NOSE_DOCTEST_EXTENSION environment variable to indicate which file extension(s) to load. When loading doctests from non-module files, use the ``--doctest-fixtures`` switch to specify how to find modules containing fixtures for the tests. A module name will be produced by appending the value of that switch to the base name of each doctest file loaded. For example, a doctest file "widgets.rst" with the switch ``--doctest_fixtures=_fixt`` will load fixtures from the module ``widgets_fixt.py``. A fixtures module may define any or all of the following functions: * setup([module]) or setup_module([module]) Called before the test runs. You may raise SkipTest to skip all tests. * teardown([module]) or teardown_module([module]) Called after the test runs, if setup/setup_module did not raise an unhandled exception. * setup_test(test) Called before the test. NOTE: the argument passed is a doctest.DocTest instance, *not* a unittest.TestCase. * teardown_test(test) Called after the test, if setup_test did not raise an exception. NOTE: the argument passed is a doctest.DocTest instance, *not* a unittest.TestCase. Doctests are run like any other test, with the exception that output capture does not work; doctest does its own output capture while running a test. .. note :: See :doc:`../doc_tests/test_doctest_fixtures/doctest_fixtures` for additional documentation and examples. N) getmodule)Plugin) ContextList)anyp getpackage test_address resolve_namesrctolist isproperty)StringIOc@s$eZdZddZddZddZdS)NoseOutputRedirectingPdbcCsd|_t||dS)NF)(_NoseOutputRedirectingPdb__debugger_used_orp__init__)selfoutrC/opt/hc_python/lib/python3.8/site-packages/nose/plugins/doctests.pyrZsz!NoseOutputRedirectingPdb.__init__cCsd|_t|tjdS)NT)rr set_tracesys _getframef_backrrrrr^sz"NoseOutputRedirectingPdb.set_tracecCs|jrt|dSN)rr set_continuerrrrrbsz%NoseOutputRedirectingPdb.set_continueN)__name__ __module__ __qualname__rrrrrrrr Ysr c@s6eZdZdZdZd ddZddZd d Zd d ZdS) DoctestSuitea Doctest suites are parallelizable at the module or file level only, since they may be attached to objects that are not individually addressable (like properties). This suite subclass is used when loading doctests from a module to ensure that behavior. This class is used only if the plugin is not fully prepared; in normal use, the loader's suiteClass is used. FrNcCs ||_||_tjj||ddS)N)tests)context can_splitunittest TestSuiter)rr r!r"rrrrwszDoctestSuite.__init__cCs t|jSr)rr!rrrraddress|szDoctestSuite.addresscCs t|jSr)iter_testsrrrr__iter__szDoctestSuite.__iter__cCs t|jSr)strr'rrrr__str__szDoctestSuite.__str__)rNF) rrr__doc__r"rr%r(r*rrrrrjs   rc@sXeZdZdZdZeZddZddZddZ d d Z d d Z d dZ ddZ ddZdS)DoctestzO Activate doctest plugin to find and run doctests in non-test modules. NcCst||||jddd|ddd|jddd d d d |jd d|dddd|jdddddd |jdddddd |d}|dk r|jt|ddS)z'Register commmandline options. z--doctest-tests store_true doctest_testsZNOSE_DOCTEST_TESTSzAlso look for doctests in test modules. Note that classes, methods and functions should have either doctests or non-doctest tests, not both. [NOSE_DOCTEST_TESTS])actiondestdefaulthelpz--doctest-extensionappenddoctestExtensionZEXTzLAlso look for doctests in files with this extension [NOSE_DOCTEST_EXTENSION])r/r0metavarr2z--doctest-result-variabledoctest_result_varZNOSE_DOCTEST_RESULT_VARZVARzChange the variable name set to the result of the last interpreter command from the default '_'. Can be used to avoid conflicts with the _() function used for text translation. [NOSE_DOCTEST_RESULT_VAR])r0r1r5r2z--doctest-fixturesstoredoctestFixturesZSUFFIXzgFind fixtures for a doctest file in module with this name appended to the base name of the doctest filez--doctest-optionsdoctestOptionsOPTIONSzISpecify options to pass to doctest. Eg. '+ELLIPSIS,+NORMALIZE_WHITESPACE'ZNOSE_DOCTEST_EXTENSIONN)r4)roptions add_optionget set_defaultsr )rparserenvZ env_settingrrrr;s<  zDoctest.optionscCst||||j|_|j|_t|j|_|j|_t |_ d|_ |j rd|j d}|D]}|rr|ddkrtdd|f|d|dd}}t j|}|std|f|d kr|j |O_ q^|d kr^|j |M_ q^dS) zConfigure plugin. r,z+-z+Must specify doctest options with starting z'+' or '-'. Got %sNzUnknown doctest option %s+-)r configurer6r.r r4 extensionr8fixturesdoctestZ DocTestFinderfinder optionflagsr9joinsplit ValueErrorZOPTIONFLAGS_BY_NAMEr=)rr;configflagsflagmodeZ option_nameZ option_flagrrrrEs4   zDoctest.configurecCs |j|_dS)znCapture loader's suiteClass. This is used to create test suites from doctest files. N) suiteClass)rloaderrrrprepareTestLoaderszDoctest.prepareTestLoaderccstd|||js(td|dSz|j|}Wn"tk rZtd|YdSX|sptd|dS|t |j }g}|D]0}|j sq|j s||_ | t||j|jdq|r|j||ddVdS) z'Load doctests from the module. zloading from %szDoctest doesn't want module %sNzAttribute error loading from %szNo tests found in %s)rJ result_varF)r!r")logdebugmatchesrrIfindAttributeError exceptionsortr __file__examplesfilenamer3 DocTestCaserJr6rR)rmoduler Z module_fileZcasestestrrrloadTestsFromModules4       zDoctest.loadTestsFromModulec csl|jrht|j|jrhtj|}t|}z |}W5|Xd}d|i}|j rtj |\}}tj |} t j | ||j } zt| ttdg}Wn4tk r} ztd| | t jW5d} ~ XYnXtd| |t|dr||}t} | j||||dd} | jrbt| |jt|d dt|d d|jd }|rZt|f|d Vn|Vnd VdS)zLoad doctests from the file. Tests are loaded only if filename's extension matches configured doctest extension. Nr]ZnopzCould not import %s: %s (%s)z Fixture module %s resolved to %sglobsr)rdnamer_linenoZ setup_testZ teardown_test)rJsetUptearDownrU)r!F)rFrendswithospathbasenameopenclosereadrGsplitextdirnamerr3 __import__globalslocals ImportErrorrVrWhasattrrdrHZ DocTestParserZ get_doctestr^ DocFileCaserJgetattrr6r)rr_reZdhdocZfixture_contextrdbaseextrqZfixt_moder?rbcaserrrloadTestsFromFilesj          zDoctest.loadTestsFromFileccsbt|ddt|}|jj|t||d}|r^|D]*}t|jdkrFq2t|||j|j dVq2dS)zbLook for doctests in the given object, which will be a function, method or class. rz Unnammed %s)rarer)objrJrUN) rxtyperIrYrlenr^r`rJr6)rrparentreZdoctestsrbrrrmakeTest/s zDoctest.makeTestcsvdkr dS|jsJ|jj sJ|jjotddfdd|jjDDot|jj ptddfdd|jjDD S)Nz __init__.pyFcSsg|] }|r|qSrr.0Z_frrr Dsz#Doctest.matches..csg|]}|qSrsearch)rincrerrrDscSsg|] }|r|qSrrrrrrrGscsg|]}|qSrrrexcrrrrGs)r.confZ testMatchrincludeexcluderrerrrrX<s  zDoctest.matchescsRdrdS|jrNtj|jrN|jjrJddfdd|jjDDsNdSdS)zfOverride to select all modules and any file ending with configured doctest extension. z.pyTcSsg|] }|r|qSrrrrrrrUsz$Doctest.wantFile..csg|]}|qSrrrfilerrrUsN)rirFrrr)rrrrrwantFileJs  zDoctest.wantFile)rrrr+rFrrRr;rErTrcr~rrXrrrrrr,s+!2 r,csfeZdZdZdfdd ZddZd d Zd d ZeZd dZ fddZ ddZ fddZ Z S)r`a#Overrides DocTestCase to provide an address() method that returns the correct address for the doctest case. To provide hints for address(), an obj may also be passed -- this will be used as the test object for purposes of determining the test address, if it is provided. rN_cs*||_||_tt|j|||||ddSN)rJrgrhchecker) _result_var _nose_objsuperr`r)rrbrJrgrhrrrU __class__rrrbs zDocTestCase.__init__cCs|jdk rt|jSt|jj}t|r||jjd}d|dd}t|}t|}|d|dd|d|dgfSt|SdS)N.rrB)rrr_dt_testrer rLrK)rrparts class_nameclsZ base_addrrrrr%js    zDocTestCase.addresscCsF|jj}|jj}|dk rBt|}|dkr,|S||sBd||f}|S)Nz%s.%s)rrer_r startswith)rrer_pkrrrids  zDocTestCase.idcCs0|}|d}d|dd|ddfS)Nrz%s (%s)r)rrLrKrrrr__repr__s zDocTestCase.__repr__cCs d|S)Nz Doctest: %s)rrrrrshortDescriptionszDocTestCase.shortDescriptioncs,|jdk rtj|_|jt_tt|dSr)rr displayhook_old_displayhook _displayhookrr`rgrrrrrgs zDocTestCase.setUpcCs*|dkr dStt|j|tt|dSrsetattr builtin_modrprintreprrvaluerrrrszDocTestCase._displayhookcs0tt||jdk r,|jt_tt|jdSr) rr`rhrrrrdelattrrrrrrrhs zDocTestCase.tearDown)rNNNNr)rrrr+rr%rrr*rrgrrh __classcell__rrrrr`[s  r`csJeZdZdZdfdd ZddZfd d Zd d Zfd dZZ S)rwzfOverrides to provide address() method that returns the correct address for the doc file case. rNrcs$||_tt|j||||dddSr)rrrwr)rrbrJrgrhrrUrrrrs zDocFileCase.__init__cCs|jjddfSr)rr_rrrrr%szDocFileCase.addresscs,|jdk rtj|_|jt_tt|dSr)rrrrrrrwrgrrrrrgs zDocFileCase.setUpcCs*|dkr dStt|j|tt|dSrrrrrrrszDocFileCase._displayhookcs0tt||jdk r,|jt_tt|jdSr) rrwrhrrrrrrrrrrrhs zDocFileCase.tearDown)rNNNr) rrrr+rr%rgrrhrrrrrrws rw)(r+loggingrjrr#inspectrZnose.plugins.baserZ nose.suiterZ nose.utilrrrrr r r ior rubuiltinsr getLoggerrrVrHr`rZZnose.ext.dtcompatr{ZdtcompatZ_OutputRedirectingPdbrr r$rr,rwrrrrs83   $   UK