a
    )¶(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                       sH   e Zd ZdZejdfeeee	 ddœ‡ fdd„Z
eddœ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__© ú:lib/python3.9/site-packages/twisted/logger/test/test_io.pyr      s    zTestLoggingFile.__init__©Úeventr   c                 C   s(   | j  |¡ d|v r$| j |d ¡ d S )NZ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                   @   sF  e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zddœdd„Zddœdd„Z	ddœdd„Z
ddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœd d!„Zddœd"d#„Zddœd$d%„Zddœd&d'„Zddœd(d)„Zddœd*d+„Zejdfeee ed,œd-d.„ZdS )/ÚLoggingFileTestsz#
    Tests for L{LoggingFile}.
    N)r   c                 C   s   t ƒ | _t| jd| _dS )zK
        Create a logger for test L{LoggingFile} instances to use.
        ©ÚobserverN)r   Z	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   Z	softspaceZflushWarningsÚtest_softspaceÚlenÚDeprecationWarning)r   ZwarningsShownZdeprecatedClassr   r   r   r0   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Ú
ÚnameZ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Ú	readlinesZ
xreadlinesÚseekÚtellÚtruncater=   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   r/   r   r   r%   Úerrorr=   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.
        r4   ©r   N)r
   r   r/   r   ÚsysÚgetdefaultencodingr=   r   r   r   Útest_encodingm   s    
zLoggingFileTests.test_encodingc                 C   s   t | jƒ}|  |jd¡ dS )z*
        Reported mode is C{"w"}.
        ÚwN)r
   r   r/   r5   r=   r   r   r   Ú	test_modew   s    
zLoggingFileTests.test_modec                 C   s   t | jƒ}|  |j¡ dS )z7
        The C{newlines} attribute is L{None}.
        N)r
   r   ZassertIsNoner7   r=   r   r   r   Útest_newlines~   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/   r9   r=   r   r   r   Ú	test_name…   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
assertTruer3   r:   Ú
ValueErrorÚwriter=   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   Ú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   sZ   |   ¡ }| d¡ |  |jg ¡ | d¡ |  |jdg¡ | d¡ |  |jg d¢¡ dS )z,
        Writing buffers correctly.
        rT   z	, world!
úHello, world!zIt's nice to meet you.

Indeed.©r`   zIt's nice to meet you.Ú N©ÚobservedFilerW   r/   r   r=   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 text.
        r4   rL   s   Hello, Mr. SÃ¡nchez
õ   Hello, Mr. SÃ¡nchezNrc   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.
        r4   rL   u   Hello, Mr. SÃ¡nchez
rf   Nrc   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,   r   Z	log_levelrI   N)rd   rW   r/   r1   r   r   r%   rJ   r=   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}"}.
        ri   r,   r   Z
log_formatz{log_io}N)rd   rW   r/   r1   r   r=   r   r   r   Útest_writeFormatÜ   s    
z!LoggingFileTests.test_writeFormatc                 C   sZ   |   ¡ }| d¡ |  |jg ¡ | d¡ |  |jdg¡ | d¡ |  |jg d¢¡ dS )z6
        C{writelines} does not add newlines.
        )rT   z, rb   )zworld!
r`   )zIt's nice to meet you.

zIndeed.ra   N)rd   Ú
writelinesr/   r   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)rd   ZpatchrM   Úprintr/   r   r=   r   r   r   Ú
test_printù   s
    zLoggingFileTests.test_print)r   r   r   c                    sF   g ‰ t tƒtddœ‡ 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}).
        Nr   c                    s   ˆ d | ƒ d S )Nr   r   )r   ©ZloggingFilesr   r   r*     s    z/LoggingFileTests.observedFile.<locals>.observerr)   r   r   )r   r   r	   r   r   r   )r   r   r   r*   Úlogr   rs   r   rd     s    
zLoggingFileTests.observedFile)r!   r"   r#   r$   r+   r0   r?   rH   rK   rO   rQ   rR   rS   rX   rZ   r]   r_   re   rg   rh   rj   rk   rm   rr   r   r%   r   r   r&   r   rd   r   r   r   r   r(   -   s8   


	ýür(   )r$   rM   Útypingr   r   Zzope.interfacer   Z
constantlyr   Ztwisted.trialr   Z_interfacesr   r	   Ú_ior
   Z_levelsr   Z_loggerr   Z	_observerr   r   ZTestCaser(   r   r   r   r   Ú<module>   s   