a
    ,=icŌ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 
” h d£” |  	|j 
” h d£” |  	|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ŚfffŚiiiŚcccŚjjjŚbbb>   r   śaaa.bbb.cccśggg.hhh.iiiśeee.fffr   śaaa.bbbśccc.dddśggg.hhhr   śggg.hhh.jjjŚeeeŚgggr   r   r   r   Śdddr   r   r   r   r   r   r   r   r   r   r   >   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© r3   śf/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/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 ” h d£” |  
|j ” h d£” |  |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   ŚaŚbr   >   r   r   r   r   r   r   r6   r7   r   )r   r   r    r!   r"   r   Śfind_nodes_by_typeŚaliasr   r$   r%   r#   r&   r'   r,   r*   ŚassertIsNoner+   ©	r-   r.   r/   r0   Śnode_aaaZnode_bbbZnode_cccr1   r2   r3   r3   r4   Śtest_if_nested_importsu   s     


’z ScopeTest.test_if_nested_importsc           	      C   sČ   t  d”}t |”}|j}t |tj”\}}}t 	|”}|  
|j ” h d£” |  
|j ” h d£” |  |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   r8   r9   r   r$   r%   r#   r&   r'   r,   r*   r+   r;   r3   r3   r4   Ś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   r8   r9   r   r$   r%   r#   r&   r'   ©r-   r.   r/   r0   r<   r1   r3   r3   r4   Ś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@   r3   r3   r4   Ś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 ” h d£” |  	|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!   r"   ŚvalueŚfuncr   r$   r%   r#   r&   r'   r+   r(   )r-   r.   r/   r0   Znode_refr1   r3   r3   r4   Ś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   r?   )r   r   r    r!   r"   rE   Śdecorator_listr   r$   r%   r#   r&   r'   r+   )r-   r.   r/   r0   Zreturn_valueŚ	decoratorr1   r3   r3   r4   Ś 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   rB   )r   r   r    r!   r"   r#   rH   rE   Śbasesr   r$   r%   r&   r'   r+   )r-   r.   r/   r0   r<   rI   Śbaser1   r3   r3   r4   Ś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   r?   )r   r   r    r!   r"   ŚargsŚdefaultsr   r$   r%   r#   r&   r'   r+   )r-   r.   r/   r0   Śargvalr1   r3   r3   r4   Ś"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   r?   )r   r   r    r!   r"   rE   r   r$   r%   r#   r&   r'   r+   r@   r3   r3   r4   Ś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   r?   )r   r   r    r!   r"   rH   rE   r   r$   r%   r#   r&   r'   r+   )r-   r.   r/   r0   rI   r1   r3   r3   r4   Ś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   r?   )r   r   r    r!   r"   r   r$   r%   r#   r&   r'   r+   ŚreturnsrE   ©r-   r.   r/   r0   rF   r1   r3   r3   r4   Ś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   r?   r   )r   r   r    r!   r"   r   r$   r%   r#   r&   r'   r+   rN   Ś
annotationrE   rU   r3   r3   r4   Ś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 ” h d£” |  	|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   rD   )r-   r.   r/   r0   Zcall1Zcall2Zcall3r1   r3   r3   r4   Ś test_import_attribute_referencesb  s(    


’’’’z*ScopeTest.test_import_attribute_referencesc                 C   s:  t  d”}t |”}|j\}}|j\}}t |”}| |”}|  |j	|” |  ||” |  
|jddg” | |”}	|  |	j	|” |  |	j|” |  
|	jg d¢” | |”}
|  |
j	|” |  |
j|	” |  
|
jdg” | |”}|  |j	|” |  ||	” |  
|jg d¢” |  |
 |”|	” |  | tjdd”” d S )Nzn        import a
        def b(c, d, e=1):
          class F(d):
            g = 1
          return c
        r6   r7   )ŚcŚdŚeŚFŚgr?   )Śid)r   r   r    r!   r"   r   r$   Ślookup_scoper)   Śnoder%   r#   Zparent_scoper:   ŚName)r-   ŚsrcŚtZimport_nodeZ	func_nodeZ
class_nodeZreturn_nodeŚscZimport_node_scopeZfunc_node_scopeZclass_node_scopeZreturn_node_scoper3   r3   r4   Ś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+   rE   )r-   r.   r/   Z
importstmtZclassdefZ
method_aaaZ
method_bbbr1   r3   r3   r4   Ś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$   r`   ZassertInr#   r%   r+   rE   )	r-   r.   r/   ZfuncdefŚcallZccc_exprZddd_exprre   Z
func_scoper3   r3   r4   Ś-test_vararg_kwarg_references_in_function_bodyø  s    





z7ScopeTest.test_vararg_kwarg_references_in_function_bodyN)Ś__name__Ś
__module__Ś__qualname__r5   r=   r>   rA   rC   rG   rJ   rM   rQ   rR   rS   r   Zrequires_featuresrV   rX   rY   rf   rh   rj   r3   r3   r3   r4   r      s&   U

&r   c                  C   s   t  ” } |  t  t”” | S )N)ŚunittestZ	TestSuiteZaddTestsZ	makeSuiter   )Śresultr3   r3   r4   ŚsuiteĢ  s    rp   Ś__main__)Ś__doc__Ś
__future__r   r   r   r    r   rn   Z
pasta.baser   r   r   ZTestCaser   rp   rk   Śmainr3   r3   r3   r4   Ś<module>   s       1