o
    Zha$                     @   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yA   Y nw 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   U/var/www/html/lang_env/lib/python3.10/site-packages/IPython/lib/tests/test_display.pytest_instantiation_FileLink,   s   
r   c                  C       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      
r   c                  C   s:   t  } t| j}| }d| j| jf }||ksJ 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   r   )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[   r   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sLJ 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"   



r7   c                  C   sj   t  } t| d}t| d}dd }tj| |d}| }|d}|  ddg}|  ||ks3J dS )	zMFileLinks: Calling _repr_html_ functions as expected w/ an alt formatter
    r,   c                 S      ddgS Nhelloworldr   r   fnamesZincluded_suffixesr   r   r   fake_formatter}      zBtest_existing_path_FileLinks_alt_formatter.<locals>.fake_formatter)Znotebook_display_formatterr.   r:   r;   N)r   r   r
   r(   r   r   r2   r%   r5   r6   r>   r   r   r   r   r   r   *test_existing_path_FileLinks_alt_formatterw      


rA   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  %sr1   N)r   r   r
   r(   r    r   r2   r   r4   r   r   r   !test_existing_path_FileLinks_repr   s   



*rC   c                  C   sj   t  } t| d}t| d}dd }tj| |d}t|}|d}|  ddg}|  ||ks3J dS )	zEFileLinks: Calling repr() functions as expected w/ alt formatter
    r,   c                 S   r8   r9   r   r<   r   r   r   r>      r?   zGtest_existing_path_FileLinks_repr_alt_formatter.<locals>.fake_formatter)Zterminal_display_formatterr.   r:   r;   N)r   r   r
   r(   r    r   r2   r@   r   r   r   /test_existing_path_FileLinks_repr_alt_formatter   rB   rD   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%   r5   r   r   r   test_error_on_file_to_FileLinks   s   
rE   c                  C   s   t  } t| d}t | d}t|d}t| }t|}|d}t|dks*J |tj| dd}t|}|d}t|dksDJ |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   





rK   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   rR   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 ND  rate)get_test_toner
   rP   rJ   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sJ d S rT   )rX   r
   rP   listrJ   rY   rZ   r[   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
   rP   rX   r\   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 ]}tjt|dd}t t t|j	}||ks%J q	d S )N)r1         ?rH   rU   rV   )
numpyiinfoint16maxr
   rP   rX   absrY   rZ   )r\   expected_max_valuescaler^   actual_max_valuer   r   r   test_audio_data_normalization   s   z4TestAudioDataWithNumpy.test_audio_data_normalizationc                 C   sr   t t jj}dD ]-}t|}t t |}t|| }tj|ddd}t t t	|j
}||ks6J q	d S )N)r1   rf   g?rU   FrW   	normalize)rg   rh   ri   rj   rX   rk   intr
   rP   rY   rZ   )r\   Z	max_int16rm   r]   Ztest_tone_max_absrl   r^   rn   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      t jdgdddS )Ngjt?rU   Frp   r
   rP   r   r   r   r   <lambda>       zfTestAudioDataWithNumpy.test_audio_data_without_normalization_raises_for_invalid_data.<locals>.<lambda>c                   S   rt   )NgjtrU   Frp   ru   r   r   r   r   rv      rw   )rc   r$   rd   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   r_   ra   re   ro   rs   rx   r   r   r   r   rS      s    





rS   c                  C   s:   zdd l } tdtjtdW S  ty   dd  Y S w )Nr   znumpy.array)Zside_effectc                 S   s   | S rb   r   )xr   r   r   rv      s    z.simulate_numpy_not_installed.<locals>.<lambda>)rg   r   patchZ	MagicMockImportErrorModuleNotFoundError)rg   r   r   r   simulate_numpy_not_installed   s   r   c                   @   s   e Zd Zedd ZdS )TestAudioDataWithoutNumpyc                    s(   t t gd  | t fdd d S )NrH   c                      s   t j ddS rT   ru   r   Zstereo_signalr   r   rv     s    zMTestAudioDataWithoutNumpy.test_audio_raises_for_nested_list.<locals>.<lambda>)r`   rX   rc   	TypeErrorrd   r   r   r   !test_audio_raises_for_nested_list   s   z;TestAudioDataWithoutNumpy.test_audio_raises_for_nested_listN)ry   rz   r{   r   r   r   r   r   r   r      s    r   r1   c                 C   s&   t dt j d t ddd |  S )NrH   i  r   r1   rU   )rg   sinpiZlinspace)rm   r   r   r   rX     s   &rX   c                 C   s`   t t| }|| }| |  }td| |W  d    S 1 s)w   Y  d S )Nz<%sh)waveopenr	   Z
readframesZ
getnframesZgetnchannelsstructunpack)rZ   Z	wave_fileZ	wave_dataZnum_samplesr   r   r   rY     s
   $rY   c                  C   s"   t jtd} |  dsJ d S )NrL   z<style>)r
   ZCoderO   r   r   )cr   r   r   test_code_from_file  s   r   )r1   ),__doc__tempfiler   r   Zos.pathr   r   rN   r   r   Zunittestr   r   r   r   ior	   r"   rg   r~   ZIPython.libr
   ZIPython.testing.decoratorsr   r   r   r   r!   r&   r*   r+   r7   rA   rC   rD   rE   rK   rR   rS   r   r   rX   rY   r   r   r   r   r   <module>   sL    



-	