
mYG.                 @   sN  d  d l  m 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
 Z
 d  d l m Z m Z m Z d  d l 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  z e j d	  Z Wd e j j   Xe j d
  Z Gd d   d e	 j  Z d S)    )with_statementN)refactorpygram
fixer_base)token   )supportdatafixersmyfixeszlib2to3.fixesc               @   s9  e  Z d  Z d d   Z d d   Z d d   Z d e d 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 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/ d0   Z d S)1TestRefactoringToolc             C   s   t  j j t  d  S)N)syspathappend	FIXER_DIR)self r   N/build/mts/release/bora-5583004/publish/lin64/lib64/python3.5/test_refactor.pysetUp$   s    zTestRefactoringTool.setUpc             C   s   t  j j   d  S)N)r   r   pop)r   r   r   r   tearDown'   s    zTestRefactoringTool.tearDownc             C   sG   x@ t  | |  D]/ \ } } t | |  s |  j d | |  q Wd  S)Nz%s are not instances of %s)zip
isinstancefail)r   Z	instancesclassesinstclsr   r   r   check_instances*   s    z#TestRefactoringTool.check_instancesNc             C   s   t  j | | |  S)N)r   RefactoringTool)r   optionsr
   explicitr   r   r   rt/   s    zTestRefactoringTool.rtc             C   sH   |  j  d d i  } |  j | j t j  |  j | j j t j  d  S)Nprint_functionT)r!   ZassertIsZgrammarr   Z!python_grammar_no_print_statementZdriver)r   r!   r   r   r   test_print_function_option2   s    z.TestRefactoringTool.test_print_function_optionc             C   sE   |  j    } |  j | j  |  j  d d i  } |  j | j  d  S)Nwrite_unchanged_filesT)r!   ZassertFalser$   
assertTrue)r   r!   r   r   r   !test_write_unchanged_files_option8   s    z5TestRefactoringTool.test_write_unchanged_files_optionc             C   s   d d d d d g } t  j d  } t  j d d  } t  j d  } |  j | d d	   | D  |  j | |  |  j | d
 d	   | D  d  S)Nr    firstZlastZparrotZpreorderr   Fc             S   s   g  |  ] } d  |  q S)Zfix_r   ).0namer   r   r   
<listcomp>C   s   	 zBTestRefactoringTool.test_fixer_loading_helpers.<locals>.<listcomp>c             S   s   g  |  ] } d  |  q S)zmyfixes.fix_r   )r(   r)   r   r   r   r*   F   s   	 )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 } |   } |  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$ } x$ | D] } |  j | |  |  q[Wd } |  j | |  | d%   d } |  j | |  |  d } |  j | |  | d&   d } |  j | |  | d'   d } |  j | |  |  d  S)(N z%from __future__ import print_functionr"   z!from __future__ import generators
generatorsz*from __future__ import generators, featurefeaturez1from __future__ import generators, print_functionz1from __future__ import print_function, generatorsz(from __future__ import (print_function,)z3from __future__ import (generators, print_function)z2from __future__ import (generators, nested_scopes)nested_scopeszGfrom __future__ import generators
from __future__ import print_functionfromfrom 4from xfrom x 5	from x imfrom x import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)print_function)r0   )r0   r1   )r0   r:   )r:   r0   )r:   )r0   r:   )r0   r2   )r0   r:   )zfromr4   r5   r6   r7   r8   r9   )r:   )r:   )r:   )r   Z_detect_future_features	frozensetr,   )r   runfsemptyZinpinvalidr   r   r   test_detect_future_featuresH   sR    		      z/TestRefactoringTool.test_detect_future_featuresc             C   s  Gd d   d t  j  } Gd d   d t  j  } Gd d   d t  j  } | i  g   } | i  g   } | i  g   } t j | | | g  } | j t j j  } |  j | | | g  | j t	 j
  }	 |  j |	 | | g  x' | j   D] }
 |  j |
 | g  q Wd  S)Nc               @   s   e  Z d  Z d S)z;TestRefactoringTool.test_get_headnode_dict.<locals>.NoneFixN)__name__
__module____qualname__r   r   r   r   NoneFixv   s   rD   c               @   s   e  Z d  Z d Z d S)z@TestRefactoringTool.test_get_headnode_dict.<locals>.FileInputFixzfile_input< any * >N)rA   rB   rC   PATTERNr   r   r   r   FileInputFixy   s   rF   c               @   s   e  Z d  Z d Z d S)z=TestRefactoringTool.test_get_headnode_dict.<locals>.SimpleFixz'name'N)rA   rB   rC   rE   r   r   r   r   	SimpleFix|   s   rG   )r   ZBaseFixr   Z_get_headnode_dictr   r   Zpython_symbolsZ
file_inputr,   r   NAMEvalues)r   rD   rF   rG   Zno_headZ	with_headZsimpledZ	top_fixesZ
name_fixesfixesr   r   r   test_get_headnode_dictu   s    z*TestRefactoringTool.test_get_headnode_dictc             C   s   d d l  m } d d l m } d d l m } d d l m } |  j   } | j	   \ } } |  j
 | | g  |  j
 | | | | g  d  S)Nr   )FixFirst)FixLast)	FixParrot)FixPreorder)Zmyfixes.fix_firstrM   Zmyfixes.fix_lastrN   Zmyfixes.fix_parrotrO   Zmyfixes.fix_preorderrP   r!   Z
get_fixersr   )r   rM   rN   rO   rP   r!   preZpostr   r   r   test_fixer_loading   s    z&TestRefactoringTool.test_fixer_loadingc             C   s^   |  j  t |  j d d g |  j  t j |  j d d g |  j  t j |  j d d g d  S)Nr
   Znot_hereZno_fixer_clsZ	bad_order)ZassertRaisesImportErrorr!   r   Z
FixerError)r   r   r   r   test_naughty_fixers   s    z'TestRefactoringTool.test_naughty_fixersc             C   sl   |  j    } d } | j | d  } |  j t |  |  d } | j | d  } |  j t |  |  d  S)Nzdef parrot(): pass

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

)r!   Zrefactor_stringassertNotEqualstrr,   )r   r!   inputtreer   r   r   test_refactor_string   s    z(TestRefactoringTool.test_refactor_stringc                s   G  f d d   d t  j  } g    | t  } t j } t j d  t _ z | j   Wd  | t _ Xd d d d g } |  j   |  d  S)Nc                   s"   e  Z d  Z   f d d   Z d S)z5TestRefactoringTool.test_refactor_stdin.<locals>.MyRTc                s     j  | | | | g  d  S)N)extend)r   Zold_textZnew_textfilenameZequal)resultsr   r   print_output   s    zBTestRefactoringTool.test_refactor_stdin.<locals>.MyRT.print_outputN)rA   rB   rC   r]   r   )r\   r   r   MyRT   s   r^   zdef parrot(): pass

zdef cheese(): pass

z<stdin>F)	r   r   _DEFAULT_FIXERSr   stdinioStringIOZrefactor_stdinr,   )r   r^   r!   saveexpectedr   )r\   r   test_refactor_stdin   s    	
z'TestRefactoringTool.test_refactor_stdinTc                s   t  j d d  } |  j t j |  t j   |  t j j | t j j	       t j
   d    f d d   } |   } |  j d | d |  }	 | r | |	 _ |	 j    |  j | |    | s d  S|	 j   d  |   }
 |  j | |
  |
 S)	Nprefixz2to3-test_refactori  c           	      s'   t    d   }  |  j   SWd  QRXd  S)Nrb)openread)fp)	test_filer   r   	read_file   s    z=TestRefactoringTool.check_file_refactoring.<locals>.read_filer
   r   T)tempfilemkdtempZ
addCleanupshutilrmtreecopyosr   joinbasenamechmodr!   Z	log_debugrefactor_filer,   rU   )r   rk   r
   r   mock_log_debugactually_writeZtmpdirrl   Zold_contentsr!   Znew_contentsr   )rk   r   check_file_refactoring   s$    !			z*TestRefactoringTool.check_file_refactoringc             C   s)   t  j j t d  } |  j | t  d  S)Nzparrot_example.py)rr   r   rs   r   ry   r_   )r   rk   r   r   r   test_refactor_file   s    z&TestRefactoringTool.test_refactor_filec          
      s   t  j j t d  } g      f d d   } |  j | d f  d d d i d | d	 d
 d t  j t  j j |  f } xB   D]# } d | k r~ |  j | |  Pq~ W|  j d |   f  d  S)Nzparrot_example.pyc                s     j  |  |  d  S)N)r   )msgargs)debug_messagesr   r   recording_log_debug   s    zXTestRefactoringTool.test_refactor_file_write_unchanged_file.<locals>.recording_log_debugr
   r   r$   Trw   rx   FzNot writing changes to .*%s%szNot writing changesz%r not matched in %r)	rr   r   rs   r   ry   seprt   ZassertRegexr   )r   rk   r~   Zmessage_regexmessager   )r}   r   'test_refactor_file_write_unchanged_file   s    z;TestRefactoringTool.test_refactor_file_write_unchanged_filec                sy     f d d   } | g  g   d d d d d d g } d g } | | |  d t  j j d	 d
  g } | | |  d  S)Nc                s    f d d   } t  j j } | t  j _  j   } g   t j d d    z` t j t j j	   d   x0 |  D]( } t
 t j j	   |  d  j   qt W| j    Wd  | t  j _ t j    X j    f d d   | D  d  S)	Nc                s     j  |  d  S)N)r   )r   fr|   )gotr   r   mock_refactor_file   s    zPTestRefactoringTool.test_refactor_dir.<locals>.check.<locals>.mock_refactor_filerf   z2to3-test_refactora_dirwbc                s%   g  |  ] } t  j j   |   q Sr   )rr   r   rs   )r(   r   )dirr   r   r*      s   	 zHTestRefactoringTool.test_refactor_dir.<locals>.check.<locals>.<listcomp>)r   r   rv   r!   rm   rn   rr   mkdirr   rs   rh   closeZrefactor_dirro   rp   r,   )Z	structurerd   r   Z	save_funcr!   fn)r   )r   r   r   check   s    &	z4TestRefactoringTool.test_refactor_dir.<locals>.checkZnothingzhi.pyz.dumbz	.after.pyz	notpy.npyZsappyr   zstuff.py)rr   r   rs   )r   r   rX   rd   r   )r   r   test_refactor_dir   s    		z%TestRefactoringTool.test_refactor_dirc             C   s&   t  j j t d  } |  j |  d  S)Nzdifferent_encoding.py)rr   r   rs   TEST_DATA_DIRry   )r   r   r   r   r   test_file_encoding  s    z&TestRefactoringTool.test_file_encodingc             C   s(   t  j j t d  } |  j |  } d  S)Nzfalse_encoding.py)rr   r   rs   r   ry   )r   r   r	   r   r   r   test_false_file_encoding  s    z,TestRefactoringTool.test_false_file_encodingc             C   sA   t  j j t d  } |  j |  } |  j | j t j   d  S)Nzbom.py)	rr   r   rs   r   ry   r%   
startswithcodecsBOM_UTF8)r   r   r	   r   r   r   test_bom  s    zTestRefactoringTool.test_bomc             C   s[   t  j } d t  _ z8 t  j j t d  } t j d  } |  j | |  Wd  | t  _ Xd  S)Nz
zcrlf.pyzlib2to3.fixes)rr   linesepr   rs   r   r   r+   ry   )r   Zold_sepr   rK   r   r   r   test_crlf_newlines  s    		z&TestRefactoringTool.test_crlf_newlinesc             C   s`   |  j    } d } | j | d  } |  j | |  d } | j | d  } |  j | |  d  S)Nz
>>> example()
42
z<test>z&
>>> def parrot():
...      return 43
)r!   Zrefactor_docstringr,   rU   )r   r!   docoutr   r   r   test_refactor_docstring%  s    z+TestRefactoringTool.test_refactor_docstringc             C   s   d d l  m } |  j d d g  } |  j t | j  d  |  j d d g  } x. | j D] } t | |  r] Pq] W|  j d  d  S)Nr   )FixExplicitr
   zmyfixes.fix_explicitr    zexplicit fixer not loaded)Zmyfixes.fix_explicitr   r!   r,   lenZ
post_orderr   r   )r   r   r!   Zfixr   r   r   test_explicit6  s    z!TestRefactoringTool.test_explicit)rA   rB   rC   r   r   r   r_   r!   r#   r&   r.   r@   rL   rR   rT   rY   re   _2TO3_FIXERSry   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   "   s2   
-
 
r   ) Z
__future__r   r   rr   r   operatorra   rm   ro   ZunittestwarningsZlib2to3r   r   r   Zlib2to3.pgen2r   r/   r   r   rs   dirname__file__r   r   r   r+   r_   r   r   ZTestCaser   r   r   r   r   <module>   s(   !