U
    T‚gƒ  ã                   @  s˜   d dl mZ 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 d dlmZmZ G d	d
„ d
eƒZG dd„ deƒZdS )é    )ÚannotationsN)ÚskipIf)Úimplementer)ÚFailure)Ú	IReporterÚ	ITestCase)Úpyunitcases)ÚPyUnitResultAdapterÚSynchronousTestCasec                   @  s(   e Zd Zddœdd„Zddœdd„ZdS )ÚPyUnitTestTestsÚNone©Úreturnc                 C  s   t  d¡| _t| jƒ| _d S )NZ	test_pass)r   Z
PyUnitTestÚoriginalr   Útest©Úself© r   úH/tmp/pip-unpacked-wheel-1_7di8ic/twisted/trial/test/test_pyunitcompat.pyÚ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)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r      s   r   c                   @  s¬   e Zd ZdZG dd„ deƒZddœdd„Zddœdd	„Zddœd
d„Zddœdd„Z	ddœdd„Z
ddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœ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œ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.
        Fr   r   c                 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_foo0   s    z$PyUnitResultTests.ErrorTest.test_fooN)r   r   r   Ú__doc__r   r    r   r   r   r   Ú	ErrorTest'   s   r"   r   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                   @  sP   e Zd ZdZddœdd„Zdddœdd	„Zdddœd
d„Zddddœ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}.
            r   r   c                 S  s   g | _ g | _d S )N)ÚerrorsÚfailuresr   r   r   r   Ú__init__F   s    zaPyUnitResultTests.test_dontUseAdapterWhenReporterProvidesIReporter.<locals>.StubReporter.__init__Úobject)r   r   c                 S  s   dS ©z-
                Do nothing.
                Nr   ©r   r   r   r   r   Ú	startTestJ   s    zbPyUnitResultTests.test_dontUseAdapterWhenReporterProvidesIReporter.<locals>.StubReporter.startTestc                 S  s   dS r'   r   r(   r   r   r   ÚstopTestO   s    zaPyUnitResultTests.test_dontUseAdapterWhenReporterProvidesIReporter.<locals>.StubReporter.stopTestr   )r   Úerrorr   c                 S  s   | j  |¡ dS )z3
                Record the error.
                N)r#   Úappend)r   r   r+   r   r   r   ÚaddErrorT   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"   ÚrunZassertIsInstancer#   r   )r   r.   r   Úresultr   r   r   Ú0test_dontUseAdapterWhenReporterProvidesIReporter7   s    

zBPyUnitResultTests.test_dontUseAdapterWhenReporterProvidesIReporterc                 C  sV   G dd„ dt ƒ}|dƒ}t ¡ }| |¡ |  |j¡ |  d|j¡ |  | ¡ ¡ d S )Nc                   @  s   e Zd ZdZddœdd„ZdS )z3PyUnitResultTests.test_success.<locals>.SuccessTestFr   r   c                 S  s
   d| _ d S ©NTr   ©Úsr   r   r   r    c   s    z<PyUnitResultTests.test_success.<locals>.SuccessTest.test_fooN©r   r   r   r   r    r   r   r   r   ÚSuccessTest`   s   r6   r    r   )	r
   ÚpyunitÚ
TestResultr/   r   r   ÚassertEqualÚtestsRunÚwasSuccessful)r   r6   r   r0   r   r   r   Útest_success_   s    
zPyUnitResultTests.test_successc                 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œdd„ZdS )z3PyUnitResultTests.test_failure.<locals>.FailureTestFr   r   c                 S  s   d| _ |  d¡ d S )NTzboom!)r   Zfailr3   r   r   r   r    r   s    z<PyUnitResultTests.test_failure.<locals>.FailureTest.test_fooNr5   r   r   r   r   ÚFailureTesto   s   r=   r    r   )r
   r7   r8   r/   r   r   r9   r:   Úlenr$   ÚassertFalser;   )r   r=   r   r0   r   r   r   Útest_failuren   s    
zPyUnitResultTests.test_failurec                 C  sZ   |   d¡}t ¡ }| |¡ |  |j¡ |  d|j¡ |  dt|j	ƒ¡ |  
| ¡ ¡ d S )Nr    r   )r"   r7   r8   r/   r   r   r9   r:   r>   r#   r?   r;   )r   r   r0   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œdd„Zddœdd„ZdS )	z4PyUnitResultTests.test_setUpError.<locals>.ErrorTestFr   r   c                 S  s   dd  d S ©Nr   r   r   r   r   r   r   r   �   s    z:PyUnitResultTests.test_setUpError.<locals>.ErrorTest.setUpc                 S  s
   d| _ d S r2   r   r3   r   r   r   r    �   s    z=PyUnitResultTests.test_setUpError.<locals>.ErrorTest.test_fooN)r   r   r   r   r   r    r   r   r   r   r"   Š   s   r"   r    r   )r
   r7   r8   r/   r?   r   r9   r:   r>   r#   r;   )r   r"   r   r0   r   r   r   Útest_setUpError‰   s    	
z!PyUnitResultTests.test_setUpErrorc                 C  sr   zdd  W n" t k
r.   t ¡ }tƒ }Y nX 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   r7   r8   r	   r-   r9   r#   ÚjoinÚ	tracebackÚformat_exception)r   rG   ÚfÚpyresultr0   r   r   r   Útest_tracebackFromFailureœ   s     ÿz+PyUnitResultTests.test_tracebackFromFailurec                 C  s”   G dd„ dt ƒ}|dƒ}t ¡ }| |¡ |jdk	s8t‚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œdd„ZdS )z3PyUnitResultTests.test_traceback.<locals>.ErrorTestNr   r   c                 S  s2   zdd  W n  t k
r,   t ¡ | _‚ Y nX d S rB   )rE   rF   rG   r   r   r   r   r    µ   s
    
z<PyUnitResultTests.test_traceback.<locals>.ErrorTest.test_foo)r   r   r   rG   r    r   r   r   r   r"   ²   s   r"   r    NrD   é   Ú
r   r   éÿÿÿÿ)r
   r7   r8   r/   rG   ÚAssertionErrorrH   rI   Ú	format_tbr#   Ú
splitlinesr9   Ústripr>   )r   r"   r   r0   Zexpected_stackZobserved_stackr   r   r   Útest_traceback­   s    

  ÿz PyUnitResultTests.test_tracebackc                 C  sŠ   zdd  W n" t k
r.   t ¡ }tƒ }Y nX | ¡  t ¡ }t|ƒ}| | |¡ d 	t
j|Ž ¡}|  |jd d  d¡| d¡¡ 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   rD   z$ZeroDivisionError: division by zero
N)rE   rF   rG   r   ZcleanFailurer7   r8   r	   r-   rH   rI   rJ   r9   r#   Úendswith)r   rG   rK   rL   r0   Ztbackr   r   r   Útest_tracebackFromCleanFailureÎ   s    þz0PyUnitResultTests.test_tracebackFromCleanFailurec                 C  sB   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œdd„ƒZdS )z2PyUnitResultTests.test_trialSkip.<locals>.SkipTestTúLet's skip!r   r   c                 S  s   dd  d S rB   r   r   r   r   r   Ú	test_skipê   s    z<PyUnitResultTests.test_trialSkip.<locals>.SkipTest.test_skipN)r   r   r   r   rY   r   r   r   r   ÚSkipTesté   s   rZ   rY   rX   N©r
   r7   r8   r/   r9   Úskipped©r   rZ   r   r0   r   r   r   Útest_trialSkipã   s
    
z PyUnitResultTests.test_trialSkipc                 C  sB   G dd„ dt ƒ}|dƒ}t ¡ }| |¡ |  |j|dfg¡ dS )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œdd„ƒZdS )z3PyUnitResultTests.test_pyunitSkip.<locals>.SkipTestÚskippyr   r   c                 S  s   dd  d S rB   r   r   r   r   r   rY   ú   s    z=PyUnitResultTests.test_pyunitSkip.<locals>.SkipTest.test_skipN)r   r   r   r7   ÚskiprY   r   r   r   r   rZ   ù   s   rZ   rY   r_   Nr[   r]   r   r   r   Útest_pyunitSkipó   s
    
z!PyUnitResultTests.test_pyunitSkipN)r   r   r   r!   r
   r"   r1   r<   r@   rA   rC   rM   rU   rW   r^   ra   r   r   r   r   r      s   (
!r   )Ú
__future__r   rF   rI   Zunittestr7   r   Zzope.interfacer   Ztwisted.python.failurer   Ztwisted.trial.itrialr   r   Ztwisted.trial.testr   Ztwisted.trial.unittestr	   r
   r   r   r   r   r   r   Ú<module>   s   