U
    ZeW                     @   sD  d 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 ddlZejdkrejdd d	k rddlZed
 ddlmZ nG dd deZeeZe Zdd ZG dd deZG dd dejZG dd deZG dd deZ G dd deZ!G dd dejZ"G dd dZ#G dd dZ$dS ) z
Test Suites
-----------

Provides a LazySuite, which is a suite whose test list is a generator
function, and ContextSuite,which can run fixtures (setup/teardown
functions or methods) for the context that contains its tests.

    N)Test)Config)ResultProxyFactory)isclassresolve_nametry_runcli   )r	      
IronPython)StringExceptionc                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r   i/mounts/lovelace/software/anaconda3/envs/qiime2-amplicon-2024.2/lib/python3.8/site-packages/nose/suite.pyr      s   r   c                 C   s   d| j | jf S )Nz%s.%s)r   r   )clsr   r   r   	_strclass%   s    r   c                   @   s   e Zd ZdZdS )MixedContextErrorzQError raised when a context suite sees tests from more than
    one context.
    N)r   r   r   __doc__r   r   r   r   r   (   s   r   c                       st   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	dd Z
dd Zdd Zdd ZeeeddZ  ZS )	LazySuitez:A suite that may use a generator as its list of tests
    r   c                    s   t t|   | | dS )zFInitialize the suite. tests may be an iterable or a generator
        N)superr   __init__
_set_tests)selftests	__class__r   r   r   2   s    zLazySuite.__init__c                 C   s
   t | jS N)iter_testsr   r   r   r   __iter__8   s    zLazySuite.__iter__c                 C   s   dt | jt| f S )Nz<%s tests=generator (%s)>)r   r   idr!   r   r   r   __repr__;   s     zLazySuite.__repr__c                 C   s
   t | S r   object__hash__r!   r   r   r   r'   ?   s    zLazySuite.__hash__c                 C   s   | j | d S r   )	_precacheappendr   testr   r   r   addTestD   s    zLazySuite.addTestc                 C   s"   | j D ]}|jr q|| q|S r   )r    
shouldStop)r   resultr+   r   r   r   runH   s
    

zLazySuite.runc                 C   sj   t dt|  | jrdS | jd kr(dS z(t| j}|d k	rN| j| W dS W n tk
rd   Y nX dS )Nztests in %s?TF)logdebugr#   r(   test_generatornextr)   StopIterationr*   r   r   r   __bool__O   s    


zLazySuite.__bool__c                 c   sD   t d| j | jD ]
}|V  q| jd kr.d S | jD ]
}|V  q4d S )Nzprecache is %s)r0   r1   r(   r2   r*   r   r   r   
_get_tests^   s    


zLazySuite._get_testsc                 C   sX   g | _ t|tj}t|tjr,|s,| | _n(|rD| |g d | _n| | d | _d S r   )r(   
isinstanceunittest	TestSuitecollectionsCallabler2   ZaddTests)r   r   Zis_suiter   r   r   r   g   s    

zLazySuite._set_testsNzbAccess the tests in this suite. Access is through a generator, so iteration may not be repeatable.)r   )r   r   r   r   r   r"   r$   r'   __str__r,   r/   r5   r6   r   propertyr    __classcell__r   r   r   r   r   /   s   	r   c                       s   e Zd ZdZejjZdZdZdZ	dZ
dZdZdZdZd- fdd	Zdd ZeZdd Zdd Zdd Zdd Zdd Zdd Zd.ddZdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Ze ee!j"d
d,Z#  Z$S )/ContextSuitea4  A suite with context.

    A ContextSuite executes fixtures (setup and teardown functions or
    methods) for the context containing its tests.

    The context may be explicitly passed. If it is not, a context (or
    nested set of contexts) will be constructed by examining the tests
    in the suite.
    F)Zsetup_classZ	setup_allZ
setupClassZsetupAllZ
setUpClassZsetUpAll)Zteardown_classZteardown_allZteardownClassZteardownAllZtearDownClassZtearDownAll)Zsetup_moduleZsetupModuleZsetUpModulesetupsetUp)Zteardown_moduleZteardownModuleZtearDownModuleteardowntearDown)Zsetup_packageZsetupPackageZsetUpPackage)Zteardown_packageZteardownPackageZtearDownPackager   NTc                    s`   t d||t|  || _|| _|d kr.t }|| _|| _d| _|| _	d | _
tt| | d S )NzContext suite for %s (%s) (%s)F)r0   r1   r#   contextfactoryr   configresultProxyhas_run	can_spliterror_contextr   r?   r   )r   r   rD   rE   rF   rG   rI   r   r   r   r      s    zContextSuite.__init__c                 C   s   dt | jt| jd| jf S )Nz<%s context=%s>r   )r   r   getattrrD   r!   r   r   r   r$      s    zContextSuite.__repr__c                 C   s$   | j rdt| | j f S t| S d S )Nz%s:%s)rJ   reprr!   r   r   r   r#      s    zContextSuite.idc                 C   s
   t | S r   r%   r!   r   r   r   r'      s    zContextSuite.__hash__c                 O   s   | j ||S r   r/   r   argkwr   r   r   __call__   s    zContextSuite.__call__c                 C   s   t  S )z.Hook for replacing error tuple output
        )sysexc_infor!   r   r   r   rS      s    zContextSuite.exc_infoc                 C   s>   |   }tjdkr:t|d tr:t|d |d |d f}|S )z:Bottleneck to fix up IronPython string exceptions
        r   r      r	   )rS   rR   platformr7   r   str)r   er   r   r   	_exc_info   s
    
zContextSuite._exc_infoc                 C   s   t dt| | | j | jr0| || | }}n
|| }}z|   W n8 tk
r\    Y n$   d| _|| | 	  Y dS X z,| jD ] }|jrt d  q|| qW 5 d| _
z|   W n6 tk
r    Y n"   d| _|| | 	  Y nX X dS )z5Run tests in suite inside of suite fixtures.
        z#suite %s (%s) run called, tests: %sr@   NTrB   Zstopping)r0   r1   r#   r    rG   rA   KeyboardInterruptrJ   ZaddErrorrX   rH   rC   r-   )r   r.   origr+   r   r   r   r/      s4    


zContextSuite.runc                 C   s^   | j }|d krdS | j||dr$dS | j}|rZ|j | g }|D ]}| j||dr@ dS q@dS )NF)ctx_callbackT)rD   implementsAnyFixturerE   get)r   r[   rD   rE   	ancestorsancestorr   r   r   hasFixtures   s     zContextSuite.hasFixturesc                 C   sr   t |r| j| j }n&| j| j }t|dr<|| j| j 7 }d}|D ]}t||rDd} q\qD|d krh|S |||S )N__path__FT)r   
classSetupclassTeardownmoduleSetupmoduleTeardownhasattrpackageSetuppackageTeardown)r   rD   r[   namesZfixtmr   r   r   r\      s    

z!ContextSuite.implementsAnyFixturec                 C   s   t dt| | j | s,t dt|  d S | jrFt dt|  d S | j}|d krXd S | j}|r|j| g d d  }|r| }t d| ||jkrqxt d| | 	| qx||jkr| 	| n
| 	| d| _t d d S )Nz suite %s setUp called, tests: %szsuite %s has no testszsuite %s already set upzancestor %s may need setupzancestor %s does need setupTzcompleted suite setup)
r0   r1   r#   r    	was_setuprD   rE   r]   popsetupContext)r   rD   rE   r^   r_   r   r   r   rA     s2    


zContextSuite.setUpc                 C   sv   | j j| td| | | jr>|| jjkr2d S | | jj|< t|rN| j}n| j	}t
|drh| j| }t|| d S )Nz%s setup context %sra   )rF   pluginsZstartContextr0   r1   rE   rk   r   rb   rd   rf   rg   r   r   rD   ri   r   r   r   rm   -  s    

zContextSuite.setupContextc                 C   s   | j d krdS d| j  S )Nz
test suiteztest suite for %srD   r!   r   r   r   shortDescription>  s    
zContextSuite.shortDescriptionc                 C   s   t d | jr| jr0t d| j| jf  d S d| _| j}|d krRt d d S | j}|r|j| g |g }|D ]j}t d| ||jkrt d| qt||jkrt d| qt|j| }t d|| || krt| | qtn
| | d S )	Nzcontext teardownz6No reason to teardown (was_setup? %s was_torndown? %s)TzNo context to tear downzancestor %s may need teardownzancestor %s was not setupzancestor %s already torn downz%s setup ancestor %s)r0   r1   rk   was_torndownrD   rE   r]   teardownContext)r   rD   rE   r^   r_   r@   r   r   r   rC   C  s:    





zContextSuite.tearDownc                 C   sv   t d| | | jr0|| jjkr$d S | | jj|< t|r@| j}n| j}t|drZ| j| }t	|| | j
j| d S )Nz%s teardown context %sra   )r0   r1   rE   rr   r   rc   re   rf   rh   r   rF   rn   ZstopContextro   r   r   r   rs   d  s    


zContextSuite.teardownContextc                 c   sD   |   D ]6}t|ts"t|tjr*|V  qt|| j| jdV  qd S )NrF   rG   )r6   r7   r   r8   r9   rF   rG   r*   r   r   r   _get_wrapped_testst  s    zContextSuite._get_wrapped_testszOAccess the tests in this suite. Tests are returned inside of a context wrapper.)r   NNNNT)N)%r   r   r   r   r8   ZTestCaseZfailureExceptionrk   rr   rb   rc   rd   re   rg   rh   r   r$   r<   r#   r'   rQ   rS   rX   r/   r`   r\   rA   rm   rq   rC   rs   ru   r=   r   r   r    r>   r   r   r   r   r?   z   sD   	      &
 !	
r?   c                   @   sT   e Zd ZdZeZddefddZdd Zdd Z	d	d
 Z
dd Zdd Zdd ZdS )ContextSuiteFactoryaC  Factory for ContextSuites. Called with a collection of tests,
    the factory decides on a hierarchy of contexts by introspecting
    the collection or the tests themselves to find the objects
    containing the test objects. It always returns one suite, but that
    suite may consist of a hierarchy of nested suites.
    Nc                 C   sV   |d krt  }|| _|d k	r"|| _|tkr4t|d}|| _i | _i | _i | _i | _	d S )N)rF   )
r   rF   
suiteClass_defr   rG   suitesrD   rk   rr   )r   rF   rw   rG   r   r   r   r     s    
zContextSuiteFactory.__init__c                 K   s   t d| |dt|dd}t d|| |dkrz| |}z| |}W n* tk
rx   | j| |df| Y S X | j||f|S )a  Return ``ContextSuite`` for tests. ``tests`` may either
        be a callable (in which case the resulting ContextSuite will
        have no parent context and be evaluated lazily) or an
        iterable. In that case the tests will wrapped in
        nose.case.Test, be examined and the context of each found and a
        suite of suites returned, organized into a stack with the
        outermost suites belonging to the outermost contexts.
        zCreate suite for %srD   Nztests %s context %s)	r0   r1   rl   rK   	wrapTestsfindContextr   	makeSuitemixedSuites)r   r   rP   rD   r   r   r   rQ     s    	
zContextSuiteFactory.__call__c                 c   s   t d| |dkrdS t|dr,|jj}nt|dr>|jj}t|drV|jd}n,t|drv|jddd }ntd	| |rt d
|| t	d
|V  |  qdS )zReturn the ancestry of the context (that is, all of the
        packages and modules containing the context), in order of
        descent with the outermost ancestor last.
        This method is a generator.
        zget ancestry %sNZim_class__self__r   .r   z%s has no ancestors?z %s ancestors %s)r0   r1   rf   r~   r   r   splitr   	TypeErrorr   joinrl   )r   rD   r^   r   r   r   ancestry  s     




zContextSuiteFactory.ancestryc                 C   sj   t |tjst |tjrd S d }|D ]@}t|dd }|d kr>q$|d krL|}q$||kr$td||f q$|S )NrD   z5Tests with different contexts in same suite! %s != %s)r7   r:   r;   r8   r9   rK   r   )r   r   rD   r+   ctxr   r   r   r{     s     zContextSuiteFactory.findContextc                 K   s   | j |f|| j| | jd|}|d k	r| j|g | | j|g | td|t	|dd  | 
|D ]8}| j|g | | j| | td||j qp|S )N)rD   rF   rE   rG   zsuite %s has context %sr   zsuite %s has ancestor %s)rw   rF   rG   ry   
setdefaultr)   rD   r0   r1   rK   r   r   )r   r   rD   rP   suiter_   r   r   r   r|     s*      
zContextSuiteFactory.makeSuitec                 C   s  |sg S | d}|s|gS |}|dd }t|dd}|dk	r|gdd | |D  }|D ]}|g}g }	|D ]t}
d}t|
dd}|dkr|	|
 qr||kr||
 qr| |D ]}||kr||
 d} qq|sr|	|
 qr|r| ||}| |	}q`|g| S )ac  The complex case where there are tests that don't all share
        the same context. Groups tests into suites with common ancestors,
        according to the following (essentially tail-recursive) procedure:

        Starting with the context of the first test, if it is not
        None, look for tests in the remaining tests that share that
        ancestor. If any are found, group into a suite with that
        ancestor as the context, and replace the current suite with
        that suite. Continue this process for each ancestor of the
        first test, until all ancestors have been processed. At this
        point if any tests remain, recurse with those tests as the
        input, returning a list of the common suite (which may be the
        suite or test we started with, if no common tests were found)
        plus the results of recursion.
        r   NrD   c                 S   s   g | ]}|qS r   r   ).0ar   r   r   
<listcomp>  s     z3ContextSuiteFactory.mixedSuites.<locals>.<listcomp>FT)rl   rK   r   r)   r|   r}   )r   r   headr   tailrD   r^   r_   commonZremainr+   Zfound_commonZtest_ctxZtest_ancestorr   r   r   r}     sB    




zContextSuiteFactory.mixedSuitesc                 C   s   t d| t|tjs$t|tjr2t d |S g }|D ]l}t d| t|ts`t|tjrl|| q:t|t	r|| j
||jd q:|t|| j| jd q:|S )Nzwrap %szI won't wrapzwrapping %srp   rt   )r0   r1   r7   r:   r;   r8   r9   r   r)   ContextListr|   rD   rF   rG   )r   r   wrappedr+   r   r   r   rz     s    

zContextSuiteFactory.wrapTests)r   r   r   r   r?   rw   rx   r   rQ   r   r{   r|   r}   rz   r   r   r   r   rv     s   2rv   c                   @   s"   e Zd ZdZdddZdd ZdS )r   zNot quite a suite -- a group of tests in a context. This is used
    to hint the ContextSuiteFactory about what context the tests
    belong to, in cases where it may be ambiguous or missing.
    Nc                 C   s   || _ || _d S r   )r   rD   )r   r   rD   r   r   r   r   3  s    zContextList.__init__c                 C   s
   t | jS r   )r   r   r!   r   r   r   r"   7  s    zContextList.__iter__)N)r   r   r   r   r   r"   r   r   r   r   r   .  s   
r   c                       s8   e Zd ZdZ fddZdd Zdd Zdd	 Z  ZS )
FinalizingSuiteWrapperzWraps suite and calls final function after suite has
    executed. Used to call final functions in cases (like running in
    the standard test runner) where test running is not under nose's
    control.
    c                    s   t t|   || _|| _d S r   )r   r   r   r   finalize)r   r   r   r   r   r   r   A  s    zFinalizingSuiteWrapper.__init__c                 O   s   | j ||S r   rM   rN   r   r   r   rQ   F  s    zFinalizingSuiteWrapper.__call__c                 C   s
   t | jS r   )r   r   r!   r   r   r   r"   J  s    zFinalizingSuiteWrapper.__iter__c              	   O   s$   z| j||W S | j || X d S r   )r   r   rN   r   r   r   r/   M  s    zFinalizingSuiteWrapper.run)	r   r   r   r   r   rQ   r"   r/   r>   r   r   r   r   r   ;  s
   r   c                   @   s   e Zd Zdd ZdS )TestDirc                  O   s   t dd S )NzTestDir is not usable with nose 0.10. The class is present in nose.suite for backwards compatibility purposes but it may not be used.NotImplementedErrorrO   rP   r   r   r   r   V  s    zTestDir.__init__Nr   r   r   r   r   r   r   r   r   U  s   r   c                   @   s   e Zd Zdd ZdS )
TestModulec                  O   s   t dd S )NzTestModule is not usable with nose 0.10. The class is present in nose.suite for backwards compatibility purposes but it may not be used.r   r   r   r   r   r   ^  s    zTestModule.__init__Nr   r   r   r   r   r   ]  s   r   )%r   loggingrR   r8   Z	nose.caser   Znose.configr   Z
nose.proxyr   Z	nose.utilr   r   r   r:   rU   version_infoZclrZAddReferenceZIronPython.Runtime.Exceptionsr   	Exception	getLoggerr   r0   r&   rx   r   r   r9   r   r?   rv   r   r   r   r   r   r   r   r   <module>   s8   


K  
 -