B
    u9a•  ã               @   s„   d Z ddlZddlZddlZddlZddlZddlmZ ejdkrLe 	d¡‚ddl
mZ ejZG dd„ dejƒZed	kr€e ¡  dS )
zTests for WindowsConsoleIO
é    N)ÚsupportÚwin32ztest only relevant on win32)Úwrite_inputc               @   s€   e Zd Zdd„ Zdd„ Zdd„ Ze e 	¡ dd… d	kd
¡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 )ÚWindowsConsoleIOTestsc             C   s:   |   tttjƒ¡ |  tttjƒ¡ |  tttjƒ¡ d S )N)Ú
assertTrueÚ
issubclassÚConIOÚioÚ	RawIOBaseÚassertFalseÚBufferedIOBaseÚ
TextIOBase)Úself© r   ú'/usr/lib/python3.7/test_winconsoleio.pyÚtest_abc   s    zWindowsConsoleIOTests.test_abcc          	   C   sb  |   tdtd¡ t ¡ \}}z|   tdt|¡ W d t |¡ X ytdƒ}W n tk
r^   Y n>X |  | ¡ ¡ |  	| 
¡ ¡ |  d| ¡ ¡ | ¡  | ¡  ytddƒ}W n tk
r¾   Y n>X |  	| ¡ ¡ |  | 
¡ ¡ |  d| ¡ ¡ | ¡  | ¡  ytddƒ}W n tk
r    Y n>X |  	| ¡ ¡ |  | 
¡ ¡ |  d| ¡ ¡ | ¡  | ¡  d S )Nznegative file descriptoréÿÿÿÿz&Cannot open (console|non-console file)r   é   Úwé   )ZassertRaisesRegexÚ
ValueErrorr   ÚtempfileZmkstempÚosÚcloser   Úreadabler   ÚwritableÚassertEqualÚfileno)r   ÚfdÚ_Úfr   r   r   Útest_open_fd   sD    
z"WindowsConsoleIOTests.test_open_fdc             C   sþ   |   tttj¡ tdƒ}|  | ¡ ¡ |  | ¡ ¡ |  	| 
¡ ¡ | ¡  | ¡  tdƒ}|  | ¡ ¡ |  | ¡ ¡ |  	| 
¡ ¡ | ¡  | ¡  tddƒ}|  | ¡ ¡ |  | ¡ ¡ |  	| 
¡ ¡ | ¡  | ¡  tdddd}|  |t¡ | ¡  d S )	NZCONzCONIN$zCONOUT$r   zC:/conÚrbr   )Ú	buffering)ZassertRaisesr   r   ÚsysÚ
executabler   r   r   r   ZassertIsNotNoner   r   ÚopenÚassertIsInstance)r   r    r   r   r   Útest_open_nameI   s,    
z$WindowsConsoleIOTests.test_open_nameNr   )é   r   z+test does not work on Windows 7 and earlierc             C   sH   t dddd}|  |t¡ | ¡  t dddd}|  |t¡ | ¡  d S )Nz
\\.\conin$r"   r   )r#   z//?/conout$Úwb)r&   r'   r   r   )r   r    r   r   r   Útest_conin_conout_namese   s    z-WindowsConsoleIOTests.test_conin_conout_namesc          	   C   sp   t  ¡ }|  tj|¡ tj |d¡}t|ddd4}t	 
¡ d d… dkrV|  |t¡ n|  |t¡ W d Q R X d S )NzCONOUT$r*   r   )r#   r   )r)   r   )r   ZmkdtempZ
addCleanupr   Zrmtreer   ÚpathÚjoinr&   r$   Úgetwindowsversionr'   r   ZassertNotIsInstance)r   Z	temp_pathZconout_pathr    r   r   r   Útest_conout_pathp   s    z&WindowsConsoleIOTests.test_conout_pathc          	   C   s,   t ddƒ}|  | d¡d¡ W d Q R X d S )NzCONOUT$r   ó    r   )r   r   Úwrite)r   r    r   r   r   Útest_write_empty_data|   s    z+WindowsConsoleIOTests.test_write_empty_datac             C   sT   t ddƒ}tj}z*|t_t|jj|d  dd¡ƒ tƒ }W d |t_X |  ||¡ d S )NzCONIN$Úrz
z	utf-16-leÚsurrogatepass)	r&   r$   Ústdinr   ÚbufferÚrawÚencodeÚinputr   )r   Útextr5   Z	old_stdinÚactualr   r   r   ÚassertStdinRoundTrip€   s    

z*WindowsConsoleIOTests.assertStdinRoundTripc             C   s,   |   d¡ |   d¡ |   d¡ |   d¡ d S )NZabc123u   Ï¼ÑžÐ¢Î»Ð¤Ð™u   AÍB ï¬–Ì³AAÌu   ô€€€ô¿¿ô¿½)r<   )r   r   r   r   Ú
test_inputŽ   s    


z WindowsConsoleIOTests.test_inputc          
   C   sŠ   d  d¡}d  d¡}xptddƒD ]b}tdddd	J}t||ƒ d
}x | d¡sb| |¡}||7 }qDW |  ||d |¡¡ W d Q R X q W d S )Nu   Ï¼ÑžÐ¢Î»Ð¤Ð™
z	utf-16-lezutf-8r   é   zCONIN$r"   r   )r#   r0   ó   
zstdin.read({}))r8   Úranger&   r   ÚendswithÚreadr   Úformat)r   ÚsourceÚexpectedÚ
read_countr5   r;   Úbr   r   r   Útest_partial_reads˜   s    



z(WindowsConsoleIOTests.test_partial_readsc          
   C   sŠ   d  d¡}d  d¡}xptddƒD ]b}tdddd	J}t||ƒ d
}x | d¡sb| |¡}||7 }qDW |  ||d |¡¡ W d Q R X q W d S )Nu
   ô¿¿ô€
z	utf-16-lezutf-8r   r>   zCONIN$r"   r   )r#   r0   r?   zstdin.read({}))r8   r@   r&   r   rA   rB   r   rC   )r   rD   rE   rF   r5   r;   rG   r   r   r   Útest_partial_surrogate_reads¨   s    



z2WindowsConsoleIOTests.test_partial_surrogate_readsc          	   C   sx   t dddd`}d d¡}d d¡}t||ƒ | d	¡| ¡  }}|  |dd	… |¡ |  |d	d … |¡ W d Q R X d S )
NzCONIN$r"   r   )r#   u   Ã„
z	utf-16-leõ   Ã„zutf-8r   )r&   r8   r   rB   Úreadallr   )r   r5   rD   rE   ÚarG   r   r   r   Útest_ctrl_z¹   s    


z!WindowsConsoleIOTests.test_ctrl_z)Ú__name__Ú
__module__Ú__qualname__r   r!   r(   ÚunittestZskipIfr$   r.   r+   r/   r2   r<   r=   rH   rI   rM   r   r   r   r   r      s   1

r   Ú__main__)Ú__doc__r	   r   r$   r   rQ   Ztestr   ÚplatformZSkipTestZ_testconsoler   Z_WindowsConsoleIOr   ZTestCaser   rN   Úmainr   r   r   r   Ú<module>   s   

 1