3 from Cython.Compiler import CmdLine
4 from Cython.TestUtils import TransformTest
5 from Cython.Compiler.ParseTreeTransforms import *
6 from Cython.Compiler.Nodes import *
9 class TestNormalizeTree(TransformTest):
10 def test_parserbehaviour_is_what_we_coded_for(self):
11 t = self.fragment(u"if x: y").root
15 if_clauses[0]: IfClauseNode
19 """, self.treetypes(t))
21 def test_wrap_singlestat(self):
22 t = self.run_pipeline([NormalizeTree(None)], u"if x: y")
26 if_clauses[0]: IfClauseNode
29 stats[0]: ExprStatNode
31 """, self.treetypes(t))
33 def test_wrap_multistat(self):
34 t = self.run_pipeline([NormalizeTree(None)], u"""
42 if_clauses[0]: IfClauseNode
45 stats[0]: ExprStatNode
47 stats[1]: ExprStatNode
49 """, self.treetypes(t))
51 def test_statinexpr(self):
52 t = self.run_pipeline([NormalizeTree(None)], u"""
57 stats[0]: SingleAssignmentNode
64 """, self.treetypes(t))
66 def test_wrap_offagain(self):
67 t = self.run_pipeline([NormalizeTree(None)], u"""
75 stats[0]: ExprStatNode
77 stats[1]: ExprStatNode
80 if_clauses[0]: IfClauseNode
83 stats[0]: ExprStatNode
85 """, self.treetypes(t))
88 def test_pass_eliminated(self):
89 t = self.run_pipeline([NormalizeTree(None)], u"pass")
90 self.assert_(len(t.stats) == 0)
92 class TestWithTransform(object): # (TransformTest): # Disabled!
94 def test_simplified(self):
95 t = self.run_pipeline([WithTransform(None)], u"""
116 $0_2(None, None, None)
120 def test_basic(self):
121 t = self.run_pipeline([WithTransform(None)], u"""
129 $0_3 = $0_0.__enter__()
142 $0_2(None, None, None)
147 # TODO: Re-enable once they're more robust.
148 if sys.version_info[:2] >= (2, 5) and False:
149 from Cython.Debugger import DebugWriter
150 from Cython.Debugger.Tests.TestLibCython import DebuggerTestCase
152 # skip test, don't let it inherit unittest.TestCase
153 DebuggerTestCase = object
155 class TestDebugTransform(DebuggerTestCase):
157 def elem_hasattrs(self, elem, attrs):
158 # we shall supporteth python 2.3 !
159 return all([attr in elem.attrib for attr in attrs])
161 def test_debug_info(self):
163 assert os.path.exists(self.debug_dest)
165 t = DebugWriter.etree.parse(self.debug_dest)
166 # the xpath of the standard ElementTree is primitive, don't use
168 L = list(t.find('/Module/Globals'))
169 # assertTrue is retarded, use the normal assert statement
172 [(e.attrib['name'], e.attrib['type']) for e in L])
173 self.assertEqual(len(L), len(xml_globals))
175 L = list(t.find('/Module/Functions'))
177 xml_funcs = dict([(e.attrib['qualified_name'], e) for e in L])
178 self.assertEqual(len(L), len(xml_funcs))
181 self.assertEqual('CObject', xml_globals.get('c_var'))
182 self.assertEqual('PythonObject', xml_globals.get('python_var'))
185 funcnames = 'codefile.spam', 'codefile.ham', 'codefile.eggs'
186 required_xml_attrs = 'name', 'cname', 'qualified_name'
187 assert all([f in xml_funcs for f in funcnames])
188 spam, ham, eggs = [xml_funcs[funcname] for funcname in funcnames]
190 self.assertEqual(spam.attrib['name'], 'spam')
191 self.assertNotEqual('spam', spam.attrib['cname'])
192 assert self.elem_hasattrs(spam, required_xml_attrs)
194 # test locals of functions
195 spam_locals = list(spam.find('Locals'))
197 spam_locals.sort(key=lambda e: e.attrib['name'])
198 names = [e.attrib['name'] for e in spam_locals]
199 self.assertEqual(list('abcd'), names)
200 assert self.elem_hasattrs(spam_locals[0], required_xml_attrs)
202 # test arguments of functions
203 spam_arguments = list(spam.find('Arguments'))
204 assert spam_arguments
205 self.assertEqual(1, len(list(spam_arguments)))
207 # test step-into functions
208 step_into = spam.find('StepIntoFunctions')
209 spam_stepinto = [x.attrib['name'] for x in step_into]
211 self.assertEqual(2, len(spam_stepinto))
212 assert 'puts' in spam_stepinto
213 assert 'some_c_function' in spam_stepinto
215 print open(self.debug_dest).read()
222 if __name__ == "__main__":