B
    u9a'  ã               @   sF   d Z ddlZddlZdZdd„ ZG dd„ dejƒZedkrBe ¡  dS )	z&
Test the API of the symtable module.
é    Na3  
import sys

glob = 42

class Mine:
    instance_var = 24
    def a_method(p1, p2):
        pass

def spam(a, b, *var, **kw):
    global bar
    bar = 47
    x = 23
    glob
    def internal():
        return x
    return internal

def foo():
    pass

def namespace_test(): pass
def namespace_test(): pass
c             C   s&   x |   ¡ D ]}| ¡ |kr
|S q
W d S )N)Úget_childrenÚget_name)ÚblockÚnameZch© r   ú#/usr/lib/python3.7/test_symtable.pyÚ
find_block$   s    r   c               @   sô   e Zd Ze edd¡ZeedƒZeedƒZeedƒZ	ee	dƒZ
ee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 d2S )3ÚSymtableTestú?ÚexecÚMineÚa_methodÚspamÚinternalÚfooc             C   s^   |   | j ¡ d¡ |   | j ¡ d¡ |   | j ¡ d¡ |   | j ¡ d¡ |   | j ¡ d¡ d S )NÚmoduleÚclassZfunction)ÚassertEqualÚtopZget_typer   r   r   r   )Úselfr   r   r   Ú	test_type4   s
    zSymtableTest.test_typec             C   s4   |   | j ¡ ¡ |   | j ¡ ¡ |  | j ¡ ¡ d S )N)ÚassertFalser   Zis_optimizedZhas_execÚ
assertTruer   )r   r   r   r   Útest_optimized;   s    zSymtableTest.test_optimizedc             C   sD   |   | j ¡ ¡ |   | j ¡ ¡ |   | j ¡ ¡ |  | j ¡ ¡ d S )N)r   r   Z	is_nestedr   r   r   r   )r   r   r   r   Útest_nestedA   s    zSymtableTest.test_nestedc             C   s4   |   | j ¡ ¡ |   | j ¡ ¡ |  | j ¡ ¡ d S )N)r   r   Zhas_childrenr   r   r   )r   r   r   r   Útest_childrenG   s    zSymtableTest.test_childrenc             C   s(   |   | j ¡ d¡ |   | j ¡ d¡ d S )Nr   é   )r   r   Z
get_linenor   )r   r   r   r   Útest_linenoL   s    zSymtableTest.test_linenoc             C   st   | j }|  t| ¡ ƒddddg¡ ddddddg}|  t| ¡ ƒ|¡ |  t| ¡ ƒddg¡ |  | j ¡ d	¡ d S )
NÚaÚbÚkwÚvarr   ÚxÚbarÚglob)r"   )r   r   ÚsortedZget_parametersZ
get_localsZget_globalsr   Z	get_frees)r   ÚfuncZexpectedr   r   r   Útest_function_infoP   s    zSymtableTest.test_function_infoc             C   sˆ   |   | j d¡ ¡ ¡ |  | j d¡ ¡ ¡ |   | j d¡ ¡ ¡ |   | j d¡ ¡ ¡ |  | j d¡ ¡ ¡ |  | j d¡ ¡ ¡ d S )Nr$   r#   r"   Zinstance_var)r   r   ÚlookupÚ	is_globalr   Zis_declared_globalr   r   )r   r   r   r   Útest_globalsX   s    zSymtableTest.test_globalsc             C   s0   |   | j d¡ ¡ ¡ |  | j d¡ ¡ ¡ d S )Nr"   )r   r   r(   Úis_localr   r   )r   r   r   r   Ú
test_local`   s    zSymtableTest.test_localc             C   sF   |   | j d¡ ¡ ¡ |   | j d¡ ¡ ¡ |  | j d¡ ¡ ¡ d S )Nr"   r   )r   r   r(   Zis_referencedr   r   )r   r   r   r   Útest_referencedd   s    zSymtableTest.test_referencedc             C   s>   x"dD ]}|   | j |¡ ¡ ¡ qW |  | j d¡ ¡ ¡ d S )N)r   r!   r    r"   )r   r   r(   Zis_parameterr   )r   Zsymr   r   r   Útest_parametersi   s    
zSymtableTest.test_parametersc             C   s6   |   t| j ¡ ƒt| j ¡ ƒ¡ |  t| jjd¡ d S )NZnot_here)r   Úlenr   Zget_identifiersZget_symbolsÚassertRaisesÚKeyErrorr(   )r   r   r   r   Útest_symbol_lookupn   s    zSymtableTest.test_symbol_lookupc             C   sÒ   |   | j d¡ ¡ ¡ |   | j d¡ ¡ ¡ |   | j d¡ ¡ ¡ |   | j d¡ ¡ ¡ |   | j d¡ ¡ ¡ |  | j d¡ ¡ ¡ |   | j d¡ ¡ | jk¡ | j d¡}|  t	| 
¡ ƒd¡ |  t|j¡ d S )Nr   r   r   r   Znamespace_testr"   é   )r   r   r(   Úis_namespacer   r   r   Zget_namespacer   r/   Zget_namespacesr0   Ú
ValueError)r   Zns_testr   r   r   Útest_namespacest   s    zSymtableTest.test_namespacesc             C   sr   |   | j d¡ ¡ ¡ |   | j d¡ ¡ ¡ |   | j d¡ ¡ ¡ |   | j d¡ ¡ ¡ |  | j d¡ ¡ ¡ d S )Nr"   r#   r   r   )r   r   r(   Zis_assignedr   r   r   r   )r   r   r   r   Útest_assigned   s
    zSymtableTest.test_assignedc             C   sœ   t   ddd¡}| ¡ d }|  | d¡ ¡ ¡ |  | d¡ ¡ ¡ |  | d¡ ¡ ¡ t   ddd¡}| ¡ d }|  | d¡ ¡ ¡ |  | d¡ ¡ ¡ d S )Nzdef f():
    x: int
Ztestr   r   r"   zdef f():
    x = 1
)Úsymtabler   r   r(   r+   Zis_annotatedr   r)   )r   Zst1Zst2Zst3Zst4r   r   r   Útest_annotatedˆ   s    zSymtableTest.test_annotatedc             C   s   |   | j d¡ ¡ ¡ d S )NÚsys)r   r   r(   Zis_imported)r   r   r   r   Útest_imported“   s    zSymtableTest.test_importedc             C   sR   |   | j ¡ d¡ |   | j ¡ d¡ |   | j d¡ ¡ d¡ |   | j ¡ d¡ d S )Nr   r   r"   r   )r   r   r   r   r(   r   )r   r   r   r   Ú	test_name–   s    zSymtableTest.test_namec             C   s   |   | j ¡ d¡ d S )N)r   )r   r   Zget_methods)r   r   r   r   Útest_class_infoœ   s    zSymtableTest.test_class_infoc          
      sÀ   ‡ fdd„}|ddƒ |ddƒ t   ddd	¡ ˆ  t¡. ˆ  t¡ t   dtdƒd	¡ W d Q R X W d Q R X ˆ  t¡ t   dtdƒd	¡ W d Q R X ˆ  t¡ t   dtdƒd	¡ W d Q R X d S )
Nc          
      st   yt   | dd¡ W nL tk
r^ } z.ˆ  |jd¡ ˆ  |jd¡ ˆ  |j|¡ W d d }~X Y nX ˆ  d| f ¡ d S )Nr   r   é   zno SyntaxError for %r)r8   ÚSyntaxErrorr   ÚfilenameÚlinenoÚoffsetZfail)Z
brokencoderB   Úe)r   r   r   Úcheckfilename¢   s     z9SymtableTest.test_filename_correct.<locals>.checkfilenamezdef f(x): foo)(é   zdef f(x): global xé
   Úpasss   spamr   )r8   ZassertWarnsÚDeprecationWarningr0   Ú	TypeErrorÚ	bytearrayÚ
memoryviewÚlist)r   rD   r   )r   r   Útest_filename_correctŸ   s    	

&z"SymtableTest.test_filename_correctc             C   s   t   ddd¡}d S )NÚ42r
   Úeval)r8   )r   Úsymbolsr   r   r   Ú	test_eval¶   s    zSymtableTest.test_evalc             C   s   t   ddd¡}d S )NrN   r
   Zsingle)r8   )r   rP   r   r   r   Útest_single¹   s    zSymtableTest.test_singlec             C   s   t   ddd¡}d S )Nzdef f(x): return xr
   r   )r8   )r   rP   r   r   r   Ú	test_exec¼   s    zSymtableTest.test_execN)!Ú__name__Ú
__module__Ú__qualname__r8   Ú	TEST_CODEr   r   r   r   r   r   r   r   r   r   r   r   r'   r*   r,   r-   r.   r2   r6   r7   r9   r;   r<   r=   rM   rQ   rR   rS   r   r   r   r   r	   *   s6   




r	   Ú__main__)	Ú__doc__r8   ZunittestrW   r   ZTestCaser	   rT   Úmainr   r   r   r   Ú<module>   s    