U
    `[#X                     @   s$  d Z ddlmZmZmZ zddlmZ e W n ek
rD   dZY nX 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mZmZmZ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eZ G dd de!Z"G dd de	jZ#dS )z+
Test cases for L{twisted.logger._format}.
    )mktimeaddTZCleanupsetTZ)tzsetN)unittest)SkipTest)_PY3unicode   )LogLevel)formatEventformatUnformattableEvent
formatTimeformatEventAsClassicLogTextformatWithCalleventAsText)Failurec                   @   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 )FormattingTestsz5
    Tests for basic event formatting functions.
    c                    s    fdd}  d|d   d|d   d|ddd   d|d	d
dd d   d|d |d} d| |ddd}ts d| n d| ttd} d| d |ddd dS )ag  
        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{unicode}, and if given bytes, will
        always treat its format string as UTF-8 encoded.
        c                    s$   | |d< t |} t|t |S )N
log_format)r   assertIstyper	   )Z	logFormateventresultself A/usr/lib/python3/dist-packages/twisted/logger/test/test_format.pyformat5   s    z0FormattingTests.test_formatEvent.<locals>.format     abcz{x})xzno, yes.z{not_called}, {called()}.Znoc                   S   s   dS )NZyesr   r   r   r   r   <lambda>B   r   z2FormattingTests.test_formatEvent.<locals>.<lambda>)Z
not_calledZcalledu   Sánchezs   Sánchezs   SnchezUnable to format events   S{a!s}nchez   )azSb'\xe1'nchezSZnchezs   S{a!r}nchezN)assertEqualassertInr   r	   repr)r   r   Z	badResultZmaybeResultZ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   r'   r   r   r   r   r   r   test_formatEventNoFormatU   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 unicode or bytesN)r.   objectr   r(   r)   r/   r   r   r   test_formatEventWeirdFormat_   s    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   s   dd S Nr+   r   r   r   r   r   r   r"   n   r   z?FormattingTests.test_formatUnformattableEvent.<locals>.<lambda>r   evilr#   N)r.   r   r(   r)   r/   r   r   r   test_formatUnformattableEventj   s    z-FormattingTests.test_formatUnformattableEventc                 C   sF   ddddd t  di}t|}| d| | d| | d	| d
S )zR
        Formatting an unformattable event that has an unformattable key.
        r   r3   r6   c                   S   s   dd S r4   r   r   r   r   r   r"   {   r   zSFormattingTests.test_formatUnformattableEventWithUnformattableKey.<locals>.<lambda>gurk*MESSAGE LOST: unformattable object logged:Recoverable data:Exception during formatting:N)Unformattabler   r(   r/   r   r   r   1test_formatUnformattableEventWithUnformattableKeyu   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.
        r3   c                   S   s   dd S r4   r   r   r   r   r   r"      r   zUFormattingTests.test_formatUnformattableEventWithUnformattableValue.<locals>.<lambda>)r   r6   r8   r9   r:   r;   N)r.   r<   r   r(   r/   r   r   r   3test_formatUnformattableEventWithUnformattableValue   s    zCFormattingTests.test_formatUnformattableEventWithUnformattableValuec                 C   sJ   t ddd dd}t|t }| d| | tdd td | d	S )
r>   r3   c                   S   s   dd S r4   r   r   r   r   r   r"      r   zbFormattingTests.test_formatUnformattableEventWithUnformattableErrorOMGWillItStop.<locals>.<lambda>Zokay)r   r6   recoverabler9   r@   z = N)r.   r   r<   r(   r)   r/   r   r   r   @test_formatUnformattableEventWithUnformattableErrorOMGWillItStop   s    zPFormattingTests.test_formatUnformattableEventWithUnformattableErrorOMGWillItStopN)__name__
__module____qualname____doc__r*   r0   r2   r7   r=   r?   rA   r   r   r   r   r      s   1
r   c                   @   s@   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S )TimeFormattingTestsz.
    Tests for time formatting functions.
    c                 C   s   t |  d S )N)r   r   r   r   r   setUp   s    zTimeFormattingTests.setUpc                    sP   t dkrtd 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.c                    s<   t |  td}td} t||  t|| d S )N)	i        r   r   r         r+   )	i  r+      r   r   r   r
   rM   r   )r   r   r'   r   )nameZexpectedDSTZexpectedSTDZlocalDSTZ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   rO   r   r   r    test_formatTimeWithDefaultFormat   s2    
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r'   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
timeFormatrR   rS   )r^   rT   r   r'   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.
        rW   z%Y/%Wr]   z2013/38Nr_   r`   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%fr]   Z234560NrU   r   r   r   r   test_formatTimePercentF   s    z+TimeFormattingTests.test_formatTimePercentFN)
rB   rC   rD   rE   rG   rQ   rV   rb   rc   rd   r   r   r   r   rF      s   0	rF   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 )ClassicLogFormattingTestsz@
    Tests for classic text log event formatting functions.
    c                 C   sH   t dk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.
        NrH   rP   rW   XYZZYr   log_timez%2013-09-24T11:40:47+0000 [-#-] XYZZY
)r   r   r   r   r   r.   r'   r   )r   ra   r   r   r   r   test_formatTimeDefault  s    z0ClassicLogFormattingTests.test_formatTimeDefaultc                 C   s,   dd }t ddd}| t||dd dS )	zc
        Time is first field.  Custom formatting function is an optional
        argument.
        c                 S   s
   d | S )Nz__{0}__)r   )ra   r   r   r   r"     r   zAClassicLogFormattingTests.test_formatTimeCustom.<locals>.<lambda>rf   i90  rg   )r   z__12345__ [-#-] XYZZY
Nr.   r'   r   )r   r   r   r   r   r   test_formatTimeCustom  s    
z/ClassicLogFormattingTests.test_formatTimeCustomc                 C   s    t ddd}| t|d dS )z:
        Namespace is first part of second field.
        rf   my.namespace)r   log_namespacez- [my.namespace#-] XYZZY
Nrj   r   r   r   r   r   test_formatNamespace%  s
    z.ClassicLogFormattingTests.test_formatNamespacec                 C   s"   t dtjd}| t|d dS )z7
        Level is second part of second field.
        rf   )r   	log_levelz- [-#warn] XYZZY
Nr.   r   warnr'   r   rn   r   r   r   test_formatLevel0  s
    z*ClassicLogFormattingTests.test_formatLevelc                 C   s    t ddd}| t|d dS )z)
        System is second field.
        rf   S.Y.S.T.E.M.r   
log_system- [S.Y.S.T.E.M.] XYZZY
Nrj   rn   r   r   r   test_formatSystem;  s
    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.
        rf   rl   rt   )r   rm   rp   rv   rw   Nrq   rn   r   r   r   test_formatSystemRulzF  s    z/ClassicLogFormattingTests.test_formatSystemRulzc                 C   s"   t dt d}| t|d dS )ry   rf   ru   z- [UNFORMATTABLE] XYZZY
N)r.   r<   r'   r   rn   r   r   r   test_formatSystemUnformattableV  s
    z8ClassicLogFormattingTests.test_formatSystemUnformattablec                 C   s    t ddd}| t|d dS )z0
        Formatted event is last field.
        zid:{id}123r   idz- [-#-] id:123
Nrj   rn   r   r   r   test_formatFormata  s
    z+ClassicLogFormattingTests.test_formatFormatc                 C   s   t dd}| t|d dS )z#
        No format string.
        r|   )r~   Nr.   r   r   rn   r   r   r   test_formatNoFormatl  s
    
z-ClassicLogFormattingTests.test_formatNoFormatc                 C   s    t ddd}| t|d dS )z&
        Empty format string.
        r   r|   r}   Nr   rn   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"
Nrj   rn   r   r   r   test_formatFormatMultiLine  s
    
z4ClassicLogFormattingTests.test_formatFormatMultiLineN)rB   rC   rD   rE   ri   rk   ro   rs   rx   rz   r{   r   r   r   r   r   r   r   r   re      s   re   c                   @   s   e Zd ZdZdd ZdS )FormatFieldTestsz+
    Tests for format field functions.
    c                 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{unicode.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()}.Zearthc                   S   s   dS )Nmayber   r   r   r   r   r"     r   z6FormatFieldTests.test_formatWithCall.<locals>.<lambda>)ZworldZcallmezHello, earth. maybe.zHello, {repr()!r}.c                   S   s   dS )Nr)   r   r   r   r   r   r"     r   )r)   zHello, 'repr'.N)r'   r   r.   r   r   r   r   test_formatWithCall  s    z$FormatFieldTests.test_formatWithCallN)rB   rC   rD   rE   r   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdd ZdS )r<   z>
    An object that raises an exception from C{__repr__}.
    c                 C   s   t dd S r4   )strr   r   r   r   __repr__  s    zUnformattable.__repr__N)rB   rC   rD   rE   r   r   r   r   r   r<     s   r<   c                   @   s   e Zd ZdZdS )CapturedErrorz3
    A captured error for use in format tests.
    N)rB   rC   rD   rE   r   r   r   r   r     s   r   c                   @   sx   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d Zdd ZdS )EventAsTextTestszi
    Tests for L{eventAsText}, all of which ensure that the
    returned type is UTF-8 decoded text.
    c                 C   sj   zt dW n t k
r&   t }Y nX 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   r(   r	   getTracebackr   fr   	eventTextr   r   r   test_eventWithTraceback  s     z(EventAsTextTests.test_eventWithTracebackc                 C   sj   zt dW n t k
r&   t }Y nX 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   s|   zt dW n t k
r&   t }Y nX 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   r3   c                   S   s   dd S r4   r   r   r   r   r   r"     r   zHEventAsTextTests.test_formatUnformattableWithTraceback.<locals>.<lambda>r5   r   TFr   N)r   r   r   assertIsInstancer	   r(   r   r   r   r   r   %test_formatUnformattableWithTraceback  s     z6EventAsTextTests.test_formatUnformattableWithTracebackc                 C   s   zt dW n t k
r&   t }Y nX ddddd 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   r3   r6   c                   S   s   dd S r4   r   r   r   r   r   r"     r   zMEventAsTextTests.test_formatUnformattableErrorWithTraceback.<locals>.<lambda>r8   r   TFr   zMESSAGE LOSTN)r   r   r<   r   r   r	   r(   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)r1   r   r   r	   r(   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	   r(   r   r   r   r   test_formatEventNonCritical5  s     z,EventAsTextTests.test_formatEventNonCriticalc                 C   sb   zt dW n t k
r&   t }Y nX 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   r(   r   r   r   r   test_formatTracebackMultibyteE  s     z.EventAsTextTests.test_formatTracebackMultibytec                 C   st   zt dW n t k
r&   t }Y nX ddi}||d< t|ddd}| d| tsd| d	| n| 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   u   ��t e s t z.CapturedError(b'\xff\xfet\x00e\x00s\x00t\x00')N)r   r   r   r(   r   r   r   r   r   ,test_formatTracebackHandlesUTF8DecodeFailure[  s(     z=EventAsTextTests.test_formatTracebackHandlesUTF8DecodeFailurec                 C   sd   zt dW n t k
r&   t }Y nX 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   rW   ABCDfake_systemr   rv   rh   r   FTr   ZincludeTracebackr   z[fake_system] ABCDNr   r   r   r   r'   r   r   ra   r   r   r   r   r   test_eventAsTextSystemOnlyy  s(    z+EventAsTextTests.test_eventAsTextSystemOnlyc                 C   s   t dkrtdt|  td ztdW n tk
rF   t }Y nX 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.
        NrH   rP   r   rW   r   r   r   r   TFr   z2013-09-24T11:40:47+0000 ABCD)	r   r   r   r   r   r   r   r   r'   r   r   r   r   test_eventAsTextTimestampOnly  s4    z.EventAsTextTests.test_eventAsTextTimestampOnlyc                 C   sb   zt dW n t k
r&   t }Y nX 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   rW   r   rg   r   FTr   z
[-#-] ABCDNr   r   r   r   r   test_eventAsTextSystemMissing  s&    z.EventAsTextTests.test_eventAsTextSystemMissingc                 C   sh   zt dW n t k
r&   t }Y nX 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   rW   r   Ztest)r   rh   rp   rm   r   FTr   z[test#info] ABCDNr   r   r   r   infor   r'   r   r   r   r   .test_eventAsTextSystemMissingNamespaceAndLevel  s*    z?EventAsTextTests.test_eventAsTextSystemMissingNamespaceAndLevelc                 C   sf   zt dW n t k
r&   t }Y nX 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   rW   r   )r   rh   rp   r   FTr   z[-#info] ABCDNr   r   r   r   r   &test_eventAsTextSystemMissingLevelOnly  s(    z7EventAsTextTests.test_eventAsTextSystemMissingLevelOnlyN)rB   rC   rD   rE   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   %r   )$rE   Z!twisted.python.test.test_tzhelperr   r   r   timer   ImportErrorZtwisted.trialr   Ztwisted.trial.unittestr   Ztwisted.python.compatr   r	   Z_levelsr   Z_formatr   r   r   r   r   r   Ztwisted.python.failurer   ZTestCaser   rF   re   r   r1   r<   	Exceptionr   r   r   r   r   r   <module>   s*   
  Z 
