U
    1ea$                     @   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k
r   Y nX 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   B/tmp/pip-unpacked-wheel-3hxk2k58/IPython/lib/tests/test_display.pytest_instantiation_FileLink,   s    
r   c                  C   s    t d} |  dstdS )zEFileLink: Calling _repr_html_ on non-existent files returns a warningr   zPath (<tt>example.txt</tt>)N)r
   r   _repr_html_
startswithAssertionErrorr   r   r   r   *test_warning_on_non_existent_path_FileLink2   s    
r   c                  C   s:   t  } t| j}| }d| j| jf }||ks6t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   r   tfr   actualexpectedr   r   r   test_existing_path_FileLink8   s
    r   c                  C   s0   t  } t| j}t|}| j}||ks,tdS )zHFileLink: Calling repr() functions as expected on existing filepath
    N)r   r
   r   r   reprr   r   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t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   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st	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   replacer   r$   tf1tf2r   r   r   r   r   r   test_existing_path_FileLinksa   s"    



r6   c                  C   sj   t  } t| d}t| d}dd }tj| |d}| }|d}|  ddg}|  ||ksft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-   r8   r9   N)r   r   r
   r'   r   r   r1   r   r$   r4   r5   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|tdS )zFFileLinks: Calling repr() functions as expected on existing directory r+   r-   z%s/z  %sr0   N)	r   r   r
   r'   r   r   r1   r   r   r3   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sftdS )	zEFileLinks: Calling repr() functions as expected w/ alt formatter
    r+   c                 S   s   ddgS r7   r   r:   r   r   r   r<      s    zGtest_existing_path_FileLinks_repr_alt_formatter.<locals>.fake_formatter)Zterminal_display_formatterr-   r8   r9   N)r   r   r
   r'   r   r   r1   r   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$   r4   r   r   r   test_error_on_file_to_FileLinks   s    
rA   c                  C   s   t  } t| d}t | d}t|d}t| }t|}|d}t|dksTt|tj| dd}t|}|d}t|dkst|dS )z5FileLinks: Does not recurse when recursive=False
    r+   r-      F)	recursive   N)r   r   r
   r'   strr   lenr   )r$   r   ZsubtdZsubtfr   r   r   r   r   test_recursive_FileLinks   s    





rG   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    rN   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.td S ND  rate)get_test_toner
   rL   rF   read_wavdatar   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s2td S rP   )rT   r
   rL   listrF   rU   rV   r   rW   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
   rL   rT   rX   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t
qd S )N)r0         ?rD   rQ   rR   )numpyiinfoint16maxr
   rL   rT   absrU   rV   r   )rX   expected_max_valuescalerZ   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tqd S )N)r0   rb   g?rQ   FrS   	normalize)rc   rd   re   rf   rT   rg   intr
   rL   rU   rV   r   )rX   Z	max_int16ri   rY   Ztest_tone_max_absrh   rZ   rj   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 )Ngjt?rQ   Frl   r
   rL   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 )NgjtrQ   Frl   rp   r   r   r   r   rq      rr   )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   r[   r]   ra   rk   ro   rs   r   r   r   r   rO      s   





rO   c                  C   s@   zdd l } tdtjtdW S  tk
r:   dd  Y S X d S )Nr   znumpy.array)Zside_effectc                 S   s   | S r^   r   )xr   r   r   rq      rr   z.simulate_numpy_not_installed.<locals>.<lambda>)rc   r   patchZ	MagicMockImportErrorModuleNotFoundError)rc   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 )NrD   c                      s   t j ddS rP   rp   r   Zstereo_signalr   r   rq     rr   zMTestAudioDataWithoutNumpy.test_audio_raises_for_nested_list.<locals>.<lambda>)r\   rT   r_   	TypeErrorr`   r   r}   r   !test_audio_raises_for_nested_list   s    z;TestAudioDataWithoutNumpy.test_audio_raises_for_nested_listN)rt   ru   rv   r   r   r   r   r   r   r|      s   r|   r0   c                 C   s&   t dt j d t ddd |  S )NrD   i  r   r0   rQ   )rc   sinpiZlinspace)ri   r   r   r   rT     s    rT   c              
   C   sT   t t| <}|| }| |  }td| |W  5 Q R  S Q R X d S )Nz<%sh)waveopenr	   Z
readframesZ
getnframesZgetnchannelsstructunpack)rV   Z	wave_fileZ	wave_dataZnum_samplesr   r   r   rU     s    rU   c                  C   s"   t jtd} |  dstd S )NrH   z<style>)r
   ZCoderK   r   r   r   )cr   r   r   test_code_from_file  s    r   )r0   ),__doc__tempfiler   r   os.pathr   r   rJ   r   r   Zunittestr   r   r   r   ior	   r!   rc   ry   ZIPython.libr
   ZIPython.testing.decoratorsr   r   r   r   r    r%   r)   r*   r6   r>   r?   r@   rA   rG   rN   rO   r{   r|   rT   rU   r   r   r   r   r   <module>   sH   



-	