ó
¦V]c           @@  s  d  d l  m Z d  d l Z d  d l Z d  d l Z d d l m Z d d l m Z d d l m	 Z	 m
 Z
 d d l m Z m Z d d l m Z d	 e f d
 „  ƒ  YZ d „  Z d e j f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d d „ Z d S(   i    (   t   absolute_importNi   (   t   Errors(   t
   CodeWriter(   t   TreeFragmentt   strip_common_indent(   t   TreeVisitort   VisitorTransform(   t   TreePatht   NodeTypeWriterc           B@  s   e  Z d  „  Z d „  Z RS(   c         C@  s)   t  t |  ƒ j ƒ  d |  _ g  |  _ d  S(   Ni    (   t   superR   t   __init__t   _indentst   result(   t   self(    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyR
      s    	c         C@  s§   |  j  s d } n; |  j  d } | d d  k	 rC d | d d !} n
 | d } |  j j d |  j d | | j j f ƒ |  j d 7_ |  j | ƒ |  j d 8_ d  S(	   Nu   (root)iÿÿÿÿi   u   %s[%d]i   i   u     u   %s: %s(   t   access_patht   NoneR   t   appendR   t	   __class__t   __name__t   visitchildren(   R   t   nodet   namet   tip(    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyt
   visit_Node   s    		
(   R   t
   __module__R
   R   (    (    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyR      s   	c         C@  s4   t  ƒ  } | j |  ƒ d j d g | j d g ƒ S(   sÊ   Returns a string representing the tree by class names.
    There's a leading and trailing whitespace so that it can be
    compared by simple string comparison while still making test
    cases look ok.u   
u    (   R   t   visitt   joinR   (   t   roott   w(    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyt	   treetypes%   s    	t
   CythonTestc           B@  st   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d d „ Z
 d „  Z e d	 „ Z d
 „  Z RS(   c         C@  s,   t  j |  _ t  j |  _ d  t  _ t  _ d  S(   N(   R   t   listing_filet	   echo_fileR   (   R   (    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyt   setUp1   s    c         C@  s   |  j  t _  |  j t _ d  S(   N(   R   R   R    (   R   (    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyt   tearDown6   s    c         C@  sÍ   t  | t ƒ s! | j d ƒ } n  t  | t ƒ sB | j d ƒ } n  xI t t | | ƒ ƒ D]2 \ } \ } } |  j | | d | | | f ƒ qX W|  j t | ƒ t | ƒ d d j | ƒ d j | ƒ f ƒ d S(   sH   Checks that the given strings or lists of strings are equal line by lineu   
s   Line %d:
Exp: %s
Got: %ss%   Unmatched lines. Got:
%s
Expected:
%ss   
N(   t
   isinstancet   listt   splitt	   enumeratet   zipt   assertEqualt   lenR   (   R   t   expectedR   t   idxt   expected_linet   result_line(    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyt   assertLines:   s    (c         C@  s    t  ƒ  } | j | ƒ | j j S(   N(   R   t   writeR   t   lines(   R   t   treet   writer(    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyt   codeToLinesF   s    	c         C@  s   d j  |  j | ƒ ƒ S(   Ns   
(   R   R3   (   R   R1   (    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyt   codeToStringK   s    c         C@  s¦   |  j  | ƒ } t | j d ƒ ƒ } xI t t | | ƒ ƒ D]2 \ } \ } } |  j | | d | | | f ƒ q: W|  j t | ƒ t | ƒ d d j | ƒ | f ƒ d  S(   Ns   
s   Line %d:
Got: %s
Exp: %ss%   Unmatched lines. Got:
%s
Expected:
%s(   R3   R   R%   R&   R'   R(   R)   R   (   R   R*   t   result_treet   result_linest   expected_linesR+   t   lineR,   (    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyt
   assertCodeN   s    (c         C@  s'   |  j  t j | | ƒ d  d | ƒ d  S(   Ns"   Path '%s' not found in result tree(   t   assertNotEqualR   t
   find_firstR   (   R   t   pathR5   (    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyt   assertNodeExistsY   s    c         C@  s€   | d k r i  } n  | d k r* g  } n  |  j ƒ  } | j d ƒ rX | t d ƒ } n  | j d d ƒ } t | | | d | ƒS(   sN   Simply create a tree fragment using the name of the test-case in parse errors.s	   __main__.t   .t   _t   pipelineN(   R   t   idt
   startswithR)   t   replaceR   (   R   t   codet   pxdsR@   R   (    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyt   fragment]   s    		c         C@  s
   t  | ƒ S(   N(   R   (   R   R   (    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyR   i   s    c         C@  sM   y | ƒ  |  j  d | ƒ Wn* | k
 rH } |  j t | | ƒ ƒ | SXd S(   s“   Calls "func" and fails if it doesn't raise the right exception
        (any exception by default). Also returns the exception in question.
        s    Expected an exception of type %rN(   t   failt
   assertTrueR#   (   R   t   funct   exc_typet   e(    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyt   should_faill   s    c         C@  s8   y | ƒ  SWn& t  k
 r3 } |  j t | ƒ ƒ n Xd S(   s©   Calls func and succeeds if and only if no exception is raised
        (i.e. converts exception raising into a failed testcase). Returns
        the return value of func.N(   t	   ExceptionRG   t   str(   R   RI   t   exc(    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyt   should_not_failw   s    N(   R   R   R!   R"   R.   R3   R4   R9   R=   R   RF   R   RM   RL   RP   (    (    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyR   /   s   								t   TransformTestc           B@  s   e  Z d  Z d d „ Z RS(   s.  
    Utility base class for transform unit tests. It is based around constructing
    test trees (either explicitly or by parsing a Cython code string); running
    the transform, serialize it using a customized Cython serializer (with
    special markup for nodes that cannot be represented in Cython),
    and do a string-comparison line-by-line of the result.

    To create a test case:
     - Call run_pipeline. The pipeline should at least contain the transform you
       are testing; pyx should be either a string (passed to the parser to
       create a post-parse tree) or a node representing input to pipeline.
       The result will be a transformed result.

     - Check that the tree is correct. If wanted, assertCode can be used, which
       takes a code string as expected, and a ModuleNode in result_tree
       (it serializes the ModuleNode to a string and compares line-by-line).

    All code strings are first stripped for whitespace lines and then common
    indentation.

    Plans: One could have a pxd dictionary parameter to run_pipeline.
    c         C@  sK   | d  k r i  } n  |  j | | ƒ j } x | D] } | | ƒ } q1 W| S(   N(   R   RF   R   (   R   R@   t   pyxRE   R1   t   T(    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyt   run_pipeline™   s    	N(   R   R   t   __doc__R   RT   (    (    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyRQ      s   t   TreeAssertVisitorc           B@  s   e  Z d  „  Z e j Z RS(   c         C@  sÆ   | j  } d | k r_ xG | d D]8 } t j | | ƒ d  k r  t j | j d | ƒ q  q  Wn  d | k rµ xG | d D]8 } t j | | ƒ d  k	 rv t j | j d | ƒ qv qv Wn  |  j | ƒ | S(   Nt   test_assert_path_existss+   Expected path '%s' not found in result treet   test_fail_if_path_existss)   Unexpected path '%s' found in result tree(   t
   directivesR   R;   R   R   t   errort   posR   (   R   R   RY   R<   (    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyt   visit_CompilerDirectivesNode§   s    	(   R   R   R\   R   t   recurse_to_childrenR   (    (    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyRV   £   s   	c   	      C@  sÀ  | d  k r t j ƒ  } n  g  } d  } t |  ƒ } z | j ƒ  } Wd  | j ƒ  X~ z<x5| D]-} | d  d k r(| j ƒ  j d ƒ j ƒ  j d t j	 j
 ƒ } t j	 j | | ƒ } t j	 j t j	 j | ƒ ƒ sð t j t j	 j | ƒ ƒ n  | d  k	 r| d  } } | j ƒ  n  t | d ƒ } q^ | d  k	 rD| j | ƒ q^ | j ƒ  r^ | j ƒ  j d ƒ r^ | j ƒ  d	 k r‹| j | ƒ q‹q^ q^ WWd  | d  k	 r¬| j ƒ  n  X| d j | ƒ f S(
   Ni   s   #####t   #t   /R   s   """s   '''t    (   s   """s   '''(   R   t   tempfilet   mkdtempt   opent	   readlinest   closet   stripRC   t   osR<   t   sepR   t   existst   dirnamet   makedirsR/   t   lstripRB   R   (	   t	   tree_filet   dirt   headert   cur_filet   fR0   R8   t   filenameR<   (    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyt   unpack_source_tree»   s8    -"(   t
   __future__R    Rg   t   unittestRa   t   CompilerR   R   t   Compiler.TreeFragmentR   R   t   Compiler.VisitorR   R   R   R   R   t   TestCaseR   RQ   RV   R   Rs   (    (    (    s/   lib/python2.7/site-packages/Cython/TestUtils.pyt   <module>   s   	
R"