o
    b                       @   s   d Z ddlmZmZmZ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 dd	lmZmZ dd
lmZmZ ddlmZ G dd deZG dd dZG dd dejZdS )z+
Test cases for L{twisted.logger._logger}.
    )ListOptionalTypecast)implementer)NamedConstant)unittest   )formatEvent)globalLogPublisher)ILogObserverLogEvent)InvalidLogLevelErrorLogLevelLoggerc                	   @   s2   e Zd ZdZ	d	dedee deddfddZdS )

TestLoggerz^
    L{Logger} with an overridden C{emit} method that keeps track of received
    events.
    Nlevelformatkwargsreturnc              	      sl   t tdtdd f fdd}t| ztj ||fi | W t| nt| w |||d _d S )Neventr   c                    s
   |  _ d S Nr   r   self A/usr/lib/python3/dist-packages/twisted/logger/test/test_logger.pyobserver   s   
z!TestLogger.emit.<locals>.observer)r   r   r   )	r   r   r   r   addObserverr   emitremoveObserveremitted)r   r   r   r   r   r   r   r   r       s   
zTestLogger.emitr   )	__name__
__module____qualname____doc__r   r   strobjectr    r   r   r   r   r      s    r   c                   @   s<   e Zd ZdZe Zd	dee ddfddZdefddZ	dS )
LogComposedObjectz3
    A regular object, with a logger attached.
    Nstater   c                 C   s
   || _ d S r   r*   )r   r*   r   r   r   __init__7   s   
zLogComposedObject.__init__c                 C   s   d| j  dS )Nz<LogComposedObject >r+   r   r   r   r   __str__:   s   zLogComposedObject.__str__r   )
r#   r$   r%   r&   r   logr   r'   r,   r.   r   r   r   r   r)   0   s
    r)   c                   @   s   e Zd ZdZd ddZd ddZd dd	Zd d
dZd ddZd ddZ	d ddZ
d ddZd ddZd ddZd ddZd ddZd ddZd ddZdS )!LoggerTestsz
    Tests for L{Logger}.
    r   Nc                 C   s,   d}t |}| t|dt| d dS )z"
        repr() on Logger
        blearghz<Logger r-   N)r   assertEqualrepr)r   	namespacer/   r   r   r   	test_reprC   s    zLoggerTests.test_reprc                 C   s   t  }| |jt dS )z3
        Default namespace is module name.
        N)r   r2   r4   r#   r   r/   r   r   r   test_namespaceDefaultK   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   localsr2   r4   )r   resultr   r   r   test_namespaceOMGItsTooHardR   s   z'LoggerTests.test_namespaceOMGItsTooHardc                 C   s   t  }d|j|jj}| tt|jj	| | tt
t t jj	| | tt
t t jjt  | t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{}.{}N)r)   r   r$   	__class__r#   r2   r   r   r/   r4   r   assertIssourceassertIsNoner   )r   objexpectedNamespacer   r   r   test_namespaceAttribute`   s   z#LoggerTests.test_namespaceAttributec                    sJ   g  G  fddd}|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ee jdZdS )z5LoggerTests.test_descriptorObserver.<locals>.MyObjectr   N)r#   r$   r%   r   r   r   appendr/   r   observedr   r   MyObject|   s    rH   hello   r   
log_formatN)r/   infor2   len)r   rH   r   rF   r   test_descriptorObserverv   s
   z#LoggerTests.test_descriptorObserverc                 C   sX   t d}tt|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.
        rI   zHello, {log_source}.
log_sourcez!Hello, <LogComposedObject hello>.N)	r)   r   r   r/   errorassertInr   r2   r
   )r   rA   r/   stuffr   r   r   !test_sourceAvailableForFormatting   s   

z-LoggerTests.test_sourceAvailableForFormattingc                 C   s   t  }t D ]t}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)junkrT   r   r   r   rU   r   zNo event observed.rK   	log_levellog_namespacerO   N)r   r   iterconstantsr   namegetattrr2   r"   
assertTruehasattrr   r#   r@   r
   )r   r/   r   r   message	logMethodr   r   r   test_basicLogger   s"   zLoggerTests.test_basicLoggerc                    sF   t tdtddf fddG fddd tt j  dS )z>
        C{log_source} event key refers to the class.
        r   r   Nc                    s    | d   d S NrO   r2   r   )Thingor   r   r   r         z0LoggerTests.test_sourceOnClass.<locals>.observerc                          e Zd Ze dZdS )z.LoggerTests.test_sourceOnClass.<locals>.ThingorD   Nr#   r$   r%   r   r/   r   rD   r   r   rb          rb   r   r   r   r   r   r/   rL   r   r   )rb   r   r   r   test_sourceOnClass   s   zLoggerTests.test_sourceOnClassc                    sL   t tdtddffdd G  fddd}| ttj  dS )zA
        C{log_source} event key refers to the instance.
        r   r   Nc                    s     | d  d S r`   ra   r   )r   thingor   r   r      rc   z3LoggerTests.test_sourceOnInstance.<locals>.observerc                       rd   )z1LoggerTests.test_sourceOnInstance.<locals>.ThingorD   Nre   r   rD   r   r   rb      rf   rb   rg   )r   rb   r   )r   r   ri   r   test_sourceOnInstance   s
   z!LoggerTests.test_sourceOnInstancec                    s4   t tdtddf fdd}t|d}|  dS )z5
        C{log_source} event key is L{None}.
        r   r   Nc                    s     | d  d S r`   )r@   r   r   r   r   r      s   z0LoggerTests.test_sourceUnbound.<locals>.observerrD   r   r   r   r   rL   )r   r   r/   r   r   r   test_sourceUnbound   s   
zLoggerTests.test_sourceUnboundc                 C   sp   t  }ztd ty   |d Y nw | t}| t|d | |jd tj | |jd d dS )z?
        Test that log.failure() emits the right data.
        zbaloney!WhoopsrJ   r   r   N)	r   RuntimeErrorfailureflushLoggedErrorsr2   rM   r"   r   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*)rK   rV   rW   rO   rK   rV   rW   rO   N)r   warnr   rP   r2   r   r4   r@   r6   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*rJ   N)r   r    rp   r   r2   rM   rr   r   r   r   test_logInvalidLogLevel   s   

z#LoggerTests.test_logInvalidLogLevelc                    s\   t tdtddffddt tdtddf fddtd  jd	g d
 dS )zE
        Tracing keeps track of forwarding to the publisher.
        r   r   Nc                    s    |  d S r   r   r   rD   r   r   	publisher  s   z)LoggerTests.test_trace.<locals>.publisherc                    s    | d  fg d S )N	log_tracera   r   )r/   rz   r   r   r   r     s   z(LoggerTests.test_trace.<locals>.observerrD   zHello.)r{   rk   r   r   )r/   r   rz   r   r   
test_trace  s   
zLoggerTests.test_trace)r   N)r#   r$   r%   r&   r5   r7   r<   rC   rN   rS   r_   rh   rj   rl   rt   rx   ry   r|   r   r   r   r   r0   >   s     












r0   N)r&   typingr   r   r   r   zope.interfacer   
constantlyr   twisted.trialr   _formatr
   _globalr   _interfacesr   r   _levelsr   r   _loggerr   r   r)   TestCaser0   r   r   r   r   <module>   s   