
|WC                 @   s  d  d l  Z d  d l j j Z d  d l m Z d  d l Z d  d l	 Z	 d  d l
 m Z e d k	 rt e j j j Z n e j j j d d d Z e j j j 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 d d   Z d d   Z d d   Z Gd  d!   d!  Z Gd" d#   d#  Z d$ d%   Z  d& d'   Z! d( d)   Z" d* d+   Z# d, d-   Z$ d. d/   Z% e j j j d0  d1 d2    Z& d3 d4   Z' d5 d6   Z( d7 d8   Z) d9 d:   Z* d; d<   Z+ d= d>   Z, d? d@   Z- dA dB   Z. dC dD   Z/ dE dF   Z0 dG dH   Z1 dI dJ   Z2 dK dL   Z3 dM dN   Z4 dO dP   Z5 dQ dR   Z6 dS dT   Z7 GdU dV   dV  Z8 GdW dX   dX  Z9 GdY dZ   dZ  Z: d[ d\   Z; d] d^   Z< d_ d`   Z= d S)a    N)Source)_astTruereasonzonly works with AST-compilezsys.platform.startswith('java')c           
   C   sX  t  d  }  t |   } d } | | k } | st j d | f d | | f  d d t j   k sr t j |   r t j |   n d d t j |  d d t j   k s t j t  r t j t  n d d	 t j |  i } d d | i } t t j	 |    d  } } } t  d  }  t |   } d } | | k } | st j d | f d | | f  d d t j   k st j |   rt j |   n d d t j |  d d t j   k st j t  rt j t  n d d	 t j |  i } d d | i } t t j	 |    d  } } } t  d d d }  t |   } d } | | k } | s.t j d | f d | | f  d d t j   k st j |   rt j |   n d d t j |  d d t j   k st j t  rt j t  n d d	 t j |  i } d d | i } t t j	 |    d  } } } t  d d d }  t |   } d } | | k } | sFt j d | f d | | f  d d t j   k st j |   rt j |   n d d t j |  d d t j   k st j t  rt j t  n d d	 t j |  i } d d | i } t t j	 |    d  } } } d  S) N3==0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)spy1xpy6py0strpy3 assert %(py8)spy8z   3z
        3
    rstripFz
3
    Tz
3)r   )r   r   )r   )r   r   )r   )r   r   )r   )r   r   )
r   r   
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanation)r
   @py_assert2@py_assert5@py_assert4@py_format7@py_format9 r    >/home/ronny/Projects/pytest-dev/py/testing/code/test_source.pytest_source_str_function   sL                                r"   c        
      C   sY  y t  Wn t k
 r  d  SYn Xt t  d   }  t |   } d } | | k } | s+t j d | f d | | f  d d t j   k s t j |   r t j	 |   n d d t j	 |  d d t j   k s t j t  r t j	 t  n d d	 t j	 |  i } d d | i } t
 t j |    d  } } } t j j t  d d  d d } t |  } t | t   } | sOd
 d d d t j   k st j |  rt j	 |  n d d d t j   k st j t  rt j	 t  n d d d t j   k st j t   r!t j	 t   n d d t j	 |  i }	 t
 t j |	    d  } d  S)N4r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r
   r   r   r   r   r   assert %(py8)sr   u   u"Ã¥"utf8modeevalz5assert %(py4)s
{%(py4)s = %(py0)s(%(py1)s, %(py2)s)
}val
isinstancepy2unicodepy4)r   )r$   r%   )r,   	NameErrorr   r   r   r   r   r   r   r   r   r   pycodecompiler(   r*   )
r
   r   r   r   r   r   cor)   @py_assert3@py_format5r    r    r!   test_unicode   s(    	       !    r5   c              C   s   t  j j t  }  t |   } | j } d } | |  } | s
d d d t j |  d d t j	   k sy t j
 |   r t j |   n d d t j |  d t j |  d	 d
 t j	   k s t j
 t  r t j t  n d
 d t j |  i } t t j |    d  } } } } d  S)Nzdef test_source_str_function():r   zjassert %(py9)s
{%(py9)s = %(py5)s
{%(py5)s = %(py3)s
{%(py3)s = %(py0)s(%(py1)s)
}.startswith
}(%(py7)s)
}py7r	   sourcepy5py9r   r   r   )r/   r0   r   r"   r   
startswithr   r   r   r   r   r   r   )r7   r   r   @py_assert6@py_assert8@py_format10r    r    r!   test_source_from_function)   s     	      r>   c              C   s  Gd d   d  }  t  j j |    j  } | j } d d g } | | k } | s t j d | f d | | f  d d t j   k s t j	 |  r t j
 |  n d d	 t j
 |  d
 t j
 |  i } d d | i } t t j |    d  } } } d  S)Nc               @   s   e  Z d  Z d d   Z d S)z*test_source_from_method.<locals>.TestClassc             S   s   d  S)Nr    )selfr    r    r!   test_method/   s    z6test_source_from_method.<locals>.TestClass.test_methodN)__name__
__module____qualname__r@   r    r    r    r!   	TestClass.   s   rD   zdef test_method(self):z    passr   -%(py2)s
{%(py2)s = %(py0)s.lines
} == %(py5)sr   r7   r8   r+   r   assert %(py7)sr6   )r   )rE   rF   )r/   r0   r   r@   linesr   r   r   r   r   r   r   r   )rD   r7   @py_assert1r   r3   @py_format6@py_format8r    r    r!   test_source_from_method-   s    	    y   rK   c              C   s   d d d g }  t  j j |   } | j } d d d g } | | k } | s t j d | f d | | f  d d	 t j   k s t j |  r t j	 |  n d	 d
 t j	 |  d t j	 |  i } d d | i } t
 t j |    d  } } } d  S)Nza 
zb
cza br   -%(py2)s
{%(py2)s = %(py0)s.lines
} == %(py5)sr   r7   r8   r+   r   assert %(py7)sr6   )r   )rN   rO   )r/   r0   r   rG   r   r   r   r   r   r   r   r   )rG   r7   rH   r   r3   rI   rJ   r    r    r!   test_source_from_lines5   s    	    y   rP   c              C   sN  d d   }  t  j j |  d d } t |  } | j } d } | |  } | sd d d t j |  d	 d
 t j   k s t j	 |  r t j |  n d
 d t j |  d t j |  d d t j   k s t j	 t  r t j t  n d d t j |  i } t
 t j |    d  } } } } t  j j |   } t |  } | j } d } | |  } | s8d d d t j |  d	 d
 t j   k st j	 |  rt j |  n d
 d t j |  d t j |  d d t j   k st j	 t  r
t j t  n d d t j |  i } t
 t j |    d  } } } } d  S)Nc               S   s   d  S)Nr    r    r    r    r!   f;   s    z*test_source_from_inner_function.<locals>.fdeindentFz    def f():r   zjassert %(py9)s
{%(py9)s = %(py5)s
{%(py5)s = %(py3)s
{%(py3)s = %(py0)s(%(py1)s)
}.startswith
}(%(py7)s)
}r6   r	   r7   r8   r9   r   r   r   zdef f():)r/   r0   r   r   r:   r   r   r   r   r   r   r   )rQ   r7   r   r   r;   r<   r=   r    r    r!   test_source_from_inner_function:   s&     	       	      rS   c           
   C   s(  t  d  }  |  j d d  }  t |   } d } | | k } | st j d | f d | | f  d d t j   k s t j |   r t j |   n d d	 t j |  d
 d t j   k s t j t  r t j t  n d d t j |  i } d d | i } t	 t j
 |    d  } } } d  S)Nzraise ValueErrorztry:zN        except ValueError:
            x = 42
        else:
            x = 23zHtry:
    raise ValueError
except ValueError:
    x = 42
else:
    x = 23r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   )r   )rT   rU   )r   	putaroundr   r   r   r   r   r   r   r   r   )r7   r   r   r   r   r   r    r    r!   test_source_putaround_simpleB   s    	       rW   c              C   sZ  t    }  |  j d  }  t |   } | j } |   } d } | | k } | s@t j d | f d | | f  d t j |  d d t j   k s t j	 |   r t j |   n d d t j |  d	 d
 t j   k s t j	 t  r t j t  n d
 d t j |  d t j |  i } d d | i } t
 t j |    d  } } } } } d  S)Nz#
        if 1:
            x=1
    zif 1:
    x=1r   c%(py7)s
{%(py7)s = %(py5)s
{%(py5)s = %(py3)s
{%(py3)s = %(py0)s(%(py1)s)
}.strip
}()
} == %(py10)sr6   r	   r7   r8   r   r   r   py10r   assert %(py12)spy12)r   )rX   rZ   )r   rV   r   stripr   r   r   r   r   r   r   r   )r7   r   r   r;   @py_assert9r<   @py_format11@py_format13r    r    r!   test_source_putaroundR   s    		 	 	       r`   c        	   
   C   s  t  d  }  t    } |  | k } | s t j d | f d |  | f  d d t j   k si t j |   rx t j |   n d d d t j   k s t j t   r t j t   n d d t j |  i } d d
 | i } t t j |    d  } } t	 |   } d } | | k } | st j d | f d | | f  d d t j   k s\t j |   rkt j |   n d d
 t j |  d d t j   k st j t	  rt j t	  n d d t j |  i } d d | i } t t j |    d  } } } |  j
 } |   } | |  k } | st j d | f d | |  f  d
 d t j   k sbt j |   rqt j |   n d d d t j   k st j |   rt j |   n d d t j |  d t j |  i } d d | i } t t j |    d  } } } d  S)Nr   r   )%(py0)s == %(py4)s
{%(py4)s = %(py2)s()
}r   r7   r+   r   r-   assert %(py6)sr   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r   r   assert %(py8)sr   D%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.strip
}()
} == %(py6)s)r   )ra   rb   )r   )rc   rd   )r   )re   rd   )r   r   r   r   r   r   r   r   r   r   r\   )	r7   r3   rH   r4   r   r   r   r   r   r    r    r!   test_source_stripsZ   s0    	      
       	 	      rf   c              C   s   t    }  d d d g |  _ |  j   } | j } d g } | | k } | s t j d | f d | | f  d d t j   k s t j |  r t j |  n d d t j |  d	 t j |  i } d d | i } t	 t j
 |    d  } } } d  S)Nr   z helloz  r   -%(py2)s
{%(py2)s = %(py0)s.lines
} == %(py5)sr   source2r8   r+   assert %(py7)sr6   )r   )rg   ri   )r   rG   r\   r   r   r   r   r   r   r   r   )r7   rh   rH   r   r3   rI   rJ   r    r    r!   test_source_strip_multiline`   s    		 	   y   rj   c        	      C   s  t  j j t t  j j d  }  |  j } | j } d } | | k } | s t j	 d | f d | | f  d t j
 |  d d t j   k s t j |   r t j
 |   n d d t j
 |  d	 t j
 |  i } d d | i } t t j |    d  } } } } |  j } | j } d } | | k } | st j	 d | f d | | f  d t j
 |  d d t j   k st j |   rt j
 |   n d d t j
 |  d	 t j
 |  i } d d | i } t t j |    d  } } } } |  j } | j } | j } |   } | st j d  d d t j
 |  d t j
 |  d d t j   k slt j |   r{t j
 |   n d d t j
 |  d	 t j
 |  i } t t j |    d  } } } } d  S)Nzxyz xyz   r   I%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.value
}.lineno
} == %(py7)sr6   r   exr+   r-   r   assert %(py9)sr9         inI%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.value
}.offset
} in %(py7)szx xzw
>assert %(py8)s
{%(py8)s = %(py6)s
{%(py6)s = %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.value
}.text
}.strip
}()
}r   r   )r   )rl   rn   )ro   rp   )rq   )rr   rn   )r/   testraisesSyntaxErrorr0   r1   valuelinenor   r   r   r   r   r   r   r   offsettextr\   _format_assertmsg)	rm   rH   r3   r;   r   rJ   r=   @py_assert7r   r    r    r!   !test_syntaxerror_rerepresentationf   s6    	 	       	 	       	 	 	 	    r|   c              C   s`  d }  t  |   } | j } |   } | s d d d t j |  d t j |  d d t j   k ss t j t   r t j t   n d d t j |   d	 t j |  i } t t j |    d  }  } } } d
 }  t  |   } | j } |   } | sd d d t j |  d t j |  d d t j   k sDt j t   rSt j t   n d d t j |   d	 t j |  i } t t j |    d  }  } } } d }  t  |   } | j } |   } | sad d d t j |  d t j |  d d t j   k st j t   r$t j t   n d d t j |   d	 t j |  i } t t j |    d  }  } } } d }  t  |   } | j } |   } | } | s9d d d t j |  d t j |  d d t j   k st j t   rt j t   n d d t j |   d	 t j |  i } t t j |    d  }  } } } } d }  t  |   } | j } |   } | } | sd d d t j |  d t j |  d d t j   k st j t   rt j t   n d d t j |   d	 t j |  i } t t j |    d  }  } } } } d } t	 |  } t  |  }	 |	 j }
 |
   } | } | sBd d d t j |	  d t j |  d d t j   k st j t	  rt j t	  n d d t j |  d t j |
  d d t j   k st j t   rt j t   n d d t j |  i } t t j |    d  } } }	 }
 } } d  S)Nhellor   zdassert %(py8)s
{%(py8)s = %(py6)s
{%(py6)s = %(py4)s
{%(py4)s = %(py0)s(%(py2)s)
}.isparseable
}()
}r   r   r   r   r+   r-   zif 1:
  passz 
if 1:
  passzif 1:
zhassert not %(py8)s
{%(py8)s = %(py6)s
{%(py6)s = %(py4)s
{%(py4)s = %(py0)s(%(py2)s)
}.isparseable
}()
}z 
if 1:
passr   zassert not %(py11)s
{%(py11)s = %(py9)s
{%(py9)s = %(py7)s
{%(py7)s = %(py0)s(%(py5)s
{%(py5)s = %(py1)s(%(py3)s)
})
}.isparseable
}()
}r6   py11r	   chrr8   r9   r   )
r   isparseabler   r   r   r   r   r   r   r   )rH   r3   r   r{   r   r]   r=   r   r   r;   r<   @py_assert10@py_assert12r_   r    r    r!   test_isparseablel   sh      	 	      	 	      	 	      	 	       	 	        	 	     r   c               @   sL   e  Z d  Z e d  Z d d   Z d d   Z d d   Z d d	   Z d
 S)TestAccesseszJ        def f(x):
            pass
        def g(x):
            pass
    c             C   s  |  j  d d  } | j } |   } | s d d d d t j   k sV t j |  re t j |  n d d t j |  d t j |  i } t t j |    d  } } | j	 } t
 |  } d } | | k } | st j d | f d | | f  d d t j   k st j |  r*t j |  n d d d t j   k sQt j t
  r`t j t
  n d d t j |  d t j |  d t j |  i }	 d d |	 i }
 t t j |
    d  } } } } t |  } d } | | k } | st j d | f d | | f  d d t j   k s4t j |  rCt j |  n d d t j |  d d t j   k syt j t  rt j t  n d d t j |  i } d d | i }	 t t j |	    d  } } } d  S)Nr      r   zFassert %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.isparseable
}()
}r   r
   r+   r-   r   K%(py5)s
{%(py5)s = %(py0)s(%(py3)s
{%(py3)s = %(py1)s.lines
})
} == %(py8)sr	   lenr   r8   r   assert %(py10)srY   zdef f(x):
    pass0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr   r   assert %(py8)s)r   )r   r   )r   )r   r   )r7   r   r   r   r   r   r   r   r   rG   r   r   r   )r?   r
   rH   r3   r4   r   r   r{   r;   r   r^   r   r   r    r    r!   test_getrange{   s0    	 	  b  
	               zTestAccesses.test_getrangec             C   s   |  j  d } d } | | k } | s t j d | f d | | f  d d t j   k sg t j |  rv t j |  n d d t j |  i } d d
 | i } t t j |    d  } } d  S)Nr   z	def f(x):r   %(py0)s == %(py3)sr   r
   r   r   assert %(py5)sr8   )r   )r   r   )	r7   r   r   r   r   r   r   r   r   )r?   r
   r   rH   @py_format4rI   r    r    r!   test_getline   s       j   zTestAccesses.test_getlinec             C   s&  |  j  } t |  } d } | | k } | st j d | f d | | f  d d t j   k so t j |   r~ t j |   n d d d t j   k s t j t  r t j t  n d d t j |  d	 t j |  d
 t j |  i } d d | i } t t j	 |    d  } } } } d  S)Nro   r   L%(py5)s
{%(py5)s = %(py0)s(%(py3)s
{%(py3)s = %(py1)s.source
})
} == %(py8)sr	   r?   r   r   r   r8   r   r   assert %(py10)srY   )r   )r   r   )
r7   r   r   r   r   r   r   r   r   r   )r?   r   r   r{   r;   r   r^   r    r    r!   test_len   s    	        zTestAccesses.test_lenc          
   C   s   d d   |  j  D } t |  } d } | | k } | st j d | f d | | f  d d t j   k s| t j |  r t j |  n d d t j |  d	 d
 t j   k s t j t  r t j t  n d
 d t j |  i } d d | i } t t j	 |    d  } } } d  S)Nc             S   s   g  |  ] } |  q Sr    r    ).0r
   r    r    r!   
<listcomp>   s   	 z*TestAccesses.test_iter.<locals>.<listcomp>ro   r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   lr   r   r   r   r   assert %(py8)sr   )r   )r   r   )
r7   r   r   r   r   r   r   r   r   r   )r?   r   r   r   r   r   r   r    r    r!   	test_iter   s           zTestAccesses.test_iterN)	rA   rB   rC   r   r7   r   r   r   r   r    r    r    r!   r   t   s   	r   c               @   s  e  Z d  Z e d  j   Z d d   Z d d   Z d d   Z d d	   Z	 d
 d   Z
 e d d    Z d d   Z d d   Z d d   Z e j j j d  d d    Z d d   Z e j j j d  d d    Z d d   Z d d   Z d d    Z d! S)"TestSourceParsingAndCompilingz^        def f(x):
            assert (x ==
                    3 +
                    4)
    c             C   s   t  j j d  } i  } t | |  | d } d } | | k } | s t j d | f d | | f  d t j |  d t j |  i } d d
 | i } t t j |    d  } } } d  S)Nzx=3r
      r   %(py1)s == %(py4)sr	   r-   r   assert %(py6)sr   )r   )r   r   )	r/   r0   r1   execr   r   r   r   r   )r?   r2   d@py_assert0r3   r   r4   r   r    r    r!   test_compile   s    
    C   z*TestSourceParsingAndCompiling.test_compilec          
   C   s8  t  j j d  } t |  t  j j |  } t |  } d } | | k } | s&t j d | f d | | f  d d t j	   k s t j
 |  r t j |  n d d t j |  d d t j	   k s t j
 t  r t j t  n d d	 t j |  i } d d | i } t t j |    d  } } } d  S)Nzx=3r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   )r   )r   r   )r/   r0   r1   r   r   r   r   r   r   r   r   r   r   r   )r?   r2   r7   r   r   r   r   r   r    r    r!   !test_compile_and_getsource_simple   s    
       z?TestSourceParsingAndCompiling.test_compile_and_getsource_simplec       
      C   s  d d   } | d  } | d  } t  j j j |  } d } | | k } | s t j d | f d | | f  d t j |  d	 d
 t j   k s t j	 |  r t j |  n d
 i } d d | i } t
 t j |    d  } } t  j j j |  }	 d } | |	 k } | st j d | f d | |	 f  d t j |  d	 d t j   k sht j	 |	  rwt j |	  n d i } d d | i } t
 t j |    d  } } d  S)Nc             S   s   t  j j |   S)N)r/   r0   r1   )r7   r    r    r!   	gensource   s    zaTestSourceParsingAndCompiling.test_compile_and_getsource_through_same_function.<locals>.gensourcez?
            def f():
                raise KeyError()
        zA
            def f():
                raise ValueError()
        KeyErrorrq   %(py1)s in %(py3)sr	   r   source1r   assert %(py5)sr8   
ValueErrorrh   )rq   )r   r   )rq   )r   r   )r/   stdinspect	getsourcer   r   r   r   r   r   r   r   )
r?   r   Zco1Zco2r   r   r   r   rI   rh   r    r    r!   0test_compile_and_getsource_through_same_function   s*    		   j   
   j   zNTestSourceParsingAndCompiling.test_compile_and_getsource_through_same_functionc             C   sl  t  |  j d d    } xLt d d  D];} |  j j |  } t  |  } | | k } | sZt j d | f d | | f  d d t j   k s t j |  r t j	 |  n d d d t j   k s t j t   r t j	 t   n d d	 t j	 |  d
 d t j   k st j |  r+t j	 |  n d i } d d | i } t
 t j |    d  } } q) Wd  S)Nrk   ro   r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py5)sr	   sr   r   r   r8   assr   assert %(py7)sr6   )r   )r   r   )r   r7   rangegetstatementr   r   r   r   r   r   r   r   )r?   r   ir   r   r   rI   rJ   r    r    r!   test_getstatement   s          z/TestSourceParsingAndCompiling.test_getstatementc          
   C   st  t  d  } | j d  } t |  } | | k } | s4t j d | f d | | f  d t j |  d d t j   k s t j |  r t j |  n d d d	 t j   k s t j |  r t j |  n d	 d
 d t j   k s t j t  rt j t  n d i } d d | i } t	 t j
 |    d  } } | j d  } t |  } | | k } | sft j d | f d | | f  d t j |  d d t j   k st j |  rt j |  n d d d	 t j   k st j |  rt j |  n d	 d
 d t j   k s(t j t  r7t j t  n d i } d d | i } t	 t j
 |    d  } } d  S)Nzhello('''
        ''')r   r   0%(py0)s == %(py5)s
{%(py5)s = %(py2)s(%(py3)s)
}r8   r   r   r   r7   r+   r   r   assert %(py7)sr6   rk   )r   )r   r   )r   )r   r   )r   r   r   r   r   r   r   r   r   r   r   )r?   r7   r   r   rH   rI   rJ   r    r    r!   $test_getstatementrange_triple_quoted   s$    	      
      zBTestSourceParsingAndCompiling.test_getstatementrange_triple_quotedc             C   s*  t  d  } t |  } d } | | k } | st j d | f d | | f  d d t j   k sr t j |  r t j |  n d d t j |  d d	 t j   k s t j t  r t j t  n d	 d
 t j |  i } d d | i } t t j	 |    d  } } } | j
 } d } | |  } d }	 | |	 k }
 |
 st j d |
 f d | |	 f  d t j |	  d t j |  d d t j   k st j |  rt j |  n d d t j |  d t j |  i } d d | i } t t j	 |    d  } } } }
 }	 | j
 } d } | |  } d  }	 | |	 k }
 |
 st j d! |
 f d" | |	 f  d t j |	  d t j |  d d t j   k st j |  rt j |  n d d t j |  d t j |  i } d# d | i } t t j	 |    d  } } } }
 }	 | j
 } d } | |  } d$ }	 | |	 k }
 |
 st j d% |
 f d& | |	 f  d t j |	  d t j |  d d t j   k st j |  rt j |  n d d t j |  d t j |  i } d' d | i } t t j	 |    d  } } } }
 }	 | j
 } d } | |  } d( }	 | |	 k }
 |
 st j d) |
 f d* | |	 f  d t j |	  d t j |  d d t j   k st j |  rt j |  n d d t j |  d t j |  i } d+ d | i } t t j	 |    d  } } } }
 }	 d  S),Nz            try:
                try:
                    raise ValueError
                except SomeThing:
                    pass
            finally:
                42
        rp   r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   r   r   W%(py6)s
{%(py6)s = %(py2)s
{%(py2)s = %(py0)s.getstatementrange
}(%(py4)s)
} == %(py9)sr9   r+   r-   assert %(py11)sr~   ro         )r   )r   r   )r   r   )r   )r   r   )r   ro   )r   )r   r   )ro   r   )r   )r   r   )r   rp   )r   )r   r   )r   r   r   r   r   r   r   r   r   r   getstatementrange)r?   r7   r   r   r   r   r   rH   r3   r<   r{   r=   @py_format12r    r    r!   (test_getstatementrange_within_constructs   sd    	       	         	         	         	         zFTestSourceParsingAndCompiling.test_getstatementrange_within_constructsc             C   s  t  d  } t |  } d } | | k } | st j d | f d | | f  d d t j   k sr t j |  r t j |  n d d t j |  d d	 t j   k s t j t  r t j t  n d	 d
 t j |  i } d d | i } t t j	 |    d  } } } | j
 } d } | |  } d }	 | |	 k }
 |
 st j d |
 f d | |	 f  d t j |	  d t j |  d d t j   k st j |  rt j |  n d d t j |  d t j |  i } d d | i } t t j	 |    d  } } } }
 }	 d  S)Nz            try:
                x = (
                   y +
                   z)
            except:
                pass
        r   r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   r   rk   ro   W%(py6)s
{%(py6)s = %(py2)s
{%(py2)s = %(py0)s.getstatementrange
}(%(py4)s)
} == %(py9)sr9   r+   r-   assert %(py11)sr~   )r   )r   r   )rk   ro   )r   )r   r   )r   r   r   r   r   r   r   r   r   r   r   )r?   r7   r   r   r   r   r   rH   r3   r<   r{   r=   r   r    r    r!   test_getstatementrange_bug   s(    	       	         z8TestSourceParsingAndCompiling.test_getstatementrange_bugc             C   s  t  d  } t |  } d } | | k } | st j d | f d | | f  d d t j   k sr t j |  r t j |  n d d t j |  d d	 t j   k s t j t  r t j t  n d	 d
 t j |  i } d d | i } t t j	 |    d  } } } | j
 } d } | |  } d }	 | |	 k }
 |
 st j d |
 f d | |	 f  d t j |	  d t j |  d d t j   k st j |  rt j |  n d d t j |  d t j |  i } d d | i } t t j	 |    d  } } } }
 }	 d  S)Nz            assert (
                33
                ==
                [
                  X(3,
                      b=1, c=2
                   ),
                ]
              )
        	   r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   r   r   W%(py6)s
{%(py6)s = %(py2)s
{%(py2)s = %(py0)s.getstatementrange
}(%(py4)s)
} == %(py9)sr9   r+   r-   assert %(py11)sr~   )r   )r   r   )r   r   )r   )r   r   )r   r   r   r   r   r   r   r   r   r   r   )r?   r7   r   r   r   r   r   rH   r3   r<   r{   r=   r   r    r    r!   test_getstatementrange_bug2   s(    
	       	         z9TestSourceParsingAndCompiling.test_getstatementrange_bug2c       	      C   s  t  d  } d } t | |  } | j } | j d d  } | | k } | sAt j d | f d | | f  d t j |  d d t j   k s t j t  r t j t  n d d	 t j |  d
 t j |  d d t j   k s t j |  rt j |  n d d t j |  i } d d | i } t	 t j
 |    d  } } } } } d } t | |  } | j } | j d d  } | | k } | st j d | f d | | f  d t j |  d d t j   k st j t  rt j t  n d d	 t j |  d
 t j |  d d t j   k s?t j |  rNt j |  n d d t j |  i } d d | i } t	 t j
 |    d  } } } } } d  S)Nz
            def test_some():
                for a in [a for a in
                    CAUSE_ERROR]: pass

            x = 3
        r   r   r   U%(py7)s
{%(py7)s = %(py5)s
{%(py5)s = %(py0)s(%(py2)s, %(py3)s)
}.lines
} == %(py10)sr6   r   r   r8   r+   r   r7   rY   r   assert %(py12)sr[   ro   )r   )r   r   )r   )r   r   )r   r   rG   r   r   r   r   r   r   r   r   )	r?   r7   rH   r   r;   r]   r<   r^   r_   r    r    r!   "test_getstatementrange_ast_issue58   s,    	  	         	       z@TestSourceParsingAndCompiling.test_getstatementrange_ast_issue58zsys.version_info < (2,6)c             C   s   t  d  } | j d  } d } | | k } | s t j d | f d | | f  d d t j   k su t j |  r t j |  n d d t j |  i } d d | i } t t j	 |    d  } } d  S)Nz-if xxx:
   from .collections import somethingrk   r   r   %(py0)s == %(py3)sr   rr   r   assert %(py5)sr8   )rk   r   )r   )r   r   )
r   r   r   r   r   r   r   r   r   r   )r?   r7   r   r   rH   r   rI   r    r    r!   (test_getstatementrange_out_of_bounds_py3   s       j   zFTestSourceParsingAndCompiling.test_getstatementrange_out_of_bounds_py3c                s/   t  d    t j j t   f d d    d  S)N:c                  s     j  d  S)Nr   )r   r    )r7   r    r!   <lambda>  s    z^TestSourceParsingAndCompiling.test_getstatementrange_with_syntaxerror_issue7.<locals>.<lambda>)r   r/   rs   rt   ru   )r?   r    )r7   r!   .test_getstatementrange_with_syntaxerror_issue7  s    zLTestSourceParsingAndCompiling.test_getstatementrange_with_syntaxerror_issue7c             C   sL  d d  l  } t d  } | j d | j  } | j } t | |  } | s.d d d d t j   k sv t j	 |  r t j
 |  n d d t j
 |  d	 d
 t j   k s t j	 t  r t j
 t  n d
 d d t j   k s t j	 |  r t j
 |  n d d t j
 |  i } t t j |    d  } } t | d d  d  S)Nr   zx = 4flagr   zQassert %(py6)s
{%(py6)s = %(py0)s(%(py1)s, %(py4)s
{%(py4)s = %(py2)s.Module
})
}r	   modr   r   r*   r+   astr-   z
<filename>r   )r   r   r1   PyCF_ONLY_ASTModuler*   r   r   r   r   r   r   r   )r?   r   r7   r   r3   r   r   r    r    r!   test_compile_to_ast  s    	     
z1TestSourceParsingAndCompiling.test_compile_to_astc             C   s  |  j  j   } t j j | t    t d  t j j t	 d  } | j
 d j } | j j j | j  } t |  } | j } |   } | j } d }	 | |	  }
 |
 sd d d t j |  d t j |	  d	 d
 t j   k s t j |  rt j |  n d
 d t j |  d t j |  d d t j   k sHt j t  rWt j t  n d d t j |
  d t j |  i } t	 t j |    d  } } } } }	 }
 d  S)Nrp   zf(6)rk   assertr   zassert %(py13)s
{%(py13)s = %(py9)s
{%(py9)s = %(py7)s
{%(py7)s = %(py5)s
{%(py5)s = %(py3)s
{%(py3)s = %(py0)s(%(py1)s)
}.strip
}()
}.startswith
}(%(py11)s)
}r6   r~   r	   stmtr8   r9   r   r   py13r   )r7   r1   r/   builtinexec_globalsrQ   rs   rt   r   	tracebackframer0   
fullsourcer   rw   r   r\   r:   r   r   r   r   r   r   )r?   r2   excinfor   r   r   r   r;   r<   r   r   @py_format14r    r    r!   test_compile_and_getsource  s     
 	 	 	      z8TestSourceParsingAndCompiling.test_compile_and_getsourcec             #   s       f d d   } t  j j  j  } | j   | j  xB t  j j t  j j j f D]% } x d D] } | | | f Vqh Wq[ Wd  S)Nc          
      s-  |   j  |  } | s3 d    d d f } n d |    d d f } | j } | j } | |  } | sd d d t j |  d d	 t j   k s t j |  r t j |  n d	 d
 d t j   k s t j |  r t j |  n d d t j |  i } t t j	 |    d  } } d  S)Nzcodegen %s:%d>r   rk   zcodegen %r %s:%d>r   zJassert %(py5)s
{%(py5)s = %(py2)s
{%(py2)s = %(py0)s.endswith
}(%(py3)s)
}r8   r   fnr   expectedr+   )
r7   co_filenameendswithr   r   r   r   r   r   r   )compnamer2   r   r   rH   r   rI   )mylinenomypathr?   r    r!   check  s    		     zNTestSourceParsingAndCompiling.test_compilefuncs_and_path_sanity.<locals>.checkr   my)r   Nr   )r/   r0   Code!test_compilefuncs_and_path_sanityfirstlinenopathr1   r   )r?   r   Zmycoder   r   r    )r   r   r?   r!   r     s    			"z?TestSourceParsingAndCompiling.test_compilefuncs_and_path_sanityc             C   s&   t  j j t t  j j d d d d  S)Nzlambda a,a: 0r'   r(   )r/   rs   rt   ru   r0   r1   )r?   r    r    r!   test_offsetless_synerr,  s    z4TestSourceParsingAndCompiling.test_offsetless_synerrN)rA   rB   rC   r   r\   r7   r   r   r   r   r   astonlyr   r   r   r   r/   rs   markskipifr   r   r   r   r   r   r    r    r    r!   r      s"   		!!
r   c           
   C   sE  Gd d   d  }  |  d d  } d d   | j  j D } t |  } d } | | k } | s3t j d | f d | | f  d
 d t j   k s t j |  r t j |  n d d t j |  d d t j   k s t j t  r t j t  n d d t j |  i } d d | i } t	 t j
 |    d  } } } d  S)Nc               @   s   e  Z d  Z d d   Z d S)z+test_getstartingblock_singleline.<locals>.Ac             W   s+   t  j d  } t j j |  j |  _ d  S)Nrk   )sys	_getframer/   r0   Frame	statementr7   )r?   argsr   r    r    r!   __init__1  s    z4test_getstartingblock_singleline.<locals>.A.__init__N)rA   rB   rC   r  r    r    r    r!   A0  s   r  r
   yc             S   s"   g  |  ] } | j    r |  q Sr    )r\   )r   r   r    r    r!   r   7  s   	 z4test_getstartingblock_singleline.<locals>.<listcomp>rk   r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r   r   r   r   r   r   assert %(py8)sr   )r   )r  r  )r7   rG   r   r   r   r   r   r   r   r   r   )r  r
   r   r   r   r   r   r   r    r    r!    test_getstartingblock_singleline/  s           r  c           
   C   sH  Gd d   d  }  |  d d d  } d d   | j  j D } t |  } d } | | k } | s6t j d | f d | | f  d d t j   k s t j |  r t j |  n d d t j |  d d t j   k s t j t  r t j t  n d d t j |  i } d d | i } t	 t j
 |    d  } } } d  S)Nc               @   s   e  Z d  Z d d   Z d S)z*test_getstartingblock_multiline.<locals>.Ac             W   s+   t  j d  } t j j |  j |  _ d  S)Nrk   )r   r   r/   r0   r   r   r7   )r?   r   r   r    r    r!   r  <  s    z3test_getstartingblock_multiline.<locals>.A.__init__N)rA   rB   rC   r  r    r    r    r!   r  ;  s   r  r
   r  zc             S   s"   g  |  ] } | j    r |  q Sr    )r\   )r   r   r    r    r!   r   E  s   	 z3test_getstartingblock_multiline.<locals>.<listcomp>ro   r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r   r   r   r   r   r   assert %(py8)sr   )r   )r  r	  )r7   rG   r   r   r   r   r   r   r   r   r   )r  r
   r   r   r   r   r   r   r    r    r!   test_getstartingblock_multiline:  s    	       r
  c        
      C   ss  d d   }  t  j j t d  } | j d j } t |  } | j } |   } d } | | k } | sYt j	 d | f d | | f  d t j
 |  d	 d
 t j   k s t j |  r t j
 |  n d
 d t j
 |  d d t j   k s t j t  rt j
 t  n d d t j
 |  d t j
 |  i } d d | i }	 t t j |	    d  } } } } } d  S)Nc               S   s   d  S)Nr    r    r    r    r!   rL   I  s    ztest_getline_finally.<locals>.cz
           teardown = None
           try:
                c(1)
           finally:
                if teardown:
                    teardown()
    rk   zc(1)r   c%(py7)s
{%(py7)s = %(py5)s
{%(py5)s = %(py3)s
{%(py3)s = %(py0)s(%(py1)s)
}.strip
}()
} == %(py10)sr6   r	   r7   r8   r   r   r   rY   r   assert %(py12)sr[   r   )r   )r  r  )r/   rs   rt   	TypeErrorr   r   r   r\   r   r   r   r   r   r   r   r   )
rL   r   r7   r   r   r;   r]   r<   r^   r_   r    r    r!   test_getline_finallyH  s    	 	 	       r  c              C   s  d }  t  j j |   } t  j j | t    t  j } | j } | t  } t |  } | j	 } |   } d } | | k }	 |	 rt
 j d f |	 f d f | | f  d t
 j |  d d t j   k p t
 j t  r t
 j t  n d d d	 t j   k pt
 j t   rt
 j t   n d	 d
 t
 j |  d d t j   k pRt
 j t  rdt
 j t  n d d t
 j |  d t
 j |  d t
 j |  d t
 j |  d t
 j |  i
 }
 d d d |
 i } t t
 j |    d  } } } } } } }	 } t  j } | j } | t  } t |  } | j	 } |   } d } | | k }	 |	 rt
 j d f |	 f d f | | f  d t
 j |  d d t j   k pt
 j t  rt
 j t  n d d d	 t j   k pt
 j t   rt
 j t   n d	 d
 t
 j |  d d t j   k p(t
 j t  r:t
 j t  n d d t
 j |  d t
 j |  d t
 j |  d t
 j |  d t
 j |  i
 }
 d d d |
 i } t t
 j |    d  } } } } } } }	 } d  S)NzJ
        def f():
            raise ValueError

        def g(): pass
    zdef f():
    raise ValueErrorr   z%(py14)s
{%(py14)s = %(py12)s
{%(py12)s = %(py10)s
{%(py10)s = %(py0)s(%(py8)s
{%(py8)s = %(py5)s
{%(py5)s = %(py3)s
{%(py3)s = %(py1)s.code
}.Source
}(%(py6)s)
})
}.strip
}()
} == %(py17)spy14r   rQ   r	   r/   r8   r   r   r[   r   py17rY   r   r   zassert %(py19)spy19zdef g(): passg)r/   r0   r1   r   r   r   r   rQ   r   r\   r   r   r   r   r   r   r   r   r  )r7   r2   r   r   r{   r]   @py_assert11@py_assert13@py_assert16@py_assert15@py_format18@py_format20r    r    r!   test_getfuncsource_dynamicU  s>    	 	   	 	     7   "	 	   	 	     7   r  c              C   s  d d   }  t  j } | j } | |   } t |  } | j } |   } d } | | k } | rt j d f | f d f | | f  d t j |  d d t j	   k p t j
 |   r t j |   n d d	 d
 t j	   k p t j
 t   r t j t   n d
 d t j |  d d t j	   k p0t j
 t  rBt j t  n d d t j |  d t j |  d t j |  d t j |  d t j |  i
 }	 d d d |	 i }
 t t j |
    d  } } } } } } } } d  S)Nc              S   s
   d }  d  S)Nzwhile True:
    pass
r    )rL   r    r    r!   rQ   c  s    z1test_getfuncsource_with_multine_string.<locals>.fz,def f():
    c = '''while True:
    pass
'''r   z%(py14)s
{%(py14)s = %(py12)s
{%(py12)s = %(py10)s
{%(py10)s = %(py0)s(%(py8)s
{%(py8)s = %(py5)s
{%(py5)s = %(py3)s
{%(py3)s = %(py1)s.code
}.Source
}(%(py6)s)
})
}.strip
}()
} == %(py17)sr  r   rQ   r	   r/   r8   r   r   r[   r   r  rY   r   r   zassert %(py19)sr  )r/   r0   r   r   r\   r   r   r   r   r   r   r   r   )rQ   r   r   r{   r]   r  r  r  r  r  r  r    r    r!   &test_getfuncsource_with_multine_stringb  s    	 	   	 	     7   r  c           
   C   s  d d l  m }  d d g } |  |  } d d g } | | k } | s t j d  | f d! | | f  d	 t j |  d
 d t j   k s t j |   r t j |   n d d t j |  d t j |  i } d" d | i } t t j	 |    d  } } } } d d   } d d  l
 } |  | j |  j    }	 d d d d g }
 |	 |
 k } | st j d# | f d$ |	 |
 f  d
 d t j   k st j |	  rt j |	  n d d t j |
  i } d% d | i } t t j	 |    d  } }
 d } |  | j    }	 d d d d d g }
 |	 |
 k } | st j d& | f d' |	 |
 f  d
 d t j   k svt j |	  rt j |	  n d d t j |
  i } d( d | i } t t j	 |    d  } }
 d  S))Nr   )rR   z	fooz	barfoobarr   0%(py4)s
{%(py4)s = %(py0)s(%(py2)s)
} == %(py7)sr6   r   rR   r+   r-   r   assert %(py9)sr9   c              S   s
   d }  d  S)Nzwhile True:
    pass
r    )rL   r    r    r!   rQ   n  s    ztest_deindent.<locals>.fzdef f():z    c = '''while True:z    passz'''%(py0)s == %(py3)srG   r   assert %(py5)sr8   z@
        def f():
            def g():
                pass
    z    def g():z        passz    )r   )r  r  )r   )r  r   )r   )r  r   )py._code.sourcerR   r   r   r   r   r   r   r   r   r   r   
splitlines)rR   rH   r3   r;   r   rJ   r=   rQ   r   rG   r   r   rI   r7   r    r    r!   test_deindentj  s:               j   
   j   r#  zIsys.version_info[:3] < (2,7,0) or ((3,0) <= sys.version_info[:2] < (3,2))c             C   sI  t  j j d  } |  j d  } | j |  t  j j |  j d  j   j  } t |  } | j } |   } t |  } | j } |   }	 | |	 k }
 |
 r't	 j
 d f |
 f d f | |	 f  d t	 j |  d t	 j |  d d t j   k pt	 j |  rt	 j |  n d d	 t	 j |  d
 d t j   k pIt	 j t  r[t	 j t  n d d d t j   k pt	 j t  rt	 j t  n d d t	 j |  d t	 j |	  d t	 j |  d d t j   k pt	 j |  rt	 j |  n d i
 } d d d | i } t t	 j |    d  } } } }
 } } }	 d  S)NzR
        class A(object):
            def method(self):
                x = 1
    za.pyr   z%(py7)s
{%(py7)s = %(py5)s
{%(py5)s = %(py3)s
{%(py3)s = %(py0)s(%(py1)s)
}.strip
}()
} == %(py16)s
{%(py16)s = %(py14)s
{%(py14)s = %(py12)s
{%(py12)s = %(py9)s(%(py10)s)
}.strip
}()
}r6   r  r	   r7   r8   r9   r   r   r[   py16r   rY   s2r   zassert %(py18)sZpy18)r/   r0   r   joinwritepyimportr  r   r\   r   r   r   r   r   r   r   r   )tmpdirr7   r   r%  r   r   r;   r  r  r  r<   @py_format17Z@py_format19r    r    r!   +test_source_of_class_at_eof_without_newline~  s$    		$ 	 	  	 	    ^   r+  c               C   s   d  S)Nr    r    r    r    r!   r
     s    r
   c              C   s   d d l  m }  d } |  t  } | | k } | s t j d | f d | | f  d d t j   k sv t j |  r t j |  n d d d	 t j   k s t j |  r t j |  n d	 i } d d | i } t	 t j
 |    d  } d  S)Nr   )r   zdef x():
    passr   %(py0)s == %(py2)sr   srcr+   r   r   assert %(py4)sr-   )r   )r,  r.  )r!  r   r
   r   r   r   r   r   r   r   r   )r   r   r-  rH   @py_format3r4   r    r    r!   test_getsource_fallback  s         r0  c              C   s  d d l  m }  d } t j j |  } |  |  } | | k } | s t j d | f d | | f  d d t j   k s t j	 |  r t j
 |  n d d d	 t j   k s t j	 |  r t j
 |  n d	 i } d d | i } t t j |    d  } d  S)Nr   )r   zdef x(): passr   %(py0)s == %(py2)sr   r-  r+   r   r   assert %(py4)sr-   )r   )r1  r2  )r!  r   r/   r0   r1   r   r   r   r   r   r   r   r   )r   r   r2   r-  rH   r/  r4   r    r    r!   test_idem_compile_and_getsource  s         r3  c        
   
   C   s  d d l  m }  |  t  \ } } d } t |  } | | k } | st j d | f d | | f  d t j |  d t j |  d d	 t j   k s t j	 t  r t j t  n d	 d
 d t j   k s t j	 |  r t j |  n d i } d d | i } t
 t j |    d  } } } | | } d } | | k } | st j d | f d | | f  d t j |  d
 t j |  i }	 d d |	 i } t
 t j |    d  } } } d  S)Nr   )
findsourceZtest_findsource_simplerq   0%(py1)s in %(py6)s
{%(py6)s = %(py3)s(%(py4)s)
}r   r	   r   r   r-   r-  r   assert %(py8)sr   z    def x():r   %(py1)s == %(py4)sassert %(py6)s)rq   )r5  r6  )r   )r7  r8  )r!  r4  r
   r   r   r   r   r   r   r   r   r   )
r4  r-  rw   r   r   r   r   r   r3   r4   r    r    r!   test_findsource_fallback  s$           
    C   r9  c           
   C   s  d d l  m }  t j j d  } |  |  \ } } d } t |  } | | k } | s,t j d | f d | | f  d t j |  d t j |  d	 d
 t	 j
   k s t j t  r t j t  n d
 d d t	 j
   k s t j |  r t j |  n d i } d d | i } t t j |    d  } } } i  }	 t | |	  |  |	 d  \ } } d } t |  } | | k } | s[t j d | f d | | f  d t j |  d t j |  d	 d
 t	 j
   k st j t  rt j t  n d
 d d t	 j
   k st j |  r,t j |  n d i } d d | i } t t j |    d  } } } | | } d }
 | |
 k } | st j d | f d | |
 f  d t j |  d t j |
  i } d d | i } t t j |    d  } } }
 d  S)Nr   )r4  z if 1:
    def x():
        pass
zif 1:rq   0%(py1)s in %(py6)s
{%(py6)s = %(py3)s(%(py4)s)
}r   r	   r   r   r-   r-  r   assert %(py8)sr   r
   z    def x():r   %(py1)s == %(py4)sassert %(py6)s)rq   )r:  r;  )rq   )r:  r;  )r   )r<  r=  )r!  r4  r/   r0   r1   r   r   r   r   r   r   r   r   r   r(   )r4  r2   r-  rw   r   r   r   r   r   r   r3   r4   r    r    r!   test_findsource  s>    		              
    C   r>  c              C   sr  d d l  m }  d d   } |  |  \ } } | j } d } | | k } | r t j d f | f d f | | f  d d	 t j   k p t j |  r t j |  n d	 d
 t j |  d t j |  i } d d d | i } t	 t j
 |    d  } } } t j } | j }	 |	 |  }
 |
 j } d } | | } | | k } | rt j d f | f d f | | f  d d t j   k pt j |  rt j |  n d d t j |  d t j |	  d d t j   k pt j |  rt j |  n d d d t j   k pt j t  r1t j t  n d d t j |  d t j |  d t j |
  i } d d d | i } t	 t j
 |    d  } } }	 }
 } } } Gd d   d t  } |  |  \ } } t j j j |  \ } } | j } d } | | k } | rt j d f | f d f | | f  d d	 t j   k pYt j |  rkt j |  n d	 d
 t j |  d t j |  i } d d d | i } t	 t j
 |    d  } } } | | k } | rt j d f | f d f | | f  d d t j   k p"t j |  r4t j |  n d d d t j   k pXt j |  rjt j |  n d i } d d d | i } t	 t j
 |    d  } d  } |  |  } d d f } | | k }	 |	 rt j d f |	 f d! f | | f  d t j |  d d" t j   k p)t j |   r;t j |   n d" d t j |  d t j |  i } d d# d | i } t	 t j
 |    d  } } }	 } Gd$ d%   d%  } d& | _ |  |  d } d } | }	 | |	 k } | r\t j d f | f d' f | |	 f  d( t j |  d t j |  i } d d d | i } t	 t j
 |    d  } } } }	 d  S))Nr   )getfslinenoc             S   s   d  S)Nr    )r
   r    r    r!   rQ     s    ztest_getfslineno.<locals>.fztest_source.pyr   z0%(py2)s
{%(py2)s = %(py0)s.basename
} == %(py5)sr   fspathr8   r+   r   zassert %(py7)sr6   rk   z%(py0)s == (%(py11)s
{%(py11)s = %(py9)s
{%(py9)s = %(py6)s
{%(py6)s = %(py4)s
{%(py4)s = %(py2)s.code
}.getrawcode
}(%(py7)s)
}.co_firstlineno
} - %(py13)s)rQ   r~   r   rw   r/   r-   r   r9   zassert %(py16)sr$  c               @   s   e  Z d  Z d S)ztest_getfslineno.<locals>.AN)rA   rB   rC   r    r    r    r!   r    s   r  z%(py0)s == %(py2)sA_linenozassert %(py4)sr   z0%(py4)s
{%(py4)s = %(py0)s(%(py2)s)
} == %(py7)sr?  zassert %(py9)sc               @   s   e  Z d  Z d S)ztest_getfslineno.<locals>.BN)rA   rB   rC   r    r    r    r!   B  s   rB  ZB2z%(py1)s == -%(py4)sr	   )py.coder?  basenamer   r   r   r   r   r   r   r   r/   r0   
getrawcodeco_firstlinenoobjectr   r   r4  rA   )r?  rQ   r@  rw   rH   r   r3   rI   rJ   r   r<   r   r   Z@py_assert14@py_format15r*  r  _rA  r/  r4   r;   r=   rB  r   r   r    r    r!   test_getfslineno  sz    	       	 	  	  
       	                    	     I   rJ  c        	         s  Gd d   d    t  j j t   f d d    Gd d   d  }  t  j j |     } d } | j } |   } t |  } | | k } | st j	 d | f d | | f  d
 t j
 |  d t j
 |  d d t j   k s t j |  r t j
 |  n d d d t j   k s&t j t  r5t j
 t  n d d t j
 |  d t j
 |  i } d d | i } t t j |    d  } } } } } Gd d   d t   t  j j t  f d d    d  S)Nc               @   s   e  Z d  Z d S)z1test_code_of_object_instance_with_call.<locals>.AN)rA   rB   rC   r    r    r    r!   r    s   r  c                  s   t  j j      S)N)r/   r0   r   r    )r  r    r!   r     s    z8test_code_of_object_instance_with_call.<locals>.<lambda>c               @   s   e  Z d  Z d d   Z d S)z8test_code_of_object_instance_with_call.<locals>.WithCallc             S   s   d  S)Nr    )r?   r    r    r!   __call__  s    zAtest_code_of_object_instance_with_call.<locals>.WithCall.__call__N)rA   rB   rC   rK  r    r    r    r!   WithCall  s   rL  passrq   e%(py1)s in %(py10)s
{%(py10)s = %(py3)s(%(py8)s
{%(py8)s = %(py6)s
{%(py6)s = %(py4)s.source
}()
})
}r   r	   r-   r0   r   r   rY   r   r   assert %(py12)sr[   c               @   s   e  Z d  Z d d   Z d S)z5test_code_of_object_instance_with_call.<locals>.Helloc             S   s   d  S)Nr    )r?   r    r    r!   rK    s    z>test_code_of_object_instance_with_call.<locals>.Hello.__call__N)rA   rB   rC   rK  r    r    r    r!   Hello  s   rP  c                  s   t  j j    S)N)r/   r0   r   r    )rP  r    r!   r     s    )rq   )rN  rO  )r/   rs   rt   r  r0   r   r7   r   r   r   r   r   r   r   r   r   rG  )	rL  r0   r   r   r{   r]   r   r^   r_   r    )r  rP  r!   &test_code_of_object_instance_with_call  s      	 	       rQ  c             C   sN   d d l  m } t j j | d d } | |  |  \ } } } | | |  S)Nr   )getstatementrange_astrR   F)r!  rR  r/   r0   r   )rw   r7   rR  r   startendr    r    r!   r     s    r   c           
   C   s  t  d d  }  t |   } d } | | k } | st j d | f d | | f  d d t j   k su t j |   r t j |   n d d t j |  d d	 t j   k s t j t  r t j t  n d	 d
 t j |  i } d d | i } t t j	 |    d  } } } d  S)Nr   zraise ValueErrorr   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   )r   )rU  rV  )
r   r   r   r   r   r   r   r   r   r   )r7   r   r   r   r   r   r    r    r!   test_oneline  s           rW  c        	      C   s   d d l  m }  t d d d g  } |  d |  \ } } } d } | | k } | s t j d | f d | | f  d
 d t j   k s t j |  r t j |  n d d t j |  i } d d | i } t	 t j
 |    d  } } d  S)Nr   )rR  zdef test_basic_complex():z    assert 1 == 2z## vim: filetype=pyopencl:fdm=markerrk   r   r   %(py0)s == %(py3)sr   rT  r   r   assert %(py5)sr8   )r   )rX  rY  )r!  rR  r   r   r   r   r   r   r   r   r   )	rR  r7   r   rS  rT  r   rH   r   rI   r    r    r!   "test_comment_and_no_newline_at_end  s       j   rZ  c           
   C   s  t  d d  }  t |   } d } | | k } | st j d | f d | | f  d d t j   k su t j |   r t j |   n d d t j |  d	 d
 t j   k s t j t  r t j t  n d
 d t j |  i } d d | i } t t j	 |    d  } } } d  S)Nr   zraise ValueError
#hellozraise ValueErrorr   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   )r   )r[  r\  )
r   r   r   r   r   r   r   r   r   r   )r7   r   r   r   r   r   r    r    r!   test_oneline_and_comment  s           r]  c              C   s  d }  xt  d d  D]} t | |   } t |  } d } | | k } | rt j d f | f d f | | f  d t j |  d d	 t j   k p t j t  r t j t  n d	 d
 t j |  d d t j   k p t j |  r t j |  n d d d t j   k pt j t  r1t j t  n d d d t j   k pUt j |   rgt j |   n d d t j |  i } d d d | i } t	 t j
 |    d  } } } } q Wxt  d d  D]} t | |   } t |  } d } | | k } | rbt j d f | f d f | | f  d t j |  d d	 t j   k p]t j t  rot j t  n d	 d
 t j |  d d t j   k pt j |  rt j |  n d d d t j   k pt j t  rt j t  n d d d t j   k pt j |   r t j |   n d d t j |  i } d d d | i } t	 t j
 |    d  } } } } qWd } t | |   }	 t |	  }
 d } |
 | k } | rt j d f | f d f |
 | f  d t j |  d t j |	  d d	 t j   k pt j t  r't j t  n d	 d d t j   k pKt j |   r]t j |   n d d d t j   k pt j t  rt j t  n d d t j |  d t j |
  i } d d d | i } t	 t j
 |    d  } }	 }
 } } d  S)Nzmdef test():
    "comment 1"
    x = 1
      # comment 2
    # comment 3

    assert False

"""
comment 4
"""
r   r   z	    x = 1r   zX%(py7)s
{%(py7)s = %(py0)s(%(py5)s
{%(py5)s = %(py1)s(%(py2)s, %(py3)s)
})
} == %(py10)sr6   r	   r   r8   r+   liner   r   r   r7   rY   r   zassert %(py12)sr[   
   z    assert Falsez"""zX%(py8)s
{%(py8)s = %(py0)s(%(py6)s
{%(py6)s = %(py1)s(%(py3)s, %(py4)s)
})
} == %(py11)sr~   r   r-   r   zassert %(py13)sr   )r   r   r   r   r   r   r   r   r   r   r   )r7   r^  r   r;   r]   r<   r^   r_   r   r   r{   r   r   r   r    r    r!   test_comments  sD          1         1          
   r`  c              C   s  d }  xt  d d  D]} t | |   } t |  } d } | | k } | rt j d f | f d f | | f  d t j |  d d	 t j   k p t j t  r t j t  n d	 d
 t j |  d d t j   k p t j |  r t j |  n d d d t j   k pt j t  r1t j t  n d d d t j   k pUt j |   rgt j |   n d d t j |  i } d d d | i } t	 t j
 |    d  } } } } q Wd  S)Nz'test(foo=1,
    # comment 1
    bar=2)
rk   r   z&test(foo=1,
    # comment 1
    bar=2)r   zX%(py7)s
{%(py7)s = %(py0)s(%(py5)s
{%(py5)s = %(py1)s(%(py2)s, %(py3)s)
})
} == %(py10)sr6   r	   r   r8   r+   r^  r   r   r   r7   rY   r   zassert %(py12)sr[   )r   r   r   r   r   r   r   r   r   r   r   )r7   r^  r   r;   r]   r<   r^   r_   r    r    r!   test_comment_in_statement
  s          1   ra  c           
   C   s  t  d d  }  t |   } d } | | k } | st j d | f d | | f  d d t j   k su t j |   r t j |   n d d t j |  d	 d
 t j   k s t j t  r t j t  n d
 d t j |  i } d d | i } t t j	 |    d  } } } d  S)Nrk   zif False: 2
else: 3zelse: 3r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   )r   )rb  rc  )
r   r   r   r   r   r   r   r   r   r   )r7   r   r   r   r   r   r    r    r!   test_single_line_else  s           rd  c           
   C   s  t  d d  }  t |   } d } | | k } | st j d | f d | | f  d d t j   k su t j |   r t j |   n d d t j |  d	 d
 t j   k s t j t  r t j t  n d
 d t j |  i } d d | i } t t j	 |    d  } } } d  S)Nrk   ztry: 1
finally: 3z
finally: 3r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   )r   )re  rf  )
r   r   r   r   r   r   r   r   r   r   )r7   r   r   r   r   r   r    r    r!   test_single_line_finally  s           rg  c           
   C   s  d }  t  d |   } t |  } d } | | k } | st j d | f d | | f  d d t j   k s{ t j |  r t j |  n d d t j |  d	 d
 t j   k s t j t  r t j t  n d
 d t j |  i } d d | i } t t j	 |    d  } } } d  S)NzNdef round_trip(dinp):
  assert 1 == dinp
def test_rt():
  round_trip("""
""")
r   z  round_trip("""
""")r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r   r   r   r   r   r   assert %(py8)sr   )r   )rh  ri  )
r   r   r   r   r   r   r   r   r   r   )r7   r   r   r   r   r   r   r    r    r!   test_issue55  s           rj  c           
   C   s  t  d d  }  t |   } d } | | k } | st j d | f d | | f  d d t j   k su t j |   r t j |   n d d t j |  d	 d
 t j   k s t j t  r t j t  n d
 d t j |  i } d d | i } t t j	 |    d  } } } d  S)Nr   z!raise ValueError(
    23
)
x = 3
zraise ValueError(
    23
)r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   )r   )rk  rl  )
r   r   r   r   r   r   r   r   r   r   )r7   r   r   r   r   r   r    r    r!   XXXtest_multiline"  s    	       rm  c               @   sL   e  Z d  Z e Z d Z d d   Z d d   Z d d   Z d d	   Z	 d
 S)TestTryz_try:
    raise ValueError
except Something:
    raise IndexError(1)
else:
    raise KeyError()
c          
   C   s  t  d |  j  } t |  } d } | | k } | s
t j d | f d | | f  d d t j   k sx t j |  r t j |  n d d t j |  d d	 t j   k s t j t  r t j t  n d	 d
 t j |  i } d d | i } t	 t j
 |    d  } } } d  S)Nrk   z    raise ValueErrorr   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   )r   )ro  rp  )r   r7   r   r   r   r   r   r   r   r   r   )r?   r7   r   r   r   r   r   r    r    r!   	test_body6  s           zTestTry.test_bodyc          
   C   s  t  d |  j  } t |  } d } | | k } | s
t j d | f d | | f  d d t j   k sx t j |  r t j |  n d d t j |  d d	 t j   k s t j t  r t j t  n d	 d
 t j |  i } d d | i } t	 t j
 |    d  } } } d  S)Nr   zexcept Something:r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   )r   )rr  rs  )r   r7   r   r   r   r   r   r   r   r   r   )r?   r7   r   r   r   r   r   r    r    r!   test_except_line:  s           zTestTry.test_except_linec          
   C   s  t  d |  j  } t |  } d } | | k } | s
t j d | f d | | f  d d t j   k sx t j |  r t j |  n d d t j |  d d	 t j   k s t j t  r t j t  n d	 d
 t j |  i } d d | i } t	 t j
 |    d  } } } d  S)Nr   z    raise IndexError(1)r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   )r   )ru  rv  )r   r7   r   r   r   r   r   r   r   r   r   )r?   r7   r   r   r   r   r   r    r    r!   test_except_body>  s           zTestTry.test_except_bodyc          
   C   s  t  d |  j  } t |  } d } | | k } | s
t j d | f d | | f  d d t j   k sx t j |  r t j |  n d d t j |  d d	 t j   k s t j t  r t j t  n d	 d
 t j |  i } d d | i } t	 t j
 |    d  } } } d  S)Nr   z    raise KeyError()r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   )r   )rx  ry  )r   r7   r   r   r   r   r   r   r   r   r   )r?   r7   r   r   r   r   r   r    r    r!   	test_elseB  s           zTestTry.test_elseN)
rA   rB   rC   r   
pytestmarkr7   rq  rt  rw  rz  r    r    r    r!   rn  +  s   rn  c               @   s.   e  Z d  Z d Z d d   Z d d   Z d S)TestTryFinallyz;try:
    raise ValueError
finally:
    raise IndexError(1)
c          
   C   s  t  d |  j  } t |  } d } | | k } | s
t j d | f d | | f  d d t j   k sx t j |  r t j |  n d d t j |  d d	 t j   k s t j t  r t j t  n d	 d
 t j |  i } d d | i } t	 t j
 |    d  } } } d  S)Nrk   z    raise ValueErrorr   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   )r   )r}  r~  )r   r7   r   r   r   r   r   r   r   r   r   )r?   r7   r   r   r   r   r   r    r    r!   rq  N  s           zTestTryFinally.test_bodyc          
   C   s  t  d |  j  } t |  } d } | | k } | s
t j d | f d | | f  d d t j   k sx t j |  r t j |  n d d t j |  d d	 t j   k s t j t  r t j t  n d	 d
 t j |  i } d d | i } t	 t j
 |    d  } } } d  S)Nr   z    raise IndexError(1)r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   )r   )r  r  )r   r7   r   r   r   r   r   r   r   r   r   )r?   r7   r   r   r   r   r   r    r    r!   test_finallyR  s           zTestTryFinally.test_finallyN)rA   rB   rC   r7   rq  r  r    r    r    r!   r|  F  s   r|  c               @   sL   e  Z d  Z e Z d Z d d   Z d d   Z d d   Z d d	   Z	 d
 S)TestIfz6if 1:
    y = 3
elif False:
    y = 5
else:
    y = 7
c          
   C   s  t  d |  j  } t |  } d } | | k } | s
t j d | f d | | f  d d t j   k sx t j |  r t j |  n d d t j |  d d	 t j   k s t j t  r t j t  n d	 d
 t j |  i } d d | i } t	 t j
 |    d  } } } d  S)Nrk   z	    y = 3r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   )r   )r  r  )r   r7   r   r   r   r   r   r   r   r   r   )r?   r7   r   r   r   r   r   r    r    r!   rq  c  s           zTestIf.test_bodyc          
   C   s  t  d |  j  } t |  } d } | | k } | s
t j d | f d | | f  d d t j   k sx t j |  r t j |  n d d t j |  d d	 t j   k s t j t  r t j t  n d	 d
 t j |  i } d d | i } t	 t j
 |    d  } } } d  S)Nr   zelif False:r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   )r   )r  r  )r   r7   r   r   r   r   r   r   r   r   r   )r?   r7   r   r   r   r   r   r    r    r!   test_elif_clauseg  s           zTestIf.test_elif_clausec          
   C   s  t  d |  j  } t |  } d } | | k } | s
t j d | f d | | f  d d t j   k sx t j |  r t j |  n d d t j |  d d	 t j   k s t j t  r t j t  n d	 d
 t j |  i } d d | i } t	 t j
 |    d  } } } d  S)Nr   z	    y = 5r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   )r   )r  r  )r   r7   r   r   r   r   r   r   r   r   r   )r?   r7   r   r   r   r   r   r    r    r!   	test_elifk  s           zTestIf.test_elifc          
   C   s  t  d |  j  } t |  } d } | | k } | s
t j d | f d | | f  d d t j   k sx t j |  r t j |  n d d t j |  d d	 t j   k s t j t  r t j t  n d	 d
 t j |  i } d d | i } t	 t j
 |    d  } } } d  S)Nr   z	    y = 7r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   )r   )r  r  )r   r7   r   r   r   r   r   r   r   r   r   )r?   r7   r   r   r   r   r   r    r    r!   rz  o  s           zTestIf.test_elseN)
rA   rB   rC   r   r{  r7   rq  r  r  rz  r    r    r    r!   r  X  s   r  c              C   st  d }  t  d |   } t |  } |  j } |   } | | k } | s^t j d | f d | | f  d t j |  d d t j   k s t j |  r t j |  n d d d	 t j   k s t j |   r t j |   n d	 d
 t j |  d d t j   k st j t  r t j t  n d d t j |  i } d d | i } t	 t j
 |    d  } } } } d  S)Nzhello ; pytest.skip()
r   r   b%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py9)s
{%(py9)s = %(py7)s
{%(py7)s = %(py5)s.strip
}()
}r6   r	   r7   r8   r   r9   r   r   r   r   assert %(py11)sr~   )r   )r  r  )r   r   r\   r   r   r   r   r   r   r   r   )r   r7   r   r;   r<   r   r=   r   r    r    r!   test_semicolons  s     	 	      r  c           
   C   s  d }  t  d |   } t |  } d } | | k } | st j d | f d | | f  d d t j   k s{ t j |  r t j |  n d d t j |  d	 d
 t j   k s t j t  r t j t  n d
 d t j |  i } d d | i } t t j	 |    d  } } } d  S)Nz<def func(): raise ValueError(42)

def something():
    pass
r   z def func(): raise ValueError(42)r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   r7   r   r   r   r   r   assert %(py8)sr   )r   )r  r  )
r   r   r   r   r   r   r   r   r   r   )r   r7   r   r   r   r   r   r    r    r!   test_def_onlinez  s           r  c           
   C   s  d }  t  d |   } t |  } d } | | k } | st j d | f d | | f  d d t j   k s{ t j |  r t j |  n d d t j |  d	 d
 t j   k s t j t  r t j t  n d
 d t j |  i } d d | i } t t j	 |    d  } } } d  S)Nzsomething
'''
'''rk   z'''
'''r   0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr	   resultr   r   r   r   r   assert %(py8)sr   )r   )r  r  )
r   r   r   r   r   r   r   r   r   r   )r7   r  r   r   r   r   r   r    r    r!   XXX_test_expression_multiline  s           r  )>builtinsr   _pytest.assertion.rewrite	assertionrewriter   rC  r   r/   r   r!  r   rs   r   Znothingr   xfailfailsonjythonr"   r5   r>   rK   rP   rS   rW   r`   rf   rj   r|   r   r   r   r  r
  r  r  r  r#  r+  r
   r0  r3  r9  r>  rJ  rQ  r   rW  rZ  r]  r`  ra  rd  rg  rj  rm  rn  r|  r  r  r  r  r    r    r    r!   <module>   sj     !		
