o
    JAf×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.10/lib2to3/tests/test_util.pyÚparse   s
   
r   c                   @   ó   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      ó    r   c                   @   ó$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚTest_is_tuplec                 C   ó   t  t|dd¡S ©Né   ©r   )r   Úis_tupler   ©r   r   r   r   r   r+      ó   zTest_is_tuple.is_tuplec                 C   óT   |   |  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    ó
   zTest_is_tuple.test_validc                 C   s$   |   |  d¡¡ |   |  d¡¡ d S )Nz(a)z('foo') % (b, c))ÚassertFalser+   r0   r   r   r   Útest_invalid'   s   zTest_is_tuple.test_invalidN)r!   r"   r#   r+   r1   r4   r   r   r   r   r&      ó    r&   c                   @   r%   )ÚTest_is_listc                 C   r'   r(   )r   Úis_listr   r,   r   r   r   r7   -   r-   zTest_is_list.is_listc                 C   r.   )Nz[]z[a]z[a, b]z[a, [b, c]]z[[a, [b, c]],])r/   r7   r0   r   r   r   r1   0   r2   zTest_is_list.test_validc                 C   s   |   |  d¡¡ d S )Nz[]+[])r3   r7   r0   r   r   r   r4   7   s   zTest_is_list.test_invalidN)r!   r"   r#   r7   r1   r4   r   r   r   r   r6   ,   r5   r6   c                   @   ó   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 )Nr:   r;   )r   r   r   Útyper   )r   Úattrr   r   r   Útest_returnsB   s   zTest_Attr.test_returnsN)r!   r"   r#   r>   rA   r   r   r   r   r9   ;   s    r9   c                   @   r   )Ú	Test_Namec                 C   s8   |   tdƒd¡ |   tdƒd¡ |   tdddd¡ d S )Nr:   zfoo.foo().barr;   ©ÚprefixZba)r    r   r0   r   r   r   r>   H   s   zTest_Name.testN)r!   r"   r#   r>   r   r   r   r   rB   G   r$   rB   c                   @   s   e Zd Zddd„Zdd„ ZdS )Ú	Test_CallNc                 C   sF   g }t |tƒr|D ]}| |¡ | tƒ ¡ q	| ¡  tt|ƒ||ƒS )zHelp the next test)r   r   Úappendr
   Úpopr	   r   )r   ÚnameÚargsrD   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)   é   é   r;   Újú rC   Ú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    rK   )r   Zkidsr   r   r   r>   Y   s   
ÿÿû zTest_Call.test)NN)r!   r"   r#   rK   r>   r   r   r   r   rE   N   s    

rE   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   rH   r   ÚcÚchildr   r   r   rV   h   s   
þz$Test_does_tree_import._find_bind_recc                 C   s"   t |ƒ}|  d|¡}t |||¡S )NÚstart)r   rV   r   Údoes_tree_import)r   ÚpackagerH   r   r   r   r   r   rZ   q   s   z&Test_does_tree_import.does_tree_importc                 C   s¬   d}|D ]%\}}}|   |||d | ¡}|  |¡ |   |||d | ¡}|  |¡ qd}|D ]%\}}}|   |||d | ¡}|  |¡ |   |||d | ¡}|  |¡ q.d S )N))r:   r:   úfrom a import b)úa.dr:   úfrom a.d import b)zd.ar:   úfrom d.a import b)Nr:   úimport b)Nr:   úimport b, c, dÚ
))r:   r:   úfrom a import a)Úxr:   úfrom x import a)rd   r:   úfrom x import b, c, a, d)úx.br:   úfrom x.b import a)rg   r:   úfrom x.b import b, c, a, d)Nr:   úimport a)Nr:   úimport b, c, a, d)rZ   r3   r/   )r   r   Zfailing_testsr[   rH   Ú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)rn   r0   r   r   r   Útest_in_function   s   z&Test_does_tree_import.test_in_functionN)r!   r"   r#   rV   rZ   rn   ro   r   r   r   r   rT   g   s
    	rT   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   rU   r   )r   rH   r   r[   r   r   r   rU   ”   r-   zTest_find_binding.find_bindingc                 C   óp   |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |  |  dd¡¡ |  |  dd¡¡ d S )Nr:   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/   rU   r3   r0   r   r   r   Útest_simple_assignment—   ó   z(Test_find_binding.test_simple_assignmentc                 C   rq   )Nr:   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)rr   r0   r   r   r   Útest_tuple_assignmentŸ   rt   z'Test_find_binding.test_tuple_assignmentc                 C   rq   )Nr:   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)rr   r0   r   r   r   Útest_list_assignment§   rt   z&Test_find_binding.test_list_assignmentc                 C   sL   |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ |   |  dd¡¡ d S )Nr:   z	foo.a = 5z
foo[a] = 5z
foo(a) = 5zfoo(a, b) = 5©r3   rU   r0   r   r   r   Útest_invalid_assignments¯   ó   z*Test_find_binding.test_invalid_assignmentsc                 C   óL   |   |  dd¡¡ |   |  dd¡¡ |  |  dd¡¡ |  |  dd¡¡ d S )Nr:   rj   rk   r`   ra   rr   r0   r   r   r   Útest_simple_importµ   ry   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 )
Nr:   re   rc   rf   rh   ri   r\   r^   r_   rr   r0   r   r   r   Útest_from_import»   s   z"Test_find_binding.test_from_importc                 C   rz   )Nr:   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 err   r0   r   r   r   Útest_import_asÅ   ry   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 )	Nr:   ú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 trr   r0   r   r   r   Útest_from_import_asË   ó   z%Test_find_binding.test_from_import_asc                 C   sP   |   |  dd¡¡ |   |  dd¡¡ |  |  ddd¡¡ |  |  ddd¡¡ d S )Nr;   r`   ra   rW   rr   r0   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 )Nr:   re   rd   rc   zfrom x import *rf   rh   rg   zfrom x.b import *ri   r\   r^   r]   r_   zfrom x.y import *r<   rr   r0   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 )Nr:   zimport b.c as azb.cr}   Úfrw   r0   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 )Nr:   r   rd   r€   r   rg   r‚   rƒ   r;   Útrw   r0   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 )
Nr:   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():
                    passrr   ©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 )Nr:   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():
                    passrr   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 )	Nr:   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: passrr   r0   r   r   r   Útest_for  r…   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:
                    passr:   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 = 7rr   rŒ   r   r   r   Útest_for_nested"  s    z!Test_find_binding.test_for_nestedc                 C   ó(   |   |  dd¡¡ |  |  dd¡¡ d S )Nr:   zif b in r: a = czif a in r: d = err   r0   r   r   r   Útest_ifS  ó   zTest_find_binding.test_ifc                 C   ó0   d}|   |  d|¡¡ d}|  |  d|¡¡ d S )NzL
            if b in r:
                if c in d:
                    a = cr:   zL
            if b in r:
                if c in d:
                    c = arr   rŒ   r   r   r   Útest_if_nestedW  ó   z Test_find_binding.test_if_nestedc                 C   r’   )Nr:   zwhile b in r: a = czwhile a in r: d = err   r0   r   r   r   Ú
test_whiled  r”   zTest_find_binding.test_whilec                 C   r•   )NzR
            while b in r:
                while c in d:
                    a = cr:   zR
            while b in r:
                while c in d:
                    c = arr   rŒ   r   r   r   Útest_while_nestedh  r—   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 = 8r:   zQ
            try:
                b = 8
            except:
                a = 6zƒ
            try:
                b = 8
            except KeyError:
                pass
            except:
                a = 6úQ
            try:
                b = 8
            except:
                b = 6rr   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 = 8r:   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 = yrr   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 = 9r:   zR
            try:
                b = 8
            finally:
                a = 6zR
            try:
                b = 8
            finally:
                b = 6z|
            try:
                b = 8
            except:
                b = 9
            finally:
                b = 6rr   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 = 9r:   z
            try:
                b = 8
            finally:
                try:
                    pass
                finally:
                    a = 6zž
            try:
                b = 8
            finally:
                try:
                    b = 6
                finally:
                    b = 7rr   rŒ   r   r   r   Útest_try_except_finally_nested  s   z0Test_find_binding.test_try_except_finally_nestedr   )r!   r"   r#   rU   rs   ru   rv   rx   r{   r|   r~   r„   r†   r‡   r‰   r‹   rŽ   r   r   r‘   r“   r–   r˜   r™   r›   rœ   r   rž   r   r   r   r   rp   “   s4    

		1M!rp   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   ó*   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'  ó   z&Test_touch_import.test_after_docstringc                 C   r    )Nz"""foo"""
import bar
bar()r¡   z'"""foo"""
import bar
import foo
bar()

r¢   r£   r   r   r   Útest_after_imports,  r¥   z$Test_touch_import.test_after_importsc                 C   r    )Núbar()r¡   zimport foo
bar()

r¢   r£   r   r   r   Útest_beginning1  r¥   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   r|   6  r¥   z"Test_touch_import.test_from_importc                 C   r    )Nr§   Zcgizimport cgi
bar()

r¢   r£   r   r   r   Útest_name_import;  r¥   z"Test_touch_import.test_name_importN)r!   r"   r#   r¤   r¦   r¨   r|   rª   r   r   r   r   rŸ   %  s    rŸ   c                   @   r8   )Ú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   rM   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&   r6   r9   rB   rE   rT   rp   rŸ   r«   r   r   r   r   Ú<module>   s(    
	,   