ó
Š]c           @€  s`  d  Z  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 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 d e j  f d „  ƒ  YZ! d S(   s   
Unit tests for refactor.py.
iÿÿÿÿ(   t   with_statementN(   t   refactort   pygramt
   fixer_base(   t   tokeni   (   t   supportt   datat   fixerst   myfixess   lib2to3.fixest   TestRefactoringToolc           B€  sì   e  Z d  „  Z d „  Z d „  Z d e d d „ Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z e d d e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C€  s   t  j j t ƒ d  S(   N(   t   syst   patht   appendt	   FIXER_DIR(   t   self(    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   setUp%   s    c         C€  s   t  j j ƒ  d  S(   N(   R
   R   t   pop(   R   (    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   tearDown(   s    c         C€  sJ   xC t  | | ƒ D]2 \ } } t | | ƒ s |  j d | | ƒ q q Wd  S(   Ns   %s are not instances of %s(   t   zipt
   isinstancet   fail(   R   t	   instancest   classest   instt   cls(    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   check_instances+   s    c         C€  s   t  j | | | ƒ S(   N(   R   t   RefactoringTool(   R   t   optionsR   t   explicit(    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   rt0   s    c         C€  sI   |  j  i t d 6ƒ } |  j | j t j ƒ |  j | j j t j ƒ d  S(   Nt   print_function(   R   t   Truet   assertIst   grammarR   t!   python_grammar_no_print_statementt   driver(   R   R   (    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   test_print_function_option3   s    c         C€  sF   |  j  ƒ  } |  j | j ƒ |  j  i t d 6ƒ } |  j | j ƒ d  S(   Nt   write_unchanged_files(   R   t   assertFalseR%   R   t
   assertTrue(   R   R   (    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt!   test_write_unchanged_files_option9   s    c         C€  s§   d d d d d g } t  j d ƒ } t  j d t ƒ } t  j d ƒ } |  j | g  | D] } d | ^ qU ƒ |  j | | ƒ |  j | g  | D] } d | ^ qŒ ƒ d  S(	   NR   t   firstt   lastt   parrott   preorderR   t   fix_s   myfixes.fix_(   R   t   get_all_fix_namest   Falset   get_fixers_from_packaget   assertEqual(   R   t   contentst   non_prefixedt   prefixedt
   full_namest   name(    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   test_fixer_loading_helpers?   s    '	c         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((   Nt    s%   from __future__ import print_functionR   s!   from __future__ import generatorst
   generatorss*   from __future__ import generators, featuret   features1   from __future__ import generators, print_functions1   from __future__ import print_function, generatorss(   from __future__ import (print_function,)s3   from __future__ import (generators, print_function)s2   from __future__ import (generators, nested_scopes)t   nested_scopessG   from __future__ import generators
from __future__ import print_functiont   froms   from 4s   from xs   from x 5s	   from x ims   from x imports   from x import 4s1   'docstring'
from __future__ import print_functions9   'docstring'
'somng'
from __future__ import print_functions/   # comment
from __future__ import print_functions5   # comment
'doc'
from __future__ import print_functions3   class x: pass
from __future__ import print_function(   R   (   R9   (   R9   R:   (   R9   R   (   R   R9   (   R   (   R9   R   (   R9   R;   (   R9   R   (   R<   s   from 4s   from xs   from x 5s	   from x ims   from x imports   from x import 4(   R   (   R   (   R   (   R   t   _detect_future_featurest	   frozensetR1   (   R   t   runt   fst   emptyt   inpt   invalid(    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   test_detect_future_featuresI   sR    		      c         C€  s  d t  j f d „  ƒ  Y} d t  j f d „  ƒ  Y} d t  j f d „  ƒ  Y} | 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(   Nt   NoneFixc           B€  s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyRE   w   s   t   FileInputFixc           B€  s   e  Z d  Z RS(   s   file_input< any * >(   RF   RG   t   PATTERN(    (    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyRH   z   s   t	   SimpleFixc           B€  s   e  Z d  Z RS(   s   'name'(   RF   RG   RI   (    (    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyRJ   }   s   (   R   t   BaseFixR   t   _get_headnode_dictR   R   t   python_symbolst
   file_inputR1   R   t   NAMEt
   itervalues(   R   RE   RH   RJ   t   no_headt	   with_headt   simplet   dt	   top_fixest
   name_fixest   fixes(    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   test_get_headnode_dictv   s    c         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(   Niÿÿÿÿ(   t   FixFirst(   t   FixLast(   t	   FixParrot(   t   FixPreorder(   t   myfixes.fix_firstRY   t   myfixes.fix_lastRZ   t   myfixes.fix_parrotR[   t   myfixes.fix_preorderR\   R   t
   get_fixersR   (   R   RY   RZ   R[   R\   R   t   pret   post(    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   test_fixer_loading‹   s    c         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   t   not_heret   no_fixer_clst	   bad_order(   t   assertRaisest   ImportErrorR   R   t
   FixerError(   R   (    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   test_naughty_fixers—   s    c         C€  sl   |  j  ƒ  } d } | j | d ƒ } |  j t | ƒ | ƒ d } | j | d ƒ } |  j t | ƒ | ƒ d  S(   Ns   def parrot(): pass

s   <test>s   def f(): pass

(   R   t   refactor_stringt   assertNotEqualt   strR1   (   R   R   t   inputt   tree(    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   test_refactor_stringœ   s    c         €  s   d t  j f ‡  f d †  ƒ  Y} g  ‰  | t ƒ } t j } t j d ƒ t _ z | j ƒ  Wd  | t _ Xd d d t g } |  j ˆ  | ƒ d  S(   Nt   MyRTc           €  s   e  Z ‡  f d  †  Z RS(   c         €  s   ˆ  j  | | | | g ƒ d  S(   N(   t   extend(   R   t   old_textt   new_textt   filenamet   equal(   t   results(    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   print_outputª   s    (   RF   RG   Ry   (    (   Rx   (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyRr   ¨   s   s   def parrot(): pass

s   def cheese(): pass

s   <stdin>(	   R   R   t   _DEFAULT_FIXERSR
   t   stdint   StringIOt   refactor_stdinR/   R1   (   R   Rr   R   t   savet   expected(    (   Rx   s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   test_refactor_stdin¦   s    	
c         €  sÿ   t  j d d ƒ } |  j t j | ƒ t j ˆ  | ƒ t j j | t j j	 ˆ  ƒ ƒ ‰  t j
 ˆ  d ƒ ‡  f d †  } | ƒ  } |  j d | d | ƒ }	 | r¨ | |	 _ n  |	 j ˆ  ƒ |  j | | ƒ  ƒ | sÒ d  S|	 j ˆ  t ƒ | ƒ  }
 |  j | |
 ƒ |
 S(   Nt   prefixs   2to3-test_refactori¤  c          €  s&   t  ˆ  d ƒ  }  |  j ƒ  SWd  QXd  S(   Nt   rb(   t   opent   read(   t   fp(   t	   test_file(    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt	   read_fileÄ   s    R   R   (   t   tempfilet   mkdtempt
   addCleanupt   shutilt   rmtreet   copyt   osR   t   joint   basenamet   chmodR   t	   log_debugt   refactor_fileR1   R   Rm   (   R   R†   R   R   t   mock_log_debugt   actually_writet   tmpdirR‡   t   old_contentsR   t   new_contents(    (   R†   s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   check_file_refactoringº   s$    !		c         C€  s)   t  j j t d ƒ } |  j | t ƒ d  S(   Ns   parrot_example.py(   RŽ   R   R   R   R™   Rz   (   R   R†   (    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   test_refactor_file×   s    c      
   €  sÈ   t  j j t d ƒ } g  ‰  ‡  f d †  } |  j | d d d i t d 6d | d t ƒd t j t  j	 t  j j
 | ƒ ƒ } xE ˆ  D]& } d	 | k rƒ |  j | | ƒ Pqƒ qƒ W|  j d
 | ˆ  f ƒ d  S(   Ns   parrot_example.pyc         €  s   ˆ  j  |  | ƒ d  S(   N(   R   (   t   msgt   args(   t   debug_messages(    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   recording_log_debugÞ   s    R   R   R%   R”   R•   s   Not writing changes to .*%ss   Not writing changess   %r not matched in %r(    (   RŽ   R   R   R   R™   R   R/   t   ret   escapet   sepR   t   assertRegexpMatchesR   (   R   R†   Rž   t   message_regext   message(    (   R   s,   lib/python2.7/lib2to3/tests/test_refactor.pyt'   test_refactor_file_write_unchanged_fileÛ   s    #c         €  sv   ‡  f 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 †  } 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 ƒ  qq W| j | ƒ Wd  | t  j _ t j | ƒ Xˆ j ˆ  g  | D] } t j j	 | | ƒ ^ qØ ƒ d  S(   Nc         €  s   ˆ  j  | ƒ d  S(   N(   R   (   R   t   fRœ   (   t   got(    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   mock_refactor_fileñ   s    R   s   2to3-test_refactort   a_dirt   wb(   R   R   R“   R   Rˆ   R‰   RŽ   t   mkdirR   R   Rƒ   t   closet   refactor_dirR‹   RŒ   R1   (   t	   structureR   R¨   t	   save_funcR   t   dirt   fnR   (   R   (   R§   s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   checkð   s    &	t   nothings   hi.pys   .dumbs	   .after.pys	   notpy.npyt   sappyR©   s   stuff.py(   RŽ   R   R   (   R   R²   Rp   R   (    (   R   s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   test_refactor_dirï   s    		c         C€  s&   t  j j t d ƒ } |  j | ƒ d  S(   Ns   different_encoding.py(   RŽ   R   R   t   TEST_DATA_DIRR™   (   R   R±   (    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   test_file_encoding  s    c         C€  s(   t  j j t d ƒ } |  j | ƒ } d  S(   Ns   false_encoding.py(   RŽ   R   R   R¶   R™   (   R   R±   R   (    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   test_false_file_encoding  s    c         C€  sA   t  j j t d ƒ } |  j | ƒ } |  j | j t j ƒ ƒ d  S(   Ns   bom.py(	   RŽ   R   R   R¶   R™   R'   t
   startswitht   codecst   BOM_UTF8(   R   R±   R   (    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   test_bom  s    c         C€  s[   t  j } d t  _ z8 t  j j t d ƒ } t j d ƒ } |  j | | ƒ Wd  | t  _ Xd  S(   Ns   
s   crlf.pys   lib2to3.fixes(   RŽ   t   linesepR   R   R¶   R   R0   R™   (   R   t   old_sepR±   RW   (    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   test_crlf_newlines  s    		c         C€  s`   |  j  ƒ  } d } | j | d ƒ } |  j | | ƒ d } | j | d ƒ } |  j | | ƒ d  S(   Ns   
>>> example()
42
s   <test>s&   
>>> def parrot():
...      return 43
(   R   t   refactor_docstringR1   Rm   (   R   R   t   doct   out(    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   test_refactor_docstring&  s    c         C€  s‹   d d l  m } |  j d d g ƒ } |  j t | j ƒ d ƒ |  j d d g ƒ } x1 | j D] } t | | ƒ r] Pq] q] W|  j d ƒ d  S(   Niÿÿÿÿ(   t   FixExplicitR   s   myfixes.fix_expliciti    R   s   explicit fixer not loaded(   t   myfixes.fix_explicitRÄ   R   R1   t   lent
   post_orderR   R   (   R   RÄ   R   t   fix(    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   test_explicit7  s    N(   RF   RG   R   R   R   t   NoneRz   R   R$   R(   R7   RD   RX   Rd   Rk   Rq   R€   t   _2TO3_FIXERSR   R™   Rš   R¥   Rµ   R·   R¸   R¼   R¿   RÃ   RÉ   (    (    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyR	   #   s2   						
	-				
				 				
	("   t   __doc__t
   __future__R    R
   RŽ   Rº   t   operatorRŸ   R|   Rˆ   R‹   t   unittestt   warningst   lib2to3R   R   R   t   lib2to3.pgen2R   R8   R   R   R   t   dirnamet   __file__R¶   R   R   R0   Rz   R   RË   t   TestCaseR	   (    (    (    s,   lib/python2.7/lib2to3/tests/test_refactor.pyt   <module>   s,   !