o
    ¯bU  ã                   @   s0  d Z ddlmZmZmZ z
ddlmZ e W n ey!   dZY nw ddlm	Z	 ddl
mZmZmZ ddlmZ ddlmZ d	d
lmZmZmZmZmZmZ d	dlmZ d	dlmZ G dd„ dejƒZG dd„ dejƒZG dd„ dejƒZ G dd„ dejƒZ!G dd„ dƒZ"G dd„ de#ƒZ$G dd„ dejƒZ%dS )z+
Test cases for L{twisted.logger._format}.
é    )ÚAnyStrÚOptionalÚcast)ÚtzsetN)ÚFailure)ÚaddTZCleanupÚmktimeÚsetTZ)Úunittest)ÚSkipTesté   )ÚeventAsTextÚformatEventÚformatEventAsClassicLogTextÚ
formatTimeÚformatUnformattableEventÚformatWithCall)ÚLogEvent)ÚLogLevelc                   @   sV   e Zd ZdZddd„Zddd„Zddd	„Zdd
d„Zddd„Zddd„Z	ddd„Z
dS )ÚFormattingTestsz5
    Tests for basic event formatting functions.
    ÚreturnNc                    sÌ   dt dtdtf‡ fdd„}ˆ  d|dƒ¡ ˆ  d|dƒ¡ ˆ  d|d	dd
�¡ ˆ  d|dddd„ d�¡ ˆ  d|dƒ¡ ˆ  d|dƒ¡ |ddd�}ˆ  d|¡ ttdƒƒ}ˆ  d| d |ddd�¡ dS )ac  
        L{formatEvent} will format an event according to several rules:

            - A string with no formatting instructions will be passed straight
              through.

            - PEP 3101 strings will be formatted using the keys and values of
              the event as named fields.

            - PEP 3101 keys ending with C{()} will be treated as instructions
              to call that key (which ought to be a callable) before
              formatting.

        L{formatEvent} will always return L{str}, and if given bytes, will
        always treat its format string as UTF-8 encoded.
        Ú	logFormatÚeventr   c                    s$   | |d< t |ƒ}ˆ  t|ƒt¡ |S )NÚ
log_format)r   ÚassertIsÚtypeÚstr)r   r   Úresult©Úself© úA/usr/lib/python3/dist-packages/twisted/logger/test/test_format.pyÚformat9   s   z0FormattingTests.test_formatEvent.<locals>.formatÚ ó    Úabcz{x})Úxzno, yes.z{not_called}, {called()}.Únoc                   S   ó   dS )NÚyesr    r    r    r    r!   Ú<lambda>D   ó    z2FormattingTests.test_formatEvent.<locals>.<lambda>)Ú
not_calledÚcalledu   SÃ¡nchezs   SÃ¡nchezúUnable to format events   Sánchezs   S{a!s}nchezó   á)ÚazSb'\xe1'nchezÚSÚnchezs   S{a!r}nchezN)r   Úobjectr   ÚassertEqualÚassertInÚrepr)r   r"   ÚmaybeResultÚxe1r    r   r!   Útest_formatEvent'   s   þ z FormattingTests.test_formatEventc                 C   s$   t ddd�}t|ƒ}|  d|¡ dS )z5
        Formatting an event with no format.
        é   r   )ÚfooÚbarr#   N)Údictr   r4   ©r   r   r   r    r    r!   Útest_formatEventNoFormatN   s   z(FormattingTests.test_formatEventNoFormatc                 C   s8   t tƒ ddd�}t|ƒ}|  d|¡ |  t|ƒ|¡ dS )z:
        Formatting an event with a bogus format.
        r:   r   )r   r;   r<   zLog format must be strN)r=   r3   r   r5   r6   r>   r    r    r!   Útest_formatEventWeirdFormatW   ó   z+FormattingTests.test_formatEventWeirdFormatc                 C   s8   t ddd„ d�}t|ƒ}|  d|¡ |  t|ƒ|¡ dS )zF
        Formatting an event that's just plain out to get us.
        ú{evil()}c                   S   ó   dd S ©Nr:   r   r    r    r    r    r!   r*   e   ó    z?FormattingTests.test_formatUnformattableEvent.<locals>.<lambda>©r   Úevilr.   N)r=   r   r5   r6   r>   r    r    r!   Útest_formatUnformattableEventa   rA   z-FormattingTests.test_formatUnformattableEventc                 C   sL   ddddd„ t ttƒ ƒdi}t|ƒ}|  d|¡ |  d|¡ |  d	|¡ d
S )zR
        Formatting an unformattable event that has an unformattable key.
        r   rB   rG   c                   S   rC   rD   r    r    r    r    r!   r*   q   rE   zSFormattingTests.test_formatUnformattableEventWithUnformattableKey.<locals>.<lambda>Úgurkú*MESSAGE LOST: unformattable object logged:úRecoverable data:úException during formatting:N)r   r   ÚUnformattabler   r5   r>   r    r    r!   Ú1test_formatUnformattableEventWithUnformattableKeyk   s   ýzAFormattingTests.test_formatUnformattableEventWithUnformattableKeyc                 C   sD   t ddd„ tƒ d�}t|ƒ}|  d|¡ |  d|¡ |  d|¡ dS )	úT
        Formatting an unformattable event that has an unformattable value.
        rB   c                   S   rC   rD   r    r    r    r    r!   r*      rE   zUFormattingTests.test_formatUnformattableEventWithUnformattableValue.<locals>.<lambda>)r   rG   rI   rJ   rK   rL   N)r=   rM   r   r5   r>   r    r    r!   Ú3test_formatUnformattableEventWithUnformattableValuey   s   ýzCFormattingTests.test_formatUnformattableEventWithUnformattableValuec                 C   sP   t ddd„ dd�}t|tttƒ ƒƒ}|  d|¡ |  tdƒd tdƒ |¡ d	S )
rO   rB   c                   S   rC   rD   r    r    r    r    r!   r*   �   rE   zbFormattingTests.test_formatUnformattableEventWithUnformattableErrorOMGWillItStop.<locals>.<lambda>Úokay)r   rG   ÚrecoverablerJ   rR   z = N)r=   r   r   ÚBaseExceptionrM   r5   r6   r>   r    r    r!   Ú@test_formatUnformattableEventWithUnformattableErrorOMGWillItStop‡   s   ý zPFormattingTests.test_formatUnformattableEventWithUnformattableErrorOMGWillItStop©r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r9   r?   r@   rH   rN   rP   rT   r    r    r    r!   r   "   s    

'
	




r   c                   @   sL   e Zd ZdZddd„Zddd„Zddd	„Zdd
d„Zddd„Zddd„Z	dS )ÚTimeFormattingTestsz.
    Tests for time formatting functions.
    r   Nc                 C   s   t | ƒ d S )N)r   r   r    r    r!   ÚsetUp›   ó   zTimeFormattingTests.setUpc                    sb   t du rtdƒ‚dtdtdtddf‡ fdd„}|d	d
dƒ |dddƒ |dddƒ |dddƒ dS )z¤
        Default time stamp format is RFC 3339 and offset respects the timezone
        as set by the standard C{TZ} environment variable and L{tzset} API.
        Nú:Platform cannot change timezone; unable to verify offsets.ÚnameÚexpectedDSTÚexpectedSTDr   c                    s<   t | ƒ tdƒ}tdƒ}ˆ  t|ƒ|¡ ˆ  t|ƒ|¡ d S )N)	iÖ  é   é   r   r   r   é   éµ   r:   )	i×  r:   é   r   r   r   r   re   r   )r	   r   r4   r   )r^   r_   r`   ÚlocalDSTÚlocalSTDr   r    r!   ÚtestForTimeZone¦   s
   zMTimeFormattingTests.test_formatTimeWithDefaultFormat.<locals>.testForTimeZoneúUTC+00z2006-06-30T00:00:00+0000z2007-01-31T00:00:00+0000zEST+05EDT,M4.1.0,M10.5.0z2006-06-30T00:00:00-0400z2007-01-31T00:00:00-0500zCEST-01CEDT,M4.1.0,M10.5.0z2006-06-30T00:00:00+0200z2007-01-31T00:00:00+0100zCST+06z2006-06-30T00:00:00-0600z2007-01-31T00:00:00-0600)r   r   r   )r   rh   r    r   r!   Ú test_formatTimeWithDefaultFormatž   s.   
ýýýýz4TimeFormattingTests.test_formatTimeWithDefaultFormatc                 C   s(   |   tdƒd¡ |   tddd�d¡ dS )zL
        If C{when} argument is L{None}, we get the default output.
        Nú-ú!)Údefault©r4   r   r   r    r    r!   Útest_formatTimeWithNoTimeË   s   z-TimeFormattingTests.test_formatTimeWithNoTimec                 C   s6   t dƒ}|  t|dd�d¡ |  t|ddd�d¡ dS )zR
        If C{timeFormat} argument is L{None}, we get the default output.
        ©	iÝ  é	   é   é   é(   é/   r:   i  r:   N©Ú
timeFormatrk   rl   )rw   rm   ©r   r4   r   ©r   Útr    r    r!   Útest_formatTimeWithNoFormatÒ   s   z/TimeFormattingTests.test_formatTimeWithNoFormatc                 C   s    t dƒ}|  t|dd�d¡ dS )z2
        Alternate time format in output.
        rp   z%Y/%Wrv   z2013/38Nrx   ry   r    r    r!   Ú&test_formatTimeWithAlternateTimeFormatÚ   s   z:TimeFormattingTests.test_formatTimeWithAlternateTimeFormatc                 C   s   |   tddd�d¡ dS )z0
        "%f" supported in time format.
        g’?x€„.Az%frv   Ú234560Nrn   r   r    r    r!   Útest_formatTimePercentFá   s   z+TimeFormattingTests.test_formatTimePercentFrU   )
rV   rW   rX   rY   r[   rj   ro   r{   r|   r~   r    r    r    r!   rZ   –   s    


-

rZ   c                   @   s~   e Zd ZdZddd„Zddd„Zddd	„Zdd
d„Zddd„Zddd„Z	ddd„Z
ddd„Zddd„Zddd„Zddd„ZdS )ÚClassicLogFormattingTestsz@
    Tests for classic text log event formatting functions.
    r   Nc                 C   sH   t du rtdƒ‚t| ƒ tdƒ tdƒ}td|d�}|  t|ƒd¡ dS )zÂ
        Time is first field.  Default time stamp format is RFC 3339 and offset
        respects the timezone as set by the standard C{TZ} environment variable
        and L{tzset} API.
        Nr]   ri   rp   ÚXYZZY©r   Úlog_timez%2013-09-24T11:40:47+0000 [-#-] XYZZY
)r   r   r   r	   r   r=   r4   r   )r   rz   r   r    r    r!   Útest_formatTimeDefaultí   s   þz0ClassicLogFormattingTests.test_formatTimeDefaultc                 C   s:   dt t dtfdd„}tddd�}|  t||d�d	¡ d
S )zc
        Time is first field.  Custom formatting function is an optional
        argument.
        rz   r   c                 S   s   d| › d�S )NÚ__r    )rz   r    r    r!   r     r\   zCClassicLogFormattingTests.test_formatTimeCustom.<locals>.formatTimer€   i90  r�   )r   z__12345__ [-#-] XYZZY
N)r   Úfloatr   r=   r4   r   )r   r   r   r    r    r!   Útest_formatTimeCustom   s   
þz/ClassicLogFormattingTests.test_formatTimeCustomc                 C   ó    t ddd�}|  t|ƒd¡ dS )z:
        Namespace is first part of second field.
        r€   úmy.namespace)r   Úlog_namespacez- [my.namespace#-] XYZZY
N©r=   r4   r   ©r   r   r    r    r!   Útest_formatNamespace  ó
   þz.ClassicLogFormattingTests.test_formatNamespacec                 C   s"   t dtjd�}|  t|ƒd¡ dS )z7
        Level is second part of second field.
        r€   )r   Ú	log_levelz- [-#warn] XYZZY
N©r=   r   Úwarnr4   r   r‹   r    r    r!   Útest_formatLevel  ó
   þz*ClassicLogFormattingTests.test_formatLevelc                 C   r‡   )z)
        System is second field.
        r€   úS.Y.S.T.E.M.©r   Ú
log_systemú- [S.Y.S.T.E.M.] XYZZY
NrŠ   r‹   r    r    r!   Útest_formatSystem#  r�   z+ClassicLogFormattingTests.test_formatSystemc                 C   s&   t ddtjdd�}|  t|ƒd¡ dS )úB
        System is not supplanted by namespace and level.
        r€   rˆ   r“   )r   r‰   rŽ   r•   r–   Nr�   r‹   r    r    r!   Útest_formatSystemRulz-  s   üþz/ClassicLogFormattingTests.test_formatSystemRulzc                 C   s"   t dtƒ d�}|  t|ƒd¡ dS )r˜   r€   r”   z- [UNFORMATTABLE] XYZZY
N)r=   rM   r4   r   r‹   r    r    r!   Útest_formatSystemUnformattable<  r’   z8ClassicLogFormattingTests.test_formatSystemUnformattablec                 C   r‡   )z0
        Formatted event is last field.
        zid:{id}Ú123©r   Úidz- [-#-] id:123
NrŠ   r‹   r    r    r!   Útest_formatFormatF  r�   z+ClassicLogFormattingTests.test_formatFormatc                 C   s   t dd�}|  t|ƒd¡ dS )z#
        No format string.
        r›   )r�   N©r=   r   r   r‹   r    r    r!   Útest_formatNoFormatP  s   
z-ClassicLogFormattingTests.test_formatNoFormatc                 C   s    t ddd�}|  t|ƒd¡ dS )z&
        Empty format string.
        r#   r›   rœ   NrŸ   r‹   r    r    r!   Útest_formatEmptyFormatW  s   z0ClassicLogFormattingTests.test_formatEmptyFormatc                 C   s   t dd�}|  t|ƒd¡ dS )zO
        If the formatted event has newlines, indent additional lines.
        z"XYZZY
A hollow voice says:
"Plugh")r   z-- [-#-] XYZZY
	A hollow voice says:
	"Plugh"
NrŠ   r‹   r    r    r!   Útest_formatFormatMultiLine^  s
   
þz4ClassicLogFormattingTests.test_formatFormatMultiLinerU   )rV   rW   rX   rY   rƒ   r†   rŒ   r‘   r—   r™   rš   rž   r    r¡   r¢   r    r    r    r!   r   è   s    














r   c                   @   s   e Zd ZdZddd„ZdS )ÚFormatFieldTestsz+
    Tests for format field functions.
    r   Nc                 C   s>   |   tdtddd„ d�ƒd¡ |   tdtdd„ d	�ƒd
¡ dS )zú
        L{formatWithCall} is an extended version of L{str.format} that
        will interpret a set of parentheses "C{()}" at the end of a format key
        to mean that the format key ought to be I{called} rather than
        stringified.
        zHello, {world}. {callme()}.Úearthc                   S   r(   )NÚmayber    r    r    r    r!   r*   x  r+   z6FormatFieldTests.test_formatWithCall.<locals>.<lambda>)ÚworldÚcallmezHello, earth. maybe.zHello, {repr()!r}.c                   S   r(   )Nr6   r    r    r    r    r!   r*   }  r+   )r6   zHello, 'repr'.N)r4   r   r=   r   r    r    r!   Útest_formatWithCalln  s   þûþz$FormatFieldTests.test_formatWithCallrU   )rV   rW   rX   rY   r¨   r    r    r    r!   r£   i  s    r£   c                   @   s   e Zd ZdZdefdd„ZdS )rM   z>
    An object that raises an exception from C{__repr__}.
    r   c                 C   s   t dd ƒS rD   )r   r   r    r    r!   Ú__repr__‡  r\   zUnformattable.__repr__N)rV   rW   rX   rY   r   r©   r    r    r    r!   rM   ‚  s    rM   c                   @   s   e Zd ZdZdS )ÚCapturedErrorz3
    A captured error for use in format tests.
    N)rV   rW   rX   rY   r    r    r    r!   rª   ‹  s    rª   c                   @   s’   e Zd ZdZddd„Zddd„Zddd	„Zdd
d„Zddd„Zddd„Z	ddd„Z
ddd„Zddd„Zddd„Zddd„Zddd„Zddd„ZdS )ÚEventAsTextTestszi
    Tests for L{eventAsText}, all of which ensure that the
    returned type is UTF-8 decoded text.
    r   Nc                 C   sd   zt dƒ‚ t y   tƒ }Y nw ddi}||d< t|ddd�}|  t| ¡ ƒ|¡ |  d|¡ dS )	zT
        An event with a C{log_failure} key will have a traceback appended.
        úThis is a fake errorr   úThis is a test log messageÚlog_failureTF©ÚincludeTimestampÚincludeSystemN©rª   r   r   r5   r   ÚgetTraceback©r   Úfr   Ú	eventTextr    r    r!   Útest_eventWithTraceback—  s   
ÿz(EventAsTextTests.test_eventWithTracebackc                 C   sd   zt dƒ‚ t y   tƒ }Y nw ddi}||d< t|ddd�}|  t| ¡ ƒ|¡ |  d|¡ dS )	zu
        An event with an empty C{log_format} key appends a traceback from
        the accompanying failure.
        r¬   r   r#   r®   TFr¯   Nr²   r´   r    r    r!   Ú"test_formatEmptyEventWithTraceback¦  s   
ÿz3EventAsTextTests.test_formatEmptyEventWithTracebackc                 C   sv   zt dƒ‚ t y   tƒ }Y nw ddd„ dœ}||d< t|ddd	�}|  |t¡ |  t| ¡ ƒ|¡ |  d|¡ d
S )zw
        An event with an unformattable value in the C{log_format} key still
        has a traceback appended.
        r¬   rB   c                   S   rC   rD   r    r    r    r    r!   r*   Á  rE   zHEventAsTextTests.test_formatUnformattableWithTraceback.<locals>.<lambda>rF   r®   TFr¯   N)rª   r   r   ÚassertIsInstancer   r5   r³   r´   r    r    r!   Ú%test_formatUnformattableWithTracebackµ  s   
ÿþz6EventAsTextTests.test_formatUnformattableWithTracebackc                 C   s�   zt dƒ‚ t y   tƒ }Y nw ddddd„ tttƒ ƒdi}||d< t|d	d
d�}|  |t¡ |  d|¡ |  t| ¡ ƒ|¡ |  d|¡ dS )z²
        An event with an unformattable value in the C{log_format} key, that
        throws an exception when __repr__ is invoked still has a traceback
        appended.
        r¬   r   rB   rG   c                   S   rC   rD   r    r    r    r    r!   r*   Ö  rE   zMEventAsTextTests.test_formatUnformattableErrorWithTraceback.<locals>.<lambda>rI   r®   TFr¯   zMESSAGE LOSTN)	rª   r   r   r   rM   r   r¹   r5   r³   r´   r    r    r!   Ú*test_formatUnformattableErrorWithTracebackÉ  s   
ÿýz;EventAsTextTests.test_formatUnformattableErrorWithTracebackc                 C   s<   ddi}t ƒ |d< t|ddd�}|  |t¡ |  d|¡ dS )	zk
        If a traceback cannot be appended, a message indicating this is true
        is appended.
        r   r#   r®   TFr¯   z'(UNABLE TO OBTAIN TRACEBACK FROM EVENT)N)r3   r   r¹   r   r5   ©r   r   r¶   r    r    r!   Ú&test_formatEventUnformattableTracebackà  s
   
z7EventAsTextTests.test_formatEventUnformattableTracebackc                 C   s2   ddi}t |ddd�}|  |t¡ |  d|¡ dS )zY
        An event with no C{log_failure} key will not have a traceback appended.
        r   r­   TFr¯   N)r   r¹   r   r5   r¼   r    r    r!   Útest_formatEventNonCriticalë  s   z,EventAsTextTests.test_formatEventNonCriticalc                 C   s\   zt dƒ‚ t y   tƒ }Y nw ddi}||d< t|ddd�}|  d|¡ |  d|¡ d	S )
zU
        An exception message with multibyte characters is properly handled.
        u   â‚¬r   r­   r®   TFr¯   Ú	TracebackN©rª   r   r   r5   r´   r    r    r!   Útest_formatTracebackMultibyteô  s   
ÿz.EventAsTextTests.test_formatTracebackMultibytec                 C   s\   zt dƒ‚ t y   tƒ }Y nw ddi}||d< t|ddd�}|  d|¡ |  d	|¡ d
S )zj
        An error raised attempting to decode the UTF still produces a
        valid log message.
        s
   ÿþt e s t r   r­   r®   TFr¯   r¿   z.CapturedError(b"\xff\xfet\x00e\x00s\x00t\x00")NrÀ   r´   r    r    r!   Ú,test_formatTracebackHandlesUTF8DecodeFailure  s   
ÿz=EventAsTextTests.test_formatTracebackHandlesUTF8DecodeFailurec                 C   s^   zt dƒ‚ t y   tƒ }Y nw tdƒ}dd|dœ}||d< t|dddd	�}|  |d
¡ dS )zq
        If includeSystem is specified as the only option no timestamp or
        traceback are printed.
        r¬   rp   ÚABCDÚfake_system©r   r•   r‚   r®   FT©r°   ÚincludeTracebackr±   z[fake_system] ABCDN©rª   r   r   r   r4   ©r   rµ   rz   r   r¶   r    r    r!   Útest_eventAsTextSystemOnly  s*   
ÿýüþz+EventAsTextTests.test_eventAsTextSystemOnlyc                 C   s~   t du rtdƒ‚t| ƒ tdƒ ztdƒ‚ ty    tƒ }Y nw tdƒ}dd|dœ}||d	< t|d
ddd�}|  |d¡ dS )zq
        If includeTimestamp is specified as the only option no system or
        traceback are printed.
        Nr]   ri   r¬   rp   rÃ   rÄ   rÅ   r®   TFrÆ   z2013-09-24T11:40:47+0000 ABCD)	r   r   r   r	   rª   r   r   r   r4   rÉ   r    r    r!   Útest_eventAsTextTimestampOnly0  s2   
ÿýüþz.EventAsTextTests.test_eventAsTextTimestampOnlyc                 C   s\   zt dƒ‚ t y   tƒ }Y nw tdƒ}d|dœ}||d< t|dddd�}|  |d	¡ d
S )z\
        If includeSystem is specified with a missing system [-#-]
        is used.
        r¬   rp   rÃ   r�   r®   FTrÆ   z
[-#-] ABCDNrÈ   rÉ   r    r    r!   Útest_eventAsTextSystemMissingR  s(   
ÿþüþz.EventAsTextTests.test_eventAsTextSystemMissingc                 C   sb   zt dƒ‚ t y   tƒ }Y nw tdƒ}d|tjddœ}||d< t|dddd	�}|  |d
¡ dS )z€
        If includeSystem is specified with a missing system but
        namespace and level are present they are used.
        r¬   rp   rÃ   Útest)r   r‚   rŽ   r‰   r®   FTrÆ   z[test#info] ABCDN©rª   r   r   r   Úinfor   r4   rÉ   r    r    r!   Ú.test_eventAsTextSystemMissingNamespaceAndLevelm  s,   
ÿüüþz?EventAsTextTests.test_eventAsTextSystemMissingNamespaceAndLevelc                 C   s`   zt dƒ‚ t y   tƒ }Y nw tdƒ}d|tjdœ}||d< t|dddd�}|  |d	¡ d
S )zv
        If includeSystem is specified with a missing system but
        level is present, level is included.
        r¬   rp   rÃ   )r   r‚   rŽ   r®   FTrÆ   z[-#info] ABCDNrÎ   rÉ   r    r    r!   Ú&test_eventAsTextSystemMissingLevelOnlyŠ  s*   
ÿýüþz7EventAsTextTests.test_eventAsTextSystemMissingLevelOnlyrU   )rV   rW   rX   rY   r·   r¸   rº   r»   r½   r¾   rÁ   rÂ   rÊ   rË   rÌ   rÐ   rÑ   r    r    r    r!   r«   ‘  s    






	



"
r«   )&rY   Útypingr   r   r   Útimer   ÚImportErrorÚtwisted.python.failurer   Ú!twisted.python.test.test_tzhelperr   r   r	   Útwisted.trialr
   Útwisted.trial.unittestr   Ú_formatr   r   r   r   r   r   Ú_interfacesr   Ú_levelsr   ÚTestCaser   rZ   r   r£   rM   Ú	Exceptionrª   r«   r    r    r    r!   Ú<module>   s.   ÿ tR 	