U
    
W[!  ã                   @   s¶   d Z ddlmZmZ ddlZddlmZmZ ddlm	Z	m
Z
 ddlmZmZmZ dd„ 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ejƒZG dd„ deejƒZdS )zG
Test the interaction between trial and errors logged during test run.
é    )ÚdivisionÚabsolute_importN)ÚreactorÚtask)ÚfailureÚlog)ÚunittestÚreporterÚ	_synctestc                  C   s.   zdd  W n t k
r(   t ¡ } Y nX | S )z*
    Return a new, realistic failure.
    é   r   )ÚZeroDivisionErrorr   ÚFailure)Úf© r   ú=/usr/lib/python3/dist-packages/twisted/trial/test/test_log.pyÚmakeFailure   s
    r   c                   @   sH   e Zd ZdZG dd„ deƒZG dd„ deejƒZG dd„ deej	ƒZ
dS )	ÚMaskz?
    Hide C{MockTest}s from Trial's automatic test finder.
    c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
zMask.FailureLoggingMixinc                 C   s   dS )z/
            Don't log any errors.
            Nr   ©Úselfr   r   r   Útest_silent!   s    z$Mask.FailureLoggingMixin.test_silentc                 C   s   t  tƒ ¡ dS )z-
            Log a single error.
            N©r   Úerrr   r   r   r   r   Útest_single&   s    z$Mask.FailureLoggingMixin.test_singlec                 C   s   t  tƒ ¡ t  tƒ ¡ dS )z)
            Log two errors.
            Nr   r   r   r   r   Útest_double,   s    z$Mask.FailureLoggingMixin.test_doublec                 C   s   t  tƒ ¡ dd  dS )z8
            Log a single error, then fail.
            r   Nr   r   r   r   r   Útest_singleThenFail4   s    z,Mask.FailureLoggingMixin.test_singleThenFailN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r   ÚFailureLoggingMixin    s   r   c                   @   s   e Zd ZdS )zMask.SynchronousFailureLoggingN)r   r   r   r   r   r   r   ÚSynchronousFailureLogging<   s   r   c                   @   s   e Zd Zdd„ ZdS )zMask.AsynchronousFailureLoggingc                 C   s   t  tddd„ ¡S )zC
            Log an error in an asynchronous callback.
            r   c                   S   s   t  tƒ ¡S ©Nr   r   r   r   r   Ú<lambda>E   ó    zAMask.AsynchronousFailureLogging.test_inCallback.<locals>.<lambda>)r   Z
deferLaterr   r   r   r   r   Útest_inCallbackA   s    z/Mask.AsynchronousFailureLogging.test_inCallbackN)r   r   r   r#   r   r   r   r   ÚAsynchronousFailureLogging@   s   r$   N)r   r   r   Ú__doc__Úobjectr   r   ÚSynchronousTestCaser   ÚTestCaser$   r   r   r   r   r      s   r   c                   @   sP   e Zd Z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 )ÚObserverTestsz
    Tests for L{_synctest._LogObserver}, a helper for the implementation of
    L{SynchronousTestCase.flushLoggedErrors}.
    c                 C   s   t  ¡ | _t ¡ | _d S r    )r	   Ú
TestResultÚresultr
   Ú_LogObserverÚobserverr   r   r   r   ÚsetUpN   s    
zObserverTests.setUpc                 C   s0   | j  dt ¡ dddœ¡ |  | j  ¡ g ¡ dS )zW
        Test that a standard log message doesn't go anywhere near the result.
        )zsome messager   ú-)ÚmessageÚtimeÚisErrorÚsystemN)r-   ÚgotEventr1   ÚassertEqualÚ	getErrorsr   r   r   r   Útest_msgS   s     þzObserverTests.test_msgc              	   C   s<   t ƒ }| j dt ¡ dd|ddœ¡ |  | j ¡ |g¡ dS )zF
        Test that an observed error gets added to the result
        r   r   r/   N©r0   r1   r2   r3   r   Zwhy)r   r-   r4   r1   r5   r6   ©r   r   r   r   r   Ú
test_error]   s      ýzObserverTests.test_errorc                 C   sL   |   ¡  | j ¡ }|  | j ¡ g ¡ |  t|ƒd¡ |  |d  t¡¡ dS )zS
        Check that flushing the observer with no args removes all errors.
        r   r   N)	r:   r-   ÚflushErrorsr5   r6   ÚlenÚ
assertTrueÚcheckr   )r   Úflushedr   r   r   Ú
test_flushi   s
    
zObserverTests.test_flushc                 C   s   t  tdƒ¡S )Nz
test error)r   r   ÚRuntimeErrorr   r   r   r   Ú_makeRuntimeFailuret   s    z!ObserverTests._makeRuntimeFailurec              
   C   sx   |   ¡  |  ¡ }| j tdt ¡ dd|dd¡ | j t¡}|  | j 	¡ |g¡ |  t
|ƒd¡ |  |d  t¡¡ dS )zY
        Check that flushing the observer remove all failures of the given type.
        r   r   r/   Nr8   r   )r:   rB   r-   r4   Údictr1   r;   r   r5   r6   r<   r=   r>   )r   r   r?   r   r   r   Útest_flushByTypex   s      ÿzObserverTests.test_flushByTypec              	   C   sF   | j  t¡ tƒ }| j  dt ¡ dd|ddœ¡ |  | j  ¡ g ¡ dS )zS
        Check that C{_ignoreErrors} actually causes errors to be ignored.
        r   r   r/   Nr8   )r-   Ú_ignoreErrorsr   r   r4   r1   r5   r6   r9   r   r   r   Útest_ignoreErrors†   s      ýzObserverTests.test_ignoreErrorsc              	   C   sR   | j  t¡ | j  ¡  tƒ }| j  dt ¡ dd|ddœ¡ |  | j  ¡ |g¡ dS )zj
        Check that C{_clearIgnores} ensures that previously ignored errors
        get captured.
        r   r   r/   Nr8   )	r-   rE   r   Z_clearIgnoresr   r4   r1   r5   r6   r9   r   r   r   Útest_clearIgnores“   s    
  ýzObserverTests.test_clearIgnoresN)r   r   r   r%   r.   r7   r:   r@   rB   rD   rF   rG   r   r   r   r   r)   I   s   
r)   c                   @   sH   e Zd Z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 )ÚLogErrorsMixinzb
    High-level tests demonstrating the expected behaviour of logged errors
    during tests.
    c                 C   s   t  ¡ | _d S r    )r	   r*   r+   r   r   r   r   r.   ©   s    zLogErrorsMixin.setUpc                 C   s   |   t¡ d S r    )ZflushLoggedErrorsr   r   r   r   r   ÚtearDown¬   s    zLogErrorsMixin.tearDownc                 C   sf   |   d¡}|| jƒ |  t| jjƒd¡ |  | jjd d  t¡| jjd d ¡ |  d| jj¡ dS )zI
        Test that a logged error gets reported as a test error.
        r   r   r   N)	ÚMockTestr+   r5   r<   Úerrorsr=   r>   r   Ú	successes©r   Útestr   r   r   Útest_singleError°   s    

ÿzLogErrorsMixin.test_singleErrorc                 C   s<   |   d¡}|| jƒ |  t| jjƒd¡ |  d| jj¡ dS )zf
        Test that when two errors get logged, they both get reported as test
        errors.
        r   é   r   N©rJ   r+   r5   r<   rK   rL   rM   r   r   r   Útest_twoErrors¼   s    

zLogErrorsMixin.test_twoErrorsc                 C   sh   |   d¡}|   d¡}|| jƒ || jƒ |  t| jjƒd¡ |  | jjd d |¡ |  d| jj¡ dS )zT
        Check that an error logged in one test doesn't fail the next test.
        r   r   r   r   NrQ   ©r   Út1Út2r   r   r   Útest_errorsIsolatedÇ   s    



z"LogErrorsMixin.test_errorsIsolatedc                 C   s¬   |   d¡}|   d¡}|| jƒ || jƒ |  t| jjƒd¡ |  | jjd d |¡ | jjd d  t¡ |  | jjd d |¡ | jjd d  t¡ |  d| jj¡ dS )zN
        An error logged in a failed test doesn't fail the next test.
        r   r   rP   r   r   N)	rJ   r+   r5   r<   rK   ZtrapÚ	TypeErrorr   rL   rS   r   r   r   Ú test_errorsIsolatedWhenTestFailsÔ   s    



z/LogErrorsMixin.test_errorsIsolatedWhenTestFailsc                 C   sL   t  ¡ }|  t d|¡ tjjdd… }|  ¡ }|| jƒ |  |tjj¡ dS )zE
        There are no extra log observers after a test runs.
        Z_logObserverN)	r
   r,   Zpatchr   ZtheLogPublisherÚ	observersrJ   r+   r5   )r   r-   rY   rN   r   r   r   Útest_boundedObserversç   s    
z$LogErrorsMixin.test_boundedObserversN)r   r   r   r%   r.   rI   rO   rR   rV   rX   rZ   r   r   r   r   rH   £   s   rH   c                   @   s   e Zd ZejZdS )ÚSynchronousLogErrorsTestsN)r   r   r   r   r   rJ   r   r   r   r   r[   õ   s   r[   c                   @   s   e Zd ZejZdd„ ZdS )ÚAsynchronousLogErrorsTestsc                 C   sV   |   d¡}|| jƒ |  t| jjƒd¡ |  | jjd d  t¡| jjd d ¡ dS )zS
        Test that errors logged in callbacks get reported as test errors.
        r#   r   r   N)rJ   r+   r5   r<   rK   r=   r>   r   rM   r   r   r   r#   ý   s    

ÿz*AsynchronousLogErrorsTests.test_inCallbackN)r   r   r   r   r$   rJ   r#   r   r   r   r   r\   ú   s   r\   )r%   Z
__future__r   r   r1   Ztwisted.internetr   r   Ztwisted.pythonr   r   Ztwisted.trialr   r	   r
   r   r&   r   r'   r)   rH   r[   r(   r\   r   r   r   r   Ú<module>   s   -ZR