B
    u9aŸ  ã               @   s€   d dl 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mZm	Z	 dZ
G dd„ dejƒZG dd„ dejƒZedkr|e ¡  dS )	é    N)ÚfindfileÚTESTFNÚunlink))z
python.pngÚpng)z
python.gifZgif)z
python.bmpZbmp)z
python.ppmZppm)z
python.pgmZpgm)z
python.pbmZpbm)z
python.jpgZjpeg)z
python.rasZrast)z
python.sgiZrgb)zpython.tiffZtiff)z
python.xbmZxbm)zpython.webpZwebp)z
python.exrZexrc               @   s   e Zd Zdd„ Zdd„ ZdS )ÚUnseekableIOc             C   s
   t j‚d S )N)ÚioÚUnsupportedOperation)Úself© r
   ú!/usr/lib/python3.7/test_imghdr.pyÚtell   s    zUnseekableIO.tellc             O   s
   t j‚d S )N)r   r   )r	   ÚargsÚkwargsr
   r
   r   Úseek   s    zUnseekableIO.seekN)Ú__name__Ú
__module__Ú__qualname__r   r   r
   r
   r
   r   r      s   r   c               @   sx   e Zd Ze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 )Ú
TestImghdrc          	   C   s4   t ddd| _t| jdƒ}| ¡ | _W d Q R X d S )Nz
python.pngÚ
imghdrdata)ÚsubdirÚrb)r   ÚtestfileÚopenÚreadÚtestdata)ÚclsÚstreamr
   r
   r   Ú
setUpClass!   s    zTestImghdr.setUpClassc             C   s   t tƒ d S )N)r   r   )r	   r
   r
   r   ÚtearDown'   s    zTestImghdr.tearDownc          
   C   s¦   x t D ]˜\}}t|dd}|  t |¡|¡ t|dƒ}|  t |¡|¡ W d Q R X t|dƒ}| ¡ }W d Q R X |  t d |¡|¡ |  t d t|ƒ¡|¡ qW d S )Nr   )r   r   )Ú
TEST_FILESr   ÚassertEqualÚimghdrÚwhatr   r   Ú	bytearray)r	   ÚfilenameÚexpectedr   Údatar
   r
   r   Ú	test_data*   s    zTestImghdr.test_datac          
   C   sR   xLt D ]D\}}| j|d* t|dd}|  t t |¡¡|¡ W d Q R X qW d S )N)r$   r   )r   )r   ZsubTestr   r    r!   r"   ÚpathlibZPath)r	   r$   r%   r
   r
   r   Útest_pathlike_filename5   s    z!TestImghdr.test_pathlike_filenamec             C   s:   dd„ }t j |¡ |  t jj¡ |  t  d d¡d¡ d S )Nc             S   s   |   d¡rdS d S )Ns   eggsÚham)Ú
startswith)ÚhÚfiler
   r
   r   Ú
test_jumbo<   s    
z1TestImghdr.test_register_test.<locals>.test_jumbos   eggsr*   )r!   ZtestsÚappendZ
addCleanupÚpopr    r"   )r	   r.   r
   r
   r   Útest_register_test;   s    zTestImghdr.test_register_testc          	   C   sz   t tdƒ$}| d¡ | ¡ }| | j¡ W d Q R X t tdƒ2}| |¡ |  t |¡d¡ |  | ¡ |¡ W d Q R X d S )NÚwbs   ababagalamagar   r   )	r   r   Úwriter   r   r   r    r!   r"   )r	   r   Úposr
   r
   r   Útest_file_posC   s    

zTestImghdr.test_file_posc          
   C   sÊ   |   t¡ t ¡  W d Q R X |   t¡ t d ¡ W d Q R X |   t¡ t | jd¡ W d Q R X |   t¡ t t | j¡¡ W d Q R X t| jdƒ*}|   t¡ t | 	¡ ¡ W d Q R X W d Q R X d S )Né   r   )
ÚassertRaisesÚ	TypeErrorr!   r"   ÚAttributeErrorr   ÚosÚfsencoder   Úfileno)r	   Úfr
   r
   r   Útest_bad_argsM   s    zTestImghdr.test_bad_argsc             C   s$   xdD ]}|   t d |¡¡ qW d S )N)s   ‰PN
s   Ùs   Y¦s   cutecats	   000000JFIs   GIF80)ZassertIsNoner!   r"   )r	   Úheaderr
   r
   r   Útest_invalid_headersZ   s    
zTestImghdr.test_invalid_headersc             C   s®   t  ¡ œ t  dt¡ xˆtD ]€\}}t|dd}t|dƒ}| ¡  d¡}W d Q R X |  	t
¡ t t |¡¡ W d Q R X |  	t
¡ t d |¡ W d Q R X qW W d Q R X d S )NÚignorer   )r   r   Úlatin1)ÚwarningsÚcatch_warningsÚsimplefilterÚBytesWarningr   r   r   r   Údecoder7   r8   r!   r"   r   ÚStringIO)r	   r$   Ú_r   r&   r
   r
   r   Útest_string_datac   s    
zTestImghdr.test_string_datac          	   C   s$   |   t¡ t d¡ W d Q R X d S )NÚmissing)r7   ÚFileNotFoundErrorr!   r"   )r	   r
   r
   r   Útest_missing_fileo   s    zTestImghdr.test_missing_filec          	   C   sl   t | jdƒ}| ¡  |  t¡}t |¡ W d Q R X t | j	¡}| ¡  |  t¡}t |¡ W d Q R X d S )Nr   )
r   r   Úcloser7   Ú
ValueErrorr!   r"   r   ÚBytesIOr   )r	   r   Úcmr
   r
   r   Útest_closed_files   s    zTestImghdr.test_closed_filec          
   C   s^   t tdƒ}| | j¡ W d Q R X ttdƒ(}|  tj¡ t 	|¡ W d Q R X W d Q R X d S )Nr2   r   )
r   r   r3   r   r   r7   r   r   r!   r"   )r	   r   r
   r
   r   Útest_unseekable}   s
    zTestImghdr.test_unseekablec          
   C   sP   t tdƒ<}| | j¡ | d¡ |  t¡}t |¡ W d Q R X W d Q R X d S )Nr2   r   )	r   r   r3   r   r   r7   ÚOSErrorr!   r"   )r	   r   rQ   r
   r
   r   Útest_output_stream„   s
    
zTestImghdr.test_output_streamN)r   r   r   Úclassmethodr   r   r'   r)   r1   r5   r>   r@   rJ   rM   rR   rS   rU   r
   r
   r
   r   r       s   
	
r   Ú__main__)r!   r   r:   r(   ZunittestrC   Ztest.supportr   r   r   r   ÚFileIOr   ZTestCaser   r   Úmainr
   r
   r
   r   Ú<module>   s   k