a
    )(bU                     @   s0  d Z ddlmZmZmZ zddlmZ e W n eyB   dZY n0 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                   @   sr   e Zd ZdZddddZddddZdddd	Zddd
dZddddZddddZ	ddddZ
dS )FormattingTestsz5
    Tests for basic event formatting functions.
    Nreturnc                    s   t ttd 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 >lib/python3.9/site-packages/twisted/logger/test/test_format.pyformat9   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>D   r%   z2FormattingTests.test_formatEvent.<locals>.<lambda>)Z
not_calledZcalledu   Sánchezs   SánchezUnable to format events   Snchezs   S{a!s}nchez   )azSb'\xe1'nchezSZnchezs   S{a!r}nchezN)r   objectr   assertEqualassertInrepr)r    r#   Z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_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.
        r2   r   )r   r3   r4   zLog format must be strN)r5   r-   r   r/   r0   r6   r!   r!   r"   test_formatEventWeirdFormatW   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 Nr2   r   r!   r!   r!   r!   r"   r(   e   r%   z?FormattingTests.test_formatUnformattableEvent.<locals>.<lambda>r   evilr)   N)r5   r   r/   r0   r6   r!   r!   r"   test_formatUnformattableEventa   s    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   r9   r<   c                   S   s   dd S r:   r!   r!   r!   r!   r"   r(   q   r%   zSFormattingTests.test_formatUnformattableEventWithUnformattableKey.<locals>.<lambda>gurk*MESSAGE LOST: unformattable object logged:Recoverable data:Exception during formatting:N)r   r   Unformattabler   r/   r6   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.
        r9   c                   S   s   dd S r:   r!   r!   r!   r!   r"   r(      r%   zUFormattingTests.test_formatUnformattableEventWithUnformattableValue.<locals>.<lambda>)r   r<   r>   r?   r@   rA   N)r5   rB   r   r/   r6   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 )
rD   r9   c                   S   s   dd S r:   r!   r!   r!   r!   r"   r(      r%   zbFormattingTests.test_formatUnformattableEventWithUnformattableErrorOMGWillItStop.<locals>.<lambda>Zokay)r   r<   recoverabler?   rF   z = N)r5   r   r   BaseExceptionrB   r/   r0   r6   r!   r!   r"   @test_formatUnformattableEventWithUnformattableErrorOMGWillItStop   s    zPFormattingTests.test_formatUnformattableEventWithUnformattableErrorOMGWillItStop)__name__
__module____qualname____doc__r1   r7   r8   r=   rC   rE   rH   r!   r!   r!   r"   r   "   s   '	

r   c                   @   sd   e Zd ZdZddddZddddZdddd	Zddd
dZddddZddddZ	dS )TimeFormattingTestsz.
    Tests for time formatting functions.
    Nr   c                 C   s   t |  d S )N)r   r   r!   r!   r"   setUp   s    zTimeFormattingTests.setUpc                    s\   t du rtdtttd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.)nameexpectedDSTexpectedSTDr   c                    s<   t |  td}td} t||  t|| d S )N)	i        r   r   r         r2   )	i  r2      r   r   r   r   rW   r   )r	   r   r.   r   )rP   rQ   rR   Z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   )r    rX   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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  	         (   /   r2   i  r2   N
timeFormatr[   r\   )rg   r]   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.
        r`   z%Y/%Wrf   z2013/38Nrh   ri   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%frf   Z234560Nr^   r   r!   r!   r"   test_formatTimePercentF   s    z+TimeFormattingTests.test_formatTimePercentF)
rI   rJ   rK   rL   rN   rZ   r_   rk   rl   rm   r!   r!   r!   r"   rM      s   -rM   c                   @   s   e Zd ZdZddddZddddZdddd	Zddd
dZddddZddddZ	ddddZ
ddddZddddZddddZddddZdS )ClassicLogFormattingTestsz@
    Tests for classic text log event formatting functions.
    Nr   c                 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.
        NrO   rY   r`   XYZZYr   log_timez%2013-09-24T11:40:47+0000 [-#-] XYZZY
)r   r   r   r	   r   r5   r.   r   )r    rj   r   r!   r!   r"   test_formatTimeDefault   s    z0ClassicLogFormattingTests.test_formatTimeDefaultc                 C   s8   t t tddd}tddd}| t||dd d	S )
zc
        Time is first field.  Custom formatting function is an optional
        argument.
        )rj   r   c                 S   s   d|  dS )N__r!   )rj   r!   r!   r"   r     s    zCClassicLogFormattingTests.test_formatTimeCustom.<locals>.formatTimero   i90  rp   )r   z__12345__ [-#-] XYZZY
N)r   floatr   r5   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.
        ro   my.namespace)r   log_namespacez- [my.namespace#-] XYZZY
Nr5   r.   r   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.
        ro   )r   	log_levelz- [-#warn] XYZZY
Nr5   r   warnr.   r   ry   r!   r!   r"   test_formatLevel  s
    z*ClassicLogFormattingTests.test_formatLevelc                 C   s    t ddd}| t|d dS )z)
        System is second field.
        ro   S.Y.S.T.E.M.r   
log_system- [S.Y.S.T.E.M.] XYZZY
Nrx   ry   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.
        ro   rv   r   )r   rw   r{   r   r   Nr|   ry   r!   r!   r"   test_formatSystemRulz-  s    z/ClassicLogFormattingTests.test_formatSystemRulzc                 C   s"   t dt d}| t|d dS )r   ro   r   z- [UNFORMATTABLE] XYZZY
N)r5   rB   r.   r   ry   r!   r!   r"   test_formatSystemUnformattable<  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
Nrx   ry   r!   r!   r"   test_formatFormatF  s
    z+ClassicLogFormattingTests.test_formatFormatc                 C   s   t dd}| t|d dS )z#
        No format string.
        r   )r   Nr5   r   r   ry   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   ry   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"
Nrx   ry   r!   r!   r"   test_formatFormatMultiLine^  s
    
z4ClassicLogFormattingTests.test_formatFormatMultiLine)rI   rJ   rK   rL   rr   ru   rz   r~   r   r   r   r   r   r   r   r!   r!   r!   r"   rn      s   




rn   c                   @   s   e Zd ZdZddddZdS )FormatFieldTestsz+
    Tests for format field functions.
    Nr   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{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()}.Zearthc                   S   s   dS )Nmayber!   r!   r!   r!   r"   r(   x  r%   z6FormatFieldTests.test_formatWithCall.<locals>.<lambda>)ZworldZcallmezHello, earth. maybe.zHello, {repr()!r}.c                   S   s   dS )Nr0   r!   r!   r!   r!   r"   r(   }  r%   )r0   zHello, 'repr'.N)r.   r   r5   r   r!   r!   r"   test_formatWithCalln  s    z$FormatFieldTests.test_formatWithCall)rI   rJ   rK   rL   r   r!   r!   r!   r"   r   i  s   r   c                   @   s   e Zd ZdZedddZdS )rB   z>
    An object that raises an exception from C{__repr__}.
    r   c                 C   s   t dd S r:   )r   r   r!   r!   r"   __repr__  s    zUnformattable.__repr__N)rI   rJ   rK   rL   r   r   r!   r!   r!   r"   rB     s   rB   c                   @   s   e Zd ZdZdS )CapturedErrorz3
    A captured error for use in format tests.
    N)rI   rJ   rK   rL   r!   r!   r!   r"   r     s   r   c                   @   s   e Zd ZdZddddZddddZdddd	Zddd
dZddddZddddZ	ddddZ
ddddZddddZddddZddddZddddZddddZdS )EventAsTextTestszi
    Tests for L{eventAsText}, all of which ensure that the
    returned type is UTF-8 decoded text.
    Nr   c                 C   sh   zt dW n t y$   t }Y n0 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   sh   zt dW n t y$   t }Y n0 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   sz   zt dW n t y$   t }Y n0 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   r9   c                   S   s   dd S r:   r!   r!   r!   r!   r"   r(     r%   zHEventAsTextTests.test_formatUnformattableWithTraceback.<locals>.<lambda>r;   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 y$   t }Y n0 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   r9   r<   c                   S   s   dd S r:   r!   r!   r!   r!   r"   r(     r%   zMEventAsTextTests.test_formatUnformattableErrorWithTraceback.<locals>.<lambda>r>   r   TFr   zMESSAGE LOSTN)	r   r   r   r   rB   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)r-   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_formatEventNonCritical  s    z,EventAsTextTests.test_formatEventNonCriticalc                 C   s`   zt dW n t y$   t }Y n0 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_formatTracebackMultibyte  s    z.EventAsTextTests.test_formatTracebackMultibytec                 C   s`   zt dW n t y$   t }Y n0 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   sb   zt dW n t y$   t }Y n0 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   r`   ABCDfake_systemr   r   rq   r   FTr   ZincludeTracebackr   z[fake_system] ABCDNr   r   r   r   r.   r    r   rj   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W n tyD   t }Y n0 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.
        NrO   rY   r   r`   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_eventAsTextTimestampOnly0  s0    z.EventAsTextTests.test_eventAsTextTimestampOnlyc                 C   s`   zt dW n t y$   t }Y n0 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   r`   r   rp   r   FTr   z
[-#-] ABCDNr   r   r!   r!   r"   test_eventAsTextSystemMissingR  s&    z.EventAsTextTests.test_eventAsTextSystemMissingc                 C   sf   zt dW n t y$   t }Y n0 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   r`   r   test)r   rq   r{   rw   r   FTr   z[test#info] ABCDNr   r   r   r   infor   r.   r   r!   r!   r"   .test_eventAsTextSystemMissingNamespaceAndLevelm  s*    z?EventAsTextTests.test_eventAsTextSystemMissingNamespaceAndLevelc                 C   sd   zt dW n t y$   t }Y n0 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   r`   r   )r   rq   r{   r   FTr   z[-#info] ABCDNr   r   r!   r!   r"   &test_eventAsTextSystemMissingLevelOnly  s(    z7EventAsTextTests.test_eventAsTextSystemMissingLevelOnly)rI   rJ   rK   rL   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r!   r!   r"   r     s   	"r   )&rL   typingr   r   r   timer   ImportErrorZtwisted.python.failurer   Z!twisted.python.test.test_tzhelperr   r   r	   Ztwisted.trialr
   Ztwisted.trial.unittestr   Z_formatr   r   r   r   r   r   Z_interfacesr   Z_levelsr   ZTestCaser   rM   rn   r   rB   	Exceptionr   r   r!   r!   r!   r"   <module>   s*   
 tR 	