a  ze*1@sddlZddlZddlZddlZddlZddlZddlZddlZddlm Z m Z m Z ddl m Z ejejedZejedZejeze dZWejn ej0e dZGdd d ejZdS) N)refactorpygram fixer_base)tokendatafixersmyfixes lib2to3.fixesc@seZdZddZddZddZdedfdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZddZedddfdd Zd!d"Zd#d$Zefd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Zd9d:Z dS);TestRefactoringToolcCstjtdSN)syspathappend FIXER_DIRselfrO/opt/bitninja-python-dojo/embedded/lib/python3.9/lib2to3/tests/test_refactor.pysetUpszTestRefactoringTool.setUpcCstjdSr )r r poprrrrtearDown"szTestRefactoringTool.tearDowncCs2t||D]"\}}t||s |d||q dS)Nz%s are not instances of %s)zip isinstancefail)rZ instancesclassesinstclsrrrcheck_instances%s z#TestRefactoringTool.check_instancesNcCst|||Sr )rRefactoringTool)roptionsrexplicitrrrrt*szTestRefactoringTool.rtcCs4|ddi}|d|jj|d|jjjdS)Nprint_functionTprintr! assertNotInZgrammarkeywordsZdriverrr!rrrtest_print_function_option-sz.TestRefactoringTool.test_print_function_optioncCs4|ddi}|d|jj|d|jjjdS)NZ exec_functionTexecr$r'rrrtest_exec_function_option2sz-TestRefactoringTool.test_exec_function_optioncCs2|}||j|ddi}||jdS)Nwrite_unchanged_filesT)r!Z assertFalser+ assertTruer'rrr!test_write_unchanged_files_option7s z5TestRefactoringTool.test_write_unchanged_files_optioncCsdgd}td}tdd}td}||dd|D|||||dd|DdS)N)r firstZlastZparrotZpreorderrFcSsg|] }d|qS)Zfix_r.0namerrr BzBTestRefactoringTool.test_fixer_loading_helpers..cSsg|] }d|qS)z myfixes.fix_rr/rrrr2Er3)rZget_all_fix_namesget_fixers_from_package assertEqual)rcontentsZ non_prefixedZprefixedZ full_namesrrrtest_fixer_loading_helpers=s     z.TestRefactoringTool.test_fixer_loading_helperscCs~tj}t}|}||d|||d|d||d|d||d|dd}||||d d }||||d d }||||dd }||||d d}||||dd}||||d d}|D]}||||qd}||||dd}||||d}||||dd}||||dd}||||dS)Nz%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)rrunfsemptyinpinvalidrrrtest_detect_future_featuresGsL   z/TestRefactoringTool.test_detect_future_featuresc CsGdddtj}Gdddtj}Gdddtj}|ig}|ig}|ig}t|||g}|tjj}||||g|t j } || ||g| D]} || |gqdS)Nc@s eZdZdS)z;TestRefactoringTool.test_get_headnode_dict..NoneFixN)__name__ __module__ __qualname__rrrrNoneFixusrEc@seZdZdZdS)z@TestRefactoringTool.test_get_headnode_dict..FileInputFixzfile_input< any * >NrBrCrDZPATTERNrrrr FileInputFixxsrGc@seZdZdZdS)z=TestRefactoringTool.test_get_headnode_dict..SimpleFixz'name'NrFrrrr SimpleFix{srH) rZBaseFixrZ_get_headnode_dictrrZpython_symbolsZ file_inputr5rNAMEvalues) rrErGrHZno_headZ with_headsimpledZ top_fixesZ name_fixesfixesrrrtest_get_headnode_dictts     z*TestRefactoringTool.test_get_headnode_dictcCshddlm}ddlm}ddlm}ddlm}|}| \}}| ||g| ||||gdS)Nr)FixFirst)FixLast) FixParrot) FixPreorder) Zmyfixes.fix_firstrOZmyfixes.fix_lastrPZmyfixes.fix_parrotrQZmyfixes.fix_preorderrRr!Z get_fixersr)rrOrPrQrRr!preZpostrrrtest_fixer_loadings     z&TestRefactoringTool.test_fixer_loadingcCsD|jt|jdgd|jtj|jdgd|jtj|jdgddS)NZnot_hererZ no_fixer_clsZ bad_order)Z assertRaises ImportErrorr!rZ FixerErrorrrrrtest_naughty_fixerssz'TestRefactoringTool.test_naughty_fixerscCsL|}d}||d}|t||d}||d}|t||dS)Ndef parrot(): pass zdef f(): pass )r!Zrefactor_stringassertNotEqualstrr5)rr!inputtreerrrtest_refactor_strings  z(TestRefactoringTool.test_refactor_stringcshGfdddtj}g|t}tj}tdt_z|W|t_n|t_0gd}||dS)NcseZdZfddZdS)z5TestRefactoringTool.test_refactor_stdin..MyRTcs||||gdSr )extend)rZold_textZnew_textfilenameZequalresultsrr print_outputszBTestRefactoringTool.test_refactor_stdin..MyRT.print_outputN)rBrCrDrcrrarrMyRTsrdrX)rXzdef cheese(): pass zF) rr_DEFAULT_FIXERSr stdinioStringIOZrefactor_stdinr5)rrdr!saveexpectedrrartest_refactor_stdins  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_filerrmock_log_debugactually_write old_contentsr! new_contentsrrrcheck_file_refactorings      z*TestRefactoringTool.check_file_refactoringcCsLtjdd}|tj|t||tj|tj |}t |d|S)N2to3-test_refactorprefixi) tempfilemkdtempZ addCleanupshutilrmtreecopyosr joinbasenamechmod)rroZtmpdirrrrrls    z"TestRefactoringTool.init_test_filecCs6t|d}|WdS1s(0YdS)Nrb)openread)rrofprrrrms zTestRefactoringTool.read_filecCs>||}||}|j|d}||d||}||fS)NrUT)rlrmr!rn)rrorrrr!rsrrrrns      z!TestRefactoringTool.refactor_filecCstjtd}||tdS)Nparrot_example.py)r}r r~rrtre)rrorrrtest_refactor_filesz&TestRefactoringTool.test_refactor_filecstjtd}gfdd}|j|dddi|ddd ttjtj|}D]}d |vrV| ||qqV| d |fdS) Nrcs||dSr r)msgargsZdebug_messagesrrrecording_log_debugszXTestRefactoringTool.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~rrtreescapeseprZ assertRegexr)rrorZ message_regexmessagerrr'test_refactor_file_write_unchanged_files   z;TestRefactoringTool.test_refactor_file_write_unchanged_filecsNfdd}|gggd}dg}|||dtjddg}|||dS)Nc sfdd}tjj}|tj_}gtjddzVttj d|D]}t tj |d qN| W|tj_t n|tj_t 0fdd|DdS) Ncs|dSr r)rfr)gotrrmock_refactor_fileszPTestRefactoringTool.test_refactor_dir..check..mock_refactor_filerurva_dirwbcsg|]}tj|qSrr}r r~)r0r )dirrrr2 r3zHTestRefactoringTool.test_refactor_dir..check..)rrrnr!rxryr}mkdirr r~rcloseZ refactor_dirrzr{r5)Z structurerjrZ save_funcr!fnr)rrrchecks$     z4TestRefactoringTool.test_refactor_dir..check)Znothinghi.pyz.dumbz .after.pyz notpy.npyZsappyrrzstuff.pyr)rrr]rjrrrtest_refactor_dirs    z%TestRefactoringTool.test_refactor_dircCstjtd}||dS)Nzdifferent_encoding.pyr}r r~ TEST_DATA_DIRrt)rrrrrtest_file_encodingsz&TestRefactoringTool.test_file_encodingcCstjtd}||}dS)Nzfalse_encoding.pyrrrrrrrtest_false_file_encodingsz,TestRefactoringTool.test_false_file_encodingcCs.tjtd}||}||tjdS)Nzbom.py) r}r r~rrtr, startswithcodecsBOM_UTF8rrrrtest_boms zTestRefactoringTool.test_bomcCsHtj}dt_z.tjtd}td}|||W|t_n|t_0dS)Nz crlf.pyr )r}linesepr r~rrr4rt)rZold_seprrMrrrtest_crlf_newlines$s z&TestRefactoringTool.test_crlf_newlinescCsDtjtd}||\}}|d||d||d|dS)Nrs s )r}r r~rrnZassertInr%)rroldnewrrrtest_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!docoutrrrtest_refactor_docstring5s   z+TestRefactoringTool.test_refactor_docstringcCsbddlm}|jdgd}|t|jd|jdgd}|jD]}t||r@q^q@|ddS)Nr) FixExplicitzmyfixes.fix_explicitrU)r zexplicit fixer not loaded)Zmyfixes.fix_explicitrr!r5lenZ post_orderrr)rrr!Zfixrrr test_explicitFs   z!TestRefactoringTool.test_explicit)!rBrCrDrrrrer!r(r*r-r7rArNrTrWr^rk _2TO3_FIXERSrtrlrmrnrrrrrrrrrrrrrrr s> -      r )r r}rrgrrxrzZunittestZlib2to3rrrZ lib2to3.pgen2rr r~dirname__file__rrrr4rerrZTestCaser rrrrs"