
    Ae+                         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  G d	 d
e      Zd Z G d dej"                        Z G d de      Z G d de      ZddZy)    )absolute_importN   )Errors)
CodeWriter)TreeFragmentstrip_common_indent)TreeVisitorVisitorTransform)TreePathc                   $     e Zd Z fdZd Z xZS )NodeTypeWriterc                 F    t         t        |           d| _        g | _        y )Nr   )superr   __init___indentsresult)self	__class__s    0lib/python3.12/site-packages/Cython/TestUtils.pyr   zNodeTypeWriter.__init__   s    nd,.    c                 d   | j                   sd}n"| j                   d   }|d   	d|dd z  }n|d   }| j                  j                  d| j                  z  |d|j                  j
                  z          | xj                  dz  c_        | j                  |       | xj                  dz  c_        y )	Nz(root)   z%s[%d]r      z  z: )access_pathr   appendr   r   __name__visitchildren)r   nodenametips       r   
visit_NodezNodeTypeWriter.visit_Node   s    D""2&C1v! 3q8+1v54==0(,dnn.E.EFG 	H4 r   )r   
__module____qualname__r   r"   __classcell__)r   s   @r   r   r      s    
r   r   c                 ~    t               }|j                  |        dj                  dg|j                  z   dgz         S )zReturns 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.
 )r   visitjoinr   )rootws     r   	treetypesr-   %   s8    
 	AGGDM::seahh&#.//r   c                   T    e Zd Zd Zd Zd Zd Zd Zd Zd Z	dd	Z
d
 ZefdZd Zy)
CythonTestc                     t         j                  | _        t         j                  | _        d xt         _        t         _        y N)r   listing_file	echo_filer   s    r   setUpzCythonTest.setUp1   s-    "//))155f.r   c                 X    | j                   t        _         | j                  t        _        y r1   )r2   r   r3   r4   s    r   tearDownzCythonTest.tearDown6   s    "//>>r   c           
         t        |t              s|j                  d      }t        |t              s|j                  d      }t        t	        ||            D ]!  \  }\  }}| j                  ||d|||fz         # | j                  t        |      t        |      ddj                  |      ddj                  |             y)zHChecks that the given strings or lists of strings are equal line by liner'   zLine %d:
Exp: %s
Got: %sUnmatched lines. Got:

Expected:
N)
isinstancelistsplit	enumeratezipassertEquallenr*   )r   expectedr   idxexpected_lineresult_lines         r   assertLineszCythonTest.assertLines:   s    (D)~~e,H&$'\\%(F1:3x;P1Q 	_-C--]K9S-Q\<]]_	_ 	XFGKyyQYGZ\a\f\fgm\no	qr   c                 d    t               }|j                  |       |j                  j                  S r1   )r   writer   lines)r   treewriters      r   codeToLineszCythonTest.codeToLinesF   s%    T}}"""r   c                 B    dj                  | j                  |            S )Nr'   )r*   rL   )r   rJ   s     r   codeToStringzCythonTest.codeToStringK   s    yy))$/00r   c           	      @   | j                  |      }t        |j                  d            }t        t	        ||            D ]!  \  }\  }}| j                  ||d|||fz         # | j                  t        |      t        |      ddj                  |      d|       y )Nr'   zLine %d:
Got: %s
Exp: %sr9   r:   )rL   r   r=   r>   r?   r@   rA   r*   )r   rB   result_treeresult_linesexpected_linesrC   linerD   s           r   
assertCodezCythonTest.assertCodeN   s    ''4,X^^D-AB*3Cn4U*V 	X&C&$]D9S$<VVX	X 	\*C,?GKyyQ]G^`hi	kr   c                 X    | j                  t        j                  ||      d d|z         y )Nz"Path '%s' not found in result tree)assertNotEqualr   
find_first)r   pathrP   s      r   assertNodeExistszCythonTest.assertNodeExistsY   s*    H//TBD@4G	Ir   Nc                     |i }|g }| j                         }|j                  d      r|t        d      d }|j                  dd      }t	        ||||      S )zNSimply create a tree fragment using the name of the test-case in parse errors.Nz	__main__.._)pipeline)id
startswithrA   replacer   )r   codepxdsr]   r    s        r   fragmentzCythonTest.fragment]   sa    <DHwwy??;'K()*D||C%D$x@@r   c                     t        |      S r1   )r-   )r   r+   s     r   r-   zCythonTest.treetypesi   s    r   c                     	  |        | j                  d|z         y# |$ r'}| j                  t        ||             |cY d}~S d}~ww xY w)zCalls "func" and fails if it doesn't raise the right exception
        (any exception by default). Also returns the exception in question.
        z Expected an exception of type %rN)fail
assertTruer;   )r   funcexc_typees       r   should_failzCythonTest.should_faill   sF    	FII88CD 	OOJq(34H	s    A
AA
A
c                 r    	  |       S # t         $ r$}| j                  t        |             Y d}~yd}~ww xY w)zCalls 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)	Exceptionrf   str)r   rh   excs      r   should_not_failzCythonTest.should_not_failw   s1    	 6M 	 IIc#h	 s   	 	616)NN)r   r#   r$   r5   r7   rF   rL   rN   rT   rY   rc   r-   rm   rk   rp    r   r   r/   r/   /   sB    6
*
q#
1	kI
A *3 	 r   r/   c                       e Zd ZdZddZy)TransformTesta.  
    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.
    Nc                 d    |i }| j                  ||      j                  }|D ]
  } ||      } |S r1   )rc   r+   )r   r]   pyxrb   rJ   Ts         r   run_pipelinezTransformTest.run_pipeline   s?    <D}}S$',, 	AT7D	r   r1   )r   r#   r$   __doc__rw   rq   r   r   rs   rs      s    .r   rs   c                   *    e Zd Zd Zej
                  Zy)TreeAssertVisitorc                 `   |j                   }d|v rD|d   D ]<  }t        j                  ||      t        j                  |j
                  d|z         > d|v rD|d   D ]<  }t        j                  ||      t        j                  |j
                  d|z         > | j                  |       |S )Ntest_assert_path_existsz+Expected path '%s' not found in result treetest_fail_if_path_existsz)Unexpected path '%s' found in result tree)
directivesr   rW   r   errorposr   )r   r   r~   rX   s       r   visit_CompilerDirectivesNodez.TreeAssertVisitor.visit_CompilerDirectivesNode   s    __
$
2"#<= N&&tT2:LLELNN
 &3"#=> M&&tT2>LLCtKMM
 	4 r   N)r   r#   r$   r   r
   recurse_to_childrenr"   rq   r   r   rz   rz      s    " "55Jr   rz   c                 F   |t        j                         }g }d }t        |       }	 |j                         }|j	                          ~	 |D ]  }|d d dk(  r|j                         j                  d      j                         j                  dt        j                  j                        }t        j                  j                  ||      }t        j                  j                  t        j                  j                  |            s2t        j                  t        j                  j                  |             ||d }}|j	                          t        |d      }||j                  |       )|j                         s;|j                         j!                  d      r\|j                         dvsp|j#                  |        	 ||j	                          	 |dj                  |      fS # |j	                          w xY w# ||j	                          w w xY w)N   z######/r,   )z"""z'''r(   )tempfilemkdtempopen	readlinesclosestripr`   osrX   sepr*   existsdirnamemakedirsrH   lstripr_   r   )		tree_filedirheadercur_filefrI   rS   filenamerX   s	            r   unpack_source_treer      s   
{ FHYA			 	(DBQx7"::<--c288:BB3Tww||C2ww~~bggood&;<KK 56'"*DxAGGIc?%t$dkkm&>&>s&C::<~5MM$'	(  NN+ 	
	& NN  s*   G6 	D;H H 'H ;H 6HH r1   )
__future__r   r   unittestr   Compilerr   r   Compiler.TreeFragmentr   r   Compiler.Visitorr	   r
   r   r   r-   TestCaser/   rs   rz   r   rq   r   r   <module>r      se    & 	    " D ; [ .0O "" O dJ D6( 60 r   