B
    ­»ˆdÔ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   sh  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¡ x(dD ] }|  |j| jg d| ¡ q@W 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.dddzaaa.bbbzaaa.bbb.cccZeeezeee.fffZgggzggg.hhhzggg.hhh.iiizggg.hhh.jjjÚddd>   r   r   r   r   r   z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)   úG/var/www/html/venv/lib/python3.7/site-packages/pasta/base/scope_test.pyÚtest_top_level_imports    sv    


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¡ x&dD ]}|  |j| jg d| ¡ qÒW d S )	Nzs        if a:
          import aaa
        elif b:
          import bbb
        else:
          import ccc
        r   r   r   ÚaÚb>   r   r   r   zExpected no reads for %s)r   r   r   r   r   r   Úfind_nodes_by_typeÚaliasr   r   r   r   r   r   r"   r    ÚassertIsNoner!   )	r#   r$   r%   r&   Únode_aaaÚnode_bbbÚ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|¡ x&dD ]}|  |j| jg d| ¡ qªW d S )Nzu        try:
          import aaa
        except:
          import bbb
        finally:
          import ccc
        r   r   r   >   r   r   r   zExpected no reads for %s)r   r   r   r   r   r   r.   r/   r   r   r   r   r   r   r"   r    r!   )	r#   r$   r%   r&   r1   r2   r3   r'   r(   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   r.   r/   r   r   r   r   r   r   )r#   r$   r%   r&   r1   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   )r#   r$   r%   r&   r1   r'   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   zaaa.bbbzaaa.bbb.cccr   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   r6   )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   r8   )r   r   r   r   r   r   r=   r:   Úbasesr   r   r   r   r   r!   )r#   r$   r%   r&   r1   r>   Ú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   r6   )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   r6   )r   r   r   r   r   r:   r   r   r   r   r   r   r!   )r#   r$   r%   r&   r1   r'   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   r6   )r   r   r   r   r   r=   r:   r   r   r   r   r   r   r!   )r#   r$   r%   r&   r>   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   r6   )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   r6   r   )r   r   r   r   r   r   r   r   r   r   r   r!   rC   Ú
annotationr:   )r#   r$   r%   r&   r;   r'   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   zaaa.bbbzaaa.bbb.ccczddd.eeer   r   )r   r   r   r   r   r:   r;   r   r   r   r   r   r   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,   r-   ÚcÚdÚeÚFÚgr6   )Úid)r   r   r   r   r   r   r   Úlookup_scoper   Únoder   r   Zparent_scoper0   Ú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   rS   ZassertInr   r   r!   r:   )	r#   r$   r%   ZfuncdefÚcallZccc_exprZddd_exprrX   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+   r4   r5   r7   r9   r<   r?   rB   rF   rG   rH   r   Zrequires_featuresrI   rK   rL   rY   r[   r]   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    rc   Ú__main__)Ú__doc__Ú
__future__r   r   r   r   r   ra   Z
pasta.baser   r   r   ZTestCaser   rc   r^   Úmainr)   r)   r)   r*   Ú<module>   s       1