o
    éT•jF!  ã                   @   s\   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Zdd„ Z	d	Z
d
ZG dd„ deƒZdS )zTest result objecté    Né   )Úutil©ÚwrapsTc                    s   t ˆ ƒ‡ fdd„ƒ}|S )Nc                    s*   t | ddƒr
|  ¡  ˆ | g|¢R i |¤ŽS )NÚfailfastF)ÚgetattrÚstop)ÚselfÚargsÚkw©Úmethod© ú&/usr/lib/python3.10/unittest/result.pyÚinner   s   zfailfast.<locals>.innerr   )r   r   r   r   r   r      s   r   z
Stdout:
%sz
Stderr:
%sc                   @   sÚ   e Zd ZdZdZdZdZd0dd„Zdd„ Zdd	„ Z	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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S )1Ú
TestResultaÙ  Holder for test result information.

    Test results are automatically managed by the TestCase and TestSuite
    classes, and do not need to be explicitly manipulated by writers of tests.

    Each instance holds the total number of tests run, and collections of
    failures and errors that occurred among those test runs. The collections
    contain tuples of (testcase, exceptioninfo), where exceptioninfo is the
    formatted traceback of the error that occurred.
    NFc                 C   sb   d| _ g | _g | _d| _g | _g | _g | _d| _d| _d| _	d | _
d | _tj| _tj| _d| _d S )NFr   )r   ÚfailuresÚerrorsÚtestsRunÚskippedÚexpectedFailuresÚunexpectedSuccessesÚ
shouldStopÚbufferÚ	tb_localsÚ_stdout_bufferÚ_stderr_bufferÚsysÚstdoutÚ_original_stdoutÚstderrÚ_original_stderrÚ_mirrorOutput)r	   ÚstreamÚdescriptionsÚ	verbosityr   r   r   Ú__init__&   s   
zTestResult.__init__c                 C   ó   dS )z#Called by TestRunner after test runNr   ©r	   r   r   r   ÚprintErrors7   ó    zTestResult.printErrorsc                 C   s    |  j d7  _ d| _|  ¡  dS )z-Called when the given test is about to be runr   FN)r   r"   Ú_setupStdout©r	   Útestr   r   r   Ú	startTest:   s   zTestResult.startTestc                 C   s<   | j r| jd u rt ¡ | _t ¡ | _| jt_| jt_d S d S )N)r   r   ÚioÚStringIOr   r   r   r    r(   r   r   r   r+   @   s   


ûzTestResult._setupStdoutc                 C   r'   )zpCalled once before any tests are executed.

        See startTest for a method called before each test.
        Nr   r(   r   r   r   ÚstartTestRunH   r*   zTestResult.startTestRunc                 C   s   |   ¡  d| _dS )z'Called when the given test has been runFN)Ú_restoreStdoutr"   r,   r   r   r   ÚstopTestN   s   
zTestResult.stopTestc                 C   s°   | j rV| jr6tj ¡ }tj ¡ }|r#| d¡s|d7 }| j t	| ¡ |r6| d¡s.|d7 }| j
 t| ¡ | jt_| j
t_| j d¡ | j ¡  | j d¡ | j ¡  d S d S )NÚ
r   )r   r"   r   r   Úgetvaluer    Úendswithr   ÚwriteÚSTDOUT_LINEr!   ÚSTDERR_LINEr   ÚseekÚtruncater   )r	   ÚoutputÚerrorr   r   r   r2   S   s&   




îzTestResult._restoreStdoutc                 C   r'   )zmCalled once after all tests are executed.

        See stopTest for a method called after each test.
        Nr   r(   r   r   r   ÚstopTestRunh   r*   zTestResult.stopTestRunc                 C   ó"   | j  ||  ||¡f¡ d| _dS )zmCalled when an error has occurred. 'err' is a tuple of values as
        returned by sys.exc_info().
        TN)r   ÚappendÚ_exc_info_to_stringr"   ©r	   r-   Úerrr   r   r   ÚaddErrorn   s   
zTestResult.addErrorc                 C   r?   )zdCalled when an error has occurred. 'err' is a tuple of values as
        returned by sys.exc_info().TN)r   r@   rA   r"   rB   r   r   r   Ú
addFailurev   s   
zTestResult.addFailurec                 C   s^   |dur-t | ddƒr|  ¡  t|d |jƒr| j}n| j}| ||  ||¡f¡ d| _dS dS )z©Called at the end of a subtest.
        'err' is None if the subtest ended successfully, otherwise it's a
        tuple of values as returned by sys.exc_info().
        Nr   Fr   T)	r   r   Ú
issubclassÚfailureExceptionr   r   r@   rA   r"   )r	   r-   ÚsubtestrC   r   r   r   r   Ú
addSubTest}   s   
øzTestResult.addSubTestc                 C   r'   )z-Called when a test has completed successfullyNr   r,   r   r   r   Ú
addSuccessŽ   s   zTestResult.addSuccessc                 C   s   | j  ||f¡ dS )zCalled when a test is skipped.N)r   r@   )r	   r-   Úreasonr   r   r   ÚaddSkip’   s   zTestResult.addSkipc                 C   s   | j  ||  ||¡f¡ dS )z/Called when an expected failure/error occurred.N)r   r@   rA   rB   r   r   r   ÚaddExpectedFailure–   s   ÿzTestResult.addExpectedFailurec                 C   s   | j  |¡ dS )z5Called when a test was expected to fail, but succeed.N)r   r@   r,   r   r   r   ÚaddUnexpectedSuccess›   s   zTestResult.addUnexpectedSuccessc                 C   s>   t | jƒt | jƒ  kodkn  ot| dƒ pt | jƒdkS )z/Tells whether or not this result was a success.r   r   )Úlenr   r   Úhasattrr   r(   r   r   r   ÚwasSuccessful    s   $þzTestResult.wasSuccessfulc                 C   s
   d| _ dS )z+Indicates that the tests should be aborted.TN)r   r(   r   r   r   r   ©   s   
zTestResult.stopc           
      C   s¨   |\}}}|   ||||¡}tj|||| jdd�}t| ¡ ƒ}| jrOtj 	¡ }tj
 	¡ }	|r=| d¡s6|d7 }| t| ¡ |	rO|	 d¡sH|	d7 }	| t|	 ¡ d |¡S )z>Converts a sys.exc_info()-style tuple of values into a string.T)Úcapture_localsÚcompactr4   Ú )Ú_clean_tracebacksÚ	tracebackÚTracebackExceptionr   ÚlistÚformatr   r   r   r5   r    r6   r@   r8   r9   Újoin)
r	   rC   r-   ÚexctypeÚvalueÚtbÚtb_eÚmsgLinesr<   r=   r   r   r   rA   ­   s&   
þ




zTestResult._exc_info_to_stringc           
      C   sÔ   d }d}|||fg}t |ƒh}|rh| ¡ \}}}|r)|  |¡r)|j}|r)|  |¡s||ju r3|  |¡ |r:|}d}n||_|d urf|j|jfD ]}	|	d uret |	ƒ|vre| 	t
|	ƒ|	|	jf¡ | t |	ƒ¡ qG|s|S )NTF)ÚidÚpopÚ_is_relevant_tb_levelÚtb_nextrG   Ú_remove_unittest_tb_framesÚ__traceback__Ú	__cause__Ú__context__r@   ÚtypeÚadd)
r	   r[   r\   r]   r-   ÚretÚfirstÚexcsÚseenÚcr   r   r   rU   Ã   s.   
ÿ

€ìzTestResult._clean_tracebacksc                 C   s   d|j jv S )NÚ
__unittest)Útb_frameÚ	f_globals)r	   r]   r   r   r   rb   ß   s   z TestResult._is_relevant_tb_levelc                 C   s@   d}|r|   |¡s|}|j}|r|   |¡r	|durd|_dS dS )a  Truncates usercode tb at the first unittest frame.

        If the first frame of the traceback is in user code,
        the prefix up to the first unittest frame is returned.
        If the first frame is already in the unittest module,
        the traceback is not modified.
        N)rb   rc   )r	   r]   Úprevr   r   r   rd   â   s   þ
ÿz%TestResult._remove_unittest_tb_framesc                 C   s&   dt  | j¡| jt| jƒt| jƒf S )Nz!<%s run=%i errors=%i failures=%i>)r   ÚstrclassÚ	__class__r   rO   r   r   r(   r   r   r   Ú__repr__ñ   s
   ÿÿzTestResult.__repr__)NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú_previousTestClassÚ_testRunEnteredÚ_moduleSetUpFailedr&   r)   r.   r+   r1   r3   r2   r>   r   rD   rE   rI   rJ   rL   rM   rN   rQ   r   rA   rU   rb   rd   ru   r   r   r   r   r      s<    




	r   )ry   r/   r   rV   rT   r   Ú	functoolsr   ro   r   r8   r9   Úobjectr   r   r   r   r   Ú<module>   s    