B
    18™\ž0  ã               @   sÐ   d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
mZmZ ddlmZ ej ej e¡d¡Zej ed¡Zej e¡ ze
 d¡ZW dej ¡  X e
 d¡ZG d	d
„ d
ejƒZdS )z
Unit tests for refactor.py.
é    N)ÚrefactorÚpygramÚ
fixer_base)ÚtokenÚdataÚfixersÚmyfixeszlib2to3.fixesc               @   sú   e Zd Zdd„ Zdd„ Zdd„ Zdedf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fdd„Zdd „ Zd!d"„ Zefd#d$„Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ ZdS )9ÚTestRefactoringToolc             C   s   t j t¡ d S )N)ÚsysÚpathÚappendÚ	FIXER_DIR)Úself© r   ú_/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/lib2to3/tests/test_refactor.pyÚsetUp   s    zTestRefactoringTool.setUpc             C   s   t j ¡  d S )N)r
   r   Úpop)r   r   r   r   ÚtearDown"   s    zTestRefactoringTool.tearDownc             C   s6   x0t ||ƒD ]"\}}t||ƒs|  d| |¡ qW d S )Nz%s are not instances of %s)ÚzipÚ
isinstanceÚfail)r   Z	instancesÚclassesÚinstÚclsr   r   r   Úcheck_instances%   s    
z#TestRefactoringTool.check_instancesNc             C   s   t  |||¡S )N)r   ÚRefactoringTool)r   Úoptionsr   Úexplicitr   r   r   Úrt*   s    zTestRefactoringTool.rtc             C   s4   |   ddi¡}|  |jtj¡ |  |jjtj¡ d S )NÚprint_functionT)r   ÚassertIsÚgrammarr   Ú!python_grammar_no_print_statementÚdriver)r   r   r   r   r   Útest_print_function_option-   s    
z.TestRefactoringTool.test_print_function_optionc             C   s2   |   ¡ }|  |j¡ |   ddi¡}|  |j¡ d S )NÚwrite_unchanged_filesT)r   ÚassertFalser%   Ú
assertTrue)r   r   r   r   r   Ú!test_write_unchanged_files_option3   s    z5TestRefactoringTool.test_write_unchanged_files_optionc             C   sj   dddddg}t  d¡}t  dd¡}t  d¡}|  |dd	„ |D ƒ¡ |  ||¡ |  |d
d	„ |D ƒ¡ d S )Nr   ÚfirstÚlastZparrotZpreorderr   Fc             S   s   g | ]}d | ‘qS )Úfix_r   )Ú.0Únamer   r   r   ú
<listcomp>>   s    zBTestRefactoringTool.test_fixer_loading_helpers.<locals>.<listcomp>c             S   s   g | ]}d | ‘qS )zmyfixes.fix_r   )r,   r-   r   r   r   r.   A   s    )r   Úget_all_fix_namesÚget_fixers_from_packageÚassertEqual)r   ÚcontentsZnon_prefixedZprefixedZ
full_namesr   r   r   Útest_fixer_loading_helpers9   s    

z.TestRefactoringTool.test_fixer_loading_helpersc             C   s‚  t j}t}|ƒ }|  |dƒ|¡ |  |dƒ|dƒ¡ |  |dƒ|dƒ¡ |  |dƒ|dƒ¡ d}|  ||ƒ|d	ƒ¡ d
}|  ||ƒ|dƒ¡ d}|  ||ƒ|dƒ¡ d}|  ||ƒ|d	ƒ¡ d}|  ||ƒ|dƒ¡ d}|  ||ƒ|d	ƒ¡ d}x|D ]}|  ||ƒ|¡ qöW d}|  ||ƒ|dƒ¡ d}|  ||ƒ|¡ d}|  ||ƒ|dƒ¡ d}|  ||ƒ|dƒ¡ d}|  ||ƒ|¡ d S )NÚ z%from __future__ import print_function)r   z!from __future__ import generators)Ú
generatorsz*from __future__ import generators, feature)r5   Zfeaturez1from __future__ import generators, print_function)r5   r   z1from __future__ import print_function, generators)r   r5   z(from __future__ import (print_function,)z3from __future__ import (generators, print_function)z2from __future__ import (generators, nested_scopes))r5   Únested_scopeszGfrom __future__ import generators
from __future__ import print_function)Úfromzfrom 4zfrom xzfrom x 5z	from x imzfrom x importzfrom x import 4z1'docstring'
from __future__ import print_functionz9'docstring'
'somng'
from __future__ import print_functionz/# comment
from __future__ import print_functionz5# comment
'doc'
from __future__ import print_functionz3class x: pass
from __future__ import print_function)r   Ú_detect_future_featuresÚ	frozensetr1   )r   ÚrunÚfsÚemptyZinpÚinvalidr   r   r   Útest_detect_future_featuresC   sF    






z/TestRefactoringTool.test_detect_future_featuresc             C   sÂ   G dd„ dt jƒ}G dd„ dt jƒ}G dd„ dt jƒ}|i g ƒ}|i g ƒ}|i g ƒ}t |||g¡}| tjj¡}|  |||g¡ | t	j
¡}	|  |	||g¡ x| ¡ D ]}
|  |
|g¡ q¨W d S )Nc               @   s   e Zd ZdS )z;TestRefactoringTool.test_get_headnode_dict.<locals>.NoneFixN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   ÚNoneFixq   s   rB   c               @   s   e Zd ZdZdS )z@TestRefactoringTool.test_get_headnode_dict.<locals>.FileInputFixzfile_input< any * >N)r?   r@   rA   ÚPATTERNr   r   r   r   ÚFileInputFixt   s   rD   c               @   s   e Zd ZdZdS )z=TestRefactoringTool.test_get_headnode_dict.<locals>.SimpleFixz'name'N)r?   r@   rA   rC   r   r   r   r   Ú	SimpleFixw   s   rE   )r   ÚBaseFixr   Ú_get_headnode_dictr   r   Úpython_symbolsÚ
file_inputr1   r   ÚNAMEÚvalues)r   rB   rD   rE   Zno_headZ	with_headZsimpleÚdZ	top_fixesZ
name_fixesÚfixesr   r   r   Útest_get_headnode_dictp   s    


z*TestRefactoringTool.test_get_headnode_dictc             C   sh   ddl m} ddlm} ddlm} ddlm} |  ¡ }| 	¡ \}}|  
||g¡ |  
||||g¡ d S )Nr   )ÚFixFirst)ÚFixLast)Ú	FixParrot)ÚFixPreorder)Zmyfixes.fix_firstrO   Zmyfixes.fix_lastrP   Zmyfixes.fix_parrotrQ   Zmyfixes.fix_preorderrR   r   Ú
get_fixersr   )r   rO   rP   rQ   rR   r   ÚpreÚpostr   r   r   Útest_fixer_loading…   s    z&TestRefactoringTool.test_fixer_loadingc             C   sD   | j t| jdgd | j tj| jdgd | j tj| jdgd d S )NZnot_here)r   Zno_fixer_clsZ	bad_order)ÚassertRaisesÚImportErrorr   r   Ú
FixerError)r   r   r   r   Útest_naughty_fixers‘   s    z'TestRefactoringTool.test_naughty_fixersc             C   sL   |   ¡ }d}| |d¡}|  t|ƒ|¡ d}| |d¡}|  t|ƒ|¡ d S )Nzdef parrot(): pass

z<test>zdef f(): pass

)r   Úrefactor_stringÚassertNotEqualÚstrr1   )r   r   ÚinputÚtreer   r   r   Útest_refactor_string–   s    z(TestRefactoringTool.test_refactor_stringc                sf   G ‡ fdd„dt jƒ}g ‰ |tƒ}tj}t d¡t_z| ¡  W d |t_X ddddg}|  ˆ |¡ d S )Nc                   s   e Zd Z‡ fdd„ZdS )z5TestRefactoringTool.test_refactor_stdin.<locals>.MyRTc                s   ˆ   ||||g¡ d S )N)Úextend)r   Úold_textÚnew_textÚfilenameÚequal)Úresultsr   r   Úprint_output¤   s    zBTestRefactoringTool.test_refactor_stdin.<locals>.MyRT.print_outputN)r?   r@   rA   rg   r   )rf   r   r   ÚMyRT¢   s   rh   zdef parrot(): pass

zdef cheese(): pass

z<stdin>F)	r   r   Ú_DEFAULT_FIXERSr
   ÚstdinÚioÚStringIOÚrefactor_stdinr1   )r   rh   r   ÚsaveÚexpectedr   )rf   r   Útest_refactor_stdin    s    z'TestRefactoringTool.test_refactor_stdinTc       	      C   sv   |   |¡}|  |¡}| j||d}|r,||_| |¡ |  ||  |¡¡ |sPd S | |d¡ |  |¡}|  ||¡ |S )N)r   r   T)Úinit_test_fileÚ	read_filer   Ú	log_debugÚrefactor_filer1   r\   )	r   Ú	test_filer   r   Úmock_log_debugÚactually_writeÚold_contentsr   Únew_contentsr   r   r   Úcheck_file_refactoring´   s    



z*TestRefactoringTool.check_file_refactoringc             C   sL   t jdd}|  tj|¡ t ||¡ tj |tj 	|¡¡}t 
|d¡ |S )Nz2to3-test_refactor)Úprefixi¤  )ÚtempfileÚmkdtempÚ
addCleanupÚshutilÚrmtreeÚcopyÚosr   ÚjoinÚbasenameÚchmod)r   ru   Útmpdirr   r   r   rq   Ç   s    z"TestRefactoringTool.init_test_filec          	   C   s   t |dƒ
}| ¡ S Q R X d S )NÚrb)ÚopenÚread)r   ru   Úfpr   r   r   rr   Ï   s    zTestRefactoringTool.read_filec             C   s>   |   |¡}|  |¡}| j|d}| |d¡ |  |¡}||fS )N)r   T)rq   rr   r   rt   )r   ru   r   rx   r   ry   r   r   r   rt   Ó   s    


z!TestRefactoringTool.refactor_filec             C   s   t j td¡}|  |t¡ d S )Nzparrot_example.py)r‚   r   rƒ   r   rz   ri   )r   ru   r   r   r   Útest_refactor_fileÛ   s    z&TestRefactoringTool.test_refactor_filec                sŒ   t j td¡}g ‰ ‡ fdd„}| j|dddi|dd d	t t jt j |¡ ¡ }x4ˆ D ]}d
|krX|  	||¡ P qXW |  
d|ˆ f ¡ d S )Nzparrot_example.pyc                s   ˆ   | | ¡ d S )N)r   )ÚmsgÚargs)Údebug_messagesr   r   Úrecording_log_debugâ   s    zXTestRefactoringTool.test_refactor_file_write_unchanged_file.<locals>.recording_log_debugr   r%   TF)r   r   rv   rw   zNot writing changes to .*%szNot writing changesz%r not matched in %r)r‚   r   rƒ   r   rz   ÚreÚescapeÚsepr„   ÚassertRegexr   )r   ru   r   Zmessage_regexÚmessager   )rŽ   r   Ú'test_refactor_file_write_unchanged_fileß   s    
z;TestRefactoringTool.test_refactor_file_write_unchanged_filec                sV   ‡ fdd„}|g g ƒ ddddddg}dg}|||ƒ dt j d	d
¡g}|||ƒ d S )Nc          	      s°   ‡fdd„}t jj}|t j_ˆ ¡ }g ‰tjdd‰ zHt tj 	ˆ d¡¡ x$| D ]}t
tj 	ˆ |¡dƒ ¡  qPW | ˆ ¡ W d |t j_t ˆ ¡ X ˆ ˆ‡ fdd„|D ƒ¡ d S )	Nc                s   ˆ   |¡ d S )N)r   )r   Úfr   )Úgotr   r   Úmock_refactor_fileõ   s    zPTestRefactoringTool.test_refactor_dir.<locals>.check.<locals>.mock_refactor_filez2to3-test_refactor)r{   Úa_dirÚwbc                s   g | ]}t j ˆ |¡‘qS r   )r‚   r   rƒ   )r,   r   )Údirr   r   r.     s    zHTestRefactoringTool.test_refactor_dir.<locals>.check.<locals>.<listcomp>)r   r   rt   r   r|   r}   r‚   Úmkdirr   rƒ   rˆ   ÚcloseÚrefactor_dirr   r€   r1   )Z	structurero   r˜   Z	save_funcr   Úfn)r   )r›   r—   r   Úcheckô   s    
z4TestRefactoringTool.test_refactor_dir.<locals>.checkÚnothingzhi.pyz.dumbz	.after.pyz	notpy.npyZsappyr™   zstuff.py)r‚   r   rƒ   )r   r    r_   ro   r   )r   r   Útest_refactor_diró   s    

z%TestRefactoringTool.test_refactor_dirc             C   s   t j td¡}|  |¡ d S )Nzdifferent_encoding.py)r‚   r   rƒ   ÚTEST_DATA_DIRrz   )r   rŸ   r   r   r   Útest_file_encoding  s    z&TestRefactoringTool.test_file_encodingc             C   s   t j td¡}|  |¡}d S )Nzfalse_encoding.py)r‚   r   rƒ   r£   rz   )r   rŸ   r   r   r   r   Útest_false_file_encoding  s    z,TestRefactoringTool.test_false_file_encodingc             C   s.   t j td¡}|  |¡}|  | tj¡¡ d S )Nzbom.py)	r‚   r   rƒ   r£   rz   r'   Ú
startswithÚcodecsÚBOM_UTF8)r   rŸ   r   r   r   r   Útest_bom  s    
zTestRefactoringTool.test_bomc             C   sB   t j}dt _z(t j td¡}t d¡}|  ||¡ W d |t _X d S )Nz
zcrlf.pyzlib2to3.fixes)r‚   Úlinesepr   rƒ   r£   r   r0   rz   )r   Zold_seprŸ   rM   r   r   r   Útest_crlf_newlines   s    
z&TestRefactoringTool.test_crlf_newlinesc             C   sD   t j td¡}|  |¡\}}|  d|¡ |  d|¡ |  d|¡ d S )Nzcrlf.pys   
s   
)r‚   r   rƒ   r£   rt   ÚassertInÚassertNotIn)r   rŸ   ÚoldÚnewr   r   r   Útest_crlf_unchanged*  s
    z'TestRefactoringTool.test_crlf_unchangedc             C   sD   |   ¡ }d}| |d¡}|  ||¡ d}| |d¡}|  ||¡ d S )Nz
>>> example()
42
z<test>z&
>>> def parrot():
...      return 43
)r   Úrefactor_docstringr1   r\   )r   r   ÚdocÚoutr   r   r   Útest_refactor_docstring1  s    z+TestRefactoringTool.test_refactor_docstringc             C   sd   ddl m} | jdgd}|  t|jƒd¡ | jdgd}x$|jD ]}t||ƒrBP qBW |  d¡ d S )Nr   )ÚFixExplicitzmyfixes.fix_explicit)r   )r   zexplicit fixer not loaded)Zmyfixes.fix_explicitrµ   r   r1   ÚlenÚ
post_orderr   r   )r   rµ   r   Úfixr   r   r   Útest_explicitB  s    
z!TestRefactoringTool.test_explicit) r?   r@   rA   r   r   r   ri   r   r$   r(   r3   r>   rN   rV   rZ   r`   rp   Ú_2TO3_FIXERSrz   rq   rr   rt   r‹   r•   r¢   r¤   r¥   r©   r«   r°   r´   r¹   r   r   r   r   r	      s:   
-
 
r	   )Ú__doc__r
   r‚   r§   rk   r   r|   r   ÚunittestÚlib2to3r   r   r   Zlib2to3.pgen2r   r   rƒ   ÚdirnameÚ__file__r£   r   r   r0   ri   r   rº   ÚTestCaser	   r   r   r   r   Ú<module>   s$   
