U
    ŁfŌ9  ć                   @   s   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddlm	Z	 ddlm
Z
 ddlmZ G d	d
 d
ejZdd Zedkre ”  dS )zTests for scope.é    )Śabsolute_import)Śdivision)Śprint_functionN)Ś	ast_utils)Śscope)Ś
test_utilsc                   @   sØ   e 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e d”dd Ze d”dd Zdd Zdd Zd d! Zd"d# Zd$S )%Ś	ScopeTestc                 C   sd  d | _ t d”}t |”}|j}|d jd }|d jd }|d jd }|d jd }|d }|d jd }	|d }
|d jd }|d jd }t |”}|  	|j 
” ddd	d
ddh” |  	|j 
” ddd	dddddddddh” |  	|jd t d||jd ”t d||jd ”g” |  	|jd t d||jd ”g” |  	|jd	 t d	||jd	 ”g” |  	|jd t d||jd	 jd ”g” |  	|jd t d||jd jd ”g” |  	|jd t d||jd jd jd	 ”g” |  	|jd t d|d ”g” |  	|jd t d|	|jd
 ”g” |  	|jd t d|
d ”g” |  	|jd t d|
d ”g” |  	|jd t d||jd ”g” |  	|jd t d||jd ”g” |  |jd j|” |  |jd j|” |  |jd	 j|” |  |jd
 j|	” |  |jd j|” |  |jd j|” |  	|jd j|g” dD ] }|  |j| jg d| ” q>d S )Nz        import aaa
        import bbb, ccc.ddd
        import aaa.bbb.ccc
        from eee import fff
        from ggg.hhh import iii, jjj
        r   é   é   é   é   ŚaaaŚbbbŚcccŚfffŚiiiŚjjjzccc.dddśaaa.bbbśaaa.bbb.cccZeeezeee.fffZgggzggg.hhhzggg.hhh.iiizggg.hhh.jjjŚddd>   r   r   r   r   r   śExpected no reads for %s)ZmaxDiffŚtextwrapŚdedentŚastŚparseŚbodyŚnamesr   ŚanalyzeŚassertItemsEqualŚkeysŚexternal_referencesZExternalReferenceŚattrsŚassertIsŚ
definitionŚreadsŚassertEqual)ŚselfŚsourceŚtreeŚnodesZ
node_1_aaaZ
node_2_bbbZnode_2_ccc_dddZnode_3_aaa_bbb_cccZ
node_4_eeeZ
node_4_fffZnode_5_ggg_hhhZ
node_5_iiiZ
node_5_jjjŚsŚref© r,   ś</tmp/pip-target-lpfmz8o1/lib/python/pasta/base/scope_test.pyŚtest_top_level_imports    sø    


     ’’          ž’ž’’’’’’’’’’’’’’z ScopeTest.test_top_level_importsc           	      C   sō   t  d”}t |”}|j}t |tj”\}}}t 	|”}|  
|j ” dddddh” |  
|j ” dddh” |  |jd j|” |  |jd j|” |  |jd j|” |  |jd j” |  |jd j” dD ]}|  |j| jg d| ” qŠd S )	Nzs        if a:
          import aaa
        elif b:
          import bbb
        else:
          import ccc
        r   r   r   ŚaŚb>   r   r   r   r   )r   r   r   r   r   r   Śfind_nodes_by_typeŚaliasr   r   r   r   r   r    r%   r#   ŚassertIsNoner$   ©	r&   r'   r(   r)   Śnode_aaaZnode_bbbZnode_cccr*   r+   r,   r,   r-   Śtest_if_nested_importsu   s     


’z ScopeTest.test_if_nested_importsc           	      C   sĢ   t  d”}t |”}|j}t |tj”\}}}t 	|”}|  
|j ” dddh” |  
|j ” dddh” |  |jd j|” |  |jd j|” |  |jd j|” dD ]}|  |j| jg d| ” qØd S )Nzu        try:
          import aaa
        except:
          import bbb
        finally:
          import ccc
        r   r   r   >   r   r   r   r   )r   r   r   r   r   r   r1   r2   r   r   r   r   r   r    r%   r#   r$   r4   r,   r,   r-   Śtest_try_nested_imports   s    


’z!ScopeTest.test_try_nested_importsc                 C   sb   t  d”}t |”}|j}t |tj”d }t 	|”}|  
|j ” dh” |  
|j ” dh” d S )Nz3        def foo(bar):
          import aaa
        r   Śfoor   )r   r   r   r   r   r   r1   r2   r   r   r   r   r   r    ©r&   r'   r(   r)   r5   r*   r,   r,   r-   Śtest_functiondef_nested_imports®   s    


z)ScopeTest.test_functiondef_nested_importsc                 C   sd   t  d”}t |”}|j}|d jd jd }t |”}|  |j 	” dh” |  |j
 	” dh” d S )Nz2        class Foo():
          import aaa
        r   ŚFoor   )r   r   r   r   r   r   r   r   r   r   r    r9   r,   r,   r-   Śtest_classdef_nested_imports½   s    


z&ScopeTest.test_classdef_nested_importsc                 C   s¼   t  d”}t |”}|j}|d jjj}t |”}|  	|j
 ” dh” |  	|j ” dddh” |  	|j
d j|jjg” |  	|j
d jd j|jg” |  	|j
d jd jd j|g” d S )Nz=        import aaa.bbb.ccc
        aaa.bbb.ccc.foo()
        r	   r   r   r   r   r   ©r   r   r   r   r   ŚvalueŚfuncr   r   r   r   r   r    r$   r!   )r&   r'   r(   r)   Znode_refr*   r,   r,   r-   Śtest_multilevel_import_readsĢ   s    


’’z&ScopeTest.test_multilevel_import_readsc                 C   s   t  d”}t |”}|j}|d jd j}|d jd j}t |”}|  	|j
 ” ddh” |  	|j ” dh” |  	|j
d j||g” d S )NzU        import aaa
        @aaa.x
        def foo(bar):
          return aaa
        r	   r   r   r8   )r   r   r   r   r   r>   Śdecorator_listr   r   r   r   r   r    r$   )r&   r'   r(   r)   Zreturn_valueŚ	decoratorr*   r,   r,   r-   Ś test_import_reads_in_functiondefą   s    


z*ScopeTest.test_import_reads_in_functiondefc                 C   s   t  d”}t |”}|j}|d jd }|d jd j}|d jd j}t	 
|”}|  |j ” ddh” |  |j ” dh” |  |jd j||g” d S )NzU        import aaa
        @aaa.x
        class Foo(aaa.Bar):
          pass
        r   r	   r   r;   )r   r   r   r   r   r   rA   r>   Śbasesr   r   r   r   r    r$   )r&   r'   r(   r)   r5   rB   Śbaser*   r,   r,   r-   Śtest_import_reads_in_classdefó   s    


z'ScopeTest.test_import_reads_in_classdefc                 C   sx   t  d”}t |”}|j}|d jjd }t |”}|  	|j
 ” ddh” |  	|j ” dh” |  	|j
d j|g” d S )NzJ        import aaa
        def foo(aaa=aaa):
          return aaa
        r	   r   r   r8   )r   r   r   r   r   ŚargsŚdefaultsr   r   r   r   r   r    r$   )r&   r'   r(   r)   Śargvalr*   r,   r,   r-   Ś"test_import_masked_by_function_arg  s    


z,ScopeTest.test_import_masked_by_function_argc                 C   sr   t  d”}t |”}|j}|d j}t |”}|  |j	 
” ddh” |  |j 
” dh” |  |j	d j|g” d S )Nzc        import aaa
        def foo():
          aaa = 123
          return aaa
        aaa
        r
   r   r8   )r   r   r   r   r   r>   r   r   r   r   r   r    r$   r9   r,   r,   r-   Śtest_import_masked_by_assign  s    



z&ScopeTest.test_import_masked_by_assignc                 C   sx   t  d”}t |”}|j}|d jd j}t |”}|  	|j
 ” ddh” |  	|j ” dh” |  	|j
d j|g” d S )NzW        import aaa
        @aaa.wrapper
        def foo(aaa=1):
          pass
        r	   r   r   r8   )r   r   r   r   r   rA   r>   r   r   r   r   r   r    r$   )r&   r'   r(   r)   rB   r*   r,   r,   r-   Śtest_import_in_decortator+  s    


z#ScopeTest.test_import_in_decortatorZtype_annotationsc                 C   st   t  d”}t |”}|j}|d }t |”}|  |j 	” ddh” |  |j
 	” dh” |  |jd j|jjg” d S )NzH        import aaa
        def foo() -> aaa.Foo:
          pass
        r	   r   r8   )r   r   r   r   r   r   r   r   r   r   r    r$   Zreturnsr>   ©r&   r'   r(   r)   r?   r*   r,   r,   r-   Śtest_import_in_return_type=  s    


z$ScopeTest.test_import_in_return_typec                 C   s|   t  d”}t |”}|j}|d }t |”}|  |j 	” ddh” |  |j
 	” dh” |  |jd j|jjd jjg” d S )NzI        import aaa
        def foo(bar: aaa.Bar):
          pass
        r	   r   r8   r   )r   r   r   r   r   r   r   r   r   r   r    r$   rG   Ś
annotationr>   rM   r,   r,   r-   Śtest_import_in_argument_typeO  s    


’z&ScopeTest.test_import_in_argument_typec                 C   sę   t  d”}t |”}|j}|d jjj}|d jjj}|d jjj}t |”}|  	|j
 ” ddh” |  	|j ” ddddd	h” |  	|j
d j||j|jjg” |  	|j
d jd
 j||jg” |  	|j
d jd
 jd j|g” d S )Nzh        import aaa.bbb.ccc, ddd.eee
        aaa.x()
        aaa.bbb.y()
        aaa.bbb.ccc.z()
        r	   r
   r   r   r   r   r   zddd.eeer   r   r=   )r&   r'   r(   r)   Zcall1Zcall2Zcall3r*   r,   r,   r-   Ś test_import_attribute_referencesb  s(    


’’’’z*ScopeTest.test_import_attribute_referencesc                 C   sB  t  d”}t |”}|j\}}|j\}}t |”}| |”}|  |j	|” |  ||” |  
|jddg” | |”}	|  |	j	|” |  |	j|” |  
|	jddddg” | |”}
|  |
j	|” |  |
j|	” |  
|
jdg” | |”}|  |j	|” |  ||	” |  
|jddddg” |  |
 |”|	” |  | tjd	d
”” d S )Nzn        import a
        def b(c, d, e=1):
          class F(d):
            g = 1
          return c
        r/   r0   ŚcŚdŚeŚFŚgr8   )Śid)r   r   r   r   r   r   r   Ślookup_scoper"   Śnoder   r   Zparent_scoper3   ŚName)r&   ŚsrcŚtZimport_nodeZ	func_nodeZ
class_nodeZreturn_nodeŚscZimport_node_scopeZfunc_node_scopeZclass_node_scopeZreturn_node_scoper,   r,   r-   Śtest_lookup_scope|  s2    








’zScopeTest.test_lookup_scopec                 C   s   t  d”}t |”}|j\}}|j\}}t |”}|  |j 	” ddh” |  |j
 	” dh” |  |jd j|jd j|jd jg” d S )Nz        import aaa
        class C:
          def aaa(self):
            return aaa

          def bbb(self):
            return aaa
        r   ŚCr   )r   r   r   r   r   r   r   r   r   r   r    r$   r>   )r&   r'   r(   Z
importstmtZclassdefZ
method_aaaZ
method_bbbr*   r,   r,   r-   Śtest_class_methods¢  s    
	



’zScopeTest.test_class_methodsc           	      C   s   t  d”}t |”}|j\}}|j\}}t |”}| |”}|  d|j	” |  
|j	d j|jg” |  d|j	” |  
|j	d j|jg” d S )Nz]        def aaa(bbb, *ccc, **ddd):
          ccc
          ddd
        eee(ccc, ddd)
        r   r   )r   r   r   r   r   r   r   rX   ZassertInr   r   r$   r>   )	r&   r'   r(   ZfuncdefŚcallZccc_exprZddd_exprr]   Z
func_scoper,   r,   r-   Ś-test_vararg_kwarg_references_in_function_bodyø  s    





z7ScopeTest.test_vararg_kwarg_references_in_function_bodyN)Ś__name__Ś
__module__Ś__qualname__r.   r6   r7   r:   r<   r@   rC   rF   rJ   rK   rL   r   Zrequires_featuresrN   rP   rQ   r^   r`   rb   r,   r,   r,   r-   r      s&   U

&r   c                  C   s   t  ” } |  t  t”” | S )N)ŚunittestZ	TestSuiteZaddTestsZ	makeSuiter   )Śresultr,   r,   r-   ŚsuiteĢ  s    rh   Ś__main__)Ś__doc__Ś
__future__r   r   r   r   r   rf   Z
pasta.baser   r   r   ZTestCaser   rh   rc   Śmainr,   r,   r,   r-   Ś<module>   s       1