B
    v9aSá  ã               @   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Zd dlZd dl	Z	ddl
mZ ddlmZmZmZmZmZ dZeƒ ZdZG dd„ deƒZG d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z G dd„ dƒZ!G dd„ de!ƒZ"G dd „ d e"ƒZ#G d!d"„ d"e"ƒZ$e %d#d$d%g¡Z&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ƒZ,G d.d/„ d/e,ƒZ-G d0d1„ d1e,ƒZ.dS )2é    Né   )Úresult)ÚstrclassÚ	safe_reprÚ_count_diff_all_purposeÚ_count_diff_hashableÚ_common_shorten_reprTz@
Diff is %s characters long. Set self.maxDiff to None to see it.c               @   s   e Zd ZdS )ÚSkipTestN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú/usr/lib/python3.7/case.pyr	      s   r	   c               @   s   e Zd ZdS )Ú_ShouldStopN)r
   r   r   r   r   r   r   r   !   s   r   c               @   s   e Zd ZdS )Ú_UnexpectedSuccessN)r
   r   r   r   r   r   r   r   &   s   r   c               @   s&   e Zd Zddd„Zejddd„ƒZdS )	Ú_OutcomeNc             C   s4   d| _ || _t|dƒ| _d| _g | _d | _g | _d S )NFÚ
addSubTestT)Úexpecting_failurer   ÚhasattrÚresult_supports_subtestsÚsuccessÚskippedÚexpectedFailureÚerrors)Úselfr   r   r   r   Ú__init__-   s    z_Outcome.__init__Fc          
   c   sè   | j }d| _ zÈy
d V  W nœ tk
r.   ‚ Y n¤ tk
rh } zd| _ | j |t|ƒf¡ W d d }~X Y nj tk
rz   Y nX   t ¡ }| j	r–|| _
nd| _ | j ||f¡ d }Y nX | jrÒ| j rÒ| j |d f¡ W d | j oÞ|| _ X d S )NTF)r   ÚKeyboardInterruptr	   r   ÚappendÚstrr   ÚsysÚexc_infor   r   r   r   )r   Ú	test_caseÚisTestZold_successÚer    r   r   r   ÚtestPartExecutor6   s*    
$
z_Outcome.testPartExecutor)N)F)r
   r   r   r   Ú
contextlibÚcontextmanagerr$   r   r   r   r   r   ,   s   
	r   c             C   s   | S )Nr   )Úobjr   r   r   Ú_idT   s    r(   c                s   ‡ fdd„}|S )Nc                s4   t | tƒs$t | ¡‡ fdd„ƒ}|} d| _ˆ | _| S )Nc                 s   t ˆ ƒ‚d S )N)r	   )ÚargsÚkwargs)Úreasonr   r   Úskip_wrapper]   s    z-skip.<locals>.decorator.<locals>.skip_wrapperT)Ú
isinstanceÚtypeÚ	functoolsÚwrapsÚ__unittest_skip__Ú__unittest_skip_why__)Ú	test_itemr,   )r+   r   r   Ú	decorator[   s    
zskip.<locals>.decoratorr   )r+   r4   r   )r+   r   ÚskipW   s    
r5   c             C   s   | rt |ƒS tS )N)r5   r(   )Ú	conditionr+   r   r   r   ÚskipIfg   s    r7   c             C   s   | st |ƒS tS )N)r5   r(   )r6   r+   r   r   r   Ú
skipUnlesso   s    r8   c             C   s
   d| _ | S )NT)Ú__unittest_expecting_failure__)r3   r   r   r   r   w   s    r   c                s4   t | tƒr t‡ fdd„| D ƒƒS t | tƒo2t| ˆ ƒS )Nc             3   s   | ]}t |ˆ ƒV  qd S )N)Ú_is_subtype)Ú.0r#   )Úbasetyper   r   ú	<genexpr>}   s    z_is_subtype.<locals>.<genexpr>)r-   ÚtupleÚallr.   Ú
issubclass)Úexpectedr<   r   )r<   r   r:   {   s    
r:   c               @   s   e Zd Zdd„ Zdd„ ZdS )Ú_BaseTestCaseContextc             C   s
   || _ d S )N)r!   )r   r!   r   r   r   r   ‚   s    z_BaseTestCaseContext.__init__c             C   s    | j  | j|¡}| j  |¡‚d S )N)r!   Ú_formatMessageÚmsgÚfailureException)r   ÚstandardMsgrD   r   r   r   Ú_raiseFailure…   s    z"_BaseTestCaseContext._raiseFailureN)r
   r   r   r   rG   r   r   r   r   rB   €   s   rB   c               @   s   e Zd Zddd„Zdd„ ZdS )Ú_AssertRaisesBaseContextNc             C   s@   t  | |¡ || _|| _|d k	r*t |¡}|| _d | _d | _d S )N)	rB   r   rA   r!   ÚreÚcompileÚexpected_regexÚobj_namerD   )r   rA   r!   rK   r   r   r   r   ‹   s    
z!_AssertRaisesBaseContext.__init__c          	   C   sÔ   zÈt | j| jƒs"td|| jf ƒ‚|rD|d d krDt dtd¡ d}|sx| dd ¡| _	|rtt dt
t|ƒƒ td¡ | S |^}}y|j| _W n tk
rª   t|ƒ| _Y nX |  |||Ž W d Q R X W d d } X d S )Nz%s() arg 1 must be %sr   zcallable is Noneé   r   rD   z3%r is an invalid keyword argument for this function)r:   rA   Ú
_base_typeÚ	TypeErrorÚ_base_type_strÚwarningsÚwarnÚDeprecationWarningÚpoprD   ÚnextÚiterr
   rL   ÚAttributeErrorr   )r   Únamer)   r*   Zcallable_objr   r   r   Úhandle•   s.    z_AssertRaisesBaseContext.handle)N)r
   r   r   r   rY   r   r   r   r   rH   ‰   s   

rH   c               @   s$   e Zd ZeZdZdd„ Zdd„ ZdS )Ú_AssertRaisesContextz-an exception type or tuple of exception typesc             C   s   | S )Nr   )r   r   r   r   Ú	__enter__¾   s    z_AssertRaisesContext.__enter__c             C   sÆ   |d krby| j j}W n tk
r2   t| j ƒ}Y nX | jrP|  d || j¡¡ ql|  d |¡¡ n
t |¡ t	|| j ƒs|dS | 
d ¡| _| jd kr–dS | j}| t|ƒ¡sÂ|  d |jt|ƒ¡¡ dS )Nz{} not raised by {}z{} not raisedFTz"{}" does not match "{}")rA   r
   rW   r   rL   rG   ÚformatÚ	tracebackÚclear_framesr@   Úwith_tracebackZ	exceptionrK   ÚsearchÚpattern)r   Úexc_typeÚ	exc_valueÚtbÚexc_namerK   r   r   r   Ú__exit__Á   s(    


z_AssertRaisesContext.__exit__N)r
   r   r   ÚBaseExceptionrN   rP   r[   rf   r   r   r   r   rZ   ¸   s   rZ   c               @   s$   e Zd ZeZdZdd„ Zdd„ ZdS )Ú_AssertWarnsContextz(a warning type or tuple of warning typesc             C   sR   x$t j ¡ D ]}t|dd ƒri |_qW tjdd| _| j ¡ | _t 	d| j
¡ | S )NÚ__warningregistry__T)ÚrecordÚalways)r   ÚmodulesÚvaluesÚgetattrri   rQ   Úcatch_warningsÚwarnings_managerr[   ÚsimplefilterrA   )r   Úvr   r   r   r[   ã   s    
z_AssertWarnsContext.__enter__c             C   s  | j  |||¡ |d k	rd S y| jj}W n tk
rF   t| jƒ}Y nX d }xb| jD ]X}|j}t|| jƒslqT|d krx|}| j	d k	r”| j	 
t|ƒ¡s”qT|| _|j| _|j| _d S W |d k	rÒ|  d | j	jt|ƒ¡¡ | jrî|  d || j¡¡ n|  d |¡¡ d S )Nz"{}" does not match "{}"z{} not triggered by {}z{} not triggered)rp   rf   rA   r
   rW   r   rQ   Úmessager-   rK   r`   ZwarningÚfilenameÚlinenorG   r\   ra   rL   )r   rb   rc   rd   re   Zfirst_matchingÚmÚwr   r   r   rf   î   s8    

z_AssertWarnsContext.__exit__N)r
   r   r   ÚWarningrN   rP   r[   rf   r   r   r   r   rh   Ý   s   rh   Ú_LoggingWatcherÚrecordsÚoutputc               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )Ú_CapturingHandlerc             C   s   t j | ¡ tg g ƒ| _d S )N)ÚloggingÚHandlerr   ry   Úwatcher)r   r   r   r   r     s    z_CapturingHandler.__init__c             C   s   d S )Nr   )r   r   r   r   Úflush  s    z_CapturingHandler.flushc             C   s*   | j j |¡ |  |¡}| j j |¡ d S )N)r   rz   r   r\   r{   )r   rj   rD   r   r   r   Úemit"  s    
z_CapturingHandler.emitN)r
   r   r   r   r€   r   r   r   r   r   r|     s   r|   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú_AssertLogsContextz"%(levelname)s:%(name)s:%(message)sc             C   s:   t  | |¡ || _|r(tj ||¡| _ntj| _d | _d S )N)	rB   r   Úlogger_namer}   Z_nameToLevelÚgetÚlevelÚINFOrD   )r   r!   rƒ   r…   r   r   r   r   .  s    z_AssertLogsContext.__init__c             C   s’   t | jtjƒr| j }| _nt | j¡ }| _t | j¡}tƒ }| 	|¡ |j
| _
|jd d … | _|j| _|j| _|g|_| | j¡ d|_|j
S )NF)r-   rƒ   r}   ZLoggerÚloggerZ	getLoggerZ	FormatterÚLOGGING_FORMATr|   ZsetFormatterr   ÚhandlersÚold_handlersr…   Ú	old_levelÚ	propagateÚold_propagateÚsetLevel)r   r‡   Ú	formatterZhandlerr   r   r   r[   7  s    
z_AssertLogsContext.__enter__c             C   s`   | j | j_| j| j_| j | j¡ |d k	r.dS t| jj	ƒdkr\|  
d t | j¡| jj¡¡ d S )NFr   z-no logs of level {} or higher triggered on {})rŠ   r‡   r‰   r   rŒ   rŽ   r‹   Úlenr   rz   rG   r\   r}   ZgetLevelNamer…   rX   )r   rb   rc   rd   r   r   r   rf   H  s    

z_AssertLogsContext.__exit__N)r
   r   r   rˆ   r   r[   rf   r   r   r   r   r‚   )  s   	r‚   c               @   s   e Zd Zdd„ ZdS )Ú_OrderedChainMapc             c   s@   t ƒ }x4| jD ]*}x$|D ]}||kr| |¡ |V  qW qW d S )N)ÚsetÚmapsÚadd)r   ÚseenÚmappingÚkr   r   r   Ú__iter__V  s    

z_OrderedChainMap.__iter__N)r
   r   r   r˜   r   r   r   r   r‘   U  s   r‘   c               @   sØ  e Zd ZeZdZdZdZdZd‡dd„Z	dd	„ Z
d
d„ Zdd„ Zdd„ Zedd„ ƒZe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"d#„ Zd$d%„ Zejefd&d'„ƒZd(d)„ Zd*d+„ Zd,d-„ Z dˆd/d0„Z!d1d2„ Z"d3d4„ Z#d5d6„ Z$d7d8„ Z%d‰d9d:„Z&dŠd;d<„Z'd‹d=d>„Z(d?d@„ Z)dAdB„ Z*dCdD„ Z+dŒdEdF„Z,dGdH„ Z-ddIdJ„Z.dŽdKdL„Z/ddMdN„Z0ddOdP„Z1d‘dQdR„Z2d’dSdT„Z3dUdV„ Z4d“dWdX„Z5d”dYdZ„Z6d•d[d\„Z7d–d]d^„Z8d—d_d`„Z9d˜dadb„Z:d™dcdd„Z;dšdedf„Z<d›dgdh„Z=dœdidj„Z>ddkdl„Z?dždmdn„Z@dŸdodp„ZAd dqdr„ZBd¡dsdt„ZCd¢dudv„ZDd£dwdx„ZEd¤dydz„ZFd¥d{d|„ZGd}d~„ ZHdd€„ ZId¦dd‚„ZJd§dƒd„„ZKd…d†„ ZLeLe/ƒ ZMZNeLe0ƒ ZOZPeLe1ƒ ZQZReLe2ƒ ZSZTeLe(ƒ ZUZVeLe*ƒZWeLe'ƒZXeLeHƒZYeLeJƒZZeLeKƒZ[d.S )¨ÚTestCaseTi€  i   FÚrunTestc             C   s¶   || _ d | _d| _yt| |ƒ}W n. tk
rN   |dkrJtd| j|f ƒ‚Y n
X |j| _g | _d | _	i | _
|  td¡ |  td¡ |  td¡ |  td¡ |  td¡ |  td¡ d S )	NzNo testrš   zno such test method in %s: %sÚassertDictEqualÚassertListEqualÚassertTupleEqualÚassertSetEqualÚassertMultiLineEqual)Ú_testMethodNameÚ_outcomeÚ_testMethodDocrn   rW   Ú
ValueErrorÚ	__class__Ú__doc__Ú	_cleanupsÚ_subtestÚ_type_equality_funcsÚaddTypeEqualityFuncÚdictÚlistr>   r’   Ú	frozensetr   )r   Z
methodNameÚ
testMethodr   r   r   r   Ž  s&    zTestCase.__init__c             C   s   || j |< d S )N)r¨   )r   ZtypeobjÚfunctionr   r   r   r©   ®  s    zTestCase.addTypeEqualityFuncc              O   st   t | ƒdkr| ^}}} n>| s&tdƒ‚n0d|krB| d¡}| ^}} ntdt | ƒd  ƒ‚t| ƒ} |j || |f¡ d S )Né   z>descriptor 'addCleanup' of 'TestCase' object needs an argumentr®   z:addCleanup expected at least 1 positional argument, got %dr   )r   rO   rT   r>   r¦   r   )r)   r*   r   r®   r   r   r   Ú
addCleanup½  s    


zTestCase.addCleanupc             C   s   d S )Nr   )r   r   r   r   ÚsetUpÒ  s    zTestCase.setUpc             C   s   d S )Nr   )r   r   r   r   ÚtearDownÖ  s    zTestCase.tearDownc             C   s   d S )Nr   )Úclsr   r   r   Ú
setUpClassÚ  s    zTestCase.setUpClassc             C   s   d S )Nr   )r³   r   r   r   ÚtearDownClassÞ  s    zTestCase.tearDownClassc             C   s   dS )Nr   r   )r   r   r   r   ÚcountTestCasesâ  s    zTestCase.countTestCasesc             C   s   t  ¡ S )N)r   Z
TestResult)r   r   r   r   ÚdefaultTestResultå  s    zTestCase.defaultTestResultc             C   s    | j }|r| d¡d  ¡ pd S )NÚ
r   )r¢   ÚsplitÚstrip)r   Údocr   r   r   ÚshortDescriptionè  s    zTestCase.shortDescriptionc             C   s   dt | jƒ| jf S )Nz%s.%s)r   r¤   r    )r   r   r   r   Úidó  s    zTestCase.idc             C   s    t | ƒt |ƒk	rtS | j|jkS )N)r.   ÚNotImplementedr    )r   Úotherr   r   r   Ú__eq__ö  s    zTestCase.__eq__c             C   s   t t| ƒ| jfƒS )N)Úhashr.   r    )r   r   r   r   Ú__hash__ü  s    zTestCase.__hash__c             C   s   d| j t| jƒf S )Nz%s (%s))r    r   r¤   )r   r   r   r   Ú__str__ÿ  s    zTestCase.__str__c             C   s   dt | jƒ| jf S )Nz<%s testMethod=%s>)r   r¤   r    )r   r   r   r   Ú__repr__  s    zTestCase.__repr__c             C   s<   t |dd ƒ}|d k	r |||ƒ nt dtd¡ | |¡ d S )NÚaddSkipz4TestResult has no addSkip method, skips not reportedr¯   )rn   rQ   rR   ÚRuntimeWarningÚ
addSuccess)r   r   r!   r+   rÅ   r   r   r   Ú_addSkip  s    zTestCase._addSkipc          	   k   s´   | j d ks| j jsd V  d S | j}|d kr4t|ƒ}n|j |¡}t| ||ƒ| _zX| j j| jdd d V  W d Q R X | j js˜| j j	}|d k	r¤|j
r¤t‚n| j jr¤t‚W d || _X d S )NT)r"   )r¡   r   r§   r‘   ÚparamsÚ	new_childÚ_SubTestr$   r   r   Zfailfastr   r   )r   rD   rÉ   ÚparentZ
params_mapr   r   r   r   ÚsubTest  s$    
zTestCase.subTestc             C   sd   x^|D ]V\}}t |tƒr*| |j||¡ q|d k	rt|d | jƒrP| ||¡ q| ||¡ qW d S )Nr   )r-   rË   r   r!   r@   rE   Ú
addFailureZaddError)r   r   r   Útestr    r   r   r   Ú_feedErrorsToResult.  s    
zTestCase._feedErrorsToResultc             C   sD   y
|j }W n* tk
r4   t dt¡ | | ¡ Y nX || |ƒ d S )Nz@TestResult has no addExpectedFailure method, reporting as passes)ÚaddExpectedFailurerW   rQ   rR   rÆ   rÇ   )r   r   r    rÑ   r   r   r   Ú_addExpectedFailure8  s    
zTestCase._addExpectedFailurec             C   sh   y
|j }W nP tk
rZ   t dt¡ y
td ‚W n$ tk
rT   | | t ¡ ¡ Y nX Y n
X || ƒ d S )NzCTestResult has no addUnexpectedSuccess method, reporting as failure)	ÚaddUnexpectedSuccessrW   rQ   rR   rÆ   r   rÎ   r   r    )r   r   rÓ   r   r   r   Ú_addUnexpectedSuccessB  s    

zTestCase._addUnexpectedSuccessNc          
   C   s  |}|d kr.|   ¡ }t|dd ƒ}|d k	r.|ƒ  | | ¡ t| | jƒ}t| jddƒs^t|ddƒrœz,t| jddƒpxt|ddƒ}|  || |¡ W d | | ¡ X d S t|ddƒ}t| ddƒ}|pº|}t|ƒ}	zð|	| _|	 	| ¡ |  
¡  W d Q R X |	jr<||	_|	j	| dd |ƒ  W d Q R X d|	_|	 	| ¡ |  ¡  W d Q R X |  ¡  x"|	jD ]\}
}|  ||
|¡ qLW |  ||	j¡ |	jr²|r¨|	jrœ|  ||	j¡ n
|  |¡ n
| | ¡ |S | | ¡ |d kræt|d	d ƒ}|d k	ræ|ƒ  |	j ¡  d |	_d | _X d S )
NÚstartTestRunr1   Fr2   Ú r9   T)r"   ÚstopTestRun)r·   rn   Z	startTestr    r¤   rÈ   ZstopTestr   r¡   r$   r±   r   r   r²   Ú
doCleanupsr   rÐ   r   r   rÒ   rÔ   rÇ   Úclear)r   r   Zorig_resultrÕ   r­   Zskip_whyZexpecting_failure_methodZexpecting_failure_classr   ÚoutcomerÏ   r+   r×   r   r   r   ÚrunQ  sh    





zTestCase.runc          	   C   sN   | j p
tƒ }x:| jrF| j ¡ \}}}| | ¡ |||Ž W d Q R X qW |jS )N)r¡   r   r¦   rT   r$   r   )r   rÚ   r®   r)   r*   r   r   r   rØ   –  s    zTestCase.doCleanupsc             O   s   | j ||ŽS )N)rÛ   )r   r)   Úkwdsr   r   r   Ú__call__£  s    zTestCase.__call__c             C   sJ   |   ¡  t| | jƒƒ  |  ¡  x&| jrD| j d¡\}}}|||Ž q W d S )Néÿÿÿÿ)r±   rn   r    r²   r¦   rT   )r   r®   r)   r*   r   r   r   Údebug¦  s    zTestCase.debugc             C   s   t |ƒ‚d S )N)r	   )r   r+   r   r   r   ÚskipTest¯  s    zTestCase.skipTestc             C   s   |   |¡‚d S )N)rE   )r   rD   r   r   r   Úfail³  s    zTestCase.failc             C   s&   |r"|   |dt|ƒ ¡}|  |¡‚d S )Nz%s is not false)rC   r   rE   )r   ÚexprrD   r   r   r   ÚassertFalse·  s    zTestCase.assertFalsec             C   s&   |s"|   |dt|ƒ ¡}|  |¡‚d S )Nz%s is not true)rC   r   rE   )r   râ   rD   r   r   r   Ú
assertTrue½  s    zTestCase.assertTruec             C   sP   | j s|p|S |d kr|S yd||f S  tk
rJ   dt|ƒt|ƒf S X d S )Nz%s : %s)ÚlongMessageÚUnicodeDecodeErrorr   )r   rD   rF   r   r   r   rC   Ã  s    
zTestCase._formatMessagec             O   s$   t || ƒ}z| d||¡S d }X d S )NÚassertRaises)rZ   rY   )r   Úexpected_exceptionr)   r*   Úcontextr   r   r   rç   Ø  s    
zTestCase.assertRaisesc             O   s   t || ƒ}| d||¡S )NÚassertWarns)rh   rY   )r   Úexpected_warningr)   r*   ré   r   r   r   rê   ù  s    
zTestCase.assertWarnsc             C   s   t | ||ƒS )N)r‚   )r   r‡   r…   r   r   r   Ú
assertLogs  s    zTestCase.assertLogsc             C   sF   t |ƒt |ƒkr@| j t |ƒ¡}|d k	r@t|tƒr<t| |ƒ}|S | jS )N)r.   r¨   r„   r-   r   rn   Ú_baseAssertEqual)r   ÚfirstÚsecondZasserterr   r   r   Ú_getAssertEqualityFunc.  s    

zTestCase._getAssertEqualityFuncc             C   s0   ||ks,dt ||ƒ }|  ||¡}|  |¡‚d S )Nz%s != %s)r   rC   rE   )r   rî   rï   rD   rF   r   r   r   rí   H  s    zTestCase._baseAssertEqualc             C   s   |   ||¡}||||d d S )N)rD   )rð   )r   rî   rï   rD   Zassertion_funcr   r   r   ÚassertEqualO  s    zTestCase.assertEqualc             C   s2   ||ks.|   |dt|ƒt|ƒf ¡}|  |¡‚d S )Nz%s == %s)rC   r   rE   )r   rî   rï   rD   r   r   r   ÚassertNotEqualV  s    zTestCase.assertNotEqualc             C   sº   ||krd S |d k	r$|d k	r$t dƒ‚t|| ƒ}|d k	rf||krDd S dt|ƒt|ƒt|ƒt|ƒf }n:|d krrd}t||ƒdkr„d S dt|ƒt|ƒ|t|ƒf }|  ||¡}|  |¡‚d S )Nz specify delta or places not bothz(%s != %s within %s delta (%s difference)é   r   z)%s != %s within %r places (%s difference))rO   Úabsr   ÚroundrC   rE   )r   rî   rï   ÚplacesrD   ÚdeltaÚdiffrF   r   r   r   ÚassertAlmostEqual_  s0    zTestCase.assertAlmostEqualc             C   s¸   |d k	r|d k	rt dƒ‚t|| ƒ}|d k	rb||ks@||kr@d S dt|ƒt|ƒt|ƒt|ƒf }n<|d krnd}||ksˆt||ƒdkrˆd S dt|ƒt|ƒ|f }|  ||¡}|  |¡‚d S )Nz specify delta or places not bothz(%s == %s within %s delta (%s difference)ró   r   z%s == %s within %r places)rO   rô   r   rõ   rC   rE   )r   rî   rï   rö   rD   r÷   rø   rF   r   r   r   ÚassertNotAlmostEqualŒ  s(    zTestCase.assertNotAlmostEqualc             C   sì  |d k	rP|j }t||ƒs.|  d|t|ƒf ¡‚t||ƒsT|  d|t|ƒf ¡‚nd}d }yt|ƒ}W n  ttfk
r„   d| }Y nX |d kr¼yt|ƒ}W n  ttfk
rº   d| }Y nX |d kr˜||krÒd S d| ¡ ft||ƒ  }xÚt	t
||ƒƒD ]ž}	y||	 }
W n. tttfk
r8   |d|	|f 7 }P Y nX y||	 }W n. tttfk
rt   |d|	|f 7 }P Y nX |
|krú|d	|	ft|
|ƒ  7 }P qúW ||krÆ|d krÆt|ƒt|ƒkrÆd S ||kr0|d
||| f 7 }y|d|t|| ƒf 7 }W n, tttfk
r,   |d||f 7 }Y nX nh||k r˜|d||| f 7 }y|d|t|| ƒf 7 }W n, tttfk
r–   |d||f 7 }Y nX |}dd t t |¡ ¡ t |¡ ¡ ¡¡ }|  ||¡}|  ||¡}|  |¡ d S )NzFirst sequence is not a %s: %szSecond sequence is not a %s: %sZsequencez(First %s has no length.    Non-sequence?z)Second %s has no length.    Non-sequence?z%ss differ: %s != %s
z(
Unable to index element %d of first %s
z)
Unable to index element %d of second %s
z#
First differing element %d:
%s
%s
z+
First %s contains %d additional elements.
zFirst extra element %d:
%s
z'Unable to index element %d of first %s
z,
Second %s contains %d additional elements.
z(Unable to index element %d of second %s
r¸   )r
   r-   rE   r   r   rO   ÚNotImplementedErrorÚ
capitalizer   ÚrangeÚminÚ
IndexErrorr.   ÚjoinÚdifflibÚndiffÚpprintÚpformatÚ
splitlinesÚ_truncateMessagerC   rá   )r   Zseq1Zseq2rD   Úseq_typeZseq_type_nameZ	differingZlen1Zlen2ÚiZitem1Zitem2rF   ÚdiffMsgr   r   r   ÚassertSequenceEqual¯  s    




zTestCase.assertSequenceEqualc             C   s2   | j }|d kst|ƒ|kr"|| S |tt|ƒ  S )N)ÚmaxDiffr   ÚDIFF_OMITTED)r   rs   rø   Zmax_diffr   r   r   r    s    zTestCase._truncateMessagec             C   s   | j |||td d S )N)r  )r
  r«   )r   Zlist1Zlist2rD   r   r   r   rœ     s    
zTestCase.assertListEqualc             C   s   | j |||td d S )N)r  )r
  r>   )r   Ztuple1Ztuple2rD   r   r   r   r   $  s    	zTestCase.assertTupleEqualc       
   
   C   sh  y|  |¡}W n^ tk
r> } z|  d| ¡ W d d }~X Y n0 tk
rl } z|  d| ¡ W d d }~X Y nX y|  |¡}W n^ tk
r¬ } z|  d| ¡ W d d }~X Y n0 tk
rÚ } z|  d| ¡ W d d }~X Y nX |sè|sèd S g }|r| d¡ x|D ]}| t|ƒ¡ qW |rH| d¡ x|D ]}| t|ƒ¡ q0W d |¡}	|  |  ||	¡¡ d S )Nz/invalid type when attempting set difference: %sz2first argument does not support set difference: %sz3second argument does not support set difference: %sz*Items in the first set but not the second:z*Items in the second set but not the first:r¸   )Ú
differencerO   rá   rW   r   Úreprr   rC   )
r   Zset1Zset2rD   Zdifference1r#   Zdifference2ÚlinesÚitemrF   r   r   r   rž   /  s2      




zTestCase.assertSetEqualc             C   s2   ||kr.dt |ƒt |ƒf }|  |  ||¡¡ d S )Nz%s not found in %s)r   rá   rC   )r   ÚmemberÚ	containerrD   rF   r   r   r   ÚassertInZ  s    zTestCase.assertInc             C   s2   ||kr.dt |ƒt |ƒf }|  |  ||¡¡ d S )Nz%s unexpectedly found in %s)r   rá   rC   )r   r  r  rD   rF   r   r   r   ÚassertNotIna  s    zTestCase.assertNotInc             C   s2   ||k	r.dt |ƒt |ƒf }|  |  ||¡¡ d S )Nz%s is not %s)r   rá   rC   )r   Úexpr1Úexpr2rD   rF   r   r   r   ÚassertIsh  s    zTestCase.assertIsc             C   s,   ||kr(dt |ƒf }|  |  ||¡¡ d S )Nzunexpectedly identical: %s)r   rá   rC   )r   r  r  rD   rF   r   r   r   ÚassertIsNoto  s    zTestCase.assertIsNotc          	   C   s~   |   |td¡ |   |td¡ ||krzdt||ƒ }dd t t |¡ ¡ t |¡ ¡ ¡¡ }|  	||¡}|  
|  ||¡¡ d S )Nz"First argument is not a dictionaryz#Second argument is not a dictionaryz%s != %sr¸   )ÚassertIsInstancerª   r   r   r  r  r  r  r  r  rá   rC   )r   Zd1Zd2rD   rF   rø   r   r   r   r›   u  s    
zTestCase.assertDictEqualc       	   	   C   sÒ   t  dt¡ g }g }xX| ¡ D ]L\}}||kr:| |¡ q||| kr| dt|ƒt|ƒt|| ƒf ¡ qW |sz|szd S d}|ršdd dd„ |D ƒ¡ }|r¼|rª|d7 }|d	d |¡ 7 }|  |  ||¡¡ d S )
Nz&assertDictContainsSubset is deprecatedz%s, expected: %s, actual: %srÖ   zMissing: %sú,c             s   s   | ]}t |ƒV  qd S )N)r   )r;   rv   r   r   r   r=   ”  s    z4TestCase.assertDictContainsSubset.<locals>.<genexpr>z; zMismatched values: %s)	rQ   rR   rS   Úitemsr   r   r   rá   rC   )	r   ZsubsetZ
dictionaryrD   ÚmissingZ
mismatchedÚkeyÚvaluerF   r   r   r   ÚassertDictContainsSubset  s,    z!TestCase.assertDictContainsSubsetc       
      C   s¦   t |ƒt |ƒ }}yt |¡}t |¡}W n tk
rH   t||ƒ}Y nX ||krVd S t||ƒ}|r¢d}dd„ |D ƒ}d |¡}	|  ||	¡}|  ||¡}|  	|¡ d S )NzElement counts were not equal:
c             S   s   g | ]}d | ‘qS )z First has %d, Second has %d:  %rr   )r;   rø   r   r   r   ú
<listcomp>¹  s    z-TestCase.assertCountEqual.<locals>.<listcomp>r¸   )
r«   ÚcollectionsÚCounterrO   r   r   r   r  rC   rá   )
r   rî   rï   rD   Z	first_seqZ
second_seqZdifferencesrF   r  r	  r   r   r   ÚassertCountEqualž  s     


zTestCase.assertCountEqualc             C   sÚ   |   |td¡ |   |td¡ ||krÖt|ƒ| jks@t|ƒ| jkrN|  |||¡ |jdd}|jdd}t|ƒdkr”| d¡|kr”|d g}|d g}dt||ƒ }dd	 t	 
||¡¡ }|  ||¡}|  |  ||¡¡ d S )
NzFirst argument is not a stringzSecond argument is not a stringT)Úkeependsr   z
r¸   z%s != %srÖ   )r  r   r   Ú_diffThresholdrí   r  rº   r   r   r  r  r  rá   rC   )r   rî   rï   rD   Z
firstlinesZsecondlinesrF   rø   r   r   r   rŸ   ¿  s    

zTestCase.assertMultiLineEqualc             C   s2   ||k s.dt |ƒt |ƒf }|  |  ||¡¡ d S )Nz%s not less than %s)r   rá   rC   )r   ÚaÚbrD   rF   r   r   r   Ú
assertLessÓ  s    zTestCase.assertLessc             C   s2   ||ks.dt |ƒt |ƒf }|  |  ||¡¡ d S )Nz%s not less than or equal to %s)r   rá   rC   )r   r&  r'  rD   rF   r   r   r   ÚassertLessEqualÙ  s    zTestCase.assertLessEqualc             C   s2   ||ks.dt |ƒt |ƒf }|  |  ||¡¡ d S )Nz%s not greater than %s)r   rá   rC   )r   r&  r'  rD   rF   r   r   r   ÚassertGreaterß  s    zTestCase.assertGreaterc             C   s2   ||ks.dt |ƒt |ƒf }|  |  ||¡¡ d S )Nz"%s not greater than or equal to %s)r   rá   rC   )r   r&  r'  rD   rF   r   r   r   ÚassertGreaterEqualå  s    zTestCase.assertGreaterEqualc             C   s,   |d k	r(dt |ƒf }|  |  ||¡¡ d S )Nz%s is not None)r   rá   rC   )r   r'   rD   rF   r   r   r   ÚassertIsNoneë  s    zTestCase.assertIsNonec             C   s"   |d krd}|   |  ||¡¡ d S )Nzunexpectedly None)rá   rC   )r   r'   rD   rF   r   r   r   ÚassertIsNotNoneñ  s    zTestCase.assertIsNotNonec             C   s0   t ||ƒs,dt|ƒ|f }|  |  ||¡¡ d S )Nz%s is not an instance of %r)r-   r   rá   rC   )r   r'   r³   rD   rF   r   r   r   r  ÷  s    
zTestCase.assertIsInstancec             C   s0   t ||ƒr,dt|ƒ|f }|  |  ||¡¡ d S )Nz%s is an instance of %r)r-   r   rá   rC   )r   r'   r³   rD   rF   r   r   r   ÚassertNotIsInstanceþ  s    
zTestCase.assertNotIsInstancec             O   s   t || |ƒ}| d||¡S )NÚassertRaisesRegex)rZ   rY   )r   rè   rK   r)   r*   ré   r   r   r   r/    s    zTestCase.assertRaisesRegexc             O   s   t || |ƒ}| d||¡S )NÚassertWarnsRegex)rh   rY   )r   rë   rK   r)   r*   ré   r   r   r   r0    s    zTestCase.assertWarnsRegexc             C   sJ   t |ttfƒrt |¡}| |¡sFd|j|f }|  ||¡}|  |¡‚d S )Nz&Regex didn't match: %r not found in %r)	r-   r   ÚbytesrI   rJ   r`   ra   rC   rE   )r   ÚtextrK   rD   rF   r   r   r   ÚassertRegex'  s    

zTestCase.assertRegexc             C   s`   t |ttfƒrt |¡}| |¡}|r\d|| ¡ | ¡ … |j|f }|  	||¡}|  
|¡‚d S )Nz"Regex matched: %r matches %r in %r)r-   r   r1  rI   rJ   r`   ÚstartÚendra   rC   rE   )r   r2  Zunexpected_regexrD   ÚmatchrF   r   r   r   ÚassertNotRegex3  s    

zTestCase.assertNotRegexc                s   ‡ fdd„}|S )Nc                 s    t  d ˆ j¡td¡ ˆ | |ŽS )NzPlease use {0} instead.r¯   )rQ   rR   r\   r
   rS   )r)   r*   )Úoriginal_funcr   r   Údeprecated_funcC  s    
z,TestCase._deprecate.<locals>.deprecated_funcr   )r8  r9  r   )r8  r   Ú
_deprecateB  s    zTestCase._deprecate)rš   )N)N)N)N)NN)N)N)N)NNN)NNN)NN)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)\r
   r   r   ÚAssertionErrorrE   rå   r  r%  Z_classSetupFailedr   r©   r°   r±   r²   Úclassmethodr´   rµ   r¶   r·   r¼   r½   rÀ   rÂ   rÃ   rÄ   rÈ   r%   r&   Ú_subtest_msg_sentinelrÍ   rÐ   rÒ   rÔ   rÛ   rØ   rÝ   rß   rà   rá   rã   rä   rC   rç   rê   rì   rð   rí   rñ   rò   rù   rú   r
  r  rœ   r   rž   r  r  r  r  r›   r  r#  rŸ   r(  r)  r*  r+  r,  r-  r  r.  r/  r0  r3  r7  r:  ZfailUnlessEqualZassertEqualsZfailIfEqualZassertNotEqualsZfailUnlessAlmostEqualZassertAlmostEqualsZfailIfAlmostEqualZassertNotAlmostEqualsZ
failUnlessZassert_ZfailUnlessRaisesZfailIfZassertRaisesRegexpZassertRegexpMatchesZassertNotRegexpMatchesr   r   r   r   r™   _  s¤   !
 	


E	


!



	 
, 
"
c


+






!










	r™   c                   sf   e Zd Zd‡ f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d„ Zdd„ Z‡  ZS )ÚFunctionTestCaseNc                s*   t t| ƒ ¡  || _|| _|| _|| _d S )N)Úsuperr>  r   Ú
_setUpFuncÚ_tearDownFuncÚ	_testFuncÚ_description)r   ZtestFuncr±   r²   Údescription)r¤   r   r   r   a  s
    zFunctionTestCase.__init__c             C   s   | j d k	r|   ¡  d S )N)r@  )r   r   r   r   r±   h  s    
zFunctionTestCase.setUpc             C   s   | j d k	r|   ¡  d S )N)rA  )r   r   r   r   r²   l  s    
zFunctionTestCase.tearDownc             C   s   |   ¡  d S )N)rB  )r   r   r   r   rš   p  s    zFunctionTestCase.runTestc             C   s   | j jS )N)rB  r
   )r   r   r   r   r½   s  s    zFunctionTestCase.idc             C   s@   t || jƒstS | j|jko>| j|jko>| j|jko>| j|jkS )N)r-   r¤   r¾   r@  rA  rB  rC  )r   r¿   r   r   r   rÀ   v  s    zFunctionTestCase.__eq__c             C   s   t t| ƒ| j| j| j| jfƒS )N)rÁ   r.   r@  rA  rB  rC  )r   r   r   r   rÂ     s    zFunctionTestCase.__hash__c             C   s   dt | jƒ| jjf S )Nz%s (%s))r   r¤   rB  r
   )r   r   r   r   rÃ   ƒ  s    
zFunctionTestCase.__str__c             C   s   dt | jƒ| jf S )Nz<%s tec=%s>)r   r¤   rB  )r   r   r   r   rÄ   ‡  s    
zFunctionTestCase.__repr__c             C   s2   | j d k	r| j S | jj}|r.| d¡d  ¡ p0d S )Nr¸   r   )rC  rB  r¥   r¹   rº   )r   r»   r   r   r   r¼   ‹  s    
z!FunctionTestCase.shortDescription)NNN)r
   r   r   r   r±   r²   rš   r½   rÀ   rÂ   rÃ   rÄ   r¼   Ú__classcell__r   r   )r¤   r   r>  X  s   		r>  c                   sD   e Zd Z‡ fdd„Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z‡  Z	S )rË   c                s(   t ƒ  ¡  || _|| _|| _|j| _d S )N)r?  r   Ú_messager!   rÉ   rE   )r   r!   rs   rÉ   )r¤   r   r   r   ”  s
    
z_SubTest.__init__c             C   s   t dƒ‚d S )Nzsubtests cannot be run directly)rû   )r   r   r   r   rš   ›  s    z_SubTest.runTestc             C   s^   g }| j tk	r | d | j ¡¡ | jrPd dd„ | j ¡ D ƒ¡}| d |¡¡ d |¡p\dS )Nz[{}]z, c             s   s   | ]\}}d   ||¡V  qdS )z{}={!r}N)r\   )r;   r—   rr   r   r   r   r=   ¤  s   z+_SubTest._subDescription.<locals>.<genexpr>z({})ú z(<subtest>))rF  r=  r   r\   rÉ   r   r  )r   ÚpartsZparams_descr   r   r   Ú_subDescriptionž  s    
z_SubTest._subDescriptionc             C   s   d  | j ¡ |  ¡ ¡S )Nz{} {})r\   r!   r½   rI  )r   r   r   r   r½   ©  s    z_SubTest.idc             C   s
   | j  ¡ S )N)r!   r¼   )r   r   r   r   r¼   ¬  s    z_SubTest.shortDescriptionc             C   s   d  | j|  ¡ ¡S )Nz{} {})r\   r!   rI  )r   r   r   r   rÃ   ²  s    z_SubTest.__str__)
r
   r   r   r   rš   rI  r½   r¼   rÃ   rE  r   r   )r¤   r   rË   ’  s   rË   )/r   r/   r  r}   r  rI   rQ   r!  r%   r]   rÖ   r   Úutilr   r   r   r   r   Z
__unittestÚobjectr=  r  Ú	Exceptionr	   r   r   r   r(   r5   r7   r8   r   r:   rB   rH   rZ   rh   Ú
namedtuplery   r~   r|   r‚   ÚChainMapr‘   r™   r>  rË   r   r   r   r   Ú<module>   sX   (	/%5
,
        :