B
    Ç S]þ?  ã               @   sj   d Z ddlmZ ddlmZ ye W n ek
r@   dd„ ZY nX G dd„ dejƒZG d	d
„ d
ejƒZ	dS )a  Unit tests for pytree.py.

NOTE: Please *don't* add doc strings to individual test methods!
In verbose mode, printing of the module, class and method name is much
more helpful than printing of (the first line of) the docstring,
especially when debugging a test.
é   )Úsupporté    )Úpytreec             C   s   t | ƒ}| ¡  |S )N)ÚlistÚsort)ZlstÚl© r   ú*lib/python3.7/lib2to3/tests/test_pytree.pyÚsorted   s    r
   c               @   s  e Zd 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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d0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Z d<d=„ Z!d>d?„ Z"d@dA„ Z#dBS )CÚ	TestNodesz(Unit tests for nodes (Base, Leaf, Node).c             C   s   |   ttj¡ d S )N)ÚassertRaisesÚAssertionErrorr   ZBase)Úselfr   r   r	   Útest_instantiate_base   s    zTestNodes.test_instantiate_basec             C   s,   t  dd¡}|  |jd¡ |  |jd¡ d S )Néd   Úfoo)r   ÚLeafÚassertEqualÚtypeÚvalue)r   Úl1r   r   r	   Ú	test_leaf"   s    zTestNodes.test_leafc             C   s    t  dd¡}|  t|ƒd¡ d S )Nr   r   zLeaf(100, 'foo'))r   r   r   Úrepr)r   r   r   r   r	   Útest_leaf_repr'   s    zTestNodes.test_leaf_reprc             C   s@   t  dd¡}|  t|ƒd¡ t jdddd}|  t|ƒd¡ d S )Nr   r   )ú )é
   r   )Úcontextz foo)r   r   r   Ústr)r   r   Úl2r   r   r	   Útest_leaf_str+   s    zTestNodes.test_leaf_strc             C   s&   t  dd¡}d|_|  t|ƒd¡ d S )Né   é   Zfoo_Zfoo_5)r   r   Úprefixr   r   )r   r   r   r   r	   Útest_leaf_str_numeric_value1   s    z%TestNodes.test_leaf_str_numeric_valuec             C   s\   t  dd¡}t jdddd}|  ||¡ t  dd¡}t  dd¡}|  ||¡ |  ||¡ d S )Nr   r   )r   )r   r   )r   ée   Úbar)r   r   r   ÚassertNotEqual)r   r   r   Úl3Úl4r   r   r	   Útest_leaf_equality8   s    zTestNodes.test_leaf_equalityc             C   sJ   t  dd¡}|  |jd¡ |  |j¡ d|_|  |jd¡ |  |j¡ d S )Nr   r   Ú z  ##

)r   r   r   r"   ÚassertFalseÚwas_changedÚ
assertTrue)r   r   r   r   r	   Útest_leaf_prefixA   s    zTestNodes.test_leaf_prefixc             C   sL   t  dd¡}t  dd¡}t  d||g¡}|  |jd¡ |  |j||g¡ d S )Nr   r   éÈ   r%   iè  )r   r   ÚNoder   r   Úchildren)r   r   r   Ún1r   r   r	   Ú	test_nodeI   s
    zTestNodes.test_nodec             C   sP   t  dd¡}t jdddd}t  d||g¡}|  t|ƒdt|ƒt|ƒf ¡ d S )Nr   r   r%   )r   )r   r   )r   iè  zNode(1000, [%s, %s]))r   r   r0   r   r   )r   r   r   r2   r   r   r	   Útest_node_reprP   s
    
zTestNodes.test_node_reprc             C   s@   t  dd¡}t jdddd}t  d||g¡}|  t|ƒd¡ d S )Nr   r   r%   )r   )r   r   )r   iè  zfoo bar)r   r   r0   r   r   )r   r   r   r2   r   r   r	   Útest_node_strW   s    zTestNodes.test_node_strc             C   s\   t  dd¡}|  |jd¡ t  d|g¡}|  |jd¡ d|_|  |jd¡ |  |jd¡ d S )Nr   r   r*   iè  r   )r   r   r   r"   r0   )r   r   r2   r   r   r	   Útest_node_prefix]   s    zTestNodes.test_node_prefixc             C   s¸   t jdddd}t jdddd}t  d||g¡}|  | ¡ |j¡ |  | ¡ d¡ |  | ¡ d¡ t jddd	d}t  d||g¡}|  | ¡ |j¡ |  | ¡ d¡ |  | ¡ d¡ d S )
Nr   r   Úa)r"   r%   Úbiè  r*   Úc)r   r   r0   r   Z
get_suffixr"   )r   r   r   r2   r'   Ún2r   r   r	   Útest_get_suffixf   s    zTestNodes.test_get_suffixc             C   sD   t  dd¡}t jdg dd}|  ||¡ t  dd¡}|  ||¡ d S )Niè  r   )r   )r   r   )r   ié  )r   r0   r   r&   )r   r2   r:   Ún3r   r   r	   Útest_node_equalityv   s
    zTestNodes.test_node_equalityc             C   sj   t  dd¡}t  dd¡}t  d|g¡}t  d|g¡}|  ||¡ t  dd¡}t  d|g¡}|  ||¡ d S )Nr   r   iè  r%   )r   r   r0   r   r&   )r   r   r   r2   r:   r'   r<   r   r   r	   Útest_node_recursive_equality}   s    z&TestNodes.test_node_recursive_equalityc             C   s¬   t  dd¡}t  dd¡}t  dd¡}t  d|||g¡}|  |j|||g¡ |  |jt¡ |  |j¡ t  dd¡}| 	|¡ |  |j|||g¡ |  |jt¡ |  
|j¡ d S )Nr   r   ú+r%   iè  ú-)r   r   r0   r   r1   ÚassertIsInstancer   r+   r,   Úreplacer-   )r   r   r   r'   r2   Zl2newr   r   r	   Útest_replace‡   s    
zTestNodes.test_replacec             C   sv   t  dd¡}t  dd¡}t  dd¡}t  d|||g¡}| t  dd¡t  dd¡g¡ |  t|ƒd¡ |  |jt¡ d S )Nr   r   r?   r%   iè  Ú*zfoo**bar)	r   r   r0   rB   r   r   rA   r1   r   )r   r   r   r'   r2   r   r   r	   Útest_replace_with_list•   s    z TestNodes.test_replace_with_listc             C   sp   t  dd¡}t  dd¡}t  dd¡}t  d||g¡}t  d|g¡}t  d||g¡}|  t| ¡ ƒ|||g¡ d S )Nr   r   r%   Úfooeyiè  )r   r   r0   r   r   Úleaves)r   r   r   r'   r:   r<   r2   r   r   r	   Útest_leavesŸ   s    zTestNodes.test_leavesc             C   sx   t  dd¡}t  dd¡}t  d||g¡}t  dg ¡}t  d||g¡}|  | ¡ d¡ |  | ¡ d¡ |  | ¡ d¡ d S )Nr   r   r%   iè  r    r   r   )r   r   r0   r   Zdepth)r   r   r   r:   r<   r2   r   r   r	   Ú
test_depth©   s    zTestNodes.test_depthc             C   sf   t  dd¡}t  dd¡}t  dd¡}t  d||g¡}t  d||g¡}|  t| ¡ ƒ|||||g¡ d S )Nr   r   r%   rF   iè  )r   r   r0   r   r   Z
post_order)r   r   r   r'   Úc1r2   r   r   r	   Útest_post_order´   s    zTestNodes.test_post_orderc             C   sf   t  dd¡}t  dd¡}t  dd¡}t  d||g¡}t  d||g¡}|  t| ¡ ƒ|||||g¡ d S )Nr   r   r%   rF   iè  )r   r   r0   r   r   Z	pre_order)r   r   r   r'   rJ   r2   r   r   r	   Útest_pre_order¼   s    zTestNodes.test_pre_orderc             C   sþ   t  dd¡}|  |j¡ | ¡  |  |j¡ t  dd¡}t  d|g¡}|  |j¡ | ¡  |  |j¡ t  dd¡}t  dd¡}t  dd¡}t  d|||g¡}t  d|g¡}|  |j¡ |  |j¡ |  |j¡ | ¡  |  |j¡ |  |j¡ |  |j¡ d S )Nr   Úfiè  r   r?   r%   )r   r   r+   r,   Zchangedr-   r0   )r   r   r2   r   r'   r:   r   r   r	   Útest_changedÄ   s*    zTestNodes.test_changedc             C   sD   x>dD ]6}t jdd|d}|  t|ƒ|d ¡ |  |j|¡ qW d S )N)Úxyz_r*   r   r   )r"   )r   r   r-   r   r   r"   )r   r"   r   r   r   r	   Útest_leaf_constructor_prefixÞ   s    
z&TestNodes.test_leaf_constructor_prefixc             C   s€   xzdD ]r}t  dd¡}t jdddd}t jd||g|d}|  t|ƒ|d ¡ |  |j|¡ |  |j|¡ |  |jd¡ qW d S )	N)rO   r*   r   r   r   Ú_)r"   iè  Zself_foo)r   r   r0   r-   r   r   r"   )r   r"   r   r   r2   r   r   r	   Útest_node_constructor_prefixä   s    
z&TestNodes.test_node_constructor_prefixc             C   s
  t  dd¡}t  dd¡}t  d||g¡}t  d|g¡}|  | ¡ d¡ |  |jg ¡ |  |j|¡ |  |jd ¡ |  |jd ¡ |  |j¡ |  	|j¡ |  | ¡ d¡ |  | ¡ d¡ |  |jg ¡ |  |jd ¡ |  |jd ¡ |  |jd ¡ |  	|j¡ |  	|j¡ d S )Nr   r   iè  r   r   )
r   r   r0   r   Úremover1   Úparentr+   r,   r-   )r   r   r   r2   r:   r   r   r	   Útest_removeî   s&    zTestNodes.test_removec             C   sH   t  dg ¡}| ¡  |  |jd ¡ t  dd¡}| ¡  |  |jd ¡ d S )Niè  r   r   )r   r0   rS   r   rT   r   )r   r2   r   r   r   r	   Útest_remove_parentless  s    z TestNodes.test_remove_parentlessc             C   sÚ   t  dd¡}t  d|g¡}t  dd¡}| d|¡ |  |jd ¡ |  |j|¡ |  |j|g¡ t  d|g¡}| d|¡ |  |jd ¡ |  |j|¡ |  |jd ¡ |  |j|g¡ |  t|jd|¡ |  t	|jdt
¡ d S )Nr   r   iè  r%   r   é   )r   r   r0   Z	set_childr   rT   r1   r   Ú
IndexErrorÚ	Exceptionr   )r   r   r2   r   r:   r   r   r	   Útest_node_set_child  s    zTestNodes.test_node_set_childc             C   s”   t  dd¡}t  d|g¡}t  dd¡}| d|¡ |  |j|¡ |  |j||g¡ t  dd¡}| d|¡ |  |j|||g¡ |  t|jdt	¡ d S )Nr   r   iè  r%   r   Úabcr    )
r   r   r0   Zinsert_childr   rT   r1   r   rY   r   )r   r   r2   r   r'   r   r   r	   Útest_node_insert_child#  s    z TestNodes.test_node_insert_childc             C   sŠ   t  dg ¡}t  dd¡}| |¡ |  |j|¡ |  |j|g¡ t  dd¡}| |¡ |  |j|¡ |  |j||g¡ |  t|jt	¡ d S )Niè  r   r   r%   )
r   r0   r   Zappend_childr   rT   r1   r   rY   r   )r   r2   r   r   r   r   r	   Útest_node_append_child3  s    

z TestNodes.test_node_append_childc             C   sV   t  dg ¡}t  dg ¡}t  d||g¡}|  |j|¡ |  |jd ¡ |  |jd ¡ d S )Niè  )r   r0   ÚassertIsÚnext_siblingr   )r   r2   r:   Úp1r   r   r	   Útest_node_next_siblingC  s    z TestNodes.test_node_next_siblingc             C   sV   t  dd¡}t  dd¡}t  d||g¡}|  |j|¡ |  |jd ¡ |  |jd ¡ d S )Nr   r7   r8   iè  )r   r   r0   r^   r_   r   )r   r   r   r`   r   r   r	   Útest_leaf_next_siblingL  s    z TestNodes.test_leaf_next_siblingc             C   sV   t  dg ¡}t  dg ¡}t  d||g¡}|  |j|¡ |  |jd ¡ |  |jd ¡ d S )Niè  )r   r0   r^   Úprev_siblingr   )r   r2   r:   r`   r   r   r	   Útest_node_prev_siblingU  s    z TestNodes.test_node_prev_siblingc             C   sV   t  dd¡}t  dd¡}t  d||g¡}|  |j|¡ |  |jd ¡ |  |jd ¡ d S )Nr   r7   r8   iè  )r   r   r0   r^   rc   r   )r   r   r   r`   r   r   r	   Útest_leaf_prev_sibling^  s    z TestNodes.test_leaf_prev_siblingN)$Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r#   r)   r.   r3   r4   r5   r6   r;   r=   r>   rC   rE   rH   rI   rK   rL   rN   rP   rR   rU   rV   rZ   r\   r]   ra   rb   rd   re   r   r   r   r	   r      sB   		



				r   c               @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚTestPatternsz&Unit tests for tree matching patterns.c       
      C   sÎ  t  dd¡}t  dd¡}t  dd¡}t  d||g¡}t  d|g¡}t  d||g¡}t jdddd}i }|  |j||d¡ |  |i ¡ |  |j||d¡ |  |i ¡ |  |j||d¡ |  |i ¡ |  |j||d¡ |  |d|i¡ i }|  |j||d¡ |  |i ¡ t jd|gdd}	|  |	j||d¡ |  |i ¡ |  |	j||d¡ |  |i ¡ |  |	j||d¡ |  |||d	œ¡ i }|  |	j||d¡ |  |i ¡ |  |	j||d¡ |  |i ¡ d S )
Nr   r   r%   iè  Úpl)Úname)ZresultsÚpn)rm   rk   )	r   r   r0   ÚLeafPatternr+   Úmatchr   r-   ÚNodePattern)
r   r   r   r'   r2   r:   Úrootrk   Úrrm   r   r   r	   Útest_basic_patternsl  s>    z TestPatterns.test_basic_patternsc             C   sŽ  t  dd¡}t  dd¡}t  dd¡}t  d||g¡}t  d|g¡}t  d||g¡}t jdddd}t jd|gdd}t j|g||ggdd}	i }
|  |	 |g|
¡¡ |  |
i ¡ |  |	 |g|
¡¡ |  |
i ¡ |  	|	 |g|
¡¡ |  t
|
 ¡ ƒdddg¡ |  |
d |¡ |  |
d |¡ |  |
d |g¡ |  |
|||gd	œ¡ i }
|  	|	 ||g|
¡¡ |  |
|||gd
œ¡ |  |
d |¡ i }
d S )Nr   r   r%   iè  rk   )rl   rm   Úpw)rk   rm   rt   )rk   rt   )r   r   r0   rn   rp   ÚWildcardPatternr+   Z	match_seqr   r-   r
   Úkeysr^   )r   r   r   r'   r2   r:   rq   rk   rm   rt   rr   r   r   r	   Útest_wildcard  s2    zTestPatterns.test_wildcardc          	   C   s¾  t  dd¡}t  dd¡}t  dd¡}t  dd¡}t  dd¡}t  dd¡}||||||g}t  d|¡}t  ddd	¡}	t  ddd
¡}
t  ddd¡}t  ddd¡}t  ddd¡}t  ddd¡}t j|	|
|g||g|	|
g||g||ggdddd}|  dd„ | |¡D ƒdddddg¡ t jd|gdd}tt  |g|g¡ƒ}|  t	|ƒd¡ |d \}}|  |d¡ |  t
|d ƒd¡ |  |d ||||||g¡ x*dD ]"}|  |d|  t  d|¡¡ q”W d S )Nr   r7   r8   r9   ÚdÚerM   iè  ÚpaÚpbÚpcÚpdÚpeÚpfrW   rt   )ÚminÚmaxrl   c             S   s   g | ]}|d  ‘qS )r   r   )Ú.0Úxr   r   r	   ú
<listcomp>Á  s    z6TestPatterns.test_generate_matches.<locals>.<listcomp>é   r!   r    é   Úpr)r   Zcontentrl   r   ZabcdefÚp)r   r   r0   rn   ru   r   Zgenerate_matchesrp   r   Úlenr   )r   ZlaZlbZlcZldÚleZlfrG   rq   rz   r{   r|   r}   r~   r   rt   r‡   Zmatchesr9   rr   r   r   r	   Útest_generate_matches¯  s8    
z"TestPatterns.test_generate_matchesc             C   sˆ   t  dt  d¡t jddt  d¡f¡}t  dd¡}t  dd¡}t  dd	¡}t  d|||g¡}i }|  | ||¡¡ |  |d |g¡ d S )
NiK  é   Úargs)rl   é   ú(r…   rƒ   ú))	r   rp   rn   ru   r   r0   r-   ro   r   )r   Úpatternr   r   r'   Znoderr   r   r   r	   Útest_has_key_exampleÍ  s    
z!TestPatterns.test_has_key_exampleN)rf   rg   rh   ri   rs   rw   r‹   r’   r   r   r   r	   rj   h  s
   $rj   N)
ri   r*   r   Zlib2to3r   r
   Ú	NameErrorZTestCaser   rj   r   r   r   r	   Ú<module>
   s     Q