U
    adX                     @   s  d Z ddlZddlZddlZddlZddlZddlZddlZddlmZm	Z	 ddl
mZmZmZ dZedejZG dd	 d	ejZd
d Zdd Zdd Zdd Zdd ZG dd deZe ZdddZejdfddZdejejfddZ dejejfddZ!dS ) zLoading unittests.    N)fnmatchfnmatchcase   )casesuiteutilTz[_a-z]\w*\.py$c                       s,   e Zd ZdZ fddZ fddZ  ZS )_FailedTestNc                    s   || _ tt| | d S N)
_exceptionsuperr   __init__)selfZmethod_name	exception	__class__ %/usr/lib/python3.8/unittest/loader.pyr      s    z_FailedTest.__init__c                    s*   | j krtt |S  fdd}|S )Nc                      s
    j d S r	   )r
   r   r   r   r   testFailure!   s    z,_FailedTest.__getattr__.<locals>.testFailure)_testMethodNamer   r   __getattr__)r   namer   r   r   r   r      s    
z_FailedTest.__getattr__)__name__
__module____qualname__r   r   r   __classcell__r   r   r   r   r      s   r   c                 C   s"   d| t  f }t| t|||S )Nz#Failed to import test module: %s
%s)	traceback
format_exc_make_failed_testImportError)r   
suiteClassmessager   r   r   _make_failed_import_test&   s
     r"   c                 C   s   dt  f }t| |||S )NzFailed to call load_tests:
%s)r   r   r   )r   r   r    r!   r   r   r   _make_failed_load_tests+   s       r#   c                 C   s   t | |}||f|fS r	   )r   )
methodnamer   r    r!   testr   r   r   r   0   s    
r   c                 C   s<   t t|dd }| |i}tdt jf|}||| fS )Nc                 S   s   d S r	   r   r   r   r   r   testSkipped5   s    z'_make_skipped_test.<locals>.testSkippedZModuleSkipped)r   skipstrtypeTestCase)r$   r   r    r&   ZattrsZ	TestClassr   r   r   _make_skipped_test4   s
    
r+   c                 C   s*   |   dr| d d S tj| d S )Nz	$py.classir   )lowerendswithospathsplitext)r/   r   r   r   _jython_aware_splitext<   s    r1   c                       s   e Zd ZdZdZeejZdZ	e
jZdZ fddZdd Zddd	d
Zd!ddZd"ddZdd Zd#ddZdd Zdd Zdd Zdd Zd$ddZd%dd Z  ZS )&
TestLoaderz
    This class is responsible for loading tests according to various criteria
    and returning them wrapped in a TestSuite
    r%   Nc                    s    t t|   g | _t | _d S r	   )r   r2   r   errorsset_loading_packagesr   r   r   r   r   M   s    zTestLoader.__init__c                 C   sF   t |tjrtd| |}|s2t|dr2dg}| t||}|S )z;Return a suite of all test cases contained in testCaseClasszYTest cases should not be derived from TestSuite. Maybe you meant to derive from TestCase?ZrunTest)
issubclassr   	TestSuite	TypeErrorgetTestCaseNameshasattrr    map)r   testCaseClassZtestCaseNamesZloaded_suiter   r   r   loadTestsFromTestCaseT   s    
z TestLoader.loadTestsFromTestCasepatternc             
   O   s:  t |dksd|kr,tdt |dd t |dkrRt |d }td|t |dkrxt|d }td|g }t|D ]4}t	||}t
|trt|tjr|| | qt	|dd}	| |}|	dk	r6z|	| ||W S  tk
r4 }
 z,t|j|
| j\}}| j| | W Y S d}
~
X Y nX |S )	z>Return a suite of all test cases contained in the given moduler   Zuse_load_testsz(use_load_tests is deprecated and ignoredNr   zCloadTestsFromModule() takes 1 positional argument but {} were givenz=loadTestsFromModule() got an unexpected keyword argument '{}'
load_tests)lenwarningswarnDeprecationWarningpopr8   formatsorteddirgetattr
isinstancer)   r6   r   r*   appendr=   r    	Exceptionr#   r   r3   )r   moduler?   argsZkwsZ	complainttestsr   objr@   e
error_caseerror_messager   r   r   loadTestsFromModuleb   s<    


  zTestLoader.loadTestsFromModulec                 C   sX  | d}d\}}|dkr|dd }|rzd|}t|}W qW q& tk
r   | }t|| j\}}|s| j| | Y S Y q&X q&|dd }|}	|D ]}
z|	t	|	|
 }}	W q t
k
rN } zvt	|	dddk	r|dk	r| j| | W Y D  S t|
|| jdt f \}}| j| | W Y 
  S W 5 d}~X Y qX qt|	tjrj| |	S t|	trt|	tjr| |	S t|	tjrt|trt|tjr|d }||}tt	||tjs| |gS nt|	tjr|	S t|	rH|	 }t|tjr|S t|tjr6| |gS td|	|f ntd	|	 dS )
aS  Return a suite of all test cases given a string specifier.

        The name may resolve either to a module, a test case class, a
        test method within a test case class, or a callable object which
        returns a TestCase or TestSuite instance.

        The method optionally resolves the names relative to a given module.
        .)NNNr   __path__zFailed to access attribute:
%sz"calling %s returned %s, not a testz$don't know how to make test from: %s)splitjoin
__import__r   rE   r"   r    r3   rK   rI   AttributeErrorr   r   r   rJ   types
ModuleTyperT   r)   r6   r   r*   r=   FunctionTyper   r7   callabler8   )r   r   rM   partsrR   rS   Z
parts_copymodule_nameZnext_attributerP   partparentrQ   instr%   r   r   r   loadTestsFromName   s    	

   $



zTestLoader.loadTestsFromNamec                    s    fdd|D } |S )zReturn a suite of all test cases found using the given sequence
        of string specifiers. See 'loadTestsFromName()'.
        c                    s   g | ]} | qS r   )re   ).0r   rM   r   r   r   
<listcomp>   s     z1TestLoader.loadTestsFromNames.<locals>.<listcomp>)r    )r   namesrM   Zsuitesr   rg   r   loadTestsFromNames   s    zTestLoader.loadTestsFromNamesc                    s>    fdd}t t|t} jr:|jt jd |S )zLReturn a sorted sequence of method names found within testCaseClass
        c                    sZ   |  jsdS t| }t|s&dS djj| f  jd kpXt fddjD S )NFz%s.%s.%sc                 3   s   | ]}t  |V  qd S r	   )r   )rf   r?   ZfullNamer   r   	<genexpr>   s     zKTestLoader.getTestCaseNames.<locals>.shouldIncludeMethod.<locals>.<genexpr>)
startswithtestMethodPrefixrI   r_   r   r   testNamePatternsany)attrnameZtestFuncr   r<   rk   r   shouldIncludeMethod   s    
  
z8TestLoader.getTestCaseNames.<locals>.shouldIncludeMethod)key)listfilterrH   sortTestMethodsUsingsort	functools
cmp_to_key)r   r<   rs   ZtestFnNamesr   rr   r   r9      s
    zTestLoader.getTestCaseNamestest*.pyc                 C   sJ  d}|dkr| j dk	r| j }n|dkr.d}|}tj|}|tjkrRtjd| || _ d}d}g }tjtj|rtj|}||krtjtj|d }npzt	| W n t
k
r   d}Y nJX tj| }|dd }	ztjtj|j}W n tk
r   z
|j}
W n tk
r8   d}
Y nX |
r|
jdkr|
jdk	rd}|jD ]P}|s|||s|qb||jdtjjd | _ || j||dd qbn*|jtjkrtddntd	|dY nX |r|s| |	| _ tj| ntj| |r*t
d
| |s@t| ||}|  |S )a%  Find and return all test modules from the specified start
        directory, recursing into subdirectories to find them and return all
        tests found within them. Only test files that match the pattern will
        be loaded. (Using shell style pattern matching.)

        All test modules must be importable from the top level of the project.
        If the start directory is not the top level directory then the top
        level directory must be specified separately.

        If a test package name (directory with '__init__.py') matches the
        pattern then the package will be checked for a 'load_tests' function. If
        this exists then it will be called with (loader, tests, pattern) unless
        the package has already had load_tests called from the same discovery
        invocation, in which case the package module object is not scanned for
        tests - this ensures that when a package uses discover to further
        discover child tests that infinite recursion does not happen.

        If load_tests exists then discovery does *not* recurse into the package,
        load_tests is responsible for loading all tests in the package.

        The pattern is deliberately not stored as a loader attribute so that
        packages can continue discovery themselves. top_level_dir is stored so
        load_tests does not need to pass this argument in to loader.discover().

        Paths are sorted before being imported to ensure reproducible execution
        order even on filesystems with non-alphabetical ordering like ext3/4.
        FNTr   __init__.pyrU   )	namespacez2Can not use builtin modules as dotted module namesz$don't know how to discover from {!r}z%Start directory is not importable: %r)!_top_level_dirr.   r/   abspathsysinsertisdirisfilerY   rZ   r   modulesrX   dirname__file__r[   __spec__loadersubmodule_search_locationsrV   rm   r   replacesepextend_find_testsbuiltin_module_namesr8   rF    _get_directory_containing_moduleremoveru   r    )r   	start_dirr?   Ztop_level_dirZset_implicit_topZis_not_importableis_namespacerO   Z
the_moduleZtop_partspecr/   r   r   r   discover   s    





 
zTestLoader.discoverc                 C   sR   t j| }tj|j}tj| drBtj	tj	|S tj	|S d S )Nr|   )
r   r   r.   r/   r   r   basenamer,   rm   r   )r   ra   rM   	full_pathr   r   r   r   `  s
    
z+TestLoader._get_directory_containing_modulec                 C   sh   || j krdS ttj|}tj|| j }tj|rBtd|drTtd|	tjj
d}|S )NrU   zPath must be within the projectz..)r~   r1   r.   r/   normpathrelpathisabsAssertionErrorrm   r   r   )r   r/   Z_relpathr   r   r   r   _get_name_from_pathl  s    
zTestLoader._get_name_from_pathc                 C   s   t | tj| S r	   )rZ   r   r   )r   r   r   r   r   _get_module_from_namex  s    z TestLoader._get_module_from_namec                 C   s
   t ||S r	   )r   )r   r/   r   r?   r   r   r   _match_path|  s    zTestLoader._match_pathFc           
   
   c   s   |  |}|dkrD|| jkrD| |||\}}|dk	r<|V  |sDdS tt|}|D ]t}tj||}	| |	||\}}|dk	r|V  |rV|  |	}| j| z| 
|	||E dH  W 5 | j	| X qVdS )z/Used by discovery. Yields test suites it loads.rU   N)r   r5   _find_test_pathrG   r.   listdirr/   rY   adddiscardr   )
r   r   r?   r}   r   rO   Zshould_recursepathsr/   r   r   r   r   r     s6    
    
zTestLoader._find_testsc              
   C   st  t j|}t j|rVt|s(dS | |||s:dS | |}z| |}W nh t	j
k
r } zt||| jdf W Y S d}~X Y n   t|| j\}}	| j|	 |df Y S X t jt|d|}
tt j|
}tt j|}| | kr@t j|}tt j|}t j|}d}t||||f | j||ddfS nt j|rl|st jt j|dsdS d}d}| |}z| |}W nj t	j
k
r } zt||| jdf W Y S d}~X Y n   t|| j\}}	| j|	 |df Y S X t|dd}| j| z0| j||d}|dk	rP|dfW S |d	fW S | j| X ndS dS )
zUsed by discovery.

        Loads tests from a single file, or a directories' __init__.py when
        passed the directory.

        Returns a tuple (None_or_tests_from_file, should_recurse).
        )NFFNr   zW%r module incorrectly imported from %r. Expected %r. Is this module globally installed?r>   r|   r@   T)r.   r/   r   r   VALID_MODULE_NAMEmatchr   r   r   r   ZSkipTestr+   r    r"   r3   rK   r   rI   r1   realpathr,   r   r   rT   r   rY   r5   r   r   )r   r   r?   r}   r   r   rM   rQ   rR   rS   Zmod_filer   Zfullpath_noextZ
module_dirZmod_nameZexpected_dirmsgr@   rO   packager   r   r   r     s|    

&





&

zTestLoader._find_test_path)N)N)r{   N)F)F)r   r   r   __doc__rn   staticmethodr   three_way_cmprw   ro   r   r7   r    r~   r   r=   rT   re   rj   r9   r   r   r   r   r   r   r   r   r   r   r   r   r2   B   s&   
(
N

n
"r2   c                 C   s&   t  }||_| |_||_|r"||_|S r	   )r2   rw   rn   ro   r    )prefix	sortUsingr    ro   r   r   r   r   _makeLoader  s    r   c                 C   s   t |||d| S )N)ro   )r   r9   )r<   r   r   ro   r   r   r   r9     s    r9   r%   c                 C   s   t |||| S r	   )r   r=   )r<   r   r   r    r   r   r   	makeSuite  s    r   c                 C   s   t |||| S r	   )r   rT   )rM   r   r   r    r   r   r   findTestCases  s    r   )NN)"r   r.   rer   r   r\   ry   rB   r   r    r   r   r   Z
__unittestcompile
IGNORECASEr   r*   r   r"   r#   r   r+   r1   objectr2   ZdefaultTestLoaderr   r   r9   r7   r   r   r   r   r   r   <module>   s<      /
	
