a 9êdg*1ã@sÖddlZddlZddlZddlZddlZddlZddlZddlZddlm Z m Z m Z ddl m Z ej ej e¡d¡Zej ed¡Zej e¡ze  d¡ZWej ¡n ej ¡0e  d¡ZGdd „d ejƒZdS) éN)ÚrefactorÚpygramÚ fixer_base)ÚtokenÚdataÚfixersÚmyfixesú lib2to3.fixesc@seZdZdd„Zdd„Zdd„Zdedfdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zedddfdd „Zd!d"„Zd#d$„Zefd%d&„Zd'd(„Zd)d*„Zd+d,„Zd-d.„Zd/d0„Zd1d2„Zd3d4„Zd5d6„Zd7d8„Zd9d:„Z dS);ÚTestRefactoringToolcCstj t¡dS©N)ÚsysÚpathÚappendÚ FIXER_DIR©Úself©rú7/usr/local/lib/python3.9/lib2to3/tests/test_refactor.pyÚsetUpszTestRefactoringTool.setUpcCstj ¡dSr )r r ÚpoprrrrÚtearDown"szTestRefactoringTool.tearDowncCs2t||ƒD]"\}}t||ƒs | d||¡q dS)Nz%s are not instances of %s)ÚzipÚ isinstanceÚfail)rZ instancesÚclassesÚinstÚclsrrrÚcheck_instances%s z#TestRefactoringTool.check_instancesNcCst |||¡Sr )rÚRefactoringTool)rÚoptionsrÚexplicitrrrÚrt*szTestRefactoringTool.rtcCs4| ddi¡}| d|jj¡| d|jjj¡dS)NÚprint_functionTÚprint©r!Ú assertNotInZgrammarÚkeywordsZdriver©rr!rrrÚtest_print_function_option-sz.TestRefactoringTool.test_print_function_optioncCs4| ddi¡}| d|jj¡| d|jjj¡dS)NZ exec_functionTÚexecr$r'rrrÚtest_exec_function_option2sz-TestRefactoringTool.test_exec_function_optioncCs2| ¡}| |j¡| ddi¡}| |j¡dS)NÚwrite_unchanged_filesT)r!Z assertFalser+Ú assertTruer'rrrÚ!test_write_unchanged_files_option7s z5TestRefactoringTool.test_write_unchanged_files_optioncCsdgd¢}t d¡}t dd¡}t d¡}| |dd„|Dƒ¡| ||¡| |dd„|Dƒ¡dS)N)r ÚfirstZlastZparrotZpreorderrFcSsg|] }d|‘qS)Zfix_r©Ú.0ÚnamerrrÚ BózBTestRefactoringTool.test_fixer_loading_helpers..cSsg|] }d|‘qS)z myfixes.fix_rr/rrrr2Er3)rZget_all_fix_namesÚget_fixers_from_packageÚ assertEqual)rÚcontentsZ non_prefixedZprefixedZ full_namesrrrÚtest_fixer_loading_helpers=s     ÿz.TestRefactoringTool.test_fixer_loading_helperscCs~tj}t}|ƒ}| |dƒ|¡| |dƒ|dƒ¡| |dƒ|dƒ¡| |dƒ|dƒ¡d}| ||ƒ|d ƒ¡d }| ||ƒ|d ƒ¡d }| ||ƒ|dƒ¡d }| ||ƒ|d ƒ¡d}| ||ƒ|dƒ¡d}| ||ƒ|d ƒ¡d}|D]}| ||ƒ|¡qôd}| ||ƒ|dƒ¡d}| ||ƒ|¡d}| ||ƒ|dƒ¡d}| ||ƒ|dƒ¡d}| ||ƒ|¡dS)NÚz%from __future__ import print_function)r"z!from __future__ import generators)Ú generatorsz*from __future__ import generators, feature)r9Zfeaturez1from __future__ import generators, print_function)r9r"z1from __future__ import print_function, generators)r"r9z(from __future__ import (print_function,)z3from __future__ import (generators, print_function)z2from __future__ import (generators, nested_scopes))r9Z nested_scopeszGfrom __future__ import generators from __future__ import print_function)Úfromzfrom 4zfrom xzfrom x 5z from x imz from x importzfrom 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)rZ_detect_future_featuresÚ frozensetr5)rÚrunÚfsÚemptyÚinpÚinvalidrrrÚtest_detect_future_featuresGsL ÿ ÿ ÿz/TestRefactoringTool.test_detect_future_featuresc Cs¾Gdd„dtjƒ}Gdd„dtjƒ}Gdd„dtjƒ}|igƒ}|igƒ}|igƒ}t |||g¡}| tjj¡}| |||g¡| t j ¡} | | ||g¡|  ¡D]} | | |g¡q¦dS)Nc@s eZdZdS)z;TestRefactoringTool.test_get_headnode_dict..NoneFixN)Ú__name__Ú __module__Ú __qualname__rrrrÚNoneFixusrEc@seZdZdZdS)z@TestRefactoringTool.test_get_headnode_dict..FileInputFixzfile_input< any * >N©rBrCrDZPATTERNrrrrÚ FileInputFixxsrGc@seZdZdZdS)z=TestRefactoringTool.test_get_headnode_dict..SimpleFixz'name'NrFrrrrÚ SimpleFix{srH) rZBaseFixrZ_get_headnode_dictrrZpython_symbolsZ file_inputr5rÚNAMEÚvalues) rrErGrHZno_headZ with_headÚsimpleÚdZ top_fixesZ name_fixesÚfixesrrrÚtest_get_headnode_dictts     z*TestRefactoringTool.test_get_headnode_dictcCshddlm}ddlm}ddlm}ddlm}| ¡}|  ¡\}}|  ||g¡|  ||||g¡dS)Nr)ÚFixFirst)ÚFixLast)Ú FixParrot)Ú FixPreorder) Zmyfixes.fix_firstrOZmyfixes.fix_lastrPZmyfixes.fix_parrotrQZmyfixes.fix_preorderrRr!Z get_fixersr)rrOrPrQrRr!ÚpreZpostrrrÚtest_fixer_loading‰s     z&TestRefactoringTool.test_fixer_loadingcCsD|jt|jdgd|jtj|jdgd|jtj|jdgddS)NZnot_here©rZ no_fixer_clsZ bad_order)Z assertRaisesÚ ImportErrorr!rZ FixerErrorrrrrÚtest_naughty_fixers•sz'TestRefactoringTool.test_naughty_fixerscCsL| ¡}d}| |d¡}| t|ƒ|¡d}| |d¡}| t|ƒ|¡dS)Núdef parrot(): pass úzdef f(): pass )r!Zrefactor_stringÚassertNotEqualÚstrr5)rr!ÚinputÚtreerrrÚtest_refactor_stringšs  z(TestRefactoringTool.test_refactor_stringcshG‡fdd„dtjƒ}g‰|tƒ}tj}t d¡t_z| ¡W|t_n|t_0gd¢}| ˆ|¡dS)NcseZdZ‡fdd„ZdS)z5TestRefactoringTool.test_refactor_stdin..MyRTcsˆ ||||g¡dSr )Úextend)rZold_textZnew_textÚfilenameZequal©ÚresultsrrÚ print_output¨szBTestRefactoringTool.test_refactor_stdin..MyRT.print_outputN)rBrCrDrcrrarrÚMyRT¦srdrX)rXzdef cheese(): pass zF) rrÚ_DEFAULT_FIXERSr ÚstdinÚioÚStringIOZrefactor_stdinr5)rrdr!ÚsaveÚexpectedrrarÚtest_refactor_stdin¤s  z'TestRefactoringTool.test_refactor_stdinTc Csv| |¡}| |¡}|j||d}|r,||_| |¡| || |¡¡|sPdS| |d¡| |¡}| ||¡|S)N)rrT)Úinit_test_fileÚ read_filer!Z log_debugÚ refactor_filer5rZ) rÚ test_filerrÚmock_log_debugÚactually_writeÚ old_contentsr!Ú new_contentsrrrÚcheck_file_refactoring¸s      z*TestRefactoringTool.check_file_refactoringcCsLtjdd}| tj|¡t ||¡tj |tj  |¡¡}t  |d¡|S)Nú2to3-test_refactor©Úprefixi¤) ÚtempfileÚmkdtempZ addCleanupÚshutilÚrmtreeÚcopyÚosr ÚjoinÚbasenameÚchmod)rroZtmpdirrrrrlËs    z"TestRefactoringTool.init_test_filecCs6t|dƒ}| ¡WdƒS1s(0YdS)NÚrb)ÚopenÚread)rroÚfprrrrmÓs zTestRefactoringTool.read_filecCs>| |¡}| |¡}|j|d}| |d¡| |¡}||fS)NrUT)rlrmr!rn)rrorrrr!rsrrrrn×s      z!TestRefactoringTool.refactor_filecCstj td¡}| |t¡dS)Núparrot_example.py)r}r r~rrtre)rrorrrÚtest_refactor_fileßsz&TestRefactoringTool.test_refactor_filecsŠtj td¡}g‰‡fdd„}|j|dddi|ddd t tjtj |¡¡}ˆD]}d |vrV|  ||¡q†qV|  d |ˆf¡dS) Nr…csˆ ||¡dSr ©r)ÚmsgÚargs©Zdebug_messagesrrÚrecording_log_debugæszXTestRefactoringTool.test_refactor_file_write_unchanged_file..recording_log_debugrr+TF)rrrprqzNot writing changes to .*%szNot writing changesz%r not matched in %r) r}r r~rrtÚreÚescapeÚseprZ assertRegexr)rror‹Z message_regexÚmessagerrŠrÚ'test_refactor_file_write_unchanged_fileãs  ýÿ z;TestRefactoringTool.test_refactor_file_write_unchanged_filecsN‡fdd„}|ggƒgd¢}dg}|||ƒdtj dd¡g}|||ƒdS)Nc s¾‡fdd„}tjj}|tj_ˆ ¡}g‰tjdd‰zVt tj  ˆd¡¡|D]}t tj  ˆ|¡dƒ  ¡qN|  ˆ¡W|tj_t  ˆ¡n|tj_t  ˆ¡0ˆ ˆ‡fdd„|Dƒ¡dS) Ncsˆ |¡dSr r‡)rÚfr‰)ÚgotrrÚmock_refactor_fileùszPTestRefactoringTool.test_refactor_dir..check..mock_refactor_filerurvÚa_dirÚwbcsg|]}tj ˆ|¡‘qSr©r}r r~)r0r )Údirrrr2 r3zHTestRefactoringTool.test_refactor_dir..check..)rrrnr!rxryr}Úmkdirr r~r‚ÚcloseZ refactor_dirrzr{r5)Z structurerjr“Z save_funcr!Úfnr)r—r’rÚcheckøs$    ÿ ÿz4TestRefactoringTool.test_refactor_dir..check)Znothingúhi.pyz.dumbz .after.pyz notpy.npyZsappyrœr”zstuff.pyr–)rr›r]rjrrrÚtest_refactor_dir÷s    ÿz%TestRefactoringTool.test_refactor_dircCstj td¡}| |¡dS)Nzdifferent_encoding.py©r}r r~Ú TEST_DATA_DIRrt)rršrrrÚtest_file_encodingsz&TestRefactoringTool.test_file_encodingcCstj td¡}| |¡}dS)Nzfalse_encoding.pyrž©rršrrrrÚtest_false_file_encodingsz,TestRefactoringTool.test_false_file_encodingcCs.tj td¡}| |¡}| | tj¡¡dS)Nzbom.py) r}r r~rŸrtr,Ú startswithÚcodecsÚBOM_UTF8r¡rrrÚtest_boms zTestRefactoringTool.test_bomcCsHtj}dt_z.tj td¡}t d¡}| ||¡W|t_n|t_0dS)Nz úcrlf.pyr )r}Úlinesepr r~rŸrr4rt)rZold_sepršrMrrrÚtest_crlf_newlines$s z&TestRefactoringTool.test_crlf_newlinescCsDtj td¡}| |¡\}}| d|¡| d|¡| d|¡dS)Nr§s s )r}r r~rŸrnZassertInr%)rršÚoldÚnewrrrÚtest_crlf_unchanged.s   z'TestRefactoringTool.test_crlf_unchangedcCsD| ¡}d}| |d¡}| ||¡d}| |d¡}| ||¡dS)Nz >>> example() 42 rYz& >>> def parrot(): ... return 43 )r!Zrefactor_docstringr5rZ)rr!ÚdocÚoutrrrÚtest_refactor_docstring5s   z+TestRefactoringTool.test_refactor_docstringcCsbddlm}|jdgd}| t|jƒd¡|jdgd}|jD]}t||ƒr@q^q@| d¡dS)Nr)Ú FixExplicitzmyfixes.fix_explicitrU)r zexplicit fixer not loaded)Zmyfixes.fix_explicitr°r!r5ÚlenZ post_orderrr)rr°r!ZfixrrrÚ test_explicitFs   z!TestRefactoringTool.test_explicit)!rBrCrDrrrrer!r(r*r-r7rArNrTrWr^rkÚ _2TO3_FIXERSrtrlrmrnr†rrr r¢r¦r©r¬r¯r²rrrrr s> -  þ    r )r r}r¤rgrŒrxrzZunittestZlib2to3rrrZ lib2to3.pgen2rr r~ÚdirnameÚ__file__rŸrrr4rerr³ZTestCaser rrrrÚs"