3
sY]'                 @   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j  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 | j  D ]}|j |kr
|S q
W d S )N)get_childrenget_name)blocknameZch r   #lib/python3.6/test/test_symtable.py
find_block$   s    r   c               @   s   e Zd Zej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 | jj d | j | jj d | j | jj d | j | jj d | j | j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  | j | jj  | 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 | jj  | j | j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  | j | jj  | 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 | jj d | j | jj d d S )Nr      )r   r   Z
get_linenor   )r   r   r   r   test_linenoL   s    zSymtableTest.test_linenoc             C   st   | j }| jt|j ddddg ddddddg}| jt|j | | jt|j ddg | j| j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 | jjdj  | j| jjdj  | j | jjdj  | j | jjdj  | j| jjdj  | j| jjdj  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 | jjdj  | j| jjdj  d S )Nr"   )r   r   r(   is_localr   r   )r   r   r   r   
test_local`   s    zSymtableTest.test_localc             C   sF   | j | jjdj  | j | jjdj  | j| jjdj  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 | jj|j  qW | j| jjdj  d S )Nr   r!   r    r"   )r   r!   r    )r   r   r(   Zis_parameterr   )r   Zsymr   r   r   test_parametersi   s    
zSymtableTest.test_parametersc             C   s6   | j t| jj t| jj  | 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 | jjdj  | j | jjdj  | j | jjdj  | j | jjdj  | j | jjdj  | j| jjdj  | j | jjdj | jk | jjd}| jt	|j
 d | j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 | jjdj  | j | jjdj  | j | jjdj  | j | jjdj  | j| jjdj  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 j ddd}|j d }| j|jdj  | j|jdj  | j|jdj  t j ddd}|j d }| j|jdj  | j|jdj  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 | jjdj  d S )Nsys)r   r   r(   Zis_imported)r   r   r   r   test_imported   s    zSymtableTest.test_importedc             C   sR   | j | jj d | j | jj d | j | jjdj d | j | j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 | jj d d S )Nr   )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 j ddd	  jt.  jt t j dtdd	 W d Q R X W d Q R X  jt t j dtdd	 W d Q R X  jt t j dtdd	 W d Q R X d S )
Nc                st   yt j | dd W nL tk
r^ } z0 j|jd  j|jd  j|j| W Y d d }~X nX  j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 j ddd}d S )N42r
   eval)r8   )r   symbolsr   r   r   	test_eval   s    zSymtableTest.test_evalc             C   s   t j 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 j 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    