B
    v9a®X  ã               @   s  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 )é    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.7/loader.pyr      s    z_FailedTest.__init__c                s*   |ˆ j krttˆ ƒ |¡S ‡ fdd„}|S )Nc                  s
   ˆ j ‚d S )N)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 )N)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 )Nr   )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.classi÷ÿÿÿr   )ÚlowerÚendswithÚosÚpathÚsplitext)r,   r   r   r   Ú_jython_aware_splitext<   s    r.   c                   s¨   e 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 )%Ú
TestLoaderr"   Nc                s    t t| ƒ ¡  g | _tƒ | _d S )N)r
   r/   r   ÚerrorsÚsetÚ_loading_packages)r   )r   r   r   r   M   s    zTestLoader.__init__c             C   sF   t |tjƒrtdƒ‚|  |¡}|s2t|dƒr2dg}|  t||ƒ¡}|S )NzYTest 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   s4  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 }x@t|ƒD ]4}t	||ƒ}t
|tƒr†t|tjƒr†| |  |¡¡ q†W t	|dd ƒ}	|  |¡}|	d k	r0y|	| ||ƒS  tk
r. }
 z$t|j|
| jƒ\}}| j |¡ |S d }
~
X Y nX |S )Nr   Zuse_load_testsz(use_load_tests is deprecated and ignoredr   zCloadTestsFromModule() takes 1 positional argument but {} were givenz=loadTestsFromModule() got an unexpected keyword argument '{}'Ú
load_tests)ÚlenÚwarningsÚwarnÚDeprecationWarningÚpopr5   ÚformatÚsortedÚdirÚgetattrÚ
isinstancer&   r3   r   r'   Úappendr:   r   Ú	Exceptionr    r   r0   )r   Úmoduler;   ÚargsZkwsZ	complaintÚtestsr   Úobjr<   ÚeÚ
error_caseÚerror_messager   r   r   ÚloadTestsFromModuleb   s4    


zTestLoader.loadTestsFromModulec             C   s>  |  d¡}d\}}|d kr–|d d … }xb|rˆyd |¡}t|ƒ}P W q( tk
r„   | ¡ }t|| jƒ\}}|s€| j |¡ |S Y q(X q(W |dd … }|}	xœ|D ]”}
y|	t	|	|
ƒ }}	W q  t
k
r2 } zZt	|	dd ƒd k	rô|d k	rô| j |¡ |S t|
|| jdt ¡ f ƒ\}}| j |¡ |S W d d }~X Y q X q W t|	tjƒrP|  |	¡S t|	tƒrtt|	tjƒrt|  |	¡S t|	tjƒrÎt|tƒrÎt|tjƒrÎ|d }||ƒ}tt	||ƒtjƒsà|  |g¡S nt|	tjƒrà|	S t|	ƒr.|	ƒ }t|tjƒr|S t|tjƒr|  |g¡S td|	|f ƒ‚ntd|	 ƒ‚d S )	NÚ.)NNr   Ú__path__zFailed to access attribute:
%séÿÿÿÿz"calling %s returned %s, not a testz$don't know how to make test from: %s)ÚsplitÚjoinÚ
__import__r   rA   r   r   r0   rG   rE   ÚAttributeErrorr   r   r   rF   ÚtypesÚ
ModuleTyperP   r&   r3   r   r'   r:   ÚFunctionTyper   r4   Úcallabler5   )r   r   rI   ÚpartsrN   rO   Z
parts_copyÚmodule_nameZnext_attributerL   ÚpartÚparentrM   Úinstr"   r   r   r   ÚloadTestsFromNameŠ   sl    	





zTestLoader.loadTestsFromNamec                s   ‡ ‡fdd„|D ƒ}ˆ  |¡S )Nc                s   g | ]}ˆ  |ˆ ¡‘qS r   )ra   )Ú.0r   )rI   r   r   r   ú
<listcomp>Ü   s    z1TestLoader.loadTestsFromNames.<locals>.<listcomp>)r   )r   ÚnamesrI   Zsuitesr   )rI   r   r   ÚloadTestsFromNamesØ   s    zTestLoader.loadTestsFromNamesc                s>   ‡ ‡fdd„}t t|tˆƒƒƒ}ˆ jr:|jt ˆ j¡d |S )Nc                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 )N)r   )rb   r;   )ÚfullNamer   r   ú	<genexpr>ì   s    zKTestLoader.getTestCaseNames.<locals>.shouldIncludeMethod.<locals>.<genexpr>)Ú
startswithÚtestMethodPrefixrE   r[   r   r   ÚtestNamePatternsÚany)ZattrnameZtestFunc)r   r9   )rf   r   ÚshouldIncludeMethodâ   s    

z8TestLoader.getTestCaseNames.<locals>.shouldIncludeMethod)Úkey)ÚlistÚfilterrD   ÚsortTestMethodsUsingÚsortÚ	functoolsÚ
cmp_to_key)r   r9   rl   ZtestFnNamesr   )r   r9   r   r6   ß   s
    zTestLoader.getTestCaseNamesútest*.pyc             C   sN  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 |¡¡r¨tj |¡}||kr¤tj tj |d¡¡ }ntyt	|ƒ W n t
k
rÎ   d}Y nNX tj| }| d¡d }	ytj tj |j¡¡}W næ tk
rè   y
|j}
W n tk
r8   d }
Y nX |
rº|
jd krº|
jd k	räd}x†|jD ]P}|s~| |¡s~qd| |j dtjj¡¡d | _ | | j||dd¡ qdW n*|jtjkrÔtdƒd ‚ntd |¡ƒd ‚Y nX |r|s|  |	¡| _ tj |¡ ntj |¡ |r.t
d	| ƒ‚|sDt|  ||¡ƒ}|   |¡S )
NFTr   z__init__.pyrQ   )Ú	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ÚisfilerU   rV   r   ÚmodulesrT   ÚdirnameÚ__file__rW   Ú__spec__ÚloaderÚsubmodule_search_locationsrR   rh   r   ÚreplaceÚsepÚextendÚ_find_testsÚbuiltin_module_namesr5   rB   Ú _get_directory_containing_moduleÚremovern   r   )r   Ú	start_dirr;   Ztop_level_dirZset_implicit_topZis_not_importableÚis_namespacerK   Z
the_moduleZtop_partÚspecr,   r   r   r   Údiscoverò   sv    





zTestLoader.discoverc             C   sR   t j| }tj |j¡}tj |¡ ¡  d¡rBtj 	tj 	|¡¡S tj 	|¡S d S )Nz__init__.py)
rx   r|   r+   r,   rw   r~   Úbasenamer)   rh   r}   )r   r]   rI   Ú	full_pathr   r   r   r‡   `  s
    
z+TestLoader._get_directory_containing_modulec             C   sB   || j krdS ttj |¡ƒ}tj || j ¡}| tjjd¡}|S )NrQ   )rv   r.   r+   r,   ÚnormpathÚrelpathr‚   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 )N)rV   rx   r|   )r   r   r   r   r   Ú_get_module_from_namex  s    z TestLoader._get_module_from_namec             C   s
   t ||ƒS )N)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 |¡ƒ}x||D ]t}tj ||¡}	|  |	||¡\}}|d k	rŠ|V  |rX|   |	¡}| j |¡ z|  	|	||¡E d H  W d | j 
|¡ X qXW d S )NrQ   )r‘   r2   Ú_find_test_pathrC   r+   Úlistdirr,   rU   Úaddr…   Údiscard)
r   r‰   r;   ru   r   rK   Zshould_recurseÚpathsr,   rŽ   r   r   r   r…   €  s*    


zTestLoader._find_testsc          
   C   sP  t j |¡}t j |¡rHt |¡s(dS |  |||¡s:dS |  |¡}y|  |¡}W nZ t	j
k
r„ } zt||| jƒdfS d }~X Y nÀ   t|| jƒ\}}	| j |	¡ |dfS t j t|d|ƒ¡}
tt j |
¡ƒ}tt j |¡ƒ}| ¡ | ¡ kr2t j |¡}tt j |¡ƒ}t j |¡}d}t||||f ƒ‚| j||ddfS nt j |¡rH|sxt j t j |d¡¡sxdS d }d }|  |¡}y|  |¡}W n\ t	j
k
rÌ } zt||| jƒdfS d }~X Y nz   t|| jƒ\}}	| j |	¡ |dfS t|dd ƒ}| j |¡ z(| j||d}|d k	r0|dfS |dfS | j |¡ X ndS d S )	N)NFFr~   zW%r module incorrectly imported from %r. Expected %r. Is this module globally installed?)r;   z__init__.pyr<   T)r+   r,   r   r{   ÚVALID_MODULE_NAMEÚmatchr“   r‘   r’   r   ZSkipTestr(   r   r   r0   rG   rw   rE   r.   Úrealpathr)   r}   r   rP   rz   rU   r2   r–   r—   )r   rŽ   r;   ru   r   r   rI   rM   rN   rO   Zmod_filer›   Zfullpath_noextZ
module_dirZmod_nameZexpected_dirÚmsgr<   rK   Úpackager   r   r   r”   ¢  sl    



zTestLoader._find_test_path)N)N)rt   N)F)F)r   r   r   ri   Ústaticmethodr   Úthree_way_cmprp   rj   r   r4   r   rv   r   r:   rP   ra   re   r6   rŒ   r‡   r‘   r’   r“   r…   r”   r   r   r   )r   r   r/   B   s$   
(
N

n
"r/   c             C   s&   t ƒ }||_| |_||_|r"||_|S )N)r/   rp   ri   rj   r   )ÚprefixÚ	sortUsingr   rj   r€   r   r   r   Ú_makeLoaderñ  s    r¢   c             C   s   t |||d | ¡S )N)rj   )r¢   r6   )r9   r    r¡   rj   r   r   r   r6   ú  s    r6   r"   c             C   s   t |||ƒ | ¡S )N)r¢   r:   )r9   r    r¡   r   r   r   r   Ú	makeSuiteý  s    r£   c             C   s   t |||ƒ | ¡S )N)r¢   rP   )rI   r    r¡   r   r   r   r   ÚfindTestCases  s    r¤   )NN)!r+   Úrerx   r   rX   rr   r>   r   r   Ú r   r   r   Z
__unittestÚcompileÚ
IGNORECASEr™   r'   r   r   r    r   r(   r.   Úobjectr/   ZdefaultTestLoaderr¢   rŸ   r6   r4   r£   r¤   r   r   r   r   Ú<module>   s6      /
	