o
    Ďia$                     @   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   Z/home/jeff/fluffinator/venv/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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"   



r8   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   fnamesincluded_suffixesr   r   r   fake_formatter}      zBtest_existing_path_FileLinks_alt_formatter.<locals>.fake_formatter)notebook_display_formatterr/   r;   r<   N)r   r   r
   r(   r   r   r3   r%   r6   r7   r@   r   r   r   r   r   r   *test_existing_path_FileLinks_alt_formatterw      


rD   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  %sr2   N)r   r   r
   r(   r    r   r3   r   r5   r   r   r   !test_existing_path_FileLinks_repr   s   



*rF   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   r9   r:   r   r=   r   r   r   r@      rA   zGtest_existing_path_FileLinks_repr_alt_formatter.<locals>.fake_formatter)terminal_display_formatterr/   r;   r<   N)r   r   r
   r(   r    r   r3   rC   r   r   r   /test_existing_path_FileLinks_repr_alt_formatter   rE   rH   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%   r6   r   r   r   test_error_on_file_to_FileLinks   s   
rI   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   subtdsubtfr   r   r   r   r   test_recursive_FileLinks   s   





rQ   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   rX   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
   rV   rN   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 rZ   )r^   r
   rV   listrN   r_   r`   ra   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
   rV   r^   rb   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)r2         ?rL   r[   r\   )
numpyiinfoint16maxr
   rV   r^   absr_   r`   )rb   expected_max_valuescalerd   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)r2   rl   g?r[   Fr]   	normalize)rm   rn   ro   rp   r^   rq   intr
   rV   r_   r`   )rb   	max_int16rs   rc   test_tone_max_absrr   rd   rt   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?r[   Frv   r
   rV   r   r   r   r   <lambda>       zfTestAudioDataWithNumpy.test_audio_data_without_normalization_raises_for_invalid_data.<locals>.<lambda>c                   S   r|   )Ngjtr[   Frv   r}   r   r   r   r   r~      r   )ri   r$   rj   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   re   rg   rk   ru   r{   r   r   r   r   r   rY      s    





rY   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)side_effectc                 S   s   | S rh   r   )xr   r   r   r~      s    z.simulate_numpy_not_installed.<locals>.<lambda>)rm   r   patch	MagicMockImportErrorModuleNotFoundError)rm   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 )NrL   c                      s   t j ddS rZ   r}   r   stereo_signalr   r   r~     s    zMTestAudioDataWithoutNumpy.test_audio_raises_for_nested_list.<locals>.<lambda>)rf   r^   ri   	TypeErrorrj   r   r   r   !test_audio_raises_for_nested_list   s   z;TestAudioDataWithoutNumpy.test_audio_raises_for_nested_listN)r   r   r   r   r   r   r   r   r   r      s    r   r2   c                 C   s&   t dt j d t ddd |  S )NrL   i  r   r2   r[   )rm   sinpilinspace)rs   r   r   r   r^     s   &r^   c                 C   s`   t t| }|| }| |  }td| |W  d    S 1 s)w   Y  d S )Nz<%sh)waveopenr	   
readframes
getnframesgetnchannelsstructunpack)r`   	wave_file	wave_datanum_samplesr   r   r   r_     s
   $r_   c                  C   s"   t jtd} |  dsJ d S )NrR   z<style>)r
   CoderU   r   r   )cr   r   r   test_code_from_file  s   r   )r2   ),__doc__tempfiler   r   os.pathr   r   rT   r   r   unittestr   r   r   r   ior	   r"   rm   r   IPython.libr
   IPython.testing.decoratorsr   r   r   r   r!   r&   r+   r,   r8   rD   rF   rH   rI   rQ   rX   rY   r   r   r^   r_   r   r   r   r   r   <module>   sL    



-	