a
    äze  ã                   @   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Ú
assertTrueÚcheck)ÚclsÚloader© r   ú+/usr/lib/python3.9/test/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   Ú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   sx   | j d }|  t|jƒd¡ |  t|jd ƒd¡ |  t|jd ƒd¡ |  t|jd ƒd¡ |  t|jd	 ƒd
¡ d S )Nr   é   r   zField(int, lineno)é   zField(int, col_offset)é   z Field(int, end_lineno, opt=True)é   z$Field(int, end_col_offset, opt=True))r   r!   ÚlenÚ
attributesÚrepr)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/   r    ÚtypeZ
identifierr"   r.   r   Úbody)r   r!   r0   r1   r&   r   ZConstructorÚfieldsr4   r"   r   ÚoptZassertFalseÚseq)r   ZehandlerÚconsZf0Úf1Úf2r   r   r   Útest_constructor_fieldsG   s&    




z&TestAsdlParser.test_constructor_fieldsc                 C   s@   G dd„ d| j jƒ}|ƒ }| | jd ¡ |  |jg d¢¡ 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 r   )ÚsuperÚ__init__Únames_with_seqr   ©Ú	__class__r   r   r>   b   s    
z;TestAsdlParser.test_visitor.<locals>.CustomVisitor.__init__c                 S   s   |j D ]}|  |¡ qd S r   )r%   Úvisit)r   r   Zdfnr   r   r   ÚvisitModulef   s    
z>TestAsdlParser.test_visitor.<locals>.CustomVisitor.visitModulec                 S   s   |   |j¡ d S r   )rB   r(   )r   r4   r   r   r   Ú	visitTypej   s    z<TestAsdlParser.test_visitor.<locals>.CustomVisitor.visitTypec                 S   s   |j D ]}|  |¡ qd S r   )r   rB   )r   ÚsumÚtr   r   r   ÚvisitSumm   s    
z;TestAsdlParser.test_visitor.<locals>.CustomVisitor.visitSumc                 S   s$   |j D ]}|jr| j |j¡ qd S r   )r6   r8   r?   Úappendr"   )r   r9   Úfr   r   r   ÚvisitConstructorq   s    
zCTestAsdlParser.test_visitor.<locals>.CustomVisitor.visitConstructor)	Ú__name__Ú
__module__Ú__qualname__r>   rC   rD   rG   rJ   Ú__classcell__r   r   r@   r   ÚCustomVisitora   s
   rO   r   )ÚModulerP   ZInteractiveÚFunctionType)r   ZVisitorBaserB   r   r!   r?   )r   rO   Úvr   r   r   Útest_visitor`   s    ÿzTestAsdlParser.test_visitorN)rK   rL   rM   Úclassmethodr   r   r   r#   r)   r+   r3   r<   rS   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   rK   Úmainr   r   r   r   Ú<module>   s   
i