o
    ¯bh  ã                   @   s¢   d 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	ƒZG d
d„ dejƒZG dd„ dƒ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.
é    N)ÚreactorÚtask)ÚfailureÚlog)Ú	_synctestÚreporterÚunittestc                  C   s,   zdd  W | S  t y   t ¡ } Y | S w )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                   @   sF   e Zd ZdZG dd„ dƒ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_singleThenFail2   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   ÚSynchronousFailureLogging9   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>A   s    zAMask.AsynchronousFailureLogging.test_inCallback.<locals>.<lambda>)r   Ú
deferLaterr   r   r   r   r   Útest_inCallback=   s   z/Mask.AsynchronousFailureLogging.test_inCallbackN)r   r   r   r!   r   r   r   r   ÚAsynchronousFailureLogging<   s    r"   N)
r   r   r   Ú__doc__r   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   ÚsetUpJ   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*   ÚgotEventr.   ÚassertEqualÚ	getErrorsr   r   r   r   Útest_msgN   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©r-   r.   r/   r0   r   Úwhy)r   r*   r1   r.   r2   r3   ©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)	r8   r*   ÚflushErrorsr2   r3   ÚlenÚ
assertTrueÚcheckr
   )r   Úflushedr   r   r   Ú
test_flushm   s
   
zObserverTests.test_flushc                 C   s   t  tdƒ¡S )Nz
test error)r   r   ÚRuntimeErrorr   r   r   r   Ú_makeRuntimeFailurew   ó   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,   Nr5   r   )r8   r@   r*   r1   Údictr.   r9   r
   r2   r3   r:   r;   r<   )r   r   r=   r   r   r   Útest_flushByTypez   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,   Nr5   )r*   Ú_ignoreErrorsr
   r   r1   r.   r2   r3   r7   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,   Nr5   )	r*   rD   r
   Ú_clearIgnoresr   r1   r.   r2   r3   r7   r   r   r   Útest_clearIgnoresœ   s   
úÿ
zObserverTests.test_clearIgnoresN)r   r   r   r#   r+   r4   r8   r>   r@   rC   rE   rG   r   r   r   r   r&   D   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+   ·   rA   zLogErrorsMixin.setUpc                 C   s   |   t¡ d S r   )ÚflushLoggedErrorsr
   r   r   r   r   ÚtearDownº   rA   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(   r2   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©rK   r(   r2   r:   rL   rM   rN   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   NrR   ©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   rQ   r   r	   N)	rK   r(   r2   r:   rL   ÚtrapÚ	TypeErrorr
   rM   rT   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.
        Ú_logObserverN)	r   r)   Úpatchr   ÚtheLogPublisherÚ	observersrK   r(   r2   )r   r*   r^   rO   r   r   r   Útest_boundedObserversñ   s   
z$LogErrorsMixin.test_boundedObserversN)r   r   r   r#   r+   rJ   rP   rS   rW   rZ   r_   r   r   r   r   rH   ±   s    
rH   c                   @   s   e Zd ZejZdS )ÚSynchronousLogErrorsTestsN)r   r   r   r   r   rK   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)rK   r(   r2   r:   rL   r;   r<   r
   rN   r   r   r   r!     s   

"ÿz*AsynchronousLogErrorsTests.test_inCallbackN)r   r   r   r   r"   rK   r!   r   r   r   r   ra     s    ra   )r#   r.   Útwisted.internetr   r   Útwisted.pythonr   r   Útwisted.trialr   r   r   r   r   r$   r&   rH   r`   r%   ra   r   r   r   r   Ú<module>   s   *mM