U
    {±Ëh#  ã                   @   s€   d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	m
Z
mZmZmZ dZeeƒZeefZG dd„ dƒZG dd„ dejƒZdS )	é    N)ÚStringIO)	Ú_CACHE_NAME_PATTERNÚ_build_cache_nameÚ_find_cache_attributesÚbuild_hex_versionÚcached_methodÚclear_method_cachesÚtry_finally_contextmanagerÚprint_versionÚnormalise_float_reprÚcached_nextc                   @   s   e Zd Zedd„ ƒZdS )ÚCachedc                 C   s   t |ƒS ©N)Únext)ÚselfÚx© r   ú@/tmp/pip-unpacked-wheel-fhl22ezh/Cython/Tests/TestCythonUtils.pyr      s    zCached.cached_nextN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r      s   r   c                   @   sl   e Z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 )ÚTestCythonUtilsc                 C   sD   |   dtdƒ¡ |   dtdƒ¡ |   dtdƒ¡ |   dtdƒ¡ d S )	NZ
0x001D00A1z0.29a1Z
0x001D03C4z	0.29.3rc4Z
0x001D00F0z0.29Z
0x040000F0z4.0)ÚassertEqualr   )r   r   r   r   Útest_build_hex_version   s    z&TestCythonUtils.test_build_hex_versionc                 C   s8   d}t |ƒ}t |¡}|  |d ¡ |  | d¡|¡ d S )NZfooé   )r   r   ÚmatchZassertIsNotr   Úgroup)r   Úmethod_nameÚ
cache_namer   r   r   r   Útest_cache_method_name   s
    
z&TestCythonUtils.test_cache_method_namec                 C   s8   t ƒ }|  t|tƒ¡ |  t|tƒ¡ |  |tƒ ¡ d S r   )r   ÚassertFalseÚhasattrÚ
CACHE_NAMEÚ
assertTrueÚMETHOD_NAMEÚset_of_names_equalÚset)r   Úobjr   r   r   Útest_requirements_for_Cached&   s    z,TestCythonUtils.test_requirements_for_Cachedc                 C   s   |   tt|ƒƒ|¡ d S r   )r   r&   r   )r   r'   Úvaluer   r   r   r%   -   s    z"TestCythonUtils.set_of_names_equalc                 C   sR   t ƒ }d}t|ƒ}t|ti ƒ t||i ƒ |  t||ƒ¡ |  |t||fh¡ d S ©NÚbar)r   r   Úsetattrr"   r    r!   r%   ÚNAMES)r   r'   r   r   r   r   r   Útest_find_cache_attributes0   s    z*TestCythonUtils.test_find_cache_attributesc                 C   s„   t ƒ }ttdƒƒ}|fdi}|  | |¡d¡ |  |th¡ |  t|tƒ|¡ |  | |¡d¡ |  |th¡ |  t|tƒ|¡ d S )Né   r   )	r   ÚiterÚranger   r   r%   r-   Úgetattrr"   ©r   r'   r)   Úcacher   r   r   Útest_cached_method;   s    
z"TestCythonUtils.test_cached_methodc                 C   sr   t ƒ }ttdƒƒ}|fdi}| |¡ t|ƒ |  |tƒ ¡ |  | |¡d¡ |  |th¡ |  t	|t
ƒ|¡ d S )Nr/   r   )r   r0   r1   r   r   r%   r&   r   r-   r2   r"   r3   r   r   r   Útest_clear_method_cachesJ   s    

z(TestCythonUtils.test_clear_method_cachesc                 C   s`   t ƒ }d}t|ƒ}||f}t||tƒ ƒ |  t||ƒ¡ |  ||h¡ t|ƒ |  ||h¡ d S r*   )r   r   r,   Úobjectr    r!   r%   r   )r   r'   r   r   Únamesr   r   r   Ú,test_clear_method_caches_with_missing_methodX   s    z<TestCythonUtils.test_clear_method_caches_with_missing_methodc              
      sb  g ‰ t ‡ fdd„ƒ}|ddddd(}ˆ dgks4t‚|  |d	d
dif¡ W 5 Q R X ˆ ddgksbt‚G dd„ dtƒ}ˆ d d …= |  |¡Z |dddd0}ˆ dgks¦t‚|  |dddif¡ |dƒ‚W 5 Q R X ˆ ddgksÜt‚W 5 Q R X ˆ d d …= |  t¡^ |dddd2}ˆ dgkst‚|  |dddif¡ tdƒ‚W 5 Q R X ˆ ddgksTt‚W 5 Q R X d S )Nc                  ?   s"   ˆ   d¡ | |fV  ˆ   d¡ d S )NÚenterÚexit)Úappend)ÚargsÚkwargs©Zstatesr   r   Úgenh   s    

z<TestCythonUtils.test_try_finally_contextmanager.<locals>.genr   é   r/   é   )r   r:   )r   rA   r/   r   r;   c                   @   s   e Zd ZdS )zDTestCythonUtils.test_try_finally_contextmanager.<locals>.MyExceptionN)r   r   r   r   r   r   r   ÚMyExceptions   s   rC   )Úy)r   rA   rD   zFAIL INSIDEÚSTOP)r	   ÚAssertionErrorr   ÚRuntimeErrorÚassertRaisesÚStopIteration)r   r@   Z	call_argsrC   r   r?   r   Útest_try_finally_contextmanagerf   s,    

z/TestCythonUtils.test_try_finally_contextmanagerc                 C   sz   t j}t j}tƒ  }t _tƒ  }t _z
tƒ  W 5 |t _|t _X | ¡ }| ¡ }ddlm} |  ||¡ |rv|  ||¡ d S )NrA   ©Ú__version__)	ÚsysÚstderrÚstdoutr   r
   ÚgetvalueÚ rL   ÚassertIn)r   Úorig_stderrÚorig_stdoutrN   rO   Úversionr   r   r   Útest_print_version†   s    
z"TestCythonUtils.test_print_versionc                 C   sn   t j}t j}tƒ  } t _t _z
tƒ  W 5 |t _|t _X | ¡ }ddlm} |  ||¡ |  	| 
|¡d¡ d S )NrA   rK   r   )rM   rN   rO   r   r
   rP   rQ   rL   rR   r   Úcount)r   rS   rT   rO   rU   r   r   r   Útest_print_version_stdouterr™   s    
z,TestCythonUtils.test_print_version_stdouterrc                 C   sŽ   ddddddddd	d
dddddddddddg}|D ]V\}}|   t|ƒt|ƒ¡ t|ƒ}|   t|ƒt|ƒ¡ |   ||d|||t|ƒf ¡ q2d S )N)ú.0rY   )z.000000rY   )ú.1rZ   )ú1.r[   )z1.0r[   )z1.000000000000000000000r[   )z-00000000000000000000001.000000000000000000000r[   )ú
12345.0025r\   )Z1E5z100000.)z.1E-5z.000001)z1.1E-5z.000011)z12.3E-5z.000123)z.1E10z1000000000.)z1.1E10z11000000000.)z123.4E10z1234000000000.)z	123.456E0z123.456)z
123.456E-1z12.3456)z
123.456E-2z1.23456)z	123.456E1z1234.56)z	123.456E2z12345.6)z2.1E80zR210000000000000000000000000000000000000000000000000000000000000000000000000000000.z.normalise_float_repr(%r) == %r != %r  (%.330f))r   Úfloatr   )r   ZexamplesZ	float_strZnorm_strÚresultr   r   r   Útest_normalise_float_repr©   s>    ë þz)TestCythonUtils.test_normalise_float_reprN)r   r   r   r   r   r(   r%   r.   r5   r6   r9   rJ   rV   rX   r_   r   r   r   r   r      s    r   )rM   ZunittestÚior   ZCython.Utilsr   r   r   r   r   r   r	   r
   r   r$   r"   r-   r   ZTestCaser   r   r   r   r   Ú<module>   s   ,