o
    Òó·i·  ã                   @   s`   d Z ddlmZ ddlmZ ddlmZ e ¡ sddlm	Z	 G dd„ deƒZ
eƒ  e
 ¡ ¡ dS )	z&
Tests for L{twisted.internet.stdio}.
é    )ÚProtocol)ÚReactorBuilder)Úplatform)Ú
StandardIOc                   @   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e ¡ r.dZdS dS )ÚStdioFilesTestszI
    L{StandardIO} supports reading and writing to filesystem files.
    c                 C   s4   |   ¡ }t|dƒ ¡  t|dƒ| _|  | jj¡ d S )NÚwbzrb+)ÚmktempÚopenÚcloseÚ	extraFileZ
addCleanup)ÚselfÚpath© r   úfC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/internet/test/test_stdio.pyÚsetUp   s   zStdioFilesTests.setUpc                    s¾   |   ¡ ‰ G ‡ fdd„dtƒ}|  ¡ }t|dƒ�}| d¡ W d  ƒ n1 s'w   Y  t|dƒ�}|ƒ }t|| ¡ | j ¡ ˆ d� |  ˆ ¡ W d  ƒ n1 sQw   Y  |  	|j
d¡ dS )zc
        Adding a filesystem file reader to a reactor will make sure it is
        polled.
        c                       s   e Zd ZdZ‡ fdd„ZdS )z4StdioFilesTests.test_addReader.<locals>.DataProtocoló    c                    s(   |  j |7  _ | j dkrˆ  ¡  d S d S )Nó   hello!)ÚdataÚstop)r   r   ©Úreactorr   r   ÚdataReceived&   s   
ÿzAStdioFilesTests.test_addReader.<locals>.DataProtocol.dataReceivedN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   ÚDataProtocol#   s    r   r   r   NÚrb©ÚstdinÚstdoutr   )ÚbuildReactorr   r   r	   Úwriter   Úfilenor   Ú
runReactorÚassertEqualr   )r   r   r   ÚfÚprotocolr   r   r   Útest_addReader   s"   ÿü÷zStdioFilesTests.test_addReaderc                    sÚ   |   ¡ ‰ G ‡ fdd„dtƒ}|  ¡ }t|dƒ�.}|ƒ }t|| ¡ | j ¡ ˆ d� |j d¡ |j d¡ |j 	¡  |  
ˆ ¡ W d  ƒ n1 sHw   Y  t|dƒ�}|  | ¡ d	¡ W d  ƒ dS 1 sfw   Y  dS )
zc
        Adding a filesystem file writer to a reactor will make sure it is
        polled.
        c                       s   e Zd Z‡ fdd„ZdS )z:StdioFilesTests.test_addWriter.<locals>.DisconnectProtocolc                    s   ˆ   ¡  d S )N)r   )r   Úreasonr   r   r   ÚconnectionLostH   s   zIStdioFilesTests.test_addWriter.<locals>.DisconnectProtocol.connectionLostN)r   r   r   r)   r   r   r   r   ÚDisconnectProtocolG   s    r*   r   ©r   r   r   ó   hellos   , worldNr   s   hello, world)r    r   r   r	   r   r"   r   Ú	transportr!   ZloseConnectionr#   r$   Úread)r   r*   r   r%   r&   r   r   r   Útest_addWriter@   s&   ü
ò"ÿzStdioFilesTests.test_addWriterc                 C   s˜   |   ¡ }|  ¡ }t|dƒ ¡  t|dƒ�.}ttƒ | ¡ | j ¡ |d�}|  |j	| 
¡ ¡ |j	 ¡  |  |j	| 
¡ ¡ W d  ƒ dS 1 sEw   Y  dS )zq
        Removing a filesystem file reader from a reactor will make sure it is
        no longer polled.
        r   r   r   N)r    r   r	   r
   r   r   r"   r   ÚassertInÚ_readerÚ
getReadersZstopReadingÚassertNotIn©r   r   r   r%   Ústdior   r   r   Útest_removeReader`   s   ü
"öz!StdioFilesTests.test_removeReaderc                 C   sz   |   ¡ }t|  ¡ dƒ | _}tƒ }t|| ¡ | j ¡ |d�}|j 	d¡ |  
|j| ¡ ¡ |j ¡  |  |j| ¡ ¡ dS )zq
        Removing a filesystem file writer from a reactor will make sure it is
        no longer polled.
        r   r+   r,   N)r    r	   r   r%   r   r   r"   r   r-   r!   r0   Ú_writerÚ
getWritersZstopWritingr3   )r   r   r%   r&   r5   r   r   r   Útest_removeWriterv   s   ÿ
z!StdioFilesTests.test_removeWriterc                 C   sv   |   ¡ }|  ¡ }t|dƒ ¡  t|dƒ | _}ttƒ | ¡ | j ¡ |d�}| 	¡ }|  
|j|¡ |  |j| ¡ ¡ dS )zk
        Calling C{removeAll} on a reactor includes descriptors that are
        filesystem files.
        r   r   r   N)r    r   r	   r
   r%   r   r   r"   r   Z	removeAllr0   r1   r3   r2   )r   r   r   r%   r5   Úremovedr   r   r   Útest_removeAllŠ   s   üzStdioFilesTests.test_removeAllc                 C   s|   |   ¡ }|  ¡ }t|dƒ ¡  t|dƒ� }ttƒ | ¡ | j ¡ |d�}|  |j	| 
¡ ¡ W d  ƒ dS 1 s7w   Y  dS )zW
        C{reactor.getReaders} includes descriptors that are filesystem files.
        r   r   r   N)r    r   r	   r
   r   r   r"   r   r0   r1   r2   r4   r   r   r   Útest_getReaders£   s   ü"øzStdioFilesTests.test_getReadersc                 C   sj   |   ¡ }t|  ¡ dƒ | _}ttƒ | ¡ | j ¡ |d�}|  |j	| 
¡ ¡ |j	 ¡  |  |j	| 
¡ ¡ dS )zW
        C{reactor.getWriters} includes descriptors that are filesystem files.
        r   r+   N)r    r	   r   r%   r   r   r"   r   r3   r7   r8   ZstartWritingr0   )r   r   r%   r5   r   r   r   Útest_getWriters·   s   ü
zStdioFilesTests.test_getWriterszoStandardIO does not accept stdout as an argument to Windows. Testing redirection to a file is therefore harder.N)r   r   r   Ú__doc__r   r'   r/   r6   r9   r;   r<   r=   r   Ú	isWindowsÚskipr   r   r   r   r      s    $ ÿÿr   N)r>   Ztwisted.internet.protocolr   Z#twisted.internet.test.reactormixinsr   Ztwisted.python.runtimer   r?   Ztwisted.internet.stdior   r   ÚglobalsÚupdateZmakeTestCaseClassesr   r   r   r   Ú<module>   s    B