
    daf*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                  j!                  ej                  j#                  e      d      Zej                  j!                  ed      Zej                  j+                  e       	  e
j,                  d      Zej                  j1                           e
j,                  d      Z G d	 d
ej4                        Zy# ej                  j1                          w xY w)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 y)TestRefactoringToolc                 J    t         j                  j                  t               y N)syspathappend	FIXER_DIRselfs    :/root/Python-3.12.4/Lib/test/test_lib2to3/test_refactor.pysetUpzTestRefactoringTool.setUp   s    	"    c                 @    t         j                  j                          y r   )r   r   popr   s    r   tearDownzTestRefactoringTool.tearDown"   s    r   c                 p    t        ||      D ]'  \  }}t        ||      r| j                  d|z  |       ) y )Nz%s are not instances of %s)zip
isinstancefail)r   	instancesclassesinstclss        r   check_instancesz#TestRefactoringTool.check_instances%   s<    Y0 	MID#dC(		6BGL	Mr   Nc                 0    t        j                  |||      S r   )r   RefactoringTool)r   optionsr   explicits       r   rtzTestRefactoringTool.rt*   s    ''BBr   c                     | j                  ddi      }| j                  d|j                  j                         | j                  d|j                  j                  j                         y )Nprint_functionTprintr(   assertNotIngrammarkeywordsdriverr   r(   s     r   test_print_function_optionz.TestRefactoringTool.test_print_function_option-   sP    WW&./"**"5"56"))"3"3"<"<=r   c                     | j                  ddi      }| j                  d|j                  j                         | j                  d|j                  j                  j                         y )Nexec_functionTexecr,   r1   s     r   test_exec_function_optionz-TestRefactoringTool.test_exec_function_option2   sO    WWo-.!4!45!2!2!;!;<r   c                     | j                         }| j                  |j                         | j                  ddi      }| j                  |j                         y )Nwrite_unchanged_filesT)r(   assertFalser8   
assertTruer1   s     r   !test_write_unchanged_files_optionz5TestRefactoringTool.test_write_unchanged_files_option7   sG    WWY112WW-56001r   c                 P   g d}t        j                  d      }t        j                  dd      }t        j                  d      }| j                  ||D cg c]  }d|z   	 c}       | j                  ||       | j                  ||D cg c]  }d|z   	 c}       y c c}w c c}w )N)r'   firstlastparrotpreorderr	   Ffix_zmyfixes.fix_)r   get_all_fix_namesget_fixers_from_packageassertEqual)r   contentsnon_prefixedprefixed
full_namesnames         r   test_fixer_loading_helpersz.TestRefactoringTool.test_fixer_loading_helpers=   s    F11)<--i?55i@
h#GdFTM#GHx0<DED.4/E	G $H Fs   B

B#
c                    t         j                  }t        } |       }| j                   |d      |       | j                   |d       |d             | j                   |d       |d             | j                   |d       |d             d}| j                   ||       |d	             d
}| j                   ||       |d             d}| j                   ||       |d             d}| j                   ||       |d	             d}| j                   ||       |d             d}| j                   ||       |d	             d}|D ]  }| j                   ||      |        d}| j                   ||       |d             d}| j                   ||      |       d}| j                   ||       |d             d}| j                   ||       |d             d}| j                   ||      |       y )N z%from __future__ import print_function)r*   z!from __future__ import generators)
generatorsz*from __future__ import generators, feature)rM   featurez1from __future__ import generators, print_function)rM   r*   z1from __future__ import print_function, generators)r*   rM   z(from __future__ import (print_function,)z3from __future__ import (generators, print_function)z2from __future__ import (generators, nested_scopes))rM   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	frozensetrD   )r   runfsemptyinpinvalids         r   test_detect_future_featuresz/TestRefactoringTool.test_detect_future_featuresG   s    ..R%(DE/0	2@AO,	.IJ56	8AS2&F#GH@S2&F#GH8S2&9#:;CS2&F#GHBS2&E#FG)S2&F#GH  	.CSXu-	.BS2&9#:;KS5)@S2&9#:;GS2&9#:;DS5)r   c                 0    G d dt         j                        } G d dt         j                        } G d dt         j                        } |i g       } |i g       } |i g       }t        j                  |||g      }|j	                  t
        j                  j                        }| j                  |||g       |j	                  t        j                        }	| j                  |	||g       |j                         D ]  }
| j                  |
|g        y )Nc                       e Zd Zy);TestRefactoringTool.test_get_headnode_dict.<locals>.NoneFixN)__name__
__module____qualname__ r   r   NoneFixr[   u   s    r   r`   c                       e Zd ZdZy)@TestRefactoringTool.test_get_headnode_dict.<locals>.FileInputFixzfile_input< any * >Nr\   r]   r^   PATTERNr_   r   r   FileInputFixrb   x   s    +Gr   re   c                       e Zd ZdZy)=TestRefactoringTool.test_get_headnode_dict.<locals>.SimpleFixz'name'Nrc   r_   r   r   	SimpleFixrg   {   s    Gr   rh   )r   BaseFixr   _get_headnode_dictr   r   python_symbols
file_inputrD   r   NAMEvalues)r   r`   re   rh   no_head	with_headsimpled	top_fixes
name_fixesfixess              r   test_get_headnode_dictz*TestRefactoringTool.test_get_headnode_dictt   s    	j(( 		,:-- 	,	
** 	 "b/ R(	2r"'')V(DEEE&//::;	Y$89UU5::&
fg%67XXZ 	/EUWI.	/r   c                     ddl m} ddlm} ddlm} ddlm} | j                         }|j                         \  }}| j                  ||g       | j                  ||||g       y )Nr   )FixFirst)FixLast)	FixParrot)FixPreorder)myfixes.fix_firstrx   myfixes.fix_lastry   myfixes.fix_parrotrz   myfixes.fix_preorderr{   r(   
get_fixersr#   )r   rx   ry   rz   r{   r(   preposts           r   test_fixer_loadingz&TestRefactoringTool.test_fixer_loading   sQ    .,04WWYMMO	TS;-0THi#ABr   c                     | j                  t        | j                  dg       | j                  t        j                  | j                  dg       | j                  t        j                  | j                  dg       y )Nnot_herer   no_fixer_cls	bad_order)assertRaisesImportErrorr(   r   
FixerErrorr   s    r   test_naughty_fixersz'TestRefactoringTool.test_naughty_fixers   s`    +tww
|D(--tww?OP(--tww}Mr   c                     | j                         }d}|j                  |d      }| j                  t        |      |       d}|j                  |d      }| j	                  t        |      |       y )Ndef parrot(): pass

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

)r(   refactor_stringassertNotEqualstrrD   )r   r(   inputtrees       r   test_refactor_stringz(TestRefactoringTool.test_refactor_string   sc    WWY(!!%2CIu-#!!%2TE*r   c                 8    G fddt         j                        }g  |t              }t        j                  }t        j                  d      t        _        	 |j                          |t        _        g d}| j                  |       y # |t        _        w xY w)Nc                       e Zd Z fdZy)5TestRefactoringTool.test_refactor_stdin.<locals>.MyRTc                 0    j                  ||||g       y r   )extend)r   old_textnew_textfilenameequalresultss        r   print_outputzBTestRefactoringTool.test_refactor_stdin.<locals>.MyRT.print_output   s    (HeDEr   N)r\   r]   r^   r   )r   s   r   MyRTr      s	    Fr   r   r   )r   zdef cheese(): pass

z<stdin>F)	r   r%   _DEFAULT_FIXERSr   stdinioStringIOrefactor_stdinrD   )r   r   r(   saveexpectedr   s        @r   test_refactor_stdinz'TestRefactoringTool.test_refactor_stdin   s~    	F8++ 	F
 /"yyKK 89		CI& 	(+	 CIs   B BTc                 V   | j                  |      }| j                  |      }| j                  ||      }|r||_        |j	                  |       | j                  || j                  |             |sy |j	                  |d       | j                  |      }| j                  ||       |S )N)r   r&   T)init_test_file	read_filer(   	log_debugrefactor_filerD   r   )	r   	test_filer   r&   mock_log_debugactually_writeold_contentsr(   new_contentss	            r   check_file_refactoringz*TestRefactoringTool.check_file_refactoring   s     ''	2	~~i0WWFGW4)BL
#t~~i'@A
D)~~i0L,7r   c                 D   t        j                  d      }| j                  t        j                  |       t        j
                  ||       t        j                  j                  |t        j                  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   sj    !!)=>v.Iv&GGLL)9)9))DE	
E"r   c                 f    t        |d      5 }|j                         cd d d        S # 1 sw Y   y xY w)Nrb)openread)r   r   fps      r   r   zTestRefactoringTool.read_file   s,    )T" 	b779	 	 	s   '0c                     | j                  |      }| j                  |      }| j                  |      }|j                  |d       | j                  |      }||fS )Nr   T)r   r   r(   r   )r   r   r   r   r(   r   s         r   r   z!TestRefactoringTool.refactor_file   sY    ''	2	~~i0WWFW#
D)~~i0\))r   c                 x    t         j                  j                  t        d      }| j	                  |t
               y )Nparrot_example.py)r   r   r   r   r   r   )r   r   s     r   test_refactor_filez&TestRefactoringTool.test_refactor_file   s'    GGLL,?@	##I?r   c                    t         j                  j                  t        d      }g fd}| j	                  |dddi|d       dt        j                  t         j                  t         j                  j                  |      z         z  }D ]  }d	|v s| j                  ||        y  | j                  |d
       y )Nr   c                 .    j                  | |z         y r   r   )msgargsdebug_messagess     r   recording_log_debugzXTestRefactoringTool.test_refactor_file_write_unchanged_file.<locals>.recording_log_debug   s    !!#*-r   r_   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    GGLL,?@		.##Ib-Dd,K3F38 	$ 	: 7		"&&277#3#3I#>>?@% 	PG$/  -8	P
 II~NOr   c                       fd} |g g        g d}dg} |||       dt         j                  j                  dd      g} |||       y )Nc           
          fd}t         j                  j                  }|t         j                  _        	j                         }g t	        j
                  d      }	 t        j                  t        j                  j                  |d             | D ]:  }t        t        j                  j                  ||      d      j                          < |j                  |       |t         j                  _        t        j                  |       	j                  |D cg c]"  }t        j                  j                  ||      $ c}       y # |t         j                  _        t        j                  |       w xY wc c}w )Nc                 (    j                  |       y 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wb)r   r%   r   r(   r   r   r   mkdirr   r   r   closerefactor_dirr   r   rD   )
	structurer   r   	save_funcr(   dirfnr   r   r   s
           @r   checkz4TestRefactoringTool.test_refactor_dir.<locals>.check   s    00>>I5GH$$2BC""*>?C#c734# >Bc2.5;;=>$9B((6c"SBJK$bggll35KM :C((6c"Ks    BE 'E;
,E8)nothinghi.pyz.dumbz	.after.pyz	notpy.npysappyr   r   zstuff.py)r   r   r   )r   r   r   r   s   `   r   test_refactor_dirz%TestRefactoringTool.test_refactor_dir   sT    	M$ 	b" 9dHWj13dDr   c                 n    t         j                  j                  t        d      }| j	                  |       y )Nzdifferent_encoding.pyr   r   r   TEST_DATA_DIRr   )r   r   s     r   test_file_encodingz&TestRefactoringTool.test_file_encoding  s%    WW\\-)@A##B'r   c                 n    t         j                  j                  t        d      }| j	                  |      }y )Nzfalse_encoding.pyr   r   r   r   s      r   test_false_file_encodingz,TestRefactoringTool.test_false_file_encoding  s'    WW\\-)<=**2.r   c                     t         j                  j                  t        d      }| j	                  |      }| j                  |j                  t        j                               y )Nzbom.py)	r   r   r   r   r   r:   
startswithcodecsBOM_UTF8r   s      r   test_bomzTestRefactoringTool.test_bom  s?    WW\\-2**2.89r   c                    t         j                  }dt         _        	 t         j                  j                  t        d      }t        j                  d      }| j                  ||       |t         _        y # |t         _        w xY w)Nz
crlf.pyr
   )r   linesepr   r   r   r   rC   r   )r   old_sepr   ru   s       r   test_crlf_newlinesz&TestRefactoringTool.test_crlf_newlines$  sZ    **
	!mY7B44_EE''E2 BJBJs   AA4 4Bc                     t         j                  j                  t        d      }| j	                  |      \  }}| j                  d|       | j                  d|       | j                  d|       y )Nr   s   
s   
)r   r   r   r   r   assertInr-   )r   r   oldnews       r   test_crlf_unchangedz'TestRefactoringTool.test_crlf_unchanged.  sV    WW\\-3%%b)Sgs#gs#C(r   c                     | j                         }d}|j                  |d      }| j                  ||       d}|j                  |d      }| j                  ||       y )Nz
>>> example()
42
r   z&
>>> def parrot():
...      return 43
)r(   refactor_docstringrD   r   )r   r(   docouts       r   test_refactor_docstringz+TestRefactoringTool.test_refactor_docstring5  sc    WWY ##C2c" ##C2C%r   c                    ddl m} | j                  dg      }| j                  t	        |j
                        d       | j                  dg      }|j
                  D ]  }t        ||      s y  | j                  d       y )Nr   )FixExplicitzmyfixes.fix_explicitr   )r'   zexplicit fixer not loaded)myfixes.fix_explicitr  r(   rD   len
post_orderr   r   )r   r  r(   fixs       r   test_explicitz!TestRefactoringTool.test_explicitF  su    4WW34W5R]]+Q/WW56W7== 	3C#{+	3 II12r   )!r\   r]   r^   r   r   r#   r   r(   r2   r6   r;   rJ   rX   rv   r   r   r   r   _2TO3_FIXERSr   r   r   r   r   r   r   r   r   r   r   r  r	  r  r_   r   r   r   r      s    #M
 o C>
=
2G+*Z/*
CN
+,( 8D'+D.2& /; *@P(@(/:
!)&"3r   r   )__doc__r   r   r   r   r   r   r   unittestlib2to3r   r   r   lib2to3.pgen2r   r   r   dirname__file__r   r   r   rC   r   r   r  TestCaser   r_   r   r   <module>r     s     	  	 	    0 0  RWW__X6?GGLL1	 	 6h66yAOHHLLN/x//@t3(++ t3	 HHLLNs    C4 4D