U
    
W[#                     @   s   d Z ddlmZ ddl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mZmZ G d	d
 d
ejZdS )z+
Test cases for L{twisted.logger._format}.
    )countN)tzset)unittest   )formatEvent)flattenEventextractFieldKeyFlattener
aFormatterc                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 ZdddZdd Z	dd Z
dd Zdd fddZdd Zdd Zdd Zdd Zd
S ) FlatFormattingTestsz9
    Tests for flattened event formatting functions.
    c                    st   t   G dd dt}td fdd| dddd	}t| t|}|d
= |d= tt|}| t|d dS )a)  
        L{flattenEvent} will "flatten" an event so that, if scrubbed of all but
        serializable objects, it will preserve all necessary data to be
        formatted once serialized.  When presented with an event thusly
        flattened, L{formatEvent} will produce the same output.
        c                   @   s   e Zd ZdZdS )z;FlatFormattingTests.test_formatFlatEvent.<locals>.EphemeralvalueN)__name__
__module____qualname__Z	attribute r   r   B/usr/lib/python3/dist-packages/twisted/logger/test/test_flatten.py	Ephemeral*   s   r   zcallable: {callme()} attribute: {object.attribute} numrepr: {number!r} numstr: {number!s} strrepr: {string!r} unistr: {unistr!s}c                      s   t  S N)nextr   Zcounterr   r   <lambda>6       z:FlatFormattingTests.test_formatFlatEvent.<locals>.<lambda>   hello   ö)
log_formatcallmeobjectnumberstringZunistrr   r   uM   callable: 0 attribute: value numrepr: 7 numstr: 7 strrepr: 'hello' unistr: öN)	r   r   dictr   jsonloadsdumpsassertEqualr   )selfr   event1event2Zevent3r   r   r   test_formatFlatEvent!   s&    
   z(FlatFormattingTests.test_formatFlatEventc                 C   s<   t ddd}t| tt|}| t|d dS )zH
        If the format string is invalid, an error is produced.
        zstrrepr: {string!X}r   )r   r   zUnable to format eventN)r    r   r!   r"   r#   Z
assertTruer   
startswith)r%   r&   r'   r   r   r   test_formatFlatEventBadFormatM   s    z1FlatFormattingTests.test_formatFlatEventBadFormatc                 C   sF   G dd dt }| }td|d}t| |  | t|d dS )z
        L{formatEvent} will prefer the stored C{str()} or C{repr()} value for
        an object, in case the other version.
        c                   @   s$   e Zd ZdZdZdd Zdd ZdS )zPFlatFormattingTests.test_formatFlatEventWithMutatedFields.<locals>.Unpersistablez.
            Unpersitable object.
            Fc                 S   s
   d| _ dS )z0
                Self destruct.
                TN
destructedr%   r   r   r   selfDestructk   s    z]FlatFormattingTests.test_formatFlatEventWithMutatedFields.<locals>.Unpersistable.selfDestructc                 S   s   | j r
dS dS d S )Nzpost-serialization garbagezun-persistabler+   r-   r   r   r   __repr__q   s    zYFlatFormattingTests.test_formatFlatEventWithMutatedFields.<locals>.Unpersistable.__repr__N)r   r   r   __doc__r,   r.   r/   r   r   r   r   Unpersistablee   s   r1   zunpersistable: {unpersistable})r   Zunpersistablezunpersistable: un-persistableN)r   r    r   r.   r$   r   )r%   r1   Zupr&   r   r   r   %test_formatFlatEventWithMutatedFields`   s     z9FlatFormattingTests.test_formatFlatEventWithMutatedFieldsc                 C   s
  dd }z|  |dd W n tk
r2    Y nX |  |dd |  |dd |  |d	d
 |  |dd |  |dd |  |dd |  |dd |  |dd |  |dd td\}|dd }t }|  |j| d |  |j| d dS )zh
        Test that L{KeyFlattener.flatKey} returns the expected keys for format
        fields.
        c                 S   s.   t | D ]\}}}}t |||  S d S r   )r
   parser	   flatKey)formatZliteralTextZ	fieldNameZ
formatSpecZ
conversionr   r   r   keyFromFormat   s      z=FlatFormattingTests.test_keyFlattening.<locals>.keyFromFormatz{}z!:z{foo}zfoo!:z{foo!s}zfoo!s:z{foo!r}zfoo!r:z{foo:%s}zfoo!:%sz{foo:!}zfoo!:!z{foo::}zfoo!::z
{foo!s:%s}zfoo!s:%sz	{foo!s:!}zfoo!s:!z	{foo!s::}zfoo!s::z{x}   Nzx!:zx!:/2)r$   
ValueErrorr
   r3   r	   r4   )r%   r6   ZkeyPlusLiteralkeyZsameFlattenerr   r   r   test_keyFlattening   s&    z&FlatFormattingTests.test_keyFlatteningNc                    sL   |dkr0t   G  fdddt}td| d}t| | t|d |S )z
        The same format field used twice in one event is rendered twice.

        @param event: An event to flatten.  If L{None}, create a new event.
        @return: C{event} or the event created.
        Nc                       s   e Zd ZdZ fddZdS )zJFlatFormattingTests._test_formatFlatEvent_fieldNamesSame.<locals>.CountStrz&
                Hack
                c                    s   t t S r   )strr   r-   r   r   r   __str__   s    zRFlatFormattingTests._test_formatFlatEvent_fieldNamesSame.<locals>.CountStr.__str__N)r   r   r   r0   r<   r   r   r   r   CountStr   s   r=   z{x} {x}r   xz0 1)r   r   r    r   r$   r   )r%   eventr=   r   r   r   $_test_formatFlatEvent_fieldNamesSame   s    z8FlatFormattingTests._test_formatFlatEvent_fieldNamesSamec                 C   s   |    dS )zR
        The same format field used twice in one event is rendered twice.
        NrA   r-   r   r   r   "test_formatFlatEventFieldNamesSame   s    z6FlatFormattingTests.test_formatFlatEventFieldNamesSamec                 C   s   |   }|  | dS )zb
        The same event flattened twice gives the same (already rendered)
        result.
        NrB   )r%   r@   r   r   r   'test_formatFlatEventFieldNamesSameAgain   s    z;FlatFormattingTests.test_formatFlatEventFieldNamesSameAgainc                 C   s,   t ddd}t| t|}| |d dS )zs
        L{formatEvent} will handle a flattened event with tailing text after
        a replacement field.
        ztest {x} trailingr   r>   ztest value trailingN)r    r   r   r$   )r%   r@   resultr   r   r    test_formatEventFlatTrailingText   s    z4FlatFormattingTests.test_formatEventFlatTrailingTextc                 C   s   | S r   r   )r?   r   r   r   r      r   zFlatFormattingTests.<lambda>c                    sz   G dd dt  G  fdddt }td| d}||fdd}| |d	d
 | |dd | |dd dS )z
        L{extractField} will extract a field used in the format string.

        @param flattenFirst: callable to flatten an event
        c                   @   s   e Zd Zdd ZdS )z=FlatFormattingTests.test_extractField.<locals>.ObjectWithReprc                 S   s   dS )Nreprr   r-   r   r   r   r/      s    zFFlatFormattingTests.test_extractField.<locals>.ObjectWithRepr.__repr__N)r   r   r   r/   r   r   r   r   ObjectWithRepr   s   rH   c                       s    e Zd Z fddZdd ZdS )z8FlatFormattingTests.test_extractField.<locals>.Somethingc                    s   d| _   | _d S )Nr   )r   r   r-   rH   r   r   __init__   s    zAFlatFormattingTests.test_extractField.<locals>.Something.__init__c                 S   s   t dd S )NzJust in case.)NotImplementedErrorr-   r   r   r   __getstate__   s    zEFlatFormattingTests.test_extractField.<locals>.Something.__getstate__N)r   r   r   rJ   rL   r   rI   r   r   	Something   s   rM   z%{something.number} {something.object})r   Z	somethingc                    s
   t |  S r   )r   )Zfield)	flattenedr   r   extract  s    z6FlatFormattingTests.test_extractField.<locals>.extractzsomething.numberr   zsomething.number!s7zsomething.object!srG   N)r   r    r$   )r%   ZflattenFirstrM   r@   rO   r   )rH   rN   r   test_extractField   s    z%FlatFormattingTests.test_extractFieldc                 C   s   dd }|  | dS )zi
        L{extractField} behaves identically if the event is explicitly
        flattened first.
        c                 S   s   t |  | S r   )r   )Zevtr   r   r   rN     s    zDFlatFormattingTests.test_extractFieldFlattenFirst.<locals>.flattenedN)rQ   )r%   rN   r   r   r   test_extractFieldFlattenFirst  s    z1FlatFormattingTests.test_extractFieldFlattenFirstc                 C   s(   ddd}t | | |ddd dS )zT
        L{flattenEvent} will do nothing to an event with no format string.
        br7   )acNr   r$   r%   Z
inputEventr   r   r   test_flattenEventWithoutFormat  s    
z2FlatFormattingTests.test_flattenEventWithoutFormatc                 C   s,   dddd}t | | |dddd dS )zz
        L{flattenEvent} will do nothing to an event with a format string that
        contains no format fields.
        rS   r7   zsimple messagerT   rU   r   NrV   rW   r   r   r    test_flattenEventWithInertFormat$  s    z4FlatFormattingTests.test_flattenEventWithInertFormatc                 C   s,   dddd}t | | |dddd dS )zb
        L{flattenEvent} will do nothing to an event with log_format set to
        None.
        rS   r7   NrY   rV   rW   r   r   r   test_flattenEventWithNoneFormat5  s    z3FlatFormattingTests.test_flattenEventWithNoneFormat)N)r   r   r   r0   r(   r*   r2   r:   rA   rC   rD   rF   rQ   rR   rX   rZ   r[   r   r   r   r   r      s   ,"1
	!	r   )r0   	itertoolsr   r!   timer   ImportErrorZtwisted.trialr   Z_formatr   Z_flattenr   r   r	   r
   ZTestCaser   r   r   r   r   <module>   s   
