o
    ^ùh*1  ã                   @   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 ej ¡  nej ¡  w 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Úmyfixesú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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/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Z dS );ÚTestRefactoringToolc                 C   s   t j t¡ d S ©N)ÚsysÚpathÚappendÚ	FIXER_DIR©Úself© r   á-  /home/conda/feedstock_root/build_artifacts/python-split_1761171407825/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehol/lib/python3.10/lib2to3/tests/test_refactor.pyÚsetUp   s   zTestRefactoringTool.setUpc                 C   s   t j ¡  d S r   )r   r   Úpopr   r   r   r   ÚtearDown"   ó   zTestRefactoringTool.tearDownc                 C   s2   t ||ƒD ]\}}t||ƒs|  d| |¡ qd S )Nz%s are not instances of %s)ÚzipÚ
isinstanceÚfail)r   Z	instancesZclassesÚinstÚclsr   r   r   Úcheck_instances%   s
   
€þz#TestRefactoringTool.check_instancesNc                 C   s   t  |||¡S r   )r   ÚRefactoringTool)r   Úoptionsr   Úexplicitr   r   r   Úrt*   r   zTestRefactoringTool.rtc                 C   ó4   |   ddi¡}|  d|jj¡ |  d|jjj¡ d S )NÚprint_functionTÚprint©r!   ÚassertNotInZgrammarÚkeywordsZdriver©r   r!   r   r   r   Útest_print_function_option-   ó   z.TestRefactoringTool.test_print_function_optionc                 C   r"   )NZexec_functionTÚexecr%   r(   r   r   r   Útest_exec_function_option2   r*   z-TestRefactoringTool.test_exec_function_optionc                 C   s2   |   ¡ }|  |j¡ |   ddi¡}|  |j¡ d S )NÚwrite_unchanged_filesT)r!   ZassertFalser-   Ú
assertTruer(   r   r   r   Ú!test_write_unchanged_files_option7   s   z5TestRefactoringTool.test_write_unchanged_files_optionc                 C   sd   g d¢}t  d¡}t  dd¡}t  d¡}|  |dd„ |D ƒ¡ |  ||¡ |  |dd„ |D ƒ¡ d S )N)r    ÚfirstÚlastZparrotZpreorderr   Fc                 S   ó   g | ]}d | ‘qS )Zfix_r   ©Ú.0Únamer   r   r   Ú
<listcomp>B   ó    zBTestRefactoringTool.test_fixer_loading_helpers.<locals>.<listcomp>c                 S   r2   )zmyfixes.fix_r   r3   r   r   r   r6   E   r7   )r   Zget_all_fix_namesÚget_fixers_from_packageÚassertEqual)r   ÚcontentsZnon_prefixedZprefixedZ
full_namesr   r   r   Útest_fixer_loading_helpers=   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}|D ]
}|  ||ƒ|¡ qzd}|  ||ƒ|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)r=   Zfeaturez1from __future__ import generators, print_function)r=   r#   z1from __future__ import print_function, generators)r#   r=   z(from __future__ import (print_function,)z3from __future__ import (generators, print_function)z2from __future__ import (generators, nested_scopes))r=   Z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   Z_detect_future_featuresÚ	frozensetr9   )r   ÚrunÚfsÚemptyÚinpÚinvalidr   r   r   Útest_detect_future_featuresG   sL   
ÿ
ÿ
ÿ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¡ | ¡ D ]	}
|  |
|g¡ qSd S )Nc                   @   s   e Zd ZdS )z;TestRefactoringTool.test_get_headnode_dict.<locals>.NoneFixN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   ÚNoneFixu   s    rI   c                   @   ó   e Zd ZdZdS )z@TestRefactoringTool.test_get_headnode_dict.<locals>.FileInputFixzfile_input< any * >N©rF   rG   rH   ZPATTERNr   r   r   r   ÚFileInputFixx   ó    rL   c                   @   rJ   )z=TestRefactoringTool.test_get_headnode_dict.<locals>.SimpleFixz'name'NrK   r   r   r   r   Ú	SimpleFix{   rM   rN   )r   ZBaseFixr   Z_get_headnode_dictr   r   Zpython_symbolsZ
file_inputr9   r   ÚNAMEÚvalues)r   rI   rL   rN   Zno_headZ	with_headÚsimpleÚdZ	top_fixesZ
name_fixesÚfixesr   r   r   Útest_get_headnode_dictt   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_firstrU   Zmyfixes.fix_lastrV   Zmyfixes.fix_parrotrW   Zmyfixes.fix_preorderrX   r!   Z
get_fixersr   )r   rU   rV   rW   rX   r!   ÚpreZ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)ZassertRaisesÚImportErrorr!   r   Z
FixerErrorr   r   r   r   Útest_naughty_fixers•   s   z'TestRefactoringTool.test_naughty_fixersc                 C   sL   |   ¡ }d}| |d¡}|  t|ƒ|¡ d}| |d¡}|  t|ƒ|¡ d S )Núdef parrot(): pass

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

)r!   Zrefactor_stringÚassertNotEqualÚstrr9   )r   r!   ÚinputÚtreer   r   r   Útest_refactor_stringš   s   z(TestRefactoringTool.test_refactor_stringc                    sh   G ‡ fdd„dt jƒ}g ‰ |tƒ}tj}t d¡t_z	| ¡  W |t_n|t_w g d¢}|  ˆ |¡ d S )Nc                       s   e Zd Z‡ fdd„ZdS )z5TestRefactoringTool.test_refactor_stdin.<locals>.MyRTc                    s   ˆ   ||||g¡ d S r   )Úextend)r   Zold_textZnew_textÚfilenameZequal©Úresultsr   r   Úprint_output¨   s   zBTestRefactoringTool.test_refactor_stdin.<locals>.MyRT.print_outputN)rF   rG   rH   ri   r   rg   r   r   ÚMyRT¦   s    rj   r^   )r^   zdef cheese(): pass

z<stdin>F)	r   r   Ú_DEFAULT_FIXERSr   ÚstdinÚioÚStringIOZrefactor_stdinr9   )r   rj   r!   ÚsaveÚexpectedr   rg   r   Útest_refactor_stdin¤   s   
z'TestRefactoringTool.test_refactor_stdinTc           	      C   sv   |   |¡}|  |¡}| j||d}|r||_| |¡ |  ||  |¡¡ |s(d S | |d¡ |  |¡}|  ||¡ |S )N)r   r   T)Úinit_test_fileÚ	read_filer!   Z	log_debugÚrefactor_filer9   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 )Nú2to3-test_refactor©Úprefixi¤  )ÚtempfileÚmkdtempZ
addCleanupÚshutilÚrmtreeÚcopyÚosr   ÚjoinÚbasenameÚchmod)r   ru   Ztmpdirr   r   r   rr   Ë   s   z"TestRefactoringTool.init_test_filec                 C   s6   t |dƒ}| ¡ W  d   ƒ S 1 sw   Y  d S )NÚrb)ÚopenÚread)r   ru   Úfpr   r   r   rs   Ó   s   $ÿzTestRefactoringTool.read_filec                 C   s>   |   |¡}|  |¡}| j|d}| |d¡ |  |¡}||fS )Nr[   T)rr   rs   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 )Núparrot_example.py)rƒ   r   r„   r   rz   rk   )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 |¡ ¡ }ˆ D ]}d
|v r:|  	||¡  d S q+|  
d|ˆ f ¡ d S )Nr‹   c                    s   ˆ   | | ¡ d S r   ©r   )ÚmsgÚargs©Z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…   Z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                    sN   ‡ fdd„}|g g ƒ g d¢}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‰ z+t tj 	ˆ d¡¡ | D ]}t
tj 	ˆ |¡dƒ ¡  q'| ˆ ¡ W |t j_t ˆ ¡ n
|t j_t ˆ ¡ w ˆ ˆ‡ fdd„|D ƒ¡ d S )	Nc                    s   ˆ   |¡ d S r   r   )r   Úfr   )Úgotr   r   Úmock_refactor_fileù   r   zPTestRefactoringTool.test_refactor_dir.<locals>.check.<locals>.mock_refactor_filer{   r|   Úa_dirÚwbc                    s   g | ]	}t j ˆ |¡‘qS r   ©rƒ   r   r„   )r4   r   )Údirr   r   r6   	  s    zHTestRefactoringTool.test_refactor_dir.<locals>.check.<locals>.<listcomp>)r   r   rt   r!   r~   r   rƒ   Úmkdirr   r„   rˆ   ÚcloseZrefactor_dirr€   r   r9   )Z	structurerp   r™   Z	save_funcr!   Úfnr   )r   r˜   r   Úcheckø   s$   ÿÿz4TestRefactoringTool.test_refactor_dir.<locals>.check)Znothingúhi.pyz.dumbz	.after.pyz	notpy.npyZsappyr¢   rš   zstuff.pyrœ   )r   r¡   rc   rp   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  ó   z&TestRefactoringTool.test_file_encodingc                 C   s   t j td¡}|  |¡}d S )Nzfalse_encoding.pyr¤   ©r   r    r   r   r   r   Útest_false_file_encoding  r§   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_UTF8r¨   r   r   r   Útest_bom  s   
zTestRefactoringTool.test_bomc                 C   sF   t j}dt _zt j td¡}t d¡}|  ||¡ W |t _d S |t _w )Nz
úcrlf.pyr	   )rƒ   Úlinesepr   r„   r¥   r   r8   rz   )r   Zold_sepr    rS   r   r   r   Útest_crlf_newlines$  s   
z&TestRefactoringTool.test_crlf_newlinesc                 C   sD   t j td¡}|  |¡\}}|  d|¡ |  d|¡ |  d|¡ d S )Nr®   s   
s   
)rƒ   r   r„   r¥   rt   ZassertInr&   )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
r_   z&
>>> def parrot():
...      return 43
)r!   Zrefactor_docstringr9   r`   )r   r!   ÚdocÚoutr   r   r   Útest_refactor_docstring5  s   z+TestRefactoringTool.test_refactor_docstringc                 C   sd   ddl m} | jdgd}|  t|jƒd¡ | jdgd}|jD ]
}t||ƒr* d S q |  d¡ d S )Nr   )ÚFixExplicitzmyfixes.fix_explicitr[   )r    zexplicit fixer not loaded)Zmyfixes.fix_explicitr·   r!   r9   ÚlenZ
post_orderr   r   )r   r·   r!   Zfixr   r   r   Útest_explicitF  s   

ÿz!TestRefactoringTool.test_explicit)!rF   rG   rH   r   r   r   rk   r!   r)   r,   r/   r;   rE   rT   rZ   r]   rd   rq   Ú_2TO3_FIXERSrz   rr   rs   rt   rŒ   r–   r£   r¦   r©   r­   r°   r³   r¶   r¹   r   r   r   r   r
      s@    
-

þ 
r
   )Ú__doc__r   rƒ   r«   rm   r’   r~   r€   ZunittestZlib2to3r   r   r   Zlib2to3.pgen2r   r   r„   ÚdirnameÚ__file__r¥   r   r   r8   rk   r   rº   ZTestCaser
   r   r   r   r   Ú<module>   s&    
