o
    ¯b´"  ã                   @   s®   d Z ddlZddlmZ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 dd
lmZ ddlmZ ddlmZ eeƒG dd„ deƒƒZG dd„ de
jƒZdS )z'
Test cases for L{twisted.logger._io}.
é    N)ÚListÚOptional)Úimplementer)ÚNamedConstant)Úunittesté   )ÚILogObserverÚLogEvent)ÚLoggingFile)ÚLogLevel)ÚLogger)ÚLogPublisherc                	       sP   e Zd ZdZejdfdededee	 ddf‡ fdd„Z
d	eddfd
d„Z‡  ZS )ÚTestLoggingFilezU
    L{LoggingFile} that is also an observer which captures events and messages.
    NÚloggerÚlevelÚencodingÚreturnc                    s"   t ƒ j|||d� g | _g | _d S )N©r   r   r   )ÚsuperÚ__init__ÚeventsÚmessages)Úselfr   r   r   ©Ú	__class__© ú=/usr/lib/python3/dist-packages/twisted/logger/test/test_io.pyr      s   
zTestLoggingFile.__init__Úeventc                 C   s,   | j  |¡ d|v r| j |d ¡ d S d S )NÚlog_io)r   Úappendr   )r   r   r   r   r   Ú__call__'   s   ÿzTestLoggingFile.__call__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úinfor   r   r   Ústrr   r	   r    Ú__classcell__r   r   r   r   r      s    üþýüû
r   c                   @   sú   e Zd ZdZd0dd„Zd0dd„Zd0dd	„Zd0d
d„Zd0dd„Zd0dd„Z	d0dd„Z
d0dd„Zd0dd„Zd0dd„Zd0dd„Zd0dd„Zd0dd„Zd0dd„Zd0d d!„Zd0d"d#„Zd0d$d%„Zd0d&d'„Zd0d(d)„Zd0d*d+„Zejdfd,ed-ee defd.d/„ZdS )1ÚLoggingFileTestsz#
    Tests for L{LoggingFile}.
    r   Nc                 C   s   t ƒ | _t| jd�| _dS )zK
        Create a logger for test L{LoggingFile} instances to use.
        ©ÚobserverN)r   Ú	publisherr   r   )r   r   r   r   ÚsetUp2   s   zLoggingFileTests.setUpc                 C   sf   |   t| jƒjd¡ |  | jg¡}|   t|ƒd¡ |   |d d t¡ d}|   |d d d| ¡ dS )z0
        L{LoggingFile.softspace} is 0.
        r   é   Úcategoryz(twisted.logger._io.LoggingFile.softspaceÚmessagez#%s was deprecated in Twisted 21.2.0N)ÚassertEqualr
   r   Ú	softspaceÚflushWarningsÚtest_softspaceÚlenÚDeprecationWarning)r   ÚwarningsShownÚdeprecatedClassr   r   r   r3   9   s   
þzLoggingFileTests.test_softspacec                 C   sj   t | jƒ}|  tt|dd¡ |  tt|dd¡ |  tt|dd¡ |  tt|ddg¡ |  tt|d	d
¡ dS )z?
        Some L{LoggingFile} attributes are read-only.
        ÚclosedTr   úutf-8ÚmodeÚrÚnewlinesÚ
ÚnameÚfooN)r
   r   ÚassertRaisesÚAttributeErrorÚsetattr©r   Úfr   r   r   Útest_readOnlyAttributesH   s   
z(LoggingFileTests.test_readOnlyAttributesc                 C   s~   t | jƒ}|  t|j¡ |  t|j¡ |  t|j¡ |  t|j¡ |  t|j¡ |  t|j	¡ |  t|j
¡ |  t|j¡ dS )z>
        Some L{LoggingFile} methods are unsupported.
        N)r
   r   r@   ÚIOErrorÚreadÚnextÚreadlineÚ	readlinesÚ
xreadlinesÚseekÚtellÚtruncaterC   r   r   r   Útest_unsupportedMethodsT   s   
z(LoggingFileTests.test_unsupportedMethodsc                 C   s>   t | jƒ}|  |jtj¡ t | jtjd�}|  |jtj¡ dS )z?
        Default level is L{LogLevel.info} if not set.
        ©r   N)r
   r   r0   r   r   r%   ÚerrorrC   r   r   r   Ú
test_levelc   s   
zLoggingFileTests.test_levelc                 C   s<   t | jƒ}|  |jt ¡ ¡ t | jdd�}|  |jd¡ dS )zM
        Default encoding is C{sys.getdefaultencoding()} if not set.
        r9   ©r   N)r
   r   r0   r   ÚsysÚgetdefaultencodingrC   r   r   r   Útest_encodingm   s   
zLoggingFileTests.test_encodingc                 C   ó   t | jƒ}|  |jd¡ dS )z*
        Reported mode is C{"w"}.
        ÚwN)r
   r   r0   r:   rC   r   r   r   Ú	test_modew   ó   
zLoggingFileTests.test_modec                 C   s   t | jƒ}|  |j¡ dS )z7
        The C{newlines} attribute is L{None}.
        N)r
   r   ÚassertIsNoner<   rC   r   r   r   Útest_newlines~   s   
zLoggingFileTests.test_newlinesc                 C   rW   )z1
        The C{name} attribute is fixed.
        z.<LoggingFile twisted.logger.test.test_io#info>N)r
   r   r0   r>   rC   r   r   r   Ú	test_name…   rZ   zLoggingFileTests.test_namec                 C   s2   t | jƒ}| ¡  |  |j¡ |  t|jd¡ dS )z7
        L{LoggingFile.close} closes the file.
        ÚHelloN)r
   r   ÚcloseÚ
assertTruer8   r@   Ú
ValueErrorÚwriterC   r   r   r   Ú
test_closeŒ   s   
zLoggingFileTests.test_closec                 C   s   t | jƒ}| ¡  dS )z4
        L{LoggingFile.flush} does nothing.
        N)r
   r   ÚflushrC   r   r   r   Ú
test_flush–   s   
zLoggingFileTests.test_flushc                 C   s   t | jƒ}|  | ¡ d¡ dS )z6
        L{LoggingFile.fileno} returns C{-1}.
        éÿÿÿÿN)r
   r   r0   ÚfilenorC   r   r   r   Útest_fileno�   s   
zLoggingFileTests.test_filenoc                 C   s   t | jƒ}|  | ¡ ¡ dS )z9
        L{LoggingFile.isatty} returns C{False}.
        N)r
   r   ÚassertFalseÚisattyrC   r   r   r   Útest_isatty¤   rZ   zLoggingFileTests.test_isattyc                 C   óZ   |   ¡ }| d¡ |  |jg ¡ | d¡ |  |jdg¡ | d¡ |  |jg d¢¡ dS )z,
        Writing buffers correctly.
        r^   z	, world!
úHello, world!zIt's nice to meet you.

Indeed.©rm   zIt's nice to meet you.Ú N©ÚobservedFilerb   r0   r   rC   r   r   r   Útest_writeBuffering«   s   


þz$LoggingFileTests.test_writeBufferingc                 C   ó*   | j dd�}| d¡ |  |jdg¡ dS )z,
        Bytes are decoded to text.
        r9   rS   s   Hello, Mr. SÃ¡nchez
õ   Hello, Mr. SÃ¡nchezNrp   rC   r   r   r   Útest_writeBytesDecoded¾   ó   
z'LoggingFileTests.test_writeBytesDecodedc                 C   rs   )z(
        Unicode is unmodified.
        r9   rS   u   Hello, Mr. SÃ¡nchez
rt   Nrp   rC   r   r   r   Útest_writeUnicodeÆ   rv   z"LoggingFileTests.test_writeUnicodec                 C   s‚   |   ¡ }| d¡ |  t|jƒd¡ |  |jd d tj¡ | j tjd�}| d¡ |  t|jƒd¡ |  |jd d tj¡ dS )z0
        Log level is emitted properly.
        úHello
r-   r   Ú	log_levelrP   N)rq   rb   r0   r4   r   r   r%   rQ   rC   r   r   r   Útest_writeLevelÎ   s   

z LoggingFileTests.test_writeLevelc                 C   s>   |   ¡ }| d¡ |  t|jƒd¡ |  |jd d d¡ dS )z/
        Log format is C{"{message}"}.
        rx   r-   r   Ú
log_formatz{log_io}N)rq   rb   r0   r4   r   rC   r   r   r   Útest_writeFormatÜ   s   
z!LoggingFileTests.test_writeFormatc                 C   rl   )z6
        C{writelines} does not add newlines.
        )r^   z, ro   )zworld!
rm   )zIt's nice to meet you.

zIndeed.rn   N)rq   Ú
writelinesr0   r   rC   r   r   r   Útest_writelinesBufferingå   s   


þz)LoggingFileTests.test_writelinesBufferingc                 C   s>   |   ¡ }|  td|¡ tddd� tdƒ |  |jdg¡ dS )z;
        L{LoggingFile} can replace L{sys.stdout}.
        ÚstdoutzHello,ú )Úendzworld.zHello, world.N)rq   ÚpatchrT   Úprintr0   r   rC   r   r   r   Ú
test_printù   s
   zLoggingFileTests.test_printr   r   c                    sH   g ‰ t tƒdtddf‡ fdd„ƒ}t|d�}ˆ  t|||d�¡ ˆ d S )	a¤  
        Construct a L{LoggingFile} with a built-in observer.

        @param level: C{level} argument to L{LoggingFile}
        @param encoding: C{encoding} argument to L{LoggingFile}

        @return: a L{TestLoggingFile} with an observer that appends received
            events into the file's C{events} attribute (a L{list}) and
            event messages into the file's C{messages} attribute (a L{list}).
        r   r   Nc                    s   ˆ d | ƒ d S )Nr   r   )r   ©ÚloggingFilesr   r   r*     s   z/LoggingFileTests.observedFile.<locals>.observerr)   r   r   )r   r   r	   r   r   r   )r   r   r   r*   Úlogr   r…   r   rq     s   
zLoggingFileTests.observedFile)r   N)r!   r"   r#   r$   r,   r3   rE   rO   rR   rV   rY   r\   r]   rc   re   rh   rk   rr   ru   rw   rz   r|   r~   r„   r   r%   r   r   r&   r   rq   r   r   r   r   r(   -   s>    





















	
ýþýür(   )r$   rT   Útypingr   r   Úzope.interfacer   Ú
constantlyr   Útwisted.trialr   Ú_interfacesr   r	   Ú_ior
   Ú_levelsr   Ú_loggerr   Ú	_observerr   r   ÚTestCaser(   r   r   r   r   Ú<module>   s   