<s> import Cython . Compiler . Errors as Errors <EOL> from Cython . CodeWriter import CodeWriter <EOL> import unittest <EOL> from Cython . Compiler . ModuleNode import ModuleNode <EOL> import Cython . Compiler . Main as Main <EOL> from Cython . Compiler . TreeFragment import TreeFragment , strip_common_indent <EOL> from Cython . Compiler . Visitor import TreeVisitor <EOL> class NodeTypeWriter ( TreeVisitor ) : <EOL> def __init__ ( self ) : <EOL> super ( NodeTypeWriter , self ) . __init__ ( ) <EOL> self . _indents = <NUM_LIT:0> <EOL> self . result = [ ] <EOL> def visit_Node ( self , node ) : <EOL> if len ( self . access_path ) == <NUM_LIT:0> : <EOL> name = u"<STR_LIT>" <EOL> else : <EOL> tip = self . access_path [ - <NUM_LIT:1> ] <EOL> if tip [ <NUM_LIT:2> ] is not None : <EOL> name = u"<STR_LIT>" % tip [ <NUM_LIT:1> : <NUM_LIT:3> ] <EOL> else : <EOL> name = tip [ <NUM_LIT:1> ] <EOL> self . result . append ( u"<STR_LIT:U+0020>" * self . _indents + <EOL> u"<STR_LIT>" % ( name , node . __class__ . __name__ ) ) <EOL> self . _indents += <NUM_LIT:1> <EOL> self . visitchildren ( node ) <EOL> self . _indents -= <NUM_LIT:1> <EOL> def treetypes ( root ) : <EOL> """<STR_LIT>""" <EOL> w = NodeTypeWriter ( ) <EOL> w . visit ( root ) <EOL> return u"<STR_LIT:\n>" . join ( [ u"<STR_LIT>" ] + w . result + [ u"<STR_LIT>" ] ) <EOL> class CythonTest ( unittest . TestCase ) : <EOL> def assertLines ( self , expected , result ) : <EOL> "<STR_LIT>" <EOL> if not isinstance ( expected , list ) : expected = expected . split ( u"<STR_LIT:\n>" ) <EOL> if not isinstance ( result , list ) : result = result . split ( u"<STR_LIT:\n>" ) <EOL> for idx , ( expected_line , result_line ) in enumerate ( zip ( expected , result ) ) : <EOL> self . assertEqual ( expected_line , result_line , "<STR_LIT>" % ( idx , expected_line , result_line ) ) <EOL> self . assertEqual ( len ( expected ) , len ( result ) , <EOL> "<STR_LIT>" % ( "<STR_LIT:\n>" . join ( expected ) , u"<STR_LIT:\n>" . join ( result ) ) ) <EOL> def codeToLines ( self , tree ) : <EOL> writer = CodeWriter ( ) <EOL> writer . write ( tree ) <EOL> return writer . result . lines <EOL> def codeToString ( self , tree ) : <EOL> return "<STR_LIT:\n>" . join ( self . codeToLines ( tree ) ) <EOL> def assertCode ( self , expected , result_tree ) : <EOL> result_lines = self . codeToLines ( result_tree ) <EOL> expected_lines = strip_common_indent ( expected . split ( "<STR_LIT:\n>" ) ) <EOL> for idx , ( line , expected_line ) in enumerate ( zip ( result_lines , expected_lines ) ) : <EOL> self . assertEqual ( expected_line , line , "<STR_LIT>" % ( idx , line , expected_line ) ) <EOL> self . assertEqual ( len ( result_lines ) , len ( expected_lines ) , <EOL> "<STR_LIT>" % ( "<STR_LIT:\n>" . join ( result_lines ) , expected ) ) <EOL> def fragment ( self , code , pxds = { } , pipeline = [ ] ) : <EOL> "<STR_LIT>" <EOL> name = self . id ( ) <EOL> if name . startswith ( "<STR_LIT>" ) : name = name [ len ( "<STR_LIT>" ) : ] <EOL> name = name . replace ( "<STR_LIT:.>" , "<STR_LIT:_>" ) <EOL> return TreeFragment ( code , name , pxds , pipeline = pipeline ) <EOL> def treetypes ( self , root ) : <EOL> return treetypes ( root ) <EOL> def should_fail ( self , func , exc_type = Exception ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> func ( ) <EOL> self . fail ( "<STR_LIT>" % exc_type ) <EOL> except exc_type , e : <EOL> self . assert_ ( isinstance ( e , exc_type ) ) <EOL> return e <EOL> def should_not_fail ( self , func ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return func ( ) <EOL> except : <EOL> self . fail ( ) <EOL> class TransformTest ( CythonTest ) : <EOL> """<STR_LIT>""" <EOL> def run_pipeline ( self , pipeline , pyx , pxds = { } ) : <EOL> tree = self . fragment ( pyx , pxds ) . root <EOL> for T in pipeline : <EOL> tree = T ( tree ) <EOL> return tree </s>
