U
    
W[                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
m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._logger}.
    )unittest   )InvalidLogLevelError)LogLevel)formatEventLogger)globalLogPublisherc                   @   s   e Zd ZdZdddZdS )
TestLoggerz^
    L{Logger} with an overridden C{emit} method that keeps track of received
    events.
    Nc              	      sL    fdd}t | ztj ||f| W 5 t | X |||d _d S )Nc                    s
   |  _ d S Neventr   self A/usr/lib/python3/dist-packages/twisted/logger/test/test_logger.pyobserver   s    z!TestLogger.emit.<locals>.observer)levelformatkwargs)r	   ZaddObserverZremoveObserverr   emitemitted)r   r   r   r   r   r   r   r   r      s    
zTestLogger.emit)N)__name__
__module____qualname____doc__r   r   r   r   r   r
      s   r
   c                   @   s(   e Zd ZdZe ZdddZdd ZdS )LogComposedObjectz3
    A regular object, with a logger attached.
    Nc                 C   s
   || _ d S r   state)r   r   r   r   r   __init__0   s    zLogComposedObject.__init__c                 C   s   dj | jdS )Nz<LogComposedObject {state}>r   )r   r   r   r   r   r   __str__4   s    zLogComposedObject.__str__)N)r   r   r   r   r
   logr   r    r   r   r   r   r   *   s   
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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 )LoggerTestsz
    Tests for L{Logger}.
    c                 C   s*   d}t |}| t|dt| dS )z"
        repr() on Logger
        Zblearghz<Logger {0}>N)r   assertEqualreprr   )r   	namespacer!   r   r   r   	test_repr>   s    zLoggerTests.test_reprc                 C   s   t  }| |jt dS )z3
        Default namespace is module name.
        N)r   r#   r%   r   r   r!   r   r   r   test_namespaceDefaultG   s    z!LoggerTests.test_namespaceDefaultc                 C   s.   g }t dttdt  | |d jd dS )z
        Default namespace is C{"<unknown>"} when a logger is created from a
        context in which is can't be determined automatically and no namespace
        was specified.
        zresult.append(Logger())r   r   z	<unknown>N)execdictr   localsr#   r%   )r   resultr   r   r   test_namespaceOMGItsTooHardO   s     z'LoggerTests.test_namespaceOMGItsTooHardc                 C   sj   t  }d|j|jj}| |jj| | t jj| | t jj	t  | |jj	| | 
t j	 dS )z
        Default namespace for classes using L{Logger} as a descriptor is the
        class name they were retrieved from.
        z{0}.{1}N)r   r   r   	__class__r   r#   r!   r%   ZassertIssourceassertIsNoner   )r   objZexpectedNamespacer   r   r   test_namespaceAttribute]   s    z#LoggerTests.test_namespaceAttributec                    sL   g  G  fdddt }|jd | t d |  d d d dS )zH
        When used as a descriptor, the observer is propagated.
        c                       s   e Zd Ze jdZdS )z5LoggerTests.test_descriptorObserver.<locals>.MyObjectr   N)r   r   r   r   appendr!   r   Zobservedr   r   MyObjectt   s   r6   hello   r   
log_formatN)objectr!   infor#   len)r   r6   r   r5   r   test_descriptorObservern   s
    z#LoggerTests.test_descriptorObserverc                 C   sR   t d}|j}|d | d|j | |jd | t|j}| d| dS )z
        On instances that have a L{Logger} class attribute, the C{log_source}
        key is available to format strings.
        r7   zHello, {log_source}.
log_sourcez!Hello, <LogComposedObject hello>.N)r   r!   errorZassertInr   r#   r   )r   r1   r!   Zstuffr   r   r   !test_sourceAvailableForFormatting|   s    

z-LoggerTests.test_sourceAvailableForFormattingc                 C   s   t  }t D ]}d}|j|jd}t||j}||||jd | |jd | | |jd | | |jd d | | t	|dd	 | |j
d
 | | |j
d | | |j
d t | |j
d  | |j
d | | t|j
| qdS )z]
        Test that log levels and messages are emitted correctly for
        Logger.
        zThis is a {level_name} message)
level_name)junkrA   r   r   r   rB   r   zNo event observed.r9   	log_levellog_namespacer>   N)r
   r   Ziterconstantsr   namegetattrr#   r   Z
assertTruehasattrr   r   r0   r   )r   r!   r   r   messageZ	logMethodr   r   r   test_basicLogger   s     zLoggerTests.test_basicLoggerc                    s0    fddG fdddt   j  dS )z>
        C{log_source} event key refers to the class.
        c                    s    | d   d S Nr>   r#   r   )Thingor   r   r   r      s    z0LoggerTests.test_sourceOnClass.<locals>.observerc                       s   e Zd Ze dZdS )z.LoggerTests.test_sourceOnClass.<locals>.Thingor3   Nr   r   r   r
   r!   r   r3   r   r   rL      s   rL   Nr:   r!   r;   r   r   )rL   r   r   r   test_sourceOnClass   s    zLoggerTests.test_sourceOnClassc                    s6   fdd G  fdddt }| j  dS )zA
        C{log_source} event key refers to the instance.
        c                    s     | d  d S rJ   rK   r   )r   thingor   r   r      s    z3LoggerTests.test_sourceOnInstance.<locals>.observerc                       s   e Zd Ze dZdS )z1LoggerTests.test_sourceOnInstance.<locals>.Thingor3   NrM   r   r3   r   r   rL      s   rL   NrN   )r   rL   r   )r   r   rP   r   test_sourceOnInstance   s    z!LoggerTests.test_sourceOnInstancec                    s"    fdd}t |d}|  dS )z5
        C{log_source} event key is L{None}.
        c                    s     | d  d S rJ   )r0   r   r   r   r   r      s    z0LoggerTests.test_sourceUnbound.<locals>.observerr3   Nr
   r;   )r   r   r!   r   r   r   test_sourceUnbound   s    
zLoggerTests.test_sourceUnboundc                 C   sv   t  }ztdW n tk
r0   |d Y nX | t}| t|d | |jd tj | |jd d dS )z?
        Test that log.failure() emits the right data.
        zbaloney!ZWhoopsr8   r   r   N)	r
   RuntimeErrorZfailureflushLoggedErrorsr#   r<   r   r   Zcriticalr   r!   errorsr   r   r   test_defaultFailure   s    
zLoggerTests.test_defaultFailurec                 C   sj   t  }|jddtjddd | |jd d | |jd tj | |jd |j | |jd	  d
S )zQ
        Make sure that kwargs conflicting with args don't pass through.
        *#z*namespace*z*source*)r9   rC   rD   r>   r9   rC   rD   r>   N)r
   warnr   r?   r#   r   r%   r0   r'   r   r   r   test_conflictingKwargs   s    z"LoggerTests.test_conflictingKwargsc                 C   s.   t  }|d | t}| t|d dS )zA
        Test passing in a bogus log level to C{emit()}.
        z*bogus*r8   N)r
   r   rU   r   r#   r<   rV   r   r   r   test_logInvalidLogLevel   s    

z#LoggerTests.test_logInvalidLogLevelc                    s8   fdd fddt d  jdg d dS )	zE
        Tracing keeps track of forwarding to the publisher.
        c                    s    |  d S r   r   r   r3   r   r   	publisher  s    z)LoggerTests.test_trace.<locals>.publisherc                    s    | d  fg d S )N	log_tracerK   r   )r!   r^   r   r   r   r     s    z(LoggerTests.test_trace.<locals>.observerr3   zHello.)r_   NrR   r   r   )r!   r   r^   r   r   
test_trace  s    
zLoggerTests.test_traceN)r   r   r   r   r&   r(   r-   r2   r=   r@   rI   rO   rQ   rS   rX   r\   r]   r`   r   r   r   r   r"   9   s   	r"   N)r   Ztwisted.trialr   Z_levelsr   r   Z_formatr   Z_loggerr   Z_globalr	   r
   r:   r   ZTestCaser"   r   r   r   r   <module>   s   