B
    u9av<                 @   s  d Z ddlZddlZddlZddlZddlZddlZddlmZm	Z	 ddl
mZ ddl
mZ ddl
mZ dd ZG d	d
 d
ejZdd ZG dd dejZG dd dZG dd dZG dd dZG dd deZG dd dZG dd dejZedkre  dS )z3
  Test cases for the repr module
  Nick Mathewson
    N)create_empty_fileverbose)repr)Repr)recursive_reprc             C   s    d}xt | D ]
}|f}qW |S )N )range)Znestingtir   r   "/usr/lib/python3.7/test_reprlib.pynestedTuple   s    
r   c               @   s|   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )	ReprTestsc             C   s   | j }|tdd |tdd d}t|d d d t|dd   }|t|| |td	td	 d
}t|d d d t|dd   }|t|| d S )Nabcz'abc'Zabcdefghijklmnopz'abcdefghijklmnop'Z<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbb   z...iz"'z""""""""""""""""""""""""""""""'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''')assertEqualrr   )selfeqsexpectedr   r   r   test_string   s    $$zReprTests.test_stringc             C   sZ   | j }|tdd d}|t|d t }d|_t|d d d }|||| d S )N)   z(1,))r         z	(1, 2, 3)r   z...))r   r   r   maxtupler   )r   r   Zt3Zr2r   r   r   r   
test_tuple)   s    zReprTests.test_tuplec          
   C   s  ddl m } ddlm} | j}|tdd |tdd |tdd	 |td
d |tdd |tg d |tdgd |tdddgd |tddddddgd |tdddddddgd |ttg d |ttdgd |ttdddgd |ttddddddgd |ttdddddddgd |ttg d |ttdgd  |ttdddgd! |ttddddddgd" |ttdddddddgd# |t|dddddddgd$ |ti d% ddddd&}|t|d' d|d(< |t|d) |t|d*d+ |t|d*dgd, |t|d*ddgd- |t|d*dddgd. |t|d*ddddgd/ |t|d*dddddgd0 |t|d*ddddddgd1 d S )2Nr   )array)dequer   z())r   z(1,))r   r   r   z	(1, 2, 3))r   r   r            z(1, 2, 3, 4, 5, 6))r   r   r   r   r    r!      z(1, 2, 3, 4, 5, 6, ...)z[]r   z[1]r   r   z	[1, 2, 3]r   r    r!   z[1, 2, 3, 4, 5, 6]r"   z[1, 2, 3, 4, 5, 6, ...]zset()z{1}z	{1, 2, 3}z{1, 2, 3, 4, 5, 6}z{1, 2, 3, 4, 5, 6, ...}zfrozenset()zfrozenset({1})zfrozenset({1, 2, 3})zfrozenset({1, 2, 3, 4, 5, 6})z"frozenset({1, 2, 3, 4, 5, 6, ...})zdeque([1, 2, 3, 4, 5, 6, ...])z{})ZaliceZbobZcharlesZdavez/{'alice': 1, 'bob': 2, 'charles': 3, 'dave': 4}Zarthurz6{'alice': 1, 'arthur': 1, 'bob': 2, 'charles': 3, ...}r
   z
array('i')zarray('i', [1])zarray('i', [1, 2])zarray('i', [1, 2, 3])zarray('i', [1, 2, 3, 4])zarray('i', [1, 2, 3, 4, 5])z array('i', [1, 2, 3, 4, 5, ...]))r   collectionsr   r   r   set	frozenset)r   r   r   r   dr   r   r   test_container5   sJ       zReprTests.test_containerc          	   C   sd   | j }|tdhd |tdddhd |tddddddhd	 |tddddddd
hd d S )Nr   z{1}r   r   z	{1, 2, 3}r   r    r!   z{1, 2, 3, 4, 5, 6}r"   z{1, 2, 3, 4, 5, 6, ...})r   r   )r   r   r   r   r   test_set_literalj   s
    zReprTests.test_set_literalc          
   C   st   | j }|ttdhd |ttdddhd |ttddddddhd	 |ttddddddd
hd d S )Nr   zfrozenset({1})r   r   zfrozenset({1, 2, 3})r   r    r!   zfrozenset({1, 2, 3, 4, 5, 6})r"   z"frozenset({1, 2, 3, 4, 5, 6, ...}))r   r   r%   )r   r   r   r   r   test_frozensetq   s
    zReprTests.test_frozensetc             C   sz   | j }|tdtd |tdtd |tdtd dd }t|d d d t|dd   }|t|| d S )N{   gUUUUUU?
   d      z...i)r   r   r   )r   r   nr   r   r   r   test_numbersx   s    $zReprTests.test_numbersc             C   s   | j }td}|t|t| td}t|d d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xxxxxxxxxxxxxxxxxxxxxxxr   z...iz&<ClassWithFailingRepr instance at %#x>z<class >   )r   ClassWithReprr   r   ClassWithFailingReprid
assertTrue
startswithendswithZassertInfind)r   r   Zi1Zi2r   Zi3r   r   r   r   test_instance   s    $zReprTests.test_instancec             C   s"   t dd }| |d| d S )Nc             S   s   | S )Nr   )xr   r   r   <lambda>       z'ReprTests.test_lambda.<locals>.<lambda>z0<function ReprTests.test_lambda.<locals>.<lambda)r   r6   r7   )r   r   r   r   r   test_lambda   s    zReprTests.test_lambdac             C   s.   | j }|ttd | tdjd d S )Nz<built-in function hash> z*<built-in method split of str object at 0x)r   r   hashr6   splitr7   )r   r   r   r   r   test_builtin_function   s    zReprTests.test_builtin_functionc             C   sF   | j }|ttdd |ttddd |ttdddd d S )Nr   zrange(0, 1)r   zrange(1, 2)r   r   zrange(1, 4, 3))r   r   r   )r   r   r   r   r   
test_range   s    zReprTests.test_rangec             C   s   | j }|tg ggggggd |tg gggggggd |ttdd |ttdd |ttdtdid |ttdtdid	 |ti ggggggd
 |ti gggggggd d S )Nz[[[[[[[]]]]]]]z[[[[[[[...]]]]]]]r!   z(((((((),),),),),),)r"   z(((((((...),),),),),),)r    z&{((((((),),),),),): ((((((),),),),),)}z,{((((((...),),),),),): ((((((...),),),),),)}z[[[[[[{}]]]]]])r   r   r   )r   r   r   r   r   test_nesting   s    zReprTests.test_nestingc             C   s8   dd }| j d }| t|d | t|d d S )Nc                 s   d  fdd} | S )N*   c                  s    S )Nr   r   )r;   r   r   inner   s    z4ReprTests.test_cell.<locals>.get_cell.<locals>.innerr   )rF   r   )r;   r   get_cell   s    z%ReprTests.test_cell.<locals>.get_cellr   z6<cell at 0x[0-9A-Fa-f]+: int object at 0x[0-9A-Fa-f]+>z<cell at 0x.*\.\.\..*>)__closure__ZassertRegexr   r   )r   rG   r;   r   r   r   	test_cell   s    zReprTests.test_cellc             C   sd   | j }|ttjd G dd d}t|j}| t|d t|j}| t|d d S )Nz"<method 'items' of 'dict' objects>c               @   s   e Zd Zdd ZdS )z%ReprTests.test_descriptors.<locals>.Cc             S   s   d S )Nr   )clsr   r   r   foo   r=   z)ReprTests.test_descriptors.<locals>.C.fooN)__name__
__module____qualname__rK   r   r   r   r   C   s   rO   z<staticmethod object at 0xz<classmethod object at 0x)	r   r   dictitemsstaticmethodrK   r6   r7   classmethod)r   r   rO   r;   r   r   r   test_descriptors   s    

zReprTests.test_descriptorsc             C   s<   t dddg}t|}ddd}t| t| t| d S )Ny              ?y               @y              @r   r   )y              ?y               @)r$   r%   r   )r   r;   yzr   r   r   test_unsortable   s    
zReprTests.test_unsortableN)rL   rM   rN   r   r   r'   r(   r)   r/   r:   r>   rB   rC   rD   rI   rT   rW   r   r   r   r   r      s   5
r   c          	   C   s(   t | ddd}|| W d Q R X d S )NwASCII)encoding)openwrite)pathtextfpr   r   r   
write_file   s    r`   c               @   st   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	
ddd Zdd Zdd Zdd Ze	
ddd ZdS )LongReprTest3areallylongpackageandmodulenametotestreprtruncationc             C   s   t j| j| _t j| j| j| _tj| jdd t | j t	t j| jd tj| jdd t | j t	t j| jd t 
 | _tjd| j t  d S )NT)ignore_errorsz__init__.pyr   )osr]   joinlongnamepkgname
subpkgnameshutilZrmtreemkdirr   getcwdheresysinsert	importlibinvalidate_caches)r   r   r   r   setUp   s    
zLongReprTest.setUpc             C   s   g }x@t | jD ]0\}}}x$|| D ]}|t j|| q&W qW || j |  |  x.|D ]&}t j|rt 	| qht 
| qhW tjd= d S )Nr   )rd   walkrg   appendr]   re   sortreverseisdirrmdirremoverm   )r   actionsdirpathZdirnames	filenamesnamepr   r   r   tearDown   s    
zLongReprTest.tearDownc             C   s   t | j}|dt | jd  7 }|t |d t d 7 }|t tjd t d }tjdkrv|dkrv| d|  ntjdkrt	rt
d| d S )	Nr   r   z.pyzx.pynti  zDtest paths too long (%d characters) for Windows' 260 character limitzcached_path_len =)lenrl   rf   ro   utilcache_from_sourcerd   r|   ZskipTestr   print)r   Zmodule_nameZsource_path_lenZcached_path_lenr   r   r   _check_path_limitations  s    

z$LongReprTest._check_path_limitationsc             C   st   d | _ | | j ttj| j| jd  t	  ddl
m} |}| t|d|j|jf  | ttd d S )Nz.pyr   )rb   z<module %r from %r>z<module 'sys' (built-in)>)ZmaxDiffr   rg   r   rd   r]   re   rh   ro   rp   gareallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncationrb   r   r   rL   __file__rm   )r   rb   moduler   r   r   test_module  s    zLongReprTest.test_modulec             C   sT   |  d | j}ttj| jdd t  ddl	m
} |t|j
d|j  d S )NrK   zfoo.pyzclass foo(object):
    pass
r   )rK   z<class '%s.foo'>)r   r   r`   rd   r]   re   rh   ro   rp   r   rK   r   rL   )r   r   rK   r   r   r   	test_type  s    

zLongReprTest.test_typezneed a suitable objectc             C   s   d S )Nr   )r   r   r   r   test_object*  s    zLongReprTest.test_objectc             C   sP   |  d ttj| jdd t  ddlm	} | 
t|j	d|j  d S )Nbarzbar.pyzclass bar:
    pass
r   )r   z<class '%s.bar'>)r   r`   rd   r]   re   rh   ro   rp   r   r   r   r   rL   )r   r   r   r   r   
test_class0  s    
zLongReprTest.test_classc             C   sZ   |  d ttj| jdd t  ddlm	} |	 }| 
t|d|j  d S )Nbazzbaz.pyzclass baz:
    pass
r   )r   z<%s.baz object at 0x)r   r`   rd   r]   re   rh   ro   rp   r   r   r6   r   r7   rL   )r   r   Zibazr   r   r   r:   ;  s    
zLongReprTest.test_instancec             C   s   |  d | j}ttj| jdd t  ddl	m
} t|jj}| |d| | }t|j}| |d|jf | d S )Nquxzqux.pyzclass aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
    def amethod(self): pass
r   )r   z<function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethoda  <bound method aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.amethod of <%s.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa object at 0x)r   r   r`   rd   r]   re   rh   ro   rp   r   r   r   ZaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaZamethodr6   r7   rL   )r   r   r   r   Ziquxr   r   r   test_methodG  s    

zLongReprTest.test_methodz1needs a built-in function with a really long namec             C   s   d S )Nr   )r   r   r   r   rB   Z  s    z"LongReprTest.test_builtin_functionN)rL   rM   rN   rf   rq   r~   r   r   r   unittestskipr   r   r:   r   rB   r   r   r   r   ra      s   
ra   c               @   s   e Zd Zdd Zdd ZdS )r3   c             C   s
   || _ d S )N)r   )r   r   r   r   r   __init__`  s    zClassWithRepr.__init__c             C   s
   d| j  S )NzClassWithRepr(%r))r   )r   r   r   r   __repr__b  s    zClassWithRepr.__repr__N)rL   rM   rN   r   r   r   r   r   r   r3   _  s   r3   c               @   s   e Zd Zdd ZdS )r4   c             C   s   t dd S )Nz+This should be caught by Repr.repr_instance)	Exception)r   r   r   r   r   g  s    zClassWithFailingRepr.__repr__N)rL   rM   rN   r   r   r   r   r   r4   f  s   r4   c               @   s.   e Zd ZdZdd Zdd Ze dd ZdS )	MyContainerz"Helper class for TestRecursiveReprc             C   s   t || _d S )N)listvalues)r   r   r   r   r   r   l  s    zMyContainer.__init__c             C   s   | j | d S )N)r   rs   )r   valuer   r   r   rs   n  s    zMyContainer.appendc             C   s   dd tt| j d S )N<z, r1   )re   mapstrr   )r   r   r   r   r   p  s    zMyContainer.__repr__N)rL   rM   rN   __doc__r   rs   r   r   r   r   r   r   r   j  s   r   c               @   s   e Zd Zeddd ZdS )MyContainer2z+++c             C   s   dd tt| j d S )Nr   z, r1   )re   r   r   r   )r   r   r   r   r   u  s    zMyContainer2.__repr__N)rL   rM   rN   r   r   r   r   r   r   r   t  s   r   c               @   s"   e Zd Zdd ZeZe eZdS )MyContainer3c             C   s   dS )zTest document contentNr   )r   r   r   r   r   z  s    zMyContainer3.__repr__N)rL   rM   rN   r   wrappedr   wrapperr   r   r   r   r   y  s   r   c               @   s   e Zd Zdd Zdd ZdS )TestRecursiveReprc             C   sx   t td}|| |d || | t|d ttd}|| |d || | t|d d S )NZabcder;   z<a, b, c, d, e, ..., x, ...>z<a, b, c, d, e, +++, x, +++>)r   r   rs   r   r   r   )r   mr   r   r   test_recursive_repr  s    





z%TestRecursiveRepr.test_recursive_reprc             C   sB   ddl m} tj}tj}x$|D ]}| t||t|| qW d S )Nr   )WRAPPER_ASSIGNMENTS)	functoolsr   r   r   r   ZassertIsgetattr)r   assignedr   r   r|   r   r   r   test_assigned_attributes  s
    
z*TestRecursiveRepr.test_assigned_attributesN)rL   rM   rN   r   r   r   r   r   r   r     s   r   __main__)r   rm   rd   ri   ro   importlib.utilr   Ztest.supportr   r   reprlibr   r   r   r   r   ZTestCaser   r`   ra   r3   r4   r   r   r   r   rL   mainr   r   r   r   <module>   s0    C 

