B t `FD @s\dZddlZddlZddlZddlZddlmZddlmZddl m Z ddl m Z m Z mZmZmZmZmZyddlmZWn ek rddlmZYnXddlZddlZeeZyddlZejWn*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__)selfoutr9/tmp/pip-unpacked-wheel-cjhnoqsi/nose/plugins/doctests.pyrZsz!NoseOutputRedirectingPdb.__init__cCsd|_t|tjdS)NT)rr set_tracesys _getframef_back)rrrrr^sz"NoseOutputRedirectingPdb.set_tracecCs|jrt|dS)N)rr set_continue)rrrrrbsz%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)rrrr rrrrwszDoctestSuite.__init__cCs t|jS)N)rr)rrrraddress|szDoctestSuite.addresscCs t|jS)N)iter_tests)rrrr__iter__szDoctestSuite.__iter__cCs t|jS)N)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-r.metavarr0z--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])r.r/r3r0z--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)r2)roptions add_optionget set_defaultsr )rparserenvZ env_settingrrrr9s2 zDoctest.optionscCst||||j|_|j|_t|j|_|j|_t |_ d|_ |j rd|j d}x|D]}|rt|ddkrtdd|f|d|dd}}t j|}|std|f|d kr|j |O_ q`|d kr`|j |M_ q`WdS) zConfigure plugin. r,z+-z+Must specify doctest options with starting z'+' or '-'. Got %sNzUnknown doctest option %s+-)r configurer4r,r r2 extensionr6fixturesdoctestZ DocTestFinderfinder optionflagsr7joinsplit ValueErrorZOPTIONFLAGS_BY_NAMEr;)rr9configflagsflagmodeZ option_nameZ option_flagrrrrCs.     zDoctest.configurecCs |j|_dS)znCapture loader's suiteClass. This is used to create test suites from doctest files. N) suiteClass)rloaderrrrprepareTestLoaderszDoctest.prepareTestLoaderccstd|||js(td|dSy|j|}Wn tk rXtd|dSX|sntd|dS|t |j }g}x8|D]0}|j sq|j s||_ | t||j|jdqW|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)rH result_varF)rr )logdebugmatchesrrGfindAttributeError exceptionsortr __file__examplesfilenamer1 DocTestCaserHr4rP)rmodulerZ module_fileZcasestestrrrloadTestsFromModules2       zDoctest.loadTestsFromModulec csl|jrht|j|jrhtj|}t|}z |}Wd|Xd}d|i}|j rtj |\}}tj |} t j | ||j } yt| ttdg}Wn4tk r} ztd| | t jWdd} ~ 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)rbnamer]linenoZ setup_testZ teardown_test)rHsetUptearDownrS)rF)rDrendswithospathbasenameopenreadcloserEsplitextdirnamerr1 __import__globalslocals ImportErrorrTrUhasattrrbrFZ DocTestParserZ get_doctestr\ DocFileCaserHgetattrr4r)rr]rcZdhdocZfixture_contextrbbaseextroZfixt_moder=r`ZcaserrrloadTestsFromFilesL             zDoctest.loadTestsFromFileccsft|ddt|}|jj|t||d}|rbx2|D]*}t|jdkrHq4t|||j|j dVq4WdS)zbLook for doctests in the given object, which will be a function, method or class. rz Unnammed %s)r_rcr)objrHrSN) rvtyperGrWrlenr\r^rHr4)rr|parentrcZdoctestsr`rrrmakeTest/s  zDoctest.makeTestcsvdkr dS|jsJ|jj sJ|jjotddfdd|jjDDot|jj ptddfdd|jjDD S)Nz __init__.pyFcSsg|] }|r|qSrr).0_frrr Dsz#Doctest.matches..csg|]}|qSr)search)rinc)rcrrrDscSsg|] }|r|qSrr)rrrrrrGscsg|]}|qSr)r)rexc)rcrrrGs)r,confZ testMatchrincludeexclude)rrcr)rcrrV<s zDoctest.matchescsRdrdS|jrNtj|jrN|jjrJddfdd|jjDDsNdSdS)zfOverride to select all modules and any file ending with configured doctest extension. z.pyTcSsg|] }|r|qSrr)rrrrrrUsz$Doctest.wantFile..csg|]}|qSr)r)rr)filerrrUsN)rgrDrrr)rrr)rrwantFileJs zDoctest.wantFile)rrrr)rDrrPr9rCrRrar{rrVrrrrrr*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|||||ddS)N)rHrerfchecker) _result_var _nose_objsuperr^r)rr`rHrerfrr|rS) __class__rrrbs  zDocTestCase.__init__cCs|jdk rt|jSt|jj}t|r||jjd}d|dd}t|}t|}|d|dd|d|dgfSt|SdS)N.rr@)rrr_dt_testrcr rJrI)rr|parts class_nameclsZ base_addrrrrr#js    zDocTestCase.addresscCsF|jj}|jj}|dk rBt|}|dkr,|S||sBd||f}|S)Nz%s.%s)rrcr]r startswith)rrcr]pkrrrids  zDocTestCase.idcCs0|}|d}d|dd|ddfS)Nrz%s (%s)r)rrJrI)rrcrrr__repr__s zDocTestCase.__repr__cCs d|S)Nz Doctest: %s)r)rrrrshortDescriptionszDocTestCase.shortDescriptioncs,|jdk rtj|_|jt_tt|dS)N)rr displayhook_old_displayhook _displayhookrr^re)r)rrrres zDocTestCase.setUpcCs*|dkr dStt|j|tt|dS)N)setattr builtin_modrprintrepr)rvaluerrrrszDocTestCase._displayhookcs0tt||jdk r,|jt_tt|jdS)N) rr^rfrrrrdelattrr)r)rrrrfs zDocTestCase.tearDown)rNNNNr)rrrr)rr#rrr(rrerrf __classcell__rr)rrr^[s  r^csJeZdZdZdfdd ZddZfd d Zd d Zfd dZZ S)ruzfOverrides to provide address() method that returns the correct address for the doc file case. rNrcs$||_tt|j||||dddS)N)rHrerfr)rrrur)rr`rHrerfrrS)rrrrs zDocFileCase.__init__cCs|jjddfS)N)rr])rrrrr#szDocFileCase.addresscs,|jdk rtj|_|jt_tt|dS)N)rrrrrrrure)r)rrrres zDocFileCase.setUpcCs*|dkr dStt|j|tt|dS)N)rrrrr)rrrrrrszDocFileCase._displayhookcs0tt||jdk r,|jt_tt|jdS)N) rrurfrrrrrr)r)rrrrfs zDocFileCase.tearDown)rNNNr) rrrr)rr#rerrfrrr)rrrus ru)(r)loggingrhrr!inspectrZnose.plugins.baserZ nose.suiterZ nose.utilrrrrr r r ior rsbuiltinsr getLoggerrrTrFr^rXZnose.ext.dtcompatryZdtcompatZ_OutputRedirectingPdbrr r"rr*rurrrr1s8   $   UK