o
    gFD                  	   @   sX  d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZmZmZmZmZmZmZ zddlmZ W n eyM   ddlmZ Y nw ddlZddlZeeZz	ddlZej W n eefyx   ddlm  m Z Y nw ej!Z"G dd	 d	e"Z#e#e_!G d
d dej$Z%G dd deZ&G dd dejZG dd dej'Z'dS )a  Use the Doctest plugin with ``--with-doctest`` or the NOSE_WITH_DOCTEST
environment variable to enable collection and execution of :mod:`doctests
<doctest>`.  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_namesrctolist
isproperty)StringIOc                   @   s$   e Zd Zdd Zdd Zdd ZdS )NoseOutputRedirectingPdbc                 C   s   d| _ t| | d S )NF)(_NoseOutputRedirectingPdb__debugger_used_orp__init__)selfout r   S/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/nose/plugins/doctests.pyr   Z   s   z!NoseOutputRedirectingPdb.__init__c                 C   s   d| _ t| t j d S )NT)r   r   	set_tracesys	_getframef_backr   r   r   r   r   ^   s   z"NoseOutputRedirectingPdb.set_tracec                 C   s   | j r
t|  d S d S N)r   r   set_continuer   r   r   r   r   b   s   z%NoseOutputRedirectingPdb.set_continueN)__name__
__module____qualname__r   r   r   r   r   r   r   r   Y   s    r   c                   @   s6   e Zd ZdZdZdddZdd Zd	d
 Zdd ZdS )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.
    
    Fr   Nc                 C   s    || _ || _tjj| |d d S )N)tests)context	can_splitunittest	TestSuiter   )r   r    r!   r"   r   r   r   r   w   s   zDoctestSuite.__init__c                 C   
   t | jS r   )r   r!   r   r   r   r   address|      
zDoctestSuite.addressc                 C   r%   r   )iter_testsr   r   r   r   __iter__   s   
zDoctestSuite.__iter__c                 C   r%   r   )strr)   r   r   r   r   __str__   r'   zDoctestSuite.__str__)r   NF)	r   r   r   __doc__r"   r   r&   r*   r,   r   r   r   r   r   j   s    

r   c                   @   sX   e Zd ZdZdZeZdd Zdd Zdd Z	d	d
 Z
dd Zdd Zdd Zdd ZdS )DoctestzO
    Activate doctest plugin to find and run doctests in non-test modules.
    Nc                 C   s   t | || |jddd|ddd |jddd	d
dd |jdd|dddd |jdddddd |jdddddd |d}|durR|jt|d dS dS )z'Register commmandline options.
        z--doctest-tests
store_truedoctest_testsNOSE_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])actiondestdefaulthelpz--doctest-extensionappenddoctestExtensionEXTzLAlso look for doctests in files with this extension [NOSE_DOCTEST_EXTENSION])r2   r3   metavarr5   z--doctest-result-variabledoctest_result_varNOSE_DOCTEST_RESULT_VARVARzChange 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])r3   r4   r9   r5   z--doctest-fixturesstoredoctestFixturesSUFFIXzgFind fixtures for a doctest file in module with this name appended to the base name of the doctest filez--doctest-optionsdoctestOptionsOPTIONSzISpecify options to pass to doctest. Eg. '+ELLIPSIS,+NORMALIZE_WHITESPACE'NOSE_DOCTEST_EXTENSIONN)r7   )r   options
add_optiongetset_defaultsr
   )r   parserenvenv_settingr   r   r   rC      s>   	
zDoctest.optionsc                 C   s   t | || |j| _|j| _t|j| _|j| _t	
 | _d| _|jrud|jd}|D ]G}|r9|d dvrBtdd|f  |d |dd }}t	j|}|s\td|f |d	krh|  j|O  _q/|d
krt|  j| M  _q/dS dS )zConfigure plugin.
        r   ,z+-z+Must specify doctest options with starting z'+' or '-'.  Got %s   NzUnknown doctest option %s+-)r   	configurer:   r0   r
   r7   	extensionr>   fixturesdoctestDocTestFinderfinderoptionflagsr@   joinsplit
ValueErrorOPTIONFLAGS_BY_NAMErE   )r   rC   configflagsflagmodeoption_nameoption_flagr   r   r   rN      s:   
zDoctest.configurec                 C   s   |j | _ dS )znCapture loader's suiteClass.

        This is used to create test suites from doctest files.
        
        N)
suiteClass)r   loaderr   r   r   prepareTestLoader   s   zDoctest.prepareTestLoaderc                 c   s    t d| | |jst d| dS z| j|}W n ty-   t d| Y dS w |s8t d| dS |  t	|j
}g }|D ]}|jsKqE|jsQ||_|t|| j| jd qE|rk| j||ddV  dS dS )	z'Load doctests from the module.
        zloading from %szDoctest doesn't want module %sNzAttribute error loading from %szNo tests found in %s)rT   
result_varF)r!   r"   )logdebugmatchesr   rS   findAttributeError	exceptionsortr	   __file__examplesfilenamer6   DocTestCaserT   r:   r_   )r   moduler    module_filecasestestr   r   r   loadTestsFromModule   s:   

zDoctest.loadTestsFromModulec              
   c   s|   | j rt|j| j rtj|}t|}z
| }W |  n|  w d}d|i}| j	rtj
|\}}tj|}	tj|	 || j	 }
zt|
t t dg}W n tyn } ztd|
|tj W Y d}~nd}~ww td|
| t|dr||}t }|j||||dd}|jrt|| jt|d	dt|d
d| jd}|rt|f|dV  dS |V  dS dV  dS dS dS )zLoad doctests from the file.

        Tests are loaded only if filename's extension matches
        configured doctest extension.

        Nrj   nopzCould not import %s: %s (%s)z Fixture module %s resolved to %sglobsr   )rt   namerl   lineno
setup_testteardown_test)rT   setUptearDownrb   )r!   F)rO   r   endswithospathbasenameopenreadcloserP   splitextdirnamer   r6   
__import__globalslocalsImportErrorrc   rd   hasattrrt   rQ   DocTestParserget_doctestrk   DocFileCaserT   getattrr:   r   )r   rl   ru   dhdocfixture_contextrt   baseextr   fixt_moderG   rq   caser   r   r   loadTestsFromFile   s^   








zDoctest.loadTestsFromFilec                 c   sh    t |ddt| }| jj|t||d}|r0|D ]}t|jdkr$qt||| j| j	dV  qdS dS )zbLook for doctests in the given object, which will be a
        function, method or class.
        r   zUnnammed %s)rn   ru   r   )objrT   rb   N)
r   typerS   rf   r   lenrk   rm   rT   r:   )r   r   parentru   doctestsrq   r   r   r   makeTest/  s   

zDoctest.makeTestc                    sv    dkrdS | j s%| jj  s%| jjo$dd  fdd| jjD D o:| jj p:dd  fdd| jjD D  S )Nz__init__.pyFc                 S      g | ]}|r|qS r   r   .0_fr   r   r   
<listcomp>D      
z#Doctest.matches.<locals>.<listcomp>c                       g | ]}|  qS r   search)r   incru   r   r   r   D      c                 S   r   r   r   r   r   r   r   r   G  r   c                    r   r   r   r   excr   r   r   r   G  r   )r0   conf	testMatchr   includeexcluder   ru   r   r   r   re   <  s   

zDoctest.matchesc                    sR     drdS | jr't j | jr'| jjr%dd  fdd| jjD D s'dS dS )zfOverride to select all modules and any file ending with
        configured doctest extension.
        z.pyTc                 S   r   r   r   r   r   r   r   r   U  r   z$Doctest.wantFile.<locals>.<listcomp>c                    r   r   r   r   filer   r   r   U  r   N)r{   rO   r   r   r   )r   r   r   r   r   wantFileJ  s   
zDoctest.wantFile)r   r   r   r-   rO   r   r_   rC   rN   ra   rr   r   r   re   r   r   r   r   r   r.      s    +!2r.   c                       sj   e Zd ZdZ		d fdd	Zdd Zd	d
 Zdd ZeZdd Z	 fddZ
dd Z fddZ  ZS )rm   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.
    r   N_c                    s*   || _ || _tt| j|||||d d S N)rT   ry   rz   checker)_result_var	_nose_objsuperrm   r   )r   rq   rT   ry   rz   r   r   rb   	__class__r   r   r   b  s   

zDocTestCase.__init__c                 C   s   | j d ur
t| j S t| jj}t|r>| jjd}d|d d }t|}t|}|d |d d|d |d gfS t|S )N.r   rK      )r   r   r   _dt_testru   r   rV   rU   )r   r   parts
class_namecls	base_addrr   r   r   r&   j  s   

zDocTestCase.addressc                 C   sF   | j j}| j j}|d ur!t|}|d u r|S ||s!d||f }|S )Nz%s.%s)r   ru   rl   r   
startswith)r   ru   rl   pkr   r   r   id  s   
zDocTestCase.idc                 C   s0   |   }|d}d|d d|d d f S )Nr   z%s (%s)r   )r   rV   rU   r   r   r   r   __repr__  s   
zDocTestCase.__repr__c                 C   s   d|    S )NzDoctest: %s)r   r   r   r   r   shortDescription  s   zDocTestCase.shortDescriptionc                    ,   | j d urtj| _| jt_tt|   d S r   )r   r   displayhook_old_displayhook_displayhookr   rm   ry   r   r   r   r   ry        
zDocTestCase.setUpc                 C   *   |d u rd S t t| j| tt| d S r   setattrbuiltin_modr   printreprr   valuer   r   r   r        zDocTestCase._displayhookc                    4   t t|   | jd ur| jt_tt| j d S d S r   )	r   rm   rz   r   r   r   r   delattrr   r   r   r   r   rz     
   
zDocTestCase.tearDown)r   NNNNr   )r   r   r   r-   r   r&   r   r   r,   r   ry   r   rz   __classcell__r   r   r   r   rm   [  s    rm   c                       sN   e Zd ZdZ		d fdd	Zdd Z fd	d
Zdd Z fddZ  Z	S )r   zfOverrides to provide address() method that returns the correct
    address for the doc file case.
    r   Nr   c                    s$   || _ tt| j||||d d d S r   )r   r   r   r   )r   rq   rT   ry   rz   r   rb   r   r   r   r     s
   

zDocFileCase.__init__c                 C   s   | j jd d fS r   )r   rl   r   r   r   r   r&     s   zDocFileCase.addressc                    r   r   )r   r   r   r   r   r   r   ry   r   r   r   r   ry     r   zDocFileCase.setUpc                 C   r   r   r   r   r   r   r   r     r   zDocFileCase._displayhookc                    r   r   )	r   r   rz   r   r   r   r   r   r   r   r   r   r   rz     r   zDocFileCase.tearDown)r   NNNr   )
r   r   r   r-   r   r&   ry   r   rz   r   r   r   r   r   r     s    r   )(r-   loggingr|   r   r#   inspectr   nose.plugins.baser   
nose.suiter   	nose.utilr   r   r   r   r	   r
   r   ior   r   builtinsr   	getLoggerr   rc   rQ   rm   rg   nose.ext.dtcompatr   dtcompat_OutputRedirectingPdbr   r   r$   r   r.   r   r   r   r   r   <module>   s>    3$

 UK