
    ;e*1                        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           	  e
j        d          Zej                                         n# ej                                         w xY w e
j        d          Z G d	 d
ej                  ZdS )z
Unit tests for refactor.py.
    N)refactorpygram
fixer_base)tokendatafixersmyfixeslib2to3.fixesc                       e Zd Zd Zd Zd ZdedfdZd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd ZedddfdZd Zd ZefdZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z dS )TestRefactoringToolc                 N    t           j                            t                     d S N)syspathappend	FIXER_DIRselfs    2/usr/lib/python3.11/lib2to3/tests/test_refactor.pysetUpzTestRefactoringTool.setUp   s    	"""""    c                 B    t           j                                         d S r   )r   r   popr   s    r   tearDownzTestRefactoringTool.tearDown"   s    r   c                     t          ||          D ].\  }}t          ||          s|                     d|z  |           /d S )Nz%s are not instances of %s)zip
isinstancefail)r   	instancesclassesinstclss        r   check_instancesz#TestRefactoringTool.check_instances%   s[    Y00 	M 	MID#dC(( M		6BGLLL	M 	Mr   Nc                 .    t          j        |||          S r   )r   RefactoringTool)r   optionsr   explicits       r   rtzTestRefactoringTool.rt*   s    'BBBr   c                     |                      ddi          }|                     d|j        j                   |                     d|j        j        j                   d S )Nprint_functionTprintr(   assertNotIngrammarkeywordsdriverr   r(   s     r   test_print_function_optionz.TestRefactoringTool.test_print_function_option-   sX    WW&.//"*"5666")"3"<=====r   c                     |                      ddi          }|                     d|j        j                   |                     d|j        j        j                   d S )Nexec_functionTexecr,   r1   s     r   test_exec_function_optionz-TestRefactoringTool.test_exec_function_option2   sW    WWo-..!4555!2!;<<<<<r   c                     |                                  }|                     |j                   |                      ddi          }|                     |j                   d S )Nwrite_unchanged_filesT)r(   assertFalser8   
assertTruer1   s     r   !test_write_unchanged_files_optionz5TestRefactoringTool.test_write_unchanged_files_option7   sX    WWYY1222WW-566011111r   c                 4   g d}t          j        d          }t          j        dd          }t          j        d          }|                     |d |D                        |                     ||           |                     |d |D                        d S )N)r'   firstlastparrotpreorderr	   Fc                     g | ]}d |z   S )fix_ .0names     r   
<listcomp>zBTestRefactoringTool.test_fixer_loading_helpers.<locals>.<listcomp>B   s    #G#G#GdFTM#G#G#Gr   c                     g | ]}d |z   S )zmyfixes.fix_rC   rD   s     r   rG   zBTestRefactoringTool.test_fixer_loading_helpers.<locals>.<listcomp>E   s    EEED.4/EEEr   )r   get_all_fix_namesget_fixers_from_packageassertEqual)r   contentsnon_prefixedprefixed
full_namess        r   test_fixer_loading_helpersz.TestRefactoringTool.test_fixer_loading_helpers=   s    FFF1)<<-i??5i@@
#G#Gh#G#G#GHHHx000EEHEEE	G 	G 	G 	G 	Gr   c                 2   t           j        }t          } |            }|                      |d          |           |                      |d           |d                     |                      |d           |d                     |                      |d           |d                     d}|                      ||           |d	                     d
}|                      ||           |d                     d}|                      ||           |d                     d}|                      ||           |d	                     d}|                      ||           |d                     d}|                      ||           |d	                     d}|D ]!}|                      ||          |           "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)rS   featurez1from __future__ import generators, print_function)rS   r*   z1from __future__ import print_function, generators)r*   rS   z(from __future__ import (print_function,)z3from __future__ import (generators, print_function)z2from __future__ import (generators, nested_scopes))rS   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	frozensetrK   )r   runfsemptyinpinvalids         r   test_detect_future_featuresz/TestRefactoringTool.test_detect_future_featuresG   s   .R%(((DEE/00	2 	2 	2@AAO,,	. 	. 	.IJJ566	8 	8 	8AS22&F#G#GHHH@S22&F#G#GHHH8S22&9#:#:;;;CS22&F#G#GHHHBS22&E#F#FGGG)S22&F#G#GHHH  	. 	.CSSXXu----BS22&9#:#:;;;KS5)))@S22&9#:#:;;;GS22&9#:#:;;;DS5)))))r   c                 N    G d dt           j                  } G d dt           j                  } G d dt           j                  } |i g           } |i g           } |i g           }t          j        |||g          }|                    t
          j        j                  }|                     |||g           |                    t          j
                  }	|                     |	||g           |                                D ]}
|                     |
|g           d S )Nc                       e Zd ZdS );TestRefactoringTool.test_get_headnode_dict.<locals>.NoneFixN)__name__
__module____qualname__rC   r   r   NoneFixra   u   s        Dr   re   c                       e Zd ZdZdS )@TestRefactoringTool.test_get_headnode_dict.<locals>.FileInputFixzfile_input< any * >Nrb   rc   rd   PATTERNrC   r   r   FileInputFixrg   x   s        +GGGr   rj   c                       e Zd ZdZdS )=TestRefactoringTool.test_get_headnode_dict.<locals>.SimpleFixz'name'Nrh   rC   r   r   	SimpleFixrl   {   s        GGGr   rm   )r   BaseFixr   _get_headnode_dictr   r   python_symbols
file_inputrK   r   NAMEvalues)r   re   rj   rm   no_head	with_headsimpled	top_fixes
name_fixesfixess              r   test_get_headnode_dictz*TestRefactoringTool.test_get_headnode_dictt   so   	 	 	 	 	j( 	 	 		, 	, 	, 	, 	,:- 	, 	, 	,	 	 	 	 	
* 	 	 	 '"b// LR((	2r""')V(DEEEE&/:;;	Y$8999UU5:&&
fg%6777XXZZ 	/ 	/EUWI....	/ 	/r   c                     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)myfixes.fix_firstr}   myfixes.fix_lastr~   myfixes.fix_parrotr   myfixes.fix_preorderr   r(   
get_fixersr#   )r   r}   r~   r   r   r(   preposts           r   test_fixer_loadingz&TestRefactoringTool.test_fixer_loading   s    ......,,,,,,000000444444WWYYMMOO	TS;-000THi#ABBBBBr   c                     |                      t          | j        dg           |                      t          j        | j        dg           |                      t          j        | j        dg           d S )Nnot_herer   no_fixer_cls	bad_order)assertRaisesImportErrorr(   r   
FixerErrorr   s    r   test_naughty_fixersz'TestRefactoringTool.test_naughty_fixers   sl    +tw
|DDD(-tw?OPPP(-tw}MMMMMr   c                    |                                  }d}|                    |d          }|                     t          |          |           d}|                    |d          }|                     t          |          |           d S )Ndef parrot(): pass

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

)r(   refactor_stringassertNotEqualstrrK   )r   r(   inputtrees       r   test_refactor_stringz(TestRefactoringTool.test_refactor_string   s    WWYY(!!%22CIIu---#!!%22TE*****r   c                 N    G fddt           j                  }g  |t                    }t          j        }t          j        d          t          _        	 |                                 |t          _        n# |t          _        w xY wg d}|                     |           d S )Nc                       e Zd Z fdZdS )5TestRefactoringTool.test_refactor_stdin.<locals>.MyRTc                 :                         ||||g           d S r   )extend)r   old_textnew_textfilenameequalresultss        r   print_outputzBTestRefactoringTool.test_refactor_stdin.<locals>.MyRT.print_output   s$    (HeDEEEEEr   N)rb   rc   rd   r   )r   s   r   MyRTr      s5        F F F F F F Fr   r   r   )r   zdef cheese(): pass

z<stdin>F)	r   r%   _DEFAULT_FIXERSr   stdinioStringIOrefactor_stdinrK   )r   r   r(   saveexpectedr   s        @r   test_refactor_stdinz'TestRefactoringTool.test_refactor_stdin   s    	F 	F 	F 	F 	F 	F 	F8+ 	F 	F 	F
 T/""yK 899		CIICI& & & 	(+++++s   A: :BTc                    |                      |          }|                     |          }|                     ||          }|r||_        |                    |           |                     ||                     |                     |sd S |                    |d           |                     |          }|                     ||           |S )N)r   r&   T)init_test_file	read_filer(   	log_debugrefactor_filerK   r   )	r   	test_filer   r&   mock_log_debugactually_writeold_contentsr(   new_contentss	            r   check_file_refactoringz*TestRefactoringTool.check_file_refactoring   s     ''	22	~~i00WWFGW44 	*)BL
###t~~i'@'@AAA 	F
D)))~~i00L,777r   c                 >   t          j        d          }|                     t          j        |           t          j        ||           t          j                            |t          j        	                    |                    }t          j
        |d           |S )N2to3-test_refactorprefixi  )tempfilemkdtemp
addCleanupshutilrmtreecopyosr   joinbasenamechmod)r   r   tmpdirs      r   r   z"TestRefactoringTool.init_test_file   s{    !)=>>>v...Iv&&&GLL)9)9))D)DEE	
E"""r   c                 ~    t          |d          5 }|                                cd d d            S # 1 swxY w Y   d S )Nrb)openread)r   r   fps      r   r   zTestRefactoringTool.read_file   s~    )T"" 	b7799	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   266c                     |                      |          }|                     |          }|                     |          }|                    |d           |                     |          }||fS )Nr   T)r   r   r(   r   )r   r   r   r   r(   r   s         r   r   z!TestRefactoringTool.refactor_file   sk    ''	22	~~i00WWFW##
D)))~~i00\))r   c                     t           j                            t          d          }|                     |t
                     d S )Nparrot_example.py)r   r   r   r   r   r   )r   r   s     r   test_refactor_filez&TestRefactoringTool.test_refactor_file   s4    GLL,?@@	##I?????r   c                    t           j                            t          d          }g fd}|                     |dddi|d           dt          j        t           j        t           j                            |          z             z  }D ]}d	|v r| 	                    ||            d S  | 
                    |d
           d S )Nr   c                 8                         | |z             d S r   r   )msgargsdebug_messagess     r   recording_log_debugzXTestRefactoringTool.test_refactor_file_write_unchanged_file.<locals>.recording_log_debug   s!    !!#*-----r   rC   r8   TF)r   r&   r   r   zNot writing changes to .*%szNot writing changesz not matched in )r   r   r   r   r   reescapesepr   assertRegexr   )r   r   r   message_regexmessager   s        @r   'test_refactor_file_write_unchanged_filez;TestRefactoringTool.test_refactor_file_write_unchanged_file   s    GLL,?@@		. 	. 	. 	. 	.##Ib-Dd,K3F38 	$ 	: 	: 	: 7	"&27#3#3I#>#>>??@% 	P 	PG$//  -888 0 II~~NOOOOOr   c                       fd} |g g            g d}dg} |||           dt           j                            dd          g} |||           d S )Nc                    fd}t           j        j        }|t           j        _                                        }g t	          j        d          	 t          j        t          j        	                    d                     | D ]B}t          t          j        	                    |          d                                           C|                               |t           j        _        t          j                   n*# |t           j        _        t          j                   w xY w                    fd|D                        d S )Nc                 2                         |           d S r   r   )r   fr   gots      r   mock_refactor_filezPTestRefactoringTool.test_refactor_dir.<locals>.check.<locals>.mock_refactor_file   s    

1r   r   r   a_dirwbc                 P    g | ]"}t           j                            |          #S rC   r   r   r   )rE   r   dirs     r   rG   zHTestRefactoringTool.test_refactor_dir.<locals>.check.<locals>.<listcomp>	  s)    KKK$bgll355KKKr   )r   r%   r   r(   r   r   r   mkdirr   r   r   closerefactor_dirr   r   rK   )		structurer   r   	save_funcr(   fnr   r   r   s	         @@r   checkz4TestRefactoringTool.test_refactor_dir.<locals>.check   sR        0>I5GH$2BC"*>???C#c733444# > >Bc2..55;;====$$$9B(6c"""" :C(6c""""SKKKK(KKKM M M M Ms   BD	 	'D0)nothinghi.pyz.dumbz	.after.pyz	notpy.npysappyr   r   zstuff.pyr   )r   r   r   r   s   `   r   test_refactor_dirz%TestRefactoringTool.test_refactor_dir   s    	M 	M 	M 	M 	M$ 	b"   9dHWj113dDr   c                 z    t           j                            t          d          }|                     |           d S )Nzdifferent_encoding.pyr   r   r   TEST_DATA_DIRr   )r   r   s     r   test_file_encodingz&TestRefactoringTool.test_file_encoding  s2    W\\-)@AA##B'''''r   c                 z    t           j                            t          d          }|                     |          }d S )Nzfalse_encoding.pyr   r   r   r   s      r   test_false_file_encodingz,TestRefactoringTool.test_false_file_encoding  s/    W\\-)<==**2..r   c                     t           j                            t          d          }|                     |          }|                     |                    t          j                             d S )Nzbom.py)	r   r   r   r   r   r:   
startswithcodecsBOM_UTF8r   s      r   test_bomzTestRefactoringTool.test_bom  sP    W\\-22**2..8899999r   c                    t           j        }dt           _        	 t           j                            t          d          }t          j        d          }|                     ||           |t           _        d S # |t           _        w xY w)Nz
crlf.pyr
   )r   linesepr   r   r   r   rJ   r   )r   old_sepr   rz   s       r   test_crlf_newlinesz&TestRefactoringTool.test_crlf_newlines$  sn    *
	!mY77B4_EEE''E222 BJJJBJ    s   AA7 7Bc                    t           j                            t          d          }|                     |          \  }}|                     d|           |                     d|           |                     d|           d S )Nr  s   
s   
)r   r   r   r   r   assertInr-   )r   r   oldnews       r   test_crlf_unchangedz'TestRefactoringTool.test_crlf_unchanged.  sr    W\\-33%%b))Sgs###gs###C(((((r   c                     |                                  }d}|                    |d          }|                     ||           d}|                    |d          }|                     ||           d S )Nz
>>> example()
42
r   z&
>>> def parrot():
...      return 43
)r(   refactor_docstringrK   r   )r   r(   docouts       r   test_refactor_docstringz+TestRefactoringTool.test_refactor_docstring5  s}    WWYY ##C22c""" ##C22C%%%%%r   c                 "   ddl m} |                     dg          }|                     t	          |j                  d           |                     dg          }|j        D ]}t          ||          r d S |                     d           d S )Nr   )FixExplicitzmyfixes.fix_explicitr   )r'   zexplicit fixer not loaded)myfixes.fix_explicitr  r(   rK   len
post_orderr   r   )r   r  r(   fixs       r   test_explicitz!TestRefactoringTool.test_explicitF  s    444444WW34W55R]++Q///WW56W77= 	3 	3C#{++  II122222r   )!rb   rc   rd   r   r   r#   r   r(   r2   r6   r;   rP   r^   r{   r   r   r   r   _2TO3_FIXERSr   r   r   r   r   r   r   r   r   r  r  r  r  r  rC   r   r   r   r      s       # # #  M M M
 o C C C C> > >
= = =
2 2 2G G G+* +* +*Z/ / /*
C 
C 
CN N N
+ + +, , ,( 8D'+D.2   &     /; * * * *@ @ @P P P(  @( ( (/ / /: : :
! ! !) ) )& & &"3 3 3 3 3r   r   )__doc__r   r   r   r   r   r   r   unittestlib2to3r   r   r   lib2to3.pgen2r   r   r   dirname__file__r   r   r   rJ   r   r   r  TestCaser   rC   r   r   <module>r      sf    


 				  				 				    0 0 0 0 0 0 0 0 0 0       RW__X66??GLL11	 	   6h6yAAOHLLNNNNCHLLNNNN/x/@@t3 t3 t3 t3 t3(+ t3 t3 t3 t3 t3s   C C!