o
    ¯bÄ  ã                   @   s€   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 d dlmZmZ G dd„ deƒZG d	d
„ d
eƒZdS )é    N)ÚskipIf)Úimplementer)ÚFailure)Ú	IReporterÚ	ITestCase)ÚPyUnitResultAdapterÚSynchronousTestCasec                   @   s.   e Zd ZG dd„ dejƒZdd„ Zdd„ ZdS )ÚPyUnitTestTestsc                   @   s   e Zd Zdd„ ZdS )zPyUnitTestTests.PyUnitTestc                 C   s   d S ©N© ©Úselfr   r   úF/usr/lib/python3/dist-packages/twisted/trial/test/test_pyunitcompat.pyÚ	test_pass   s   z$PyUnitTestTests.PyUnitTest.test_passN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   Ú
PyUnitTest   s    r   c                 C   s   |   d¡| _t| jƒ| _d S )Nr   )r   Úoriginalr   Útestr   r   r   r   ÚsetUp   s   zPyUnitTestTests.setUpc                 C   s   |   t| jƒ| j›d�¡ dS )zW
        Tests must be callable in order to be used with Python's unittest.py.
        z is not callable.N)Ú
assertTrueÚcallabler   r   r   r   r   Útest_callable   s   zPyUnitTestTests.test_callableN)r   r   r   ÚpyunitÚTestCaser   r   r   r   r   r   r   r	      s    r	   c                   @   sp   e Zd Zd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dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚPyUnitResultTestsz¹
    Tests to show that PyUnitResultAdapter wraps TestResult objects from the
    standard library 'unittest' module in such a way as to make them usable and
    useful from Trial.
    c                   @   s   e Zd ZdZdZdd„ ZdS )zPyUnitResultTests.ErrorTestz™
        A test case which has a L{test_foo} which will raise an error.

        @ivar ran: boolean indicating whether L{test_foo} has been run.
        Fc                 C   s   d| _ dd  dS )zR
            Set C{self.ran} to True and raise a C{ZeroDivisionError}
            Té   r   N©Úranr   r   r   r   Útest_foo3   s   z$PyUnitResultTests.ErrorTest.test_fooN)r   r   r   Ú__doc__r   r    r   r   r   r   Ú	ErrorTest*   s    r"   c                 C   sF   t tƒG dd„ dƒƒ}|  d¡}|ƒ }| |¡ |  |jd t¡ dS )zƒ
        The L{PyUnitResultAdapter} is only used when the result passed to
        C{run} does *not* provide L{IReporter}.
        c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )zXPyUnitResultTests.test_dontUseAdapterWhenReporterProvidesIReporter.<locals>.StubReporterzÆ
            A reporter which records data about calls made to it.

            @ivar errors: Errors passed to L{addError}.
            @ivar failures: Failures passed to L{addFailure}.
            c                 S   s   g | _ g | _d S r
   )ÚerrorsÚfailuresr   r   r   r   Ú__init__I   s   
zaPyUnitResultTests.test_dontUseAdapterWhenReporterProvidesIReporter.<locals>.StubReporter.__init__c                 S   ó   dS ©z-
                Do nothing.
                Nr   ©r   r   r   r   r   Ú	startTestM   ó    zbPyUnitResultTests.test_dontUseAdapterWhenReporterProvidesIReporter.<locals>.StubReporter.startTestc                 S   r&   r'   r   r(   r   r   r   ÚstopTestR   r*   zaPyUnitResultTests.test_dontUseAdapterWhenReporterProvidesIReporter.<locals>.StubReporter.stopTestc                 S   s   | j  |¡ dS )z3
                Record the error.
                N)r#   Úappend)r   r   Úerrorr   r   r   ÚaddErrorW   s   zaPyUnitResultTests.test_dontUseAdapterWhenReporterProvidesIReporter.<locals>.StubReporter.addErrorN)r   r   r   r!   r%   r)   r+   r.   r   r   r   r   ÚStubReporter@   s    r/   r    r   N)r   r   r"   ÚrunÚassertIsInstancer#   r   )r   r/   r   Úresultr   r   r   Ú0test_dontUseAdapterWhenReporterProvidesIReporter:   s   

zBPyUnitResultTests.test_dontUseAdapterWhenReporterProvidesIReporterc                 C   sV   G dd„ dt ƒ}|dƒ}t ¡ }| |¡ |  |j¡ |  d|j¡ |  | ¡ ¡ d S )Nc                   @   ó   e Zd ZdZdd„ ZdS )z3PyUnitResultTests.test_success.<locals>.SuccessTestFc                 S   ó
   d| _ d S ©NTr   ©Úsr   r   r   r    f   ó   
z<PyUnitResultTests.test_success.<locals>.SuccessTest.test_fooN©r   r   r   r   r    r   r   r   r   ÚSuccessTestc   ó    r;   r    r   )	r   r   Ú
TestResultr0   r   r   ÚassertEqualÚtestsRunÚwasSuccessful)r   r;   r   r2   r   r   r   Útest_successb   s   
zPyUnitResultTests.test_successc                 C   sh   G dd„ dt ƒ}|dƒ}t ¡ }| |¡ |  |j¡ |  d|j¡ |  dt|j	ƒ¡ |  
| ¡ ¡ d S )Nc                   @   r4   )z3PyUnitResultTests.test_failure.<locals>.FailureTestFc                 S   s   d| _ |  d¡ d S )NTzboom!)r   Úfailr7   r   r   r   r    u   s   z<PyUnitResultTests.test_failure.<locals>.FailureTest.test_fooNr:   r   r   r   r   ÚFailureTestr   r<   rC   r    r   )r   r   r=   r0   r   r   r>   r?   Úlenr$   ÚassertFalser@   )r   rC   r   r2   r   r   r   Útest_failureq   s   
zPyUnitResultTests.test_failurec                 C   sZ   |   d¡}t ¡ }| |¡ |  |j¡ |  d|j¡ |  dt|j	ƒ¡ |  
| ¡ ¡ d S )Nr    r   )r"   r   r=   r0   r   r   r>   r?   rD   r#   rE   r@   )r   r   r2   r   r   r   Ú
test_error‚   s   

zPyUnitResultTests.test_errorc                 C   sh   G dd„ dt ƒ}|dƒ}t ¡ }| |¡ |  |j¡ |  d|j¡ |  dt|j	ƒ¡ |  | 
¡ ¡ d S )Nc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )z4PyUnitResultTests.test_setUpError.<locals>.ErrorTestFc                 S   ó   dd  d S ©Nr   r   r   r   r   r   r   r   �   s   z:PyUnitResultTests.test_setUpError.<locals>.ErrorTest.setUpc                 S   r5   r6   r   r7   r   r   r   r    “   r9   z=PyUnitResultTests.test_setUpError.<locals>.ErrorTest.test_fooN)r   r   r   r   r   r    r   r   r   r   r"   �   s    r"   r    r   )r   r   r=   r0   rE   r   r>   r?   rD   r#   r@   )r   r"   r   r2   r   r   r   Útest_setUpErrorŒ   s   	
z!PyUnitResultTests.test_setUpErrorc                 C   sp   zdd  W n t y   t ¡ }tƒ }Y nw t ¡ }t|ƒ}| | |¡ |  |j	d d d 
tj|Ž ¡¡ dS )z‘
        Errors added through the L{PyUnitResultAdapter} have the same traceback
        information as if there were no adapter at all.
        r   r   Ú N)ÚZeroDivisionErrorÚsysÚexc_infor   r   r=   r   r.   r>   r#   ÚjoinÚ	tracebackÚformat_exception©r   rN   ÚfÚpyresultr2   r   r   r   Útest_tracebackFromFailureŸ   s   
þÿz+PyUnitResultTests.test_tracebackFromFailurec                 C   s†   G dd„ dt ƒ}|dƒ}t ¡ }| |¡ d t |jd ¡¡}d |jd d  	¡ d	d
… ¡}|  
| ¡ |t|ƒ d	…  ¡ ¡ d	S )zG
        As test_tracebackFromFailure, but covering more code.
        c                   @   s   e Zd ZdZdd„ ZdS )z3PyUnitResultTests.test_traceback.<locals>.ErrorTestNc                 S   s*   zdd  W d S  t y   t ¡ | _‚ w rI   )rL   rM   rN   r   r   r   r   r    ¸   s   
þz<PyUnitResultTests.test_traceback.<locals>.ErrorTest.test_foo)r   r   r   rN   r    r   r   r   r   r"   µ   r<   r"   r    rK   é   Ú
r   r   Néÿÿÿÿ)r   r   r=   r0   rO   rP   Ú	format_tbrN   r#   Ú
splitlinesr>   ÚstriprD   )r   r"   r   r2   Úexpected_stackÚobserved_stackr   r   r   Útest_traceback°   s   

	 ÿz PyUnitResultTests.test_tracebackc                 C   sx   zdd  W n t y   t ¡ }tƒ }Y nw | ¡  t ¡ }t|ƒ}| | |¡ |  	|j
d d d tj|Ž ¡¡ dS )zÙ
        Errors added through the L{PyUnitResultAdapter} have the same
        traceback information as if there were no adapter at all, even
        if the Failure that held the information has been cleaned.
        r   r   rK   N)rL   rM   rN   r   ÚcleanFailurer   r=   r   r.   r>   r#   rO   rP   rQ   rR   r   r   r   Útest_tracebackFromCleanFailureÑ   s   
þÿz0PyUnitResultTests.test_tracebackFromCleanFailurec                 C   óB   G dd„ dt ƒ}|dƒ}t ¡ }| |¡ |  |j|dfg¡ dS )zw
        Skips using trial's skipping functionality are reported as skips in
        the L{pyunit.TestResult}.
        c                   @   s   e Zd Zeddƒdd„ ƒZdS )z2PyUnitResultTests.test_trialSkip.<locals>.SkipTestTúLet's skip!c                 S   rH   rI   r   r   r   r   r   Ú	test_skipë   ó   z<PyUnitResultTests.test_trialSkip.<locals>.SkipTest.test_skipN)r   r   r   r   rc   r   r   r   r   ÚSkipTestê   ó    re   rc   rb   N©r   r   r=   r0   r>   Úskipped©r   re   r   r2   r   r   r   Útest_trialSkipä   ó
   
z PyUnitResultTests.test_trialSkipc                 C   ra   )zx
        Skips using pyunit's skipping functionality are reported as skips in
        the L{pyunit.TestResult}.
        c                   @   s   e Zd Ze d¡dd„ ƒZdS )z3PyUnitResultTests.test_pyunitSkip.<locals>.SkipTestÚskippyc                 S   rH   rI   r   r   r   r   r   rc   û   rd   z=PyUnitResultTests.test_pyunitSkip.<locals>.SkipTest.test_skipN)r   r   r   r   Úskiprc   r   r   r   r   re   ú   rf   re   rc   rl   Nrg   ri   r   r   r   Útest_pyunitSkipô   rk   z!PyUnitResultTests.test_pyunitSkipN)r   r   r   r!   r   r"   r3   rA   rF   rG   rJ   rU   r^   r`   rj   rn   r   r   r   r   r   !   s    (
!r   )rM   rP   Úunittestr   r   Úzope.interfacer   Útwisted.python.failurer   Útwisted.trial.itrialr   r   Útwisted.trial.unittestr   r   r	   r   r   r   r   r   Ú<module>   s   