B
    18™\×R  ã               @   s  d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	m
Z
mZmZ ddlmZ ddd	„ZG d
d„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZdS ) z' Test suite for the code in fixer_util é   )Úsupporté    )ÚNodeÚLeaf)Ú
fixer_util)ÚAttrÚNameÚCallÚComma)Útokenc             C   s0   t  | ¡}xt|ƒD ]}|jd }qW d |_|S )Nr   )r   Úparse_stringÚrangeÚchildrenÚparent)ÚcodeÚstrip_levelsÚtreeÚi© r   ú[/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/lib2to3/tests/test_util.pyÚparse   s
    
r   c               @   s   e Zd Zdd„ ZdS )ÚMacroTestCasec             C   s0   t |ttfƒrttjj|ƒ}|  t|ƒ|¡ d S )N)	Ú
isinstanceÚtupleÚlistr   r   ÚsymsÚsimple_stmtÚassertEqualÚstr)ÚselfÚnodeÚstringr   r   r   Ú	assertStr   s    zMacroTestCase.assertStrN)Ú__name__Ú
__module__Ú__qualname__r"   r   r   r   r   r      s   r   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚTest_is_tuplec             C   s   t  t|dd¡S )Né   )r   )r   Úis_tupler   )r   r!   r   r   r   r(      s    zTest_is_tuple.is_tuplec             C   sT   |   |  d¡¡ |   |  d¡¡ |   |  d¡¡ |   |  d¡¡ |   |  d¡¡ d S )Nz(a, b)z(a, (b, c))z((a, (b, c)),)z(a,)z())Ú
assertTruer(   )r   r   r   r   Ú
test_valid    s
    zTest_is_tuple.test_validc             C   s$   |   |  d¡¡ |   |  d¡¡ d S )Nz(a)z('foo') % (b, c))ÚassertFalser(   )r   r   r   r   Útest_invalid'   s    zTest_is_tuple.test_invalidN)r#   r$   r%   r(   r*   r,   r   r   r   r   r&      s   r&   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚTest_is_listc             C   s   t  t|dd¡S )Nr'   )r   )r   Úis_listr   )r   r!   r   r   r   r.   -   s    zTest_is_list.is_listc             C   sT   |   |  d¡¡ |   |  d¡¡ |   |  d¡¡ |   |  d¡¡ |   |  d¡¡ d S )Nz[]z[a]z[a, b]z[a, [b, c]]z[[a, [b, c]],])r)   r.   )r   r   r   r   r*   0   s
    zTest_is_list.test_validc             C   s   |   |  d¡¡ d S )Nz[]+[])r+   r.   )r   r   r   r   r,   7   s    zTest_is_list.test_invalidN)r#   r$   r%   r.   r*   r,   r   r   r   r   r-   ,   s   r-   c               @   s   e Zd Zdd„ Zdd„ ZdS )Ú	Test_Attrc             C   s@   t ddd}|  ttdƒtdƒƒd¡ |  t|tdƒƒd¡ d S )Nzfoo()r'   )r   ÚaÚbza.bzfoo().b)r   r"   r   r   )r   Úcallr   r   r   Útest<   s    zTest_Attr.testc             C   s&   t tdƒtdƒƒ}|  t|ƒt¡ d S )Nr0   r1   )r   r   r   Útyper   )r   Úattrr   r   r   Útest_returnsB   s    zTest_Attr.test_returnsN)r#   r$   r%   r3   r6   r   r   r   r   r/   ;   s   r/   c               @   s   e Zd Zdd„ ZdS )Ú	Test_Namec             C   s8   |   tdƒd¡ |   tdƒd¡ |   tdddd¡ d S )Nr0   zfoo.foo().barr1   )ÚprefixÚba)r"   r   )r   r   r   r   r3   H   s    zTest_Name.testN)r#   r$   r%   r3   r   r   r   r   r7   G   s   r7   c               @   s   e Zd Zddd„Zdd„ ZdS )Ú	Test_CallNc             C   sJ   g }t |tƒr:x"|D ]}| |¡ | tƒ ¡ qW | ¡  tt|ƒ||ƒS )zHelp the next test)r   r   Úappendr
   Úpopr	   r   )r   ÚnameÚargsr8   r   Úargr   r   r   Ú_CallO   s    


zTest_Call._Callc          	   C   sÌ   d t tjdƒt tjdƒt tjdƒgt tjdƒt tjdƒt tjdƒt tjdƒgt tjdƒt tjdddgg}|  |  d	¡d
¡ |  |  d|d ¡d¡ |  |  d|d ¡d¡ |  | jd|d ddd¡ d S )Nr   r'   é   é   r1   Újú )r8   ÚAzA()zb(1,2,3)za.b().cza.b().c(1,3,2,4)Údz d(b, j))r   r   ÚNUMBERÚSTRINGr"   r@   )r   Úkidsr   r   r   r3   Y   s    zTest_Call.test)NN)r#   r$   r%   r@   r3   r   r   r   r   r:   N   s   

r:   c               @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚTest_does_tree_importc             C   s<   t  ||¡}|r|S x"|jD ]}|  ||¡}|r|S qW d S )N)r   Úfind_bindingr   Ú_find_bind_rec)r   r=   r    ÚcÚchildr   r   r   rL   h   s      z$Test_does_tree_import._find_bind_recc             C   s"   t |ƒ}|  d|¡}t |||¡S )NÚstart)r   rL   r   Údoes_tree_import)r   Úpackager=   r!   r    r   r   r   rP   q   s    z&Test_does_tree_import.does_tree_importc             C   s´   d}xR|D ]J\}}}|   |||d | ¡}|  |¡ |   |||d | ¡}|  |¡ q
W d}xR|D ]J\}}}|   |||d | ¡}|  |¡ |   |||d | ¡}|  |¡ qbW d S )N))r0   r0   zfrom a import b)za.dr0   zfrom a.d import b)zd.ar0   zfrom d.a import b)Nr0   zimport b)Nr0   zimport b, c, dÚ
))r0   r0   zfrom a import a)Úxr0   zfrom x import a)rS   r0   zfrom x import b, c, a, d)zx.br0   zfrom x.b import a)zx.br0   zfrom x.b import b, c, a, d)Nr0   zimport a)Nr0   zimport b, c, a, d)rP   r+   r)   )r   r!   Zfailing_testsrQ   r=   Úimport_ÚnZpassing_testsr   r   r   Útry_withw   s    

zTest_does_tree_import.try_withc             C   s   |   d¡ d S )Nzdef foo():
	bar.baz()
	start=3)rV   )r   r   r   r   Útest_in_function   s    z&Test_does_tree_import.test_in_functionN)r#   r$   r%   rL   rP   rV   rW   r   r   r   r   rJ   g   s   	rJ   c               @   sÖ   e Zd Zd4d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dS )5ÚTest_find_bindingNc             C   s   t  |t|ƒ|¡S )N)r   rK   r   )r   r=   r!   rQ   r   r   r   rK   ”   s    zTest_find_binding.find_bindingc             C   sp   |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |  |  dd¡¡ |  |  dd¡¡ d S )Nr0   za = bza = [b, c, d]z	a = foo()za = foo().foo.foo[6][foo]zfoo = azfoo = (a, b, c))r)   rK   r+   )r   r   r   r   Útest_simple_assignment—   s    z(Test_find_binding.test_simple_assignmentc             C   sp   |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |  |  dd¡¡ |  |  dd¡¡ d S )Nr0   z(a,) = bz(a, b, c) = [b, c, d]z(c, (d, a), b) = foo()z(a, b) = foo().foo[6][foo]z(foo, b) = (b, a)z(foo, (b, c)) = (a, b, c))r)   rK   r+   )r   r   r   r   Útest_tuple_assignmentŸ   s    z'Test_find_binding.test_tuple_assignmentc             C   sp   |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |  |  dd¡¡ |  |  dd¡¡ d S )Nr0   z[a] = bz[a, b, c] = [b, c, d]z[c, [d, a], b] = foo()z[a, b] = foo().foo[a][foo]z[foo, b] = (b, a)z[foo, [b, c]] = (a, b, c))r)   rK   r+   )r   r   r   r   Útest_list_assignment§   s    z&Test_find_binding.test_list_assignmentc             C   sL   |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ d S )Nr0   z	foo.a = 5z
foo[a] = 5z
foo(a) = 5zfoo(a, b) = 5)r+   rK   )r   r   r   r   Útest_invalid_assignments¯   s    z*Test_find_binding.test_invalid_assignmentsc             C   sL   |   |  dd¡¡ |   |  dd¡¡ |  |  dd¡¡ |  |  dd¡¡ d S )Nr0   zimport azimport b, c, a, dzimport bzimport b, c, d)r)   rK   r+   )r   r   r   r   Útest_simple_importµ   s    z$Test_find_binding.test_simple_importc             C   s”   |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |  |  dd¡¡ |  |  dd¡¡ |  |  dd	¡¡ d S )
Nr0   zfrom x import azfrom a import azfrom x import b, c, a, dzfrom x.b import azfrom x.b import b, c, a, dzfrom a import bzfrom a.d import bzfrom d.a import b)r)   rK   r+   )r   r   r   r   Útest_from_import»   s    z"Test_find_binding.test_from_importc             C   sL   |   |  dd¡¡ |   |  dd¡¡ |  |  dd¡¡ |  |  dd¡¡ d S )Nr0   zimport b as azimport b as a, c, a as f, dzimport a as fzimport b, c as f, d as e)r)   rK   r+   )r   r   r   r   Útest_import_asÅ   s    z Test_find_binding.test_import_asc             C   s‚   |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |  |  dd¡¡ |  |  dd¡¡ |  |  dd¡¡ d S )	Nr0   zfrom x import b as azfrom x import g as a, d as bzfrom x.b import t as azfrom x.b import g as a, dzfrom a import b as tzfrom a.d import b as tzfrom d.a import b as t)r)   rK   r+   )r   r   r   r   Útest_from_import_asË   s    z%Test_find_binding.test_from_import_asc             C   sP   |   |  dd¡¡ |   |  dd¡¡ |  |  ddd¡¡ |  |  ddd¡¡ d S )Nr1   zimport bzimport b, c, drM   )r)   rK   r+   )r   r   r   r   Útest_simple_import_with_packageÔ   s    z1Test_find_binding.test_simple_import_with_packagec             C   sà   |   |  ddd¡¡ |   |  ddd¡¡ |   |  ddd¡¡ |   |  ddd¡¡ |   |  ddd¡¡ |   |  dd	d¡¡ |   |  dd
d¡¡ |  |  ddd¡¡ |  |  ddd¡¡ |  |  ddd¡¡ |  |  ddd¡¡ d S )Nr0   zfrom x import arS   zfrom a import azfrom x import *zfrom x import b, c, a, dzfrom x.b import azx.bzfrom x.b import *zfrom x.b import b, c, a, dzfrom a import bzfrom a.d import bza.dzfrom d.a import bzfrom x.y import *za.b)r)   rK   r+   )r   r   r   r   Útest_from_import_with_packageÚ   s    z/Test_find_binding.test_from_import_with_packagec             C   s@   |   |  ddd¡¡ |   |  ddd¡¡ |   |  ddd¡¡ d S )Nr0   zimport b.c as azb.czimport a as fÚf)r+   rK   )r   r   r   r   Útest_import_as_with_packageç   s    z-Test_find_binding.test_import_as_with_packagec             C   s   |   |  ddd¡¡ |   |  ddd¡¡ |   |  ddd¡¡ |   |  ddd¡¡ |   |  ddd¡¡ |   |  ddd	¡¡ |   |  ddd
¡¡ d S )Nr0   zfrom x import b as arS   zfrom x import g as a, d as bzfrom x.b import t as azx.bzfrom x.b import g as a, dzfrom a import b as tr1   Út)r+   rK   )r   r   r   r   Ú test_from_import_as_with_packageì   s    z2Test_find_binding.test_from_import_as_with_packagec             C   s˜   |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |  |  dd¡¡ |  |  dd¡¡ |  |  dd¡¡ |  |  dd¡¡ d	}|  |  d|¡¡ d S )
Nr0   zdef a(): passzdef a(b, c, d): passzdef a(): b = 7zdef d(b, (c, a), e): passzdef d(a=7): passzdef d(a): passzdef d(): a = 7zG
            def d():
                def a():
                    pass)r)   rK   r+   )r   Úsr   r   r   Útest_function_defø   s    z#Test_find_binding.test_function_defc             C   sÎ   |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |  |  dd¡¡ |  |  dd¡¡ |  |  dd¡¡ |  |  dd	¡¡ |  |  dd
¡¡ |  |  dd¡¡ d}|  |  d|¡¡ d S )Nr0   zclass a: passzclass a(): passzclass a(b): passzclass a(b, c=8): passzclass d: passzclass d(a): passzclass d(b, a=7): passzclass d(b, *a): passzclass d(b, **a): passzclass d: a = 7zK
            class d():
                class a():
                    pass)r)   rK   r+   )r   rg   r   r   r   Útest_class_def  s    z Test_find_binding.test_class_defc             C   s‚   |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |  |  dd¡¡ d S )	Nr0   zfor a in r: passzfor a, b in r: passzfor (a, b) in r: passzfor c, (a,) in r: passzfor c, (a, b) in r: passzfor c in r: a = czfor c in a: pass)r)   rK   r+   )r   r   r   r   Útest_for  s    zTest_find_binding.test_forc             C   s´   d}|   |  d|¡¡ d}|   |  d|¡¡ d}|   |  d|¡¡ d}|   |  d|¡¡ d}|   |  d|¡¡ d}|   |  d|¡¡ d}|  |  d|¡¡ d	}|  |  d|¡¡ d S )
NzM
            for b in r:
                for a in b:
                    passr0   zP
            for b in r:
                for a, c in b:
                    passzR
            for b in r:
                for (a, c) in b:
                    passzP
            for b in r:
                for (a,) in b:
                    passzU
            for b in r:
                for c, (a, d) in b:
                    passzN
            for b in r:
                for c in b:
                    a = 7zN
            for b in r:
                for c in b:
                    d = azN
            for b in r:
                for c in a:
                    d = 7)r)   rK   r+   )r   rg   r   r   r   Útest_for_nested"  s     z!Test_find_binding.test_for_nestedc             C   s(   |   |  dd¡¡ |  |  dd¡¡ d S )Nr0   zif b in r: a = czif a in r: d = e)r)   rK   r+   )r   r   r   r   Útest_ifS  s    zTest_find_binding.test_ifc             C   s0   d}|   |  d|¡¡ d}|  |  d|¡¡ d S )NzL
            if b in r:
                if c in d:
                    a = cr0   zL
            if b in r:
                if c in d:
                    c = a)r)   rK   r+   )r   rg   r   r   r   Útest_if_nestedW  s    z Test_find_binding.test_if_nestedc             C   s(   |   |  dd¡¡ |  |  dd¡¡ d S )Nr0   zwhile b in r: a = czwhile a in r: d = e)r)   rK   r+   )r   r   r   r   Ú
test_whiled  s    zTest_find_binding.test_whilec             C   s0   d}|   |  d|¡¡ d}|  |  d|¡¡ d S )NzR
            while b in r:
                while c in d:
                    a = cr0   zR
            while b in r:
                while c in d:
                    c = a)r)   rK   r+   )r   rg   r   r   r   Útest_while_nestedh  s    z#Test_find_binding.test_while_nestedc             C   s\   d}|   |  d|¡¡ d}|   |  d|¡¡ d}|   |  d|¡¡ d}|  |  d|¡¡ d S )NzQ
            try:
                a = 6
            except:
                b = 8r0   zQ
            try:
                b = 8
            except:
                a = 6zƒ
            try:
                b = 8
            except KeyError:
                pass
            except:
                a = 6zQ
            try:
                b = 8
            except:
                b = 6)r)   rK   r+   )r   rg   r   r   r   Útest_try_exceptu  s    z!Test_find_binding.test_try_exceptc             C   sž   d}|   |  d|¡¡ d}|   |  d|¡¡ d}|   |  d|¡¡ d}|   |  d|¡¡ d}|   |  d|¡¡ d}|  |  d|¡¡ d}|  |  d|¡¡ d S )	Nz›
            try:
                try:
                    a = 6
                except:
                    pass
            except:
                b = 8r0   z›
            try:
                b = 8
            except:
                try:
                    a = 6
                except:
                    passz›
            try:
                b = 8
            except:
                try:
                    pass
                except:
                    a = 6zÕ
            try:
                try:
                    b = 8
                except KeyError:
                    pass
                except:
                    a = 6
            except:
                passzÕ
            try:
                pass
            except:
                try:
                    b = 8
                except KeyError:
                    pass
                except:
                    a = 6zQ
            try:
                b = 8
            except:
                b = 6a  
            try:
                try:
                    b = 8
                except:
                    c = d
            except:
                try:
                    b = 6
                except:
                    t = 8
                except:
                    o = y)r)   rK   r+   )r   rg   r   r   r   Útest_try_except_nested”  s    		z(Test_find_binding.test_try_except_nestedc             C   s\   d}|   |  d|¡¡ d}|   |  d|¡¡ d}|  |  d|¡¡ d}|  |  d|¡¡ d S )Nz|
            try:
                c = 6
            except:
                b = 8
            finally:
                a = 9r0   zR
            try:
                b = 8
            finally:
                a = 6zR
            try:
                b = 8
            finally:
                b = 6z|
            try:
                b = 8
            except:
                b = 9
            finally:
                b = 6)r)   rK   r+   )r   rg   r   r   r   Útest_try_except_finallyá  s    z)Test_find_binding.test_try_except_finallyc             C   sF   d}|   |  d|¡¡ d}|   |  d|¡¡ d}|  |  d|¡¡ d S )Nzú
            try:
                c = 6
            except:
                b = 8
            finally:
                try:
                    a = 9
                except:
                    b = 9
                finally:
                    c = 9r0   z
            try:
                b = 8
            finally:
                try:
                    pass
                finally:
                    a = 6zž
            try:
                b = 8
            finally:
                try:
                    b = 6
                finally:
                    b = 7)r)   rK   r+   )r   rg   r   r   r   Útest_try_except_finally_nested  s    		z0Test_find_binding.test_try_except_finally_nested)N)r#   r$   r%   rK   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rd   rf   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   r   r   r   r   rX   “   s2   

		1M!rX   c               @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚTest_touch_importc             C   s*   t dƒ}t d d|¡ |  t|ƒd¡ d S )Nz"""foo"""
bar()Úfooz"""foo"""
import foo
bar()

)r   r   Útouch_importr   r   )r   r    r   r   r   Útest_after_docstring'  s    z&Test_touch_import.test_after_docstringc             C   s*   t dƒ}t d d|¡ |  t|ƒd¡ d S )Nz"""foo"""
import bar
bar()ru   z'"""foo"""
import bar
import foo
bar()

)r   r   rv   r   r   )r   r    r   r   r   Útest_after_imports,  s    z$Test_touch_import.test_after_importsc             C   s*   t dƒ}t d d|¡ |  t|ƒd¡ d S )Nzbar()ru   zimport foo
bar()

)r   r   rv   r   r   )r   r    r   r   r   Útest_beginning1  s    z Test_touch_import.test_beginningc             C   s*   t dƒ}t dd|¡ |  t|ƒd¡ d S )Nzbar()ÚhtmlÚescapezfrom html import escape
bar()

)r   r   rv   r   r   )r   r    r   r   r   r^   6  s    z"Test_touch_import.test_from_importc             C   s*   t dƒ}t d d|¡ |  t|ƒd¡ d S )Nzbar()Zcgizimport cgi
bar()

)r   r   rv   r   r   )r   r    r   r   r   Útest_name_import;  s    z"Test_touch_import.test_name_importN)r#   r$   r%   rw   rx   ry   r^   r|   r   r   r   r   rt   %  s
   rt   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚTest_find_indentationc             C   s:   t j}tdƒ}|  ||ƒd¡ tdƒ}|  ||ƒd¡ d S )Nznode()Ú )r   Úfind_indentationr   r   )r   Úfir    r   r   r   Útest_nothingB  s
    z"Test_find_indentation.test_nothingc             C   sn   t j}tdƒ}|  ||ƒd¡ |  ||jd jd jd ƒd¡ tdƒ}|  ||jd jd jd ƒd¡ d S )Nzdef f():
    x()r~   r   rB   r'   z    zdef f():
    x()
    y())r   r   r   r   r   )r   r€   r    r   r   r   Útest_simpleI  s    "z!Test_find_indentation.test_simpleN)r#   r$   r%   r   r‚   r   r   r   r   r}   @  s   r}   N)r   )Ú__doc__r~   r   Zlib2to3.pytreer   r   Úlib2to3r   Zlib2to3.fixer_utilr   r   r	   r
   Zlib2to3.pgen2r   r   ÚTestCaser   r&   r-   r/   r7   r:   rJ   rX   rt   r}   r   r   r   r   Ú<module>   s&   
	,   