a
    åze×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   s,   t  | ¡}t|ƒD ]}|jd }qd |_|S )Nr   )r   Zparse_stringÚrangeÚchildrenÚparent)ÚcodeÚstrip_levelsZtreeÚi© r   ú-/usr/lib/python3.9/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   ZsymsZ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 r$   )r   Úis_listr   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Úbú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 )Nr1   r2   )r   r   r   Útyper   )r   Úattrr   r   r   Útest_returnsB   s    zTest_Attr.test_returnsN)r    r!   r"   r5   r8   r   r   r   r   r0   ;   s   r0   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 )Nr1   zfoo.foo().barr2   ©ÚprefixZba)r   r   r*   r   r   r   r5   H   s    zTest_Name.testN)r    r!   r"   r5   r   r   r   r   r9   G   s   r9   c                   @   s   e Zd Zddd„Zdd„ ZdS )Ú	Test_CallNc                 C   sF   g }t |tƒr6|D ]}| |¡ | tƒ ¡ q| ¡  tt|ƒ||ƒS )zHelp the next test)r   r   Úappendr
   Úpopr	   r   )r   ÚnameÚargsr;   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%   é   é   r2   Újú r:   Ú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   rB   )r   Zkidsr   r   r   r5   Y   s    
ÿÿûzTest_Call.test)NN)r    r!   r"   rB   r5   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 |jD ]}|  ||¡}|r|  S qd S r   )r   Úfind_bindingr   Ú_find_bind_rec)r   r?   r   ÚcÚchildr   r   r   rM   h   s
    
z$Test_does_tree_import._find_bind_recc                 C   s"   t |ƒ}|  d|¡}t |||¡S )NÚstart)r   rM   r   Údoes_tree_import)r   Úpackager?   r   r   r   r   r   rQ   q   s    z&Test_does_tree_import.does_tree_importc                 C   s¬   d}|D ]J\}}}|   |||d | ¡}|  |¡ |   |||d | ¡}|  |¡ qd}|D ]J\}}}|   |||d | ¡}|  |¡ |   |||d | ¡}|  |¡ q\d S )N))r1   r1   úfrom a import b)úa.dr1   úfrom a.d import b)zd.ar1   úfrom d.a import b)Nr1   úimport b)Nr1   úimport b, c, dÚ
))r1   r1   úfrom a import a)Úxr1   úfrom x import a)r[   r1   úfrom x import b, c, a, d)úx.br1   úfrom x.b import a)r^   r1   úfrom x.b import b, c, a, d)Nr1   úimport a)Nr1   úimport b, c, a, d)rQ   r,   r)   )r   r   Zfailing_testsrR   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)re   r*   r   r   r   Útest_in_function   s    z&Test_does_tree_import.test_in_functionN)r    r!   r"   rM   rQ   re   rf   r   r   r   r   rK   g   s   	rK   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 r   )r   rL   r   )r   r?   r   rR   r   r   r   rL   ”   s    zTest_find_binding.find_bindingc                 C   sp   |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |  |  dd¡¡ |  |  dd¡¡ d S )Nr1   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)   rL   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 )Nr1   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)rh   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 )Nr1   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)rh   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 )Nr1   z	foo.a = 5z
foo[a] = 5z
foo(a) = 5zfoo(a, b) = 5©r,   rL   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 )Nr1   ra   rb   rW   rX   rh   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 )
Nr1   r\   rZ   r]   r_   r`   rS   rU   rV   rh   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 )Nr1   zimport b as azimport b as a, c, a as f, dúimport a as fzimport b, c as f, d as erh   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 )	Nr1   úfrom x import b as aúfrom x import g as a, d as búfrom x.b import t as aúfrom x.b import g as a, dúfrom a import b as tzfrom a.d import b as tzfrom d.a import b as trh   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 )Nr2   rW   rX   rN   rh   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 )Nr1   r\   r[   rZ   zfrom x import *r]   r_   r^   zfrom x.b import *r`   rS   rU   rT   rV   zfrom x.y import *r3   rh   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 )Nr1   zimport b.c as azb.crp   Úfrl   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 )Nr1   rr   r[   rs   rt   r^   ru   rv   r2   Útrl   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 )
Nr1   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():
                    passrh   ©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 )Nr1   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():
                    passrh   r~   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 )	Nr1   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: passrh   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:
                    passr1   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 = 7rh   r~   r   r   r   Útest_for_nested"  s     z!Test_find_binding.test_for_nestedc                 C   s(   |   |  dd¡¡ |  |  dd¡¡ d S )Nr1   zif b in r: a = czif a in r: d = erh   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 = cr1   zL
            if b in r:
                if c in d:
                    c = arh   r~   r   r   r   Útest_if_nestedW  s    z Test_find_binding.test_if_nestedc                 C   s(   |   |  dd¡¡ |  |  dd¡¡ d S )Nr1   zwhile b in r: a = czwhile a in r: d = erh   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 = cr1   zR
            while b in r:
                while c in d:
                    c = arh   r~   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 = 8r1   zQ
            try:
                b = 8
            except:
                a = 6zƒ
            try:
                b = 8
            except KeyError:
                pass
            except:
                a = 6úQ
            try:
                b = 8
            except:
                b = 6rh   r~   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 = 8r1   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 = 6rˆ   a  
            try:
                try:
                    b = 8
                except:
                    c = d
            except:
                try:
                    b = 6
                except:
                    t = 8
                except:
                    o = yrh   r~   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 = 9r1   zR
            try:
                b = 8
            finally:
                a = 6zR
            try:
                b = 8
            finally:
                b = 6z|
            try:
                b = 8
            except:
                b = 9
            finally:
                b = 6rh   r~   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 = 9r1   z
            try:
                b = 8
            finally:
                try:
                    pass
                finally:
                    a = 6zž
            try:
                b = 8
            finally:
                try:
                    b = 6
                finally:
                    b = 7rh   r~   r   r   r   Útest_try_except_finally_nested  s    z0Test_find_binding.test_try_except_finally_nested)N)r    r!   r"   rL   ri   rj   rk   rm   rn   ro   rq   rw   rx   ry   r{   r}   r€   r   r‚   rƒ   r„   r…   r†   r‡   r‰   rŠ   r‹   rŒ   r   r   r   r   rg   “   s2   

		1M!rg   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   Z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()rŽ   z'"""foo"""
import bar
import foo
bar()

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 )Núbar()rŽ   zimport foo
bar()

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 )Nr“   ZhtmlÚescapezfrom html import escape
bar()

r   r   r   r   r   ro   6  s    z"Test_touch_import.test_from_importc                 C   s*   t dƒ}t d d|¡ |  t|ƒd¡ d S )Nr“   Zcgizimport cgi
bar()

r   r   r   r   r   Útest_name_import;  s    z"Test_touch_import.test_name_importN)r    r!   r"   r‘   r’   r”   ro   r–   r   r   r   r   r   %  s
   r   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   rD   r%   z    zdef f():
    x()
    y())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   Zlib2to3r   Zlib2to3.fixer_utilr   r   r	   r
   Zlib2to3.pgen2r   r   ZTestCaser   r#   r.   r0   r9   r<   rK   rg   r   r—   r   r   r   r   Ú<module>   s&   
	,   