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   