U
    
W[¿!  ã                   @   sÔ   d Z ddlZddlmZmZ ddlZddlmZ ddl	m
Z
mZ ddlmZmZ ddlmZ ddlmZ d	d
lmZ d	dlmZ d	dlmZ dd„ ZG dd„ dejƒZG dd„ deƒZdd„ ZG dd„ dejƒZ dS )z+
Test cases for L{twisted.logger._format}.
é    N)ÚBytesIOÚTextIOWrapper)Úgetsourcefile)ÚverifyObjectÚBrokenMethodImplementation)Ú_PY3Úcurrentframe)ÚFailure)Úunittesté   )ÚLogLevel)ÚILogObserver)ÚSTDLibLogObserverc                  C   s&   t dƒ} ttj| jd  ƒ| jd fS )z¹
    Retrive the file name and line number immediately after where this function
    is called.

    @return: the file name and line number
    @rtype: 2-L{tuple} of L{str}, L{int}
    é   Ú__name__)r   r   ÚsysÚmodulesÚ	f_globalsÚf_lineno)Zcaller© r   úA/usr/lib/python3/dist-packages/twisted/logger/test/test_stdlib.pyÚnextLine   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d„ Zdd„ Zdd„ Zdd„ ZdS )ÚSTDLibLogObserverTestsz)
    Tests for L{STDLibLogObserver}.
    c              
   C   sF   t ƒ }ztt|ƒ W n, tk
r@ } z|  |¡ W 5 d}~X Y nX dS )z=
        L{STDLibLogObserver} is an L{ILogObserver}.
        N)r   r   r   r   Zfail)ÚselfÚobserverÚer   r   r   Útest_interface+   s
    z%STDLibLogObserverTests.test_interfacec                 C   s   t ƒ }|  |j¡ |S )z”
        Create a logging object we can use to test with.

        @return: a stdlib-style logger
        @rtype: L{StdlibLoggingContainer}
        )ÚStdlibLoggingContainerZ
addCleanupÚclose)r   Zloggerr   r   r   Ú	py_logger6   s    z STDLibLogObserverTests.py_loggerc                 G   s:   |   ¡ }ttjd d}|D ]}||ƒ q|jj| ¡ fS )a`  
        Send one or more events to Python's logging module, and
        capture the emitted L{logging.LogRecord}s and output stream as
        a string.

        @param events: events
        @type events: L{tuple} of L{dict}

        @return: a tuple: (records, output)
        @rtype: 2-tuple of (L{list} of L{logging.LogRecord}, L{bytes}.)
        r   )Z
stackDepth)r   r   ZdefaultStackDepthÚbufferedHandlerÚrecordsÚoutputAsText)r   ÚeventsZplr   Úeventr   r   r   ÚlogEventB   s    ý
zSTDLibLogObserverTests.logEventc                 C   s4   |   i ¡\}}|  t|ƒd¡ |  |d jd¡ dS )z
        Logger name.
        r   r   ZtwistedN)r%   ÚassertEqualÚlenÚname©r   r!   Úoutputr   r   r   Ú	test_nameY   s    z STDLibLogObserverTests.test_namec           	      C   sÄ   dt jtjt jtjt jtjt jtjt j	tj
t ji}g }| ¡ D ]2\}}i }|dk	rZ||d< t|ƒ|d< | |¡ q>| j|Ž \}}|  t|ƒt|ƒ¡ tt|ƒƒD ]}|  || j|| d ¡ q dS )z
        Log levels.
        NZ	log_levelZ
py_levelno)Ú
py_loggingÚINFOr   ÚdebugÚDEBUGÚinfoÚwarnZWARNINGÚerrorZERRORZcriticalZCRITICALÚitemsÚintÚappendr%   r&   r'   ÚrangeZlevelno)	r   ZlevelMappingr#   ÚlevelZpyLevelr$   r!   r*   Úir   r   r   Útest_levelsc   s0          ú
z"STDLibLogObserverTests.test_levelsc                 C   s`   t ƒ \}}|  i ¡\}}|  t|ƒd¡ |  |d j|¡ |  |d j|¡ |  |d j¡ dS )zb
        C{pathname}, C{lineno}, C{exc_info}, C{func} is set properly on
        records.
        r   r   N)r   r%   r&   r'   ÚpathnameÚlinenoZassertIsNoneÚexc_info)r   ÚfilenameZlogLiner!   r*   r   r   r   Útest_callerInfo‡   s    
z&STDLibLogObserverTests.test_callerInfoc                 C   sV   t ddd}|  |¡\}}|  t|ƒd¡ |  t|d jƒd¡ |  |d jd¡ dS )	zL
        Basic formattable event passes the format along correctly.
        úHello, {who}!Údude©Ú
log_formatÚwhor   r   zHello, dude!r   N)Údictr%   r&   r'   ÚstrÚmsgÚargs©r   r$   r!   r*   r   r   r   Útest_basicFormat™   s
    z'STDLibLogObserverTests.test_basicFormatc                 C   sD   t ddd}|  |¡\}}|  t|ƒd¡ |  | d¡t|ƒ¡ dS )z<
        Basic formattable event renders correctly.
        r?   r@   rA   r   z:Hello, dude!
N)rD   r%   r&   r'   Z
assertTrueÚendswithÚreprrH   r   r   r   Útest_basicFormatRendered¥   s    ÿz/STDLibLogObserverTests.test_basicFormatRenderedc                 C   s8   |   i ¡\}}|  t|ƒd¡ |  t|d jƒd¡ dS )z'
        Event with no format.
        r   r   Ú N)r%   r&   r'   rE   rF   r)   r   r   r   Útest_noFormat±   s    z$STDLibLogObserverTests.test_noFormatc                 C   s‚   dd„ }z
|ƒ  W n t k
r,   tƒ }Y nX tdd|d}|  |¡\}}|  t|ƒd¡ |  d|¡ |  d|¡ |  d|¡ d	S )
zK
        An event with a failure logs the failure details as well.
        c                   S   s   dd  d S ©Nr   r   r   r   r   r   r   Úfailing_func¿   s    z9STDLibLogObserverTests.test_failure.<locals>.failing_funcúHi momÚme©rB   rC   Zlog_failurer   úin failing_funcÚZeroDivisionErrorN)rU   r	   rD   r%   r&   r'   ÚassertIn©r   rP   Zfailurer$   r!   r*   r   r   r   Útest_failure»   s    
z#STDLibLogObserverTests.test_failurec                 C   sŠ   dd„ }z
|ƒ  W n t k
r,   tƒ }Y nX | ¡  tdd|d}|  |¡\}}|  t|ƒd¡ |  d|¡ |  d|¡ |  d|¡ d	S )
z²
        A cleaned Failure object has a fake traceback object; make sure that
        logging such a failure still results in the exception details being
        logged.
        c                   S   s   dd  d S rO   r   r   r   r   r   rP   Ó   s    z@STDLibLogObserverTests.test_cleanedFailure.<locals>.failing_funcrQ   rR   rS   r   rT   rU   N)rU   r	   ZcleanFailurerD   r%   r&   r'   rV   rW   r   r   r   Útest_cleanedFailureÍ   s    
z*STDLibLogObserverTests.test_cleanedFailureN)r   Ú
__module__Ú__qualname__Ú__doc__r   r   r%   r+   r9   r>   rI   rL   rN   rX   rY   r   r   r   r   r   &   s   
$
r   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	r   zF
    Continer for a test configuration of stdlib logging objects.
    c                 C   s\   t  d¡| _| j ¡ | _| j t j¡ tƒ | _| j 	| j¡ t
ƒ \| _| _| j 	| j¡ d S )NrM   )r,   Z	getLoggerÚ
rootLoggerZgetEffectiveLevelÚoriginalLevelÚsetLevelr/   ÚBufferedHandlerr    Z
addHandlerÚhandlerAndBytesIOÚstreamHandlerr*   ©r   r   r   r   Ú__init__è   s    zStdlibLoggingContainer.__init__c                 C   sB   | j  | j¡ | j  | j¡ | j  | j¡ | j ¡  | j ¡  dS )z#
        Close the logger.
        N)r]   r_   r^   ZremoveHandlerr    rb   r   r*   rc   r   r   r   r   õ   s
    
zStdlibLoggingContainer.closec                 C   s   | j  ¡  d¡S )z
        Get the output to the underlying stream as text.

        @return: the output text
        @rtype: L{unicode}
        úutf-8)r*   ÚgetvalueÚdecoderc   r   r   r   r"      s    z#StdlibLoggingContainer.outputAsTextN)r   rZ   r[   r\   rd   r   r"   r   r   r   r   r   ã   s   r   c                  C   sH   t ƒ } | }tj}tr"t| ddd}t |¡}t |¡}| |¡ || fS )zÏ
    Construct a 2-tuple of C{(StreamHandler, BytesIO)} for testing interaction
    with the 'logging' module.

    @return: handler and io object
    @rtype: tuple of L{StreamHandler} and L{io.BytesIO}
    re   Ú
)ÚencodingÚnewline)r   r,   ZBASIC_FORMATr   r   Z	FormatterZStreamHandlerZsetFormatter)r*   ÚstreamÚtemplateZ	formatterZhandlerr   r   r   ra     s    


ra   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r`   zN
    A L{py_logging.Handler} that remembers all logged records in a list.
    c                 C   s   t j | ¡ g | _dS )z5
        Initialize this L{BufferedHandler}.
        N)r,   ÚHandlerrd   r!   rc   r   r   r   rd   $  s    zBufferedHandler.__init__c                 C   s   | j  |¡ dS )z&
        Remember the record.
        N)r!   r5   )r   Úrecordr   r   r   Úemit,  s    zBufferedHandler.emitN)r   rZ   r[   r\   rd   ro   r   r   r   r   r`     s   r`   )!r\   r   Úior   r   Zloggingr,   Úinspectr   Zzope.interface.verifyr   r   Ztwisted.python.compatr   r   Ztwisted.python.failurer	   Ztwisted.trialr
   Z_levelsr   Z	_observerr   Z_stdlibr   r   ZTestCaser   Úobjectr   ra   rm   r`   r   r   r   r   Ú<module>   s"    >(