B
    u9a²  ã               @   sŽ   d Z ddlZddlZddlmZ ddlZddlZddlZe 	¡ sJe 
d¡‚eeeeƒƒƒZej ed¡ZG dd„ dejƒZedkrŠe ¡  dS )	z+Tests for the asdl parser in Parser/asdl.pyé    N)Údirnamez'test irrelevant for an installed PythonZParserc               @   s\   e Zd Zedd„ ƒZe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S )ÚTestAsdlParserc             C   sb   t j dt¡ tj dtj td¡¡}| 	¡ | _
| j
 tj td¡¡| _|  | j
 | j¡d¡ d S )Nr   Úasdlzasdl.pyzPython.asdlzModule validation failed)ÚsysÚpathÚinsertÚ
parser_dirÚ	importlibÚ	machineryÚSourceFileLoaderÚosÚjoinÚload_moduler   ÚparseÚmodÚ
assertTrueZcheck)ÚclsÚloader© r   ú&/usr/lib/python3.7/test_asdl_parser.pyÚ
setUpClass   s    
zTestAsdlParser.setUpClassc             C   s   t jd= d S )Nr   )r   r   )r   r   r   r   ÚtearDownClass!   s    zTestAsdlParser.tearDownClassc             C   s   t j| _t j| _| jj| _d S )N)r   r   r   Útypes)Úselfr   r   r   ÚsetUp%   s    zTestAsdlParser.setUpc             C   s>   |   | jjd¡ |  d| j¡ |  d| j¡ |  d| j¡ d S )NÚPythonÚstmtÚexprr   )ÚassertEqualr   ÚnameZassertInr   )r   r   r   r   Útest_module+   s    zTestAsdlParser.test_modulec             C   sb   | j j}|  |d | jj¡ |  |d j| jj¡ |  | jd | jj¡ |  | jd | jj¡ d S )Nr   ZwithitemÚalias)	r   ÚdfnsÚassertIsInstancer   ZTypeÚvalueZSumr   ZProduct)r   Zdefsr   r   r   Útest_definitions1   s
    zTestAsdlParser.test_definitionsc             C   s   | j d }|  t|ƒd¡ d S )Nr!   zGProduct([Field(identifier, name), Field(identifier, asname, opt=True)]))r   r   Ústr)r   r!   r   r   r   Útest_product9   s    
zTestAsdlParser.test_productc             C   sL   | j d }|  t|jƒd¡ |  t|jd ƒd¡ |  t|jd ƒd¡ d S )Nr   é   r   zField(int, lineno)é   zField(int, col_offset))r   r   ÚlenÚ
attributesr&   )r   r   r   r   r   Útest_attributes?   s    
zTestAsdlParser.test_attributesc             C   s   | j d }|  t|j ƒd¡ |  t|jƒd¡ |j d }|  || jj¡ |  t|jƒd¡ |jd }|  |jd¡ |  |j	d¡ |  
|j¡ |jd }|  |jd¡ |  |j	d	¡ |  
|j¡ |jd }|  |jd
¡ |  |j	d¡ |  |j¡ |  
|j¡ d S )NZexcepthandlerr)   r(   r   é   r   ÚtypeZ
identifierr   r   Zbody)r   r   r*   r+   r#   r   ZConstructorÚfieldsr.   r   r   ÚoptZassertFalseÚseq)r   ZehandlerÚconsZf0Úf1Úf2r   r   r   Útest_constructor_fieldsE   s&    




z&TestAsdlParser.test_constructor_fieldsc             C   sB   G dd„ d| j jƒ}|ƒ }| | jd ¡ |  |jdddg¡ d S )Nc                   s<   e Zd Z‡ fdd„Zdd„ Zdd„ Zdd„ Zd	d
„ Z‡  ZS )z2TestAsdlParser.test_visitor.<locals>.CustomVisitorc                s   t ƒ  ¡  g | _d S )N)ÚsuperÚ__init__Únames_with_seq)r   )Ú	__class__r   r   r7   `   s    
z;TestAsdlParser.test_visitor.<locals>.CustomVisitor.__init__c             S   s   x|j D ]}|  |¡ qW d S )N)r"   Úvisit)r   r   Zdfnr   r   r   ÚvisitModuled   s    z>TestAsdlParser.test_visitor.<locals>.CustomVisitor.visitModulec             S   s   |   |j¡ d S )N)r:   r$   )r   r.   r   r   r   Ú	visitTypeh   s    z<TestAsdlParser.test_visitor.<locals>.CustomVisitor.visitTypec             S   s   x|j D ]}|  |¡ qW d S )N)r   r:   )r   ÚsumÚtr   r   r   ÚvisitSumk   s    z;TestAsdlParser.test_visitor.<locals>.CustomVisitor.visitSumc             S   s(   x"|j D ]}|jr| j |j¡ qW d S )N)r/   r1   r8   Úappendr   )r   r2   Úfr   r   r   ÚvisitConstructoro   s    zCTestAsdlParser.test_visitor.<locals>.CustomVisitor.visitConstructor)	Ú__name__Ú
__module__Ú__qualname__r7   r;   r<   r?   rB   Ú__classcell__r   r   )r9   r   ÚCustomVisitor_   s
   rG   r   ZModuleZInteractiveZSuite)r   ZVisitorBaser:   r   r   r8   )r   rG   Úvr   r   r   Útest_visitor^   s    zTestAsdlParser.test_visitorN)rC   rD   rE   Úclassmethodr   r   r   r    r%   r'   r,   r5   rI   r   r   r   r   r      s   r   Ú__main__)Ú__doc__Zimportlib.machineryr	   r   Zos.pathr   r   Z	sysconfigZunittestZis_python_buildZSkipTestÚ__file__Zsrc_baser   r   r   ZTestCaser   rC   Úmainr   r   r   r   Ú<module>   s   
f