3
K^#X                 @   s$  d Z ddlmZmZmZ y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} j d|d  j d|d  j d|ddd  j d|d	d
dd d  j d|d |d} jd| |ddd}ts jd| n jd| ttd} j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 |} jt|t |S )N
log_format)r   assertIstyper	   )Z	logFormateventresult)self C/tmp/pip-unpacked-wheel-k7g0kfns/twisted/logger/test/test_format.pyformat5   s    z0FormattingTests.test_formatEvent.<locals>.format     abcz{x})xzno, yes.z{not_called}, {called()}.noc               S   s   dS )Nyesr   r   r   r   r   <lambda>B   s    z2FormattingTests.test_formatEvent.<locals>.<lambda>)Z
not_calledcalledu   Sánchezs   Sánchezs   Snchezz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|}| j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|}| jd| | j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   r   r   test_formatEventWeirdFormat_   s    z+FormattingTests.test_formatEventWeirdFormatc             C   s8   t ddd d}t|}| jd| | jt|| dS )zF
        Formatting an event that's just plain out to get us.
        z{evil()}c               S   s   dd S )Nr,   r   r   r   r   r   r   r#   n   s    z?FormattingTests.test_formatUnformattableEvent.<locals>.<lambda>)r   evilzUnable to format eventN)r/   r   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|}| jd| | jd| | jd	| d
S )zR
        Formatting an unformattable event that has an unformattable key.
        r   z{evil()}r3   c               S   s   dd S )Nr,   r   r   r   r   r   r   r#   {   s    zSFormattingTests.test_formatUnformattableEventWithUnformattableKey.<locals>.<lambda>gurkz*MESSAGE LOST: unformattable object logged:zRecoverable data:zException during formatting:N)Unformattabler   r)   )r   r   r   r   r   r   1test_formatUnformattableEventWithUnformattableKeyu   s    
zAFormattingTests.test_formatUnformattableEventWithUnformattableKeyc             C   sD   t ddd t d}t|}| jd| | jd| | jd| dS )	zT
        Formatting an unformattable event that has an unformattable value.
        z{evil()}c               S   s   dd S )Nr,   r   r   r   r   r   r   r#      s    zUFormattingTests.test_formatUnformattableEventWithUnformattableValue.<locals>.<lambda>)r   r3   r5   z*MESSAGE LOST: unformattable object logged:zRecoverable data:zException during formatting:N)r/   r6   r   r)   )r   r   r   r   r   r   3test_formatUnformattableEventWithUnformattableValue   s    
zCFormattingTests.test_formatUnformattableEventWithUnformattableValuec             C   sJ   t ddd dd}t|t }| jd| | jtdd td | d	S )
zT
        Formatting an unformattable event that has an unformattable value.
        z{evil()}c               S   s   dd S )Nr,   r   r   r   r   r   r   r#      s    zbFormattingTests.test_formatUnformattableEventWithUnformattableErrorOMGWillItStop.<locals>.<lambda>okay)r   r3   recoverablez*MESSAGE LOST: unformattable object logged:r:   z = N)r/   r   r6   r)   r*   )r   r   r   r   r   r   @test_formatUnformattableEventWithUnformattableErrorOMGWillItStop   s    zPFormattingTests.test_formatUnformattableEventWithUnformattableErrorOMGWillItStopN)__name__
__module____qualname____doc__r+   r0   r2   r4   r7   r8   r;   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.
        Nz:Platform cannot change timezone; unable to verify offsets.c          
      s<   t |  td}td} jt||  jt|| d S )N        r         r,        r
   )	rB   rC   rD   r   r   r   rE   rF   r,   )	rG   r,   rH   r   r   r   r
   rH   r   )r   r   r(   r   )nameZexpectedDSTZexpectedSTDZlocalDSTZlocalSTD)r   r   r   testForTimeZone   s
    zMTimeFormattingTests.test_formatTimeWithDefaultFormat.<locals>.testForTimeZonez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   rJ   r   )r   r    test_formatTimeWithDefaultFormat   s(    
z4TimeFormattingTests.test_formatTimeWithDefaultFormatc             C   s(   | j tdd | j 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}| jt|d	d
d | jt|d	ddd d	S )zR
        If C{timeFormat} argument is L{None}, we get the default output.
          	         (   /   r,     N)
timeFormatrL   rM   )rW   rN   )	rP   rQ   rR   rS   rT   rU   r,   rV   r,   )r   r(   r   )r   tr   r   r   test_formatTimeWithNoFormat   s    z/TimeFormattingTests.test_formatTimeWithNoFormatc          
   C   s    t d}| jt|d	d
d dS )z2
        Alternate time format in output.
          rQ   rR   rS   rT   rU   r,     z%Y/%W)rW   z2013/38N)	rZ   rQ   rR   rS   rT   rU   r,   r[   r,   )r   r(   r   )r   rX   r   r   r   &test_formatTimeWithAlternateTimeFormat   s    z:TimeFormattingTests.test_formatTimeWithAlternateTimeFormatc             C   s   | j tdddd dS )z0
        "%f" supported in time format.
        g?x.Az%f)rW   Z234560N)r(   r   )r   r   r   r   test_formatTimePercentF   s    z+TimeFormattingTests.test_formatTimePercentFN)
r<   r=   r>   r?   rA   rK   rO   rY   r\   r]   r   r   r   r   r@      s   0	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 )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}| j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.
        Nz:Platform cannot change timezone; unable to verify offsets.zUTC+00  rQ   rR   rS   rT   rU   r,     XYZZY)r   log_timez%2013-09-24T11:40:47+0000 [-#-] XYZZY
)	r_   rQ   rR   rS   rT   rU   r,   r`   r,   )r   r   r   r   r   r/   r(   r   )r   rX   r   r   r   r   test_formatTimeDefault  s    z0ClassicLogFormattingTests.test_formatTimeDefaultc             C   s,   dd }t ddd}| jt||dd dS )	zc
        Time is first field.  Custom formatting function is an optional
        argument.
        c             S   s
   dj | S )Nz__{0}__)r   )rX   r   r   r   r#     s    zAClassicLogFormattingTests.test_formatTimeCustom.<locals>.<lambda>ra   i90  )r   rb   )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}| jt|d dS )z:
        Namespace is first part of second field.
        ra   zmy.namespace)r   log_namespacez- [my.namespace#-] XYZZY
N)r/   r(   r   )r   r   r   r   r   test_formatNamespace%  s    z.ClassicLogFormattingTests.test_formatNamespacec             C   s"   t dtjd}| jt|d dS )z7
        Level is second part of second field.
        ra   )r   	log_levelz- [-#warn] XYZZY
N)r/   r   warnr(   r   )r   r   r   r   r   test_formatLevel0  s    z*ClassicLogFormattingTests.test_formatLevelc             C   s    t ddd}| jt|d dS )z)
        System is second field.
        ra   zS.Y.S.T.E.M.)r   
log_systemz- [S.Y.S.T.E.M.] XYZZY
N)r/   r(   r   )r   r   r   r   r   test_formatSystem;  s    z+ClassicLogFormattingTests.test_formatSystemc             C   s&   t ddtjdd}| jt|d dS )zB
        System is not supplanted by namespace and level.
        ra   zmy.namespacezS.Y.S.T.E.M.)r   re   rg   rj   z- [S.Y.S.T.E.M.] XYZZY
N)r/   r   rh   r(   r   )r   r   r   r   r   test_formatSystemRulzF  s    z/ClassicLogFormattingTests.test_formatSystemRulzc             C   s"   t dt d}| jt|d dS )zB
        System is not supplanted by namespace and level.
        ra   )r   rj   z- [UNFORMATTABLE] XYZZY
N)r/   r6   r(   r   )r   r   r   r   r   test_formatSystemUnformattableV  s    z8ClassicLogFormattingTests.test_formatSystemUnformattablec             C   s    t ddd}| jt|d dS )z0
        Formatted event is last field.
        zid:{id}123)r   idz- [-#-] id:123
N)r/   r(   r   )r   r   r   r   r   test_formatFormata  s    z+ClassicLogFormattingTests.test_formatFormatc             C   s   t dd}| jt|d dS )z#
        No format string.
        rn   )ro   N)r/   r   r   )r   r   r   r   r   test_formatNoFormatl  s    
z-ClassicLogFormattingTests.test_formatNoFormatc             C   s    t ddd}| jt|d dS )z&
        Empty format string.
        r   rn   )r   ro   N)r/   r   r   )r   r   r   r   r   test_formatEmptyFormatw  s    z0ClassicLogFormattingTests.test_formatEmptyFormatc             C   s   t dd}| j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"
N)r/   r(   r   )r   r   r   r   r   test_formatFormatMultiLine  s    
z4ClassicLogFormattingTests.test_formatFormatMultiLineN)r<   r=   r>   r?   rc   rd   rf   ri   rk   rl   rm   rp   rq   rr   rs   r   r   r   r   r^      s   r^   c               @   s   e Zd ZdZdd ZdS )FormatFieldTestsz+
    Tests for format field functions.
    c             C   s>   | j tdtddd dd | j 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#     s    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#     s    )r*   zHello, 'repr'.N)r(   r   r/   )r   r   r   r   test_formatWithCall  s    z$FormatFieldTests.test_formatWithCallN)r<   r=   r>   r?   rv   r   r   r   r   rt     s   rt   c               @   s   e Zd ZdZdd ZdS )r6   z>
    An object that raises an exception from C{__repr__}.
    c             C   s   t dd S )Nr,   r   )str)r   r   r   r   __repr__  s    zUnformattable.__repr__N)r<   r=   r>   r?   rx   r   r   r   r   r6     s   r6   c               @   s   e Zd ZdZdS )CapturedErrorz3
    A captured error for use in format tests.
    N)r<   r=   r>   r?   r   r   r   r   ry     s   ry   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   yt dW n t k
r&   t }Y nX ddi}||d< t|ddd}| jt|j | | jd| dS )	zT
        An event with a C{log_failure} key will have a traceback appended.
        zThis is a fake errorr   zThis is a test log messagelog_failureTF)includeTimestampincludeSystemN)ry   r   r   r)   r	   getTraceback)r   fr   	eventTextr   r   r   test_eventWithTraceback  s    z(EventAsTextTests.test_eventWithTracebackc             C   sj   yt dW n t k
r&   t }Y nX ddi}||d< t|ddd}| jt|j | | jd| dS )	zu
        An event with an empty C{log_format} key appends a traceback from
        the accompanying failure.
        zThis is a fake errorr   r   r{   TF)r|   r}   N)ry   r   r   r)   r	   r~   )r   r   r   r   r   r   r   "test_formatEmptyEventWithTraceback  s    z3EventAsTextTests.test_formatEmptyEventWithTracebackc             C   s|   yt dW n t k
r&   t }Y nX ddd d}||d< t|ddd	}| j|t | jt|j | | jd| d
S )zw
        An event with an unformattable value in the C{log_format} key still
        has a traceback appended.
        zThis is a fake errorz{evil()}c               S   s   dd S )Nr,   r   r   r   r   r   r   r#     s    zHEventAsTextTests.test_formatUnformattableWithTraceback.<locals>.<lambda>)r   r3   r{   TF)r|   r}   N)ry   r   r   assertIsInstancer	   r)   r~   )r   r   r   r   r   r   r   %test_formatUnformattableWithTraceback  s    z6EventAsTextTests.test_formatUnformattableWithTracebackc             C   s   yt dW n t k
r&   t }Y nX ddddd t di}||d< t|d	d
d}| j|t | jd| | jt|j | | j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.
        zThis is a fake errorr   z{evil()}r3   c               S   s   dd S )Nr,   r   r   r   r   r   r   r#     s    zMEventAsTextTests.test_formatUnformattableErrorWithTraceback.<locals>.<lambda>r5   r{   TF)r|   r}   zMESSAGE LOSTN)ry   r   r6   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}| j|t | jd| dS )	zk
        If a traceback cannot be appended, a message indicating this is true
        is appended.
        r   r   r{   TF)r|   r}   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}| j|t | jd| dS )zY
        An event with no C{log_failure} key will not have a traceback appended.
        r   zThis is a test log messageTF)r|   r}   N)r   r   r	   r)   )r   r   r   r   r   r   test_formatEventNonCritical5  s    z,EventAsTextTests.test_formatEventNonCriticalc             C   sb   yt dW n t k
r&   t }Y nX ddi}||d< t|ddd}| jd| | jd| d	S )
zU
        An exception message with multibyte characters is properly handled.
        u   €r   zThis is a test log messager{   TF)r|   r}   	TracebackN)ry   r   r   r)   )r   r   r   r   r   r   r   test_formatTracebackMultibyteE  s    z.EventAsTextTests.test_formatTracebackMultibytec             C   st   yt dW n t k
r&   t }Y nX ddi}||d< t|ddd}| jd| tsd| jd	| n| jd
| dS )zj
        An error raised attempting to decode the UTF still produces a
        valid log message.
        s
   t e s t r   zThis is a test log messager{   TF)r|   r}   r   u   ��t e s t z.CapturedError(b'\xff\xfet\x00e\x00s\x00t\x00')N)ry   r   r   r)   r   )r   r   r   r   r   r   r   ,test_formatTracebackHandlesUTF8DecodeFailure[  s     z=EventAsTextTests.test_formatTracebackHandlesUTF8DecodeFailurec             C   sd   yt dW n t k
r&   t }Y nX td}d
d|d}||d< t|dddd}| j|d dS )zq
        If includeSystem is specified as the only option no timestamp or
        traceback are printed.
        zThis is a fake error  rQ   rR   rS   rT   rU   r,     ABCDfake_system)r   rj   rb   r{   FT)r|   includeTracebackr}   z[fake_system] ABCDN)	r   rQ   rR   rS   rT   rU   r,   r   r,   )ry   r   r   r   r(   )r   r   rX   r   r   r   r   r   test_eventAsTextSystemOnlyy  s"    z+EventAsTextTests.test_eventAsTextSystemOnlyc             C   s   t dkrtdt|  td ytdW n tk
rF   t }Y nX td}dd|d}||d< t|dddd}| j|d dS )zq
        If includeTimestamp is specified as the only option no system or
        traceback are printed.
        Nz:Platform cannot change timezone; unable to verify offsets.zUTC+00zThis is a fake error  rQ   rR   rS   rT   rU   r,     r   r   )r   rj   rb   r{   TF)r|   r   r}   z2013-09-24T11:40:47+0000 ABCD)	r   rQ   rR   rS   rT   rU   r,   r   r,   )	r   r   r   r   ry   r   r   r   r(   )r   r   rX   r   r   r   r   r   test_eventAsTextTimestampOnly  s,    z.EventAsTextTests.test_eventAsTextTimestampOnlyc             C   sb   yt dW n t k
r&   t }Y nX td}d
|d}||d< t|dddd}| j|d dS )z\
        If includeSystem is specified with a missing system [-#-]
        is used.
        zThis is a fake error  rQ   rR   rS   rT   rU   r,     r   )r   rb   r{   FT)r|   r   r}   z
[-#-] ABCDN)	r   rQ   rR   rS   rT   rU   r,   r   r,   )ry   r   r   r   r(   )r   r   rX   r   r   r   r   r   test_eventAsTextSystemMissing  s     z.EventAsTextTests.test_eventAsTextSystemMissingc             C   sh   yt dW n t k
r&   t }Y nX td}d
|tjdd}||d< t|dddd}| j|d dS )z
        If includeSystem is specified with a missing system but
        namespace and level are present they are used.
        zThis is a fake error  rQ   rR   rS   rT   rU   r,     r   test)r   rb   rg   re   r{   FT)r|   r   r}   z[test#info] ABCDN)	r   rQ   rR   rS   rT   rU   r,   r   r,   )ry   r   r   r   infor   r(   )r   r   rX   r   r   r   r   r   .test_eventAsTextSystemMissingNamespaceAndLevel  s$    z?EventAsTextTests.test_eventAsTextSystemMissingNamespaceAndLevelc             C   sf   yt dW n t k
r&   t }Y nX td}d
|tjd}||d< t|dddd}| j|d dS )zv
        If includeSystem is specified with a missing system but
        level is present, level is included.
        zThis is a fake error  rQ   rR   rS   rT   rU   r,     r   )r   rb   rg   r{   FT)r|   r   r}   z[-#info] ABCDN)	r   rQ   rR   rS   rT   rU   r,   r   r,   )ry   r   r   r   r   r   r(   )r   r   rX   r   r   r   r   r   &test_eventAsTextSystemMissingLevelOnly  s"    
z7EventAsTextTests.test_eventAsTextSystemMissingLevelOnlyN)r<   r=   r>   r?   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rz     s   %rz   )$r?   Z!twisted.python.test.test_tzhelperr   r   r   timer   ImportErrorZtwisted.trialr   Ztwisted.trial.unittestr   Ztwisted.python.compatr   r	   Z_levelsr   _formatr   r   r   r   r   r   Ztwisted.python.failurer   ZTestCaser   r@   r^   rt   r1   r6   	Exceptionry   rz   r   r   r   r   <module>   s*   
  Z 
