a
    ø£Bea$  ã                   @   s\  d Z ddlmZmZ ddlmZmZmZ ddl	Z	ddl
mZmZ ddlZddlZddlmZ ddlZzddlZW n ey‚   Y n0 ddlmZ ddlm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%G d%d&„ d&eƒZ&d'd(„ Z'e'ƒ G d)d*„ d*e&ƒƒZ(ed2d,d-„ƒZ)d.d/„ Z*d0d1„ Z+dS )3z Tests for IPython.lib.display.

é    )ÚNamedTemporaryFileÚmkdtemp)ÚsplitÚjoinÚdirnameN)ÚTestCaseÚmock)ÚBytesIO)Údisplay)Úskipif_not_numpyc                  C   s   t  d¡} t  t d¡¡} dS )z(FileLink: Test class can be instantiatedúexample.txtN)r
   ÚFileLinkÚpathlibÚPurePath©Úfl© r   úa/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/IPython/lib/tests/test_display.pyÚtest_instantiation_FileLink,   s    
r   c                  C   s    t  d¡} |  ¡  d¡sJ ‚dS )zEFileLink: Calling _repr_html_ on non-existent files returns a warningr   zPath (<tt>example.txt</tt>)N)r
   r   Ú_repr_html_Ú
startswithr   r   r   r   Ú*test_warning_on_non_existent_path_FileLink2   s    
r   c                  C   s:   t ƒ } t | j¡}| ¡ }d| j| jf }||ks6J ‚dS )zMFileLink: Calling _repr_html_ functions as expected on existing filepath
    z'<a href='%s' target='_blank'>%s</a><br>N)r   r
   r   Únamer   ©Útfr   ÚactualÚexpectedr   r   r   Útest_existing_path_FileLink8   s
    r   c                  C   s0   t ƒ } t | j¡}t|ƒ}| j}||ks,J ‚dS )zHFileLink: Calling repr() functions as expected on existing filepath
    N)r   r
   r   r   Úreprr   r   r   r   Ú test_existing_path_FileLink_reprB   s
    r   c                  C   s   t ƒ } t ttj| ¡ dS )z1FileLink: Raises error when passed directory
    N)r   ÚpytestÚraisesÚ
ValueErrorr
   r   )Útdr   r   r   Ú#test_error_on_directory_to_FileLinkL   s    r$   c                  C   s   t  d¡} dS )z.FileLinks: Test class can be instantiated
    ÚexampleN)r
   Ú	FileLinks©Zflsr   r   r   Útest_instantiation_FileLinksV   s    r(   c                  C   s    t  d¡} |  ¡  d¡sJ ‚dS )zFFileLinks: Calling _repr_html_ on non-existent files returns a warningr%   zPath (<tt>example</tt>)N)r
   r&   r   r   r'   r   r   r   Ú+test_warning_on_non_existent_path_FileLinks[   s    
r)   c                  C   sœ   t ƒ } t| d�}t| d�}t | ¡}| ¡ }| d¡}| ¡  d|  d|j dd¡t|jƒd f d|j dd¡t|jƒd f g}| ¡  ||ks˜J ‚dS )	zIFileLinks: Calling _repr_html_ functions as expected on existing dir
    ©ÚdirÚ
z%s/<br>z3&nbsp;&nbsp;<a href='%s' target='_blank'>%s</a><br>ú\ú/é   N)	r   r   r
   r&   r   r   Úsortr   Úreplace©r#   Útf1Útf2r   r   r   r   r   r   Útest_existing_path_FileLinksa   s"    



ÿÿýr5   c                  C   sj   t ƒ } t| d�}t| d�}dd„ }tj| |d�}| ¡ }| d¡}| ¡  ddg}| ¡  ||ksfJ ‚dS )	zMFileLinks: Calling _repr_html_ functions as expected w/ an alt formatter
    r*   c                 S   s   ddgS ©NÚhelloÚworldr   ©r   ÚfnamesZincluded_suffixesr   r   r   Úfake_formatter}   s    zBtest_existing_path_FileLinks_alt_formatter.<locals>.fake_formatter)Znotebook_display_formatterr,   r7   r8   N)r   r   r
   r&   r   r   r0   ©r#   r3   r4   r;   r   r   r   r   r   r   Ú*test_existing_path_FileLinks_alt_formatterw   s    


r=   c                  C   s€   t ƒ } t| d�}t| d�}t | ¡}t|ƒ}| d¡}| ¡  d|  dt|jƒd  dt|jƒd  g}| ¡  ||ks|J ‚dS )zFFileLinks: Calling repr() functions as expected on existing directory r*   r,   z%s/z  %sr/   N)r   r   r
   r&   r   r   r0   r   r2   r   r   r   Ú!test_existing_path_FileLinks_repr‰   s    



*r>   c                  C   sj   t ƒ } t| d�}t| d�}dd„ }tj| |d�}t|ƒ}| d¡}| ¡  ddg}| ¡  ||ksfJ ‚dS )	zEFileLinks: Calling repr() functions as expected w/ alt formatter
    r*   c                 S   s   ddgS r6   r   r9   r   r   r   r;   ž   s    zGtest_existing_path_FileLinks_repr_alt_formatter.<locals>.fake_formatter)Zterminal_display_formatterr,   r7   r8   N)r   r   r
   r&   r   r   r0   r<   r   r   r   Ú/test_existing_path_FileLinks_repr_alt_formatter˜   s    


r?   c                  C   s&   t ƒ } t| d�}t ttj|j¡ dS )z-FileLinks: Raises error when passed file
    r*   N)r   r   r    r!   r"   r
   r&   r   )r#   r3   r   r   r   Útest_error_on_file_to_FileLinksª   s    
r@   c                  C   sŒ   t ƒ } t| d�}t | d�}t|d�}t | ¡}t|ƒ}| d¡}t|ƒdksTJ |ƒ‚tj| dd�}t|ƒ}| d¡}t|ƒdksˆJ |ƒ‚dS )z5FileLinks: Does not recurse when recursive=False
    r*   r,   é   F)Ú	recursiveé   N)r   r   r
   r&   Ústrr   Úlen)r#   r   ZsubtdZsubtfr   r   r   r   r   Útest_recursive_FileLinks²   s    





rF   c                  C   s   t ttƒdƒ} tj| d� d S )Nztest.wav©Úfilename)Úpjoinr   Ú__file__r
   ÚAudio)Úpathr   r   r   Útest_audio_from_fileÂ   s    rM   c                   @   sP   e Zd Zedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZed	d
„ ƒZdd„ Z	dS )ÚTestAudioDataWithNumpyc                 C   s2   t ƒ }tj|dd�}tt|jƒƒt|ƒks.J ‚d S ©NéD¬  ©Úrate)Úget_test_toner
   rK   rE   Úread_wavÚdata©ÚselfÚ	test_toneÚaudior   r   r   Útest_audio_from_numpy_arrayÈ   s    z2TestAudioDataWithNumpy.test_audio_from_numpy_arrayc                 C   s6   t ƒ }tjt|ƒdd�}tt|jƒƒt|ƒks2J ‚d S rO   )rS   r
   rK   ÚlistrE   rT   rU   rV   r   r   r   Útest_audio_from_listÎ   s    z+TestAudioDataWithNumpy.test_audio_from_listc                 C   s   |   ttjtƒ ¡ d S ©N)ÚassertRaisesr"   r
   rK   rS   ©rW   r   r   r   Ú/test_audio_from_numpy_array_without_rate_raisesÔ   s    zFTestAudioDataWithNumpy.test_audio_from_numpy_array_without_rate_raisesc                 C   sP   t  t j¡j}dD ]8}tjt|ƒdd�}t  t  t|j	ƒ¡¡}||ksJ ‚qd S )N)r/   ç      à?rC   rP   rQ   )
ÚnumpyÚiinfoÚint16Úmaxr
   rK   rS   ÚabsrT   rU   )rW   Úexpected_max_valueÚscalerY   Úactual_max_valuer   r   r   Útest_audio_data_normalizationØ   s
    z4TestAudioDataWithNumpy.test_audio_data_normalizationc                 C   sr   t  t j¡j}dD ]Z}t|ƒ}t  t  |¡¡}t|| ƒ}tj|ddd�}t  t  t	|j
ƒ¡¡}||ksJ ‚qd S )N)r/   ra   gš™™™™™É?rP   F©rR   Ú	normalize)rb   rc   rd   re   rS   rf   Úintr
   rK   rT   rU   )rW   Z	max_int16rh   rX   Ztest_tone_max_absrg   rY   ri   r   r   r   Ú%test_audio_data_without_normalizationà   s    z<TestAudioDataWithNumpy.test_audio_data_without_normalizationc                 C   s$   |   tdd„ ¡ |   tdd„ ¡ d S )Nc                   S   s   t jdgddd�S )Ngj¼t“ð?rP   Frk   ©r
   rK   r   r   r   r   Ú<lambda>î   ó    zfTestAudioDataWithNumpy.test_audio_data_without_normalization_raises_for_invalid_data.<locals>.<lambda>c                   S   s   t jdgddd�S )Ngj¼t“ð¿rP   Frk   ro   r   r   r   r   rp   ñ   rq   )r^   r"   r_   r   r   r   Ú=test_audio_data_without_normalization_raises_for_invalid_dataë   s    þþzTTestAudioDataWithNumpy.test_audio_data_without_normalization_raises_for_invalid_dataN)
Ú__name__Ú
__module__Ú__qualname__r   rZ   r\   r`   rj   rn   rr   r   r   r   r   rN   Æ   s   





rN   c                  C   s>   zdd l } t dtjtd�¡W S  ty8   dd„  Y S 0 d S )Nr   znumpy.array)Zside_effectc                 S   s   | S r]   r   )Úxr   r   r   rp   ø   rq   z.simulate_numpy_not_installed.<locals>.<lambda>)rb   r   ÚpatchZ	MagicMockÚImportErrorÚModuleNotFoundError)rb   r   r   r   Úsimulate_numpy_not_installedó   s
    rz   c                   @   s   e Zd Zedd„ ƒZdS )ÚTestAudioDataWithoutNumpyc                    s(   t tƒ ƒgd ‰ |  t‡ fdd„¡ d S )NrC   c                      s   t jˆ dd�S rO   ro   r   ©Zstereo_signalr   r   rp     rq   zMTestAudioDataWithoutNumpy.test_audio_raises_for_nested_list.<locals>.<lambda>)r[   rS   r^   Ú	TypeErrorr_   r   r|   r   Ú!test_audio_raises_for_nested_listþ   s    z;TestAudioDataWithoutNumpy.test_audio_raises_for_nested_listN)rs   rt   ru   r   r~   r   r   r   r   r{   ú   s   r{   r/   c                 C   s&   t  dt j d t  ddd¡ ¡|  S )NrC   i¸  r   r/   rP   )rb   ÚsinÚpiZlinspace)rh   r   r   r   rS     s    rS   c                 C   s`   t  t| ƒ¡�>}| | ¡ ¡}| ¡ | ¡  }t d| |¡W  d   ƒ S 1 sR0    Y  d S )Nz<%sh)ÚwaveÚopenr	   Z
readframesZ
getnframesZgetnchannelsÚstructÚunpack)rU   Z	wave_fileZ	wave_dataZnum_samplesr   r   r   rT     s    rT   c                  C   s"   t jtd�} |  ¡  d¡sJ ‚d S )NrG   z<style>)r
   ZCoderJ   r   r   )Úcr   r   r   Útest_code_from_file  s    r†   )r/   ),Ú__doc__Útempfiler   r   Úos.pathr   r   rI   r   r   Zunittestr   r   rƒ   r�   Úior	   r    rb   rx   ZIPython.libr
   ZIPython.testing.decoratorsr   r   r   r   r   r$   r(   r)   r5   r=   r>   r?   r@   rF   rM   rN   rz   r{   rS   rT   r†   r   r   r   r   Ú<module>   sH   



-	