U
    
W[,  ã                   @   sj   d Z ddlmZ ddl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jƒZdS )z'
Test cases for L{twisted.logger._io}.
é    )Úprint_functionN)Úunittesté   )ÚLogLevel)ÚLogger)ÚLogPublisher)ÚLoggingFilec                   @   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d„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,S )-ÚLoggingFileTestsz#
    Tests for L{LoggingFile}.
    c                 C   s   t ƒ | _t| jd| _dS )zK
        Create a logger for test L{LoggingFile} instances to use.
        ©ÚobserverN)r   Z	publisherr   Úlogger©Úself© r   ú=/usr/lib/python3/dist-packages/twisted/logger/test/test_io.pyÚsetUp   s    zLoggingFileTests.setUpc                 C   s   |   tjd¡ dS )z0
        L{LoggingFile.softspace} is 0.
        r   N)ÚassertEqualr   Z	softspacer   r   r   r   Útest_softspace"   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.
        ÚclosedTÚencodingúutf-8ÚmodeÚrÚnewlinesÚ
ÚnameZfooN)r   r   ÚassertRaisesÚAttributeErrorÚsetattr©r   Úfr   r   r   Útest_readOnlyAttributes)   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Ú	readlinesZ
xreadlinesÚseekÚtellÚtruncater   r   r   r   Útest_unsupportedMethods6   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.
        ©ÚlevelN)r   r   r   r,   r   ÚinfoÚerrorr   r   r   r   Ú
test_levelF   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.
        r   ©r   N)r   r   r   r   ÚsysÚgetdefaultencodingr   r   r   r   Útest_encodingQ   s    
zLoggingFileTests.test_encodingc                 C   s   t | jƒ}|  |jd¡ dS )z*
        Reported mode is C{"w"}.
        ÚwN)r   r   r   r   r   r   r   r   Ú	test_mode\   s    
zLoggingFileTests.test_modec                 C   s   t | jƒ}|  |j¡ dS )z7
        The C{newlines} attribute is L{None}.
        N)r   r   ZassertIsNoner   r   r   r   r   Útest_newlinesd   s    
zLoggingFileTests.test_newlinesc                 C   s   t | jƒ}|  |jd¡ dS )z1
        The C{name} attribute is fixed.
        z.<LoggingFile twisted.logger.test.test_io#info>N)r   r   r   r   r   r   r   r   Ú	test_namel   s
    
þzLoggingFileTests.test_namec                 C   s2   t | jƒ}| ¡  |  |j¡ |  t|jd¡ dS )z7
        L{LoggingFile.close} closes the file.
        ÚHelloN)r   r   ÚcloseZ
assertTruer   r   Ú
ValueErrorÚwriter   r   r   r   Ú
test_closew   s    
zLoggingFileTests.test_closec                 C   s   t | jƒ}| ¡  dS )z4
        L{LoggingFile.flush} does nothing.
        N)r   r   Úflushr   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   r   Úfilenor   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   ZassertFalseÚisattyr   r   r   r   Útest_isatty’   s    
zLoggingFileTests.test_isattyc                 C   s\   |   ¡ }| d¡ |  |jg ¡ | d¡ |  |jdg¡ | d¡ |  |jdddg¡ dS )z,
        Writing buffers correctly.
        r8   z	, world!
úHello, world!zIt's nice to meet you.

Indeed.úIt's nice to meet you.Ú N©ÚobservedFiler;   r   Úmessagesr   r   r   r   Útest_writeBufferingš   s    


ýþz$LoggingFileTests.test_writeBufferingc                 C   s*   | j dd}| d¡ |  |jdg¡ dS )z/
        Bytes are decoded to unicode.
        r   r0   s   Hello, Mr. SÃ¡nchez
õ   Hello, Mr. SÃ¡nchezNrG   r   r   r   r   Útest_writeBytesDecoded®   s    
z'LoggingFileTests.test_writeBytesDecodedc                 C   s*   | j dd}| d¡ |  |jdg¡ dS )z(
        Unicode is unmodified.
        r   r0   u   Hello, Mr. SÃ¡nchez
rK   NrG   r   r   r   r   Útest_writeUnicode·   s    
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   Z	log_levelr+   N)rH   r;   r   ÚlenÚeventsr   r-   r.   r   r   r   r   Útest_writeLevelÀ   s    

z LoggingFileTests.test_writeLevelc                 C   s>   |   ¡ }| d¡ |  t|jƒd¡ |  |jd d d¡ dS )z0
        Log format is C{u"{message}"}.
        rN   rO   r   Z
log_formatz{log_io}N)rH   r;   r   rP   rQ   r   r   r   r   Útest_writeFormatÏ   s    
z!LoggingFileTests.test_writeFormatc                 C   s\   |   ¡ }| d¡ |  |jg ¡ | d¡ |  |jdg¡ | d¡ |  |jdddg¡ dS )z6
        C{writelines} does not add newlines.
        )r8   z, rF   )zworld!
rD   )zIt's nice to meet you.

zIndeed.rE   rF   N)rH   Ú
writelinesr   rI   r   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)rH   Zpatchr1   Úprintr   rI   r   r   r   r   Ú
test_printî   s
    zLoggingFileTests.test_printc                    s8   ‡ fdd„}t |d}tf d|i|—Ž‰ g ˆ _g ˆ _ˆ S )a¤  
        Construct a L{LoggingFile} with a built-in observer.

        @param kwargs: keyword arguments for the L{LoggingFile}.
        @type kwargs: L{dict}

        @return: a L{LoggingFile} 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}).
        @rtype: L{LoggingFile}
        c                    s(   ˆ j  | ¡ d| kr$ˆ j | d ¡ d S )NZlog_io)rQ   ÚappendrI   )Zevent©r    r   r   r     s    z/LoggingFileTests.observedFile.<locals>.observerr
   r   )r   r   rQ   rI   )r   Úkwargsr   Úlogr   r\   r   rH   û   s    
zLoggingFileTests.observedFileN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r!   r*   r/   r3   r5   r6   r7   r<   r>   rA   rC   rJ   rL   rM   rR   rS   rU   rZ   rH   r   r   r   r   r	      s,   		
r	   )rb   Z
__future__r   r1   Ztwisted.trialr   Z_levelsr   Z_loggerr   Z	_observerr   Ú_ior   ZTestCaser	   r   r   r   r   Ú<module>   s   