Cython has moved to github.
cython-devel
view Cython/Compiler/Tests/TestParseTreeTransforms.py @ 1661:9d0ac0d9659b
Kludge for #151
| author | Dag Sverre Seljebotn <dagss@student.matnat.uio.no> |
|---|---|
| date | Thu Jan 29 19:19:06 2009 +0100 (3 years ago) |
| parents | f21bbde7d1c3 |
| children | 569ed4c256db |
line source
1 from Cython.TestUtils import TransformTest
2 from Cython.Compiler.ParseTreeTransforms import *
3 from Cython.Compiler.Nodes import *
5 class TestNormalizeTree(TransformTest):
6 def test_parserbehaviour_is_what_we_coded_for(self):
7 t = self.fragment(u"if x: y").root
8 self.assertLines(u"""
9 (root): StatListNode
10 stats[0]: IfStatNode
11 if_clauses[0]: IfClauseNode
12 condition: NameNode
13 body: ExprStatNode
14 expr: NameNode
15 """, self.treetypes(t))
17 def test_wrap_singlestat(self):
18 t = self.run_pipeline([NormalizeTree(None)], u"if x: y")
19 self.assertLines(u"""
20 (root): StatListNode
21 stats[0]: IfStatNode
22 if_clauses[0]: IfClauseNode
23 condition: NameNode
24 body: StatListNode
25 stats[0]: ExprStatNode
26 expr: NameNode
27 """, self.treetypes(t))
29 def test_wrap_multistat(self):
30 t = self.run_pipeline([NormalizeTree(None)], u"""
31 if z:
32 x
33 y
34 """)
35 self.assertLines(u"""
36 (root): StatListNode
37 stats[0]: IfStatNode
38 if_clauses[0]: IfClauseNode
39 condition: NameNode
40 body: StatListNode
41 stats[0]: ExprStatNode
42 expr: NameNode
43 stats[1]: ExprStatNode
44 expr: NameNode
45 """, self.treetypes(t))
47 def test_statinexpr(self):
48 t = self.run_pipeline([NormalizeTree(None)], u"""
49 a, b = x, y
50 """)
51 self.assertLines(u"""
52 (root): StatListNode
53 stats[0]: ParallelAssignmentNode
54 stats[0]: SingleAssignmentNode
55 lhs: NameNode
56 rhs: NameNode
57 stats[1]: SingleAssignmentNode
58 lhs: NameNode
59 rhs: NameNode
60 """, self.treetypes(t))
62 def test_wrap_offagain(self):
63 t = self.run_pipeline([NormalizeTree(None)], u"""
64 x
65 y
66 if z:
67 x
68 """)
69 self.assertLines(u"""
70 (root): StatListNode
71 stats[0]: ExprStatNode
72 expr: NameNode
73 stats[1]: ExprStatNode
74 expr: NameNode
75 stats[2]: IfStatNode
76 if_clauses[0]: IfClauseNode
77 condition: NameNode
78 body: StatListNode
79 stats[0]: ExprStatNode
80 expr: NameNode
81 """, self.treetypes(t))
84 def test_pass_eliminated(self):
85 t = self.run_pipeline([NormalizeTree(None)], u"pass")
86 self.assert_(len(t.stats) == 0)
88 class TestWithTransform:#(TransformTest): Disabled
90 def test_simplified(self):
91 t = self.run_pipeline([WithTransform(None)], u"""
92 with x:
93 y = z ** 3
94 """)
96 self.assertCode(u"""
98 $0_0 = x
99 $0_2 = $0_0.__exit__
100 $0_0.__enter__()
101 $0_1 = True
102 try:
103 try:
104 $1_0 = None
105 y = z ** 3
106 except:
107 $0_1 = False
108 if (not $0_2($1_0)):
109 raise
110 finally:
111 if $0_1:
112 $0_2(None, None, None)
114 """, t)
116 def test_basic(self):
117 t = self.run_pipeline([WithTransform(None)], u"""
118 with x as y:
119 y = z ** 3
120 """)
121 self.assertCode(u"""
123 $0_0 = x
124 $0_2 = $0_0.__exit__
125 $0_3 = $0_0.__enter__()
126 $0_1 = True
127 try:
128 try:
129 $1_0 = None
130 y = $0_3
131 y = z ** 3
132 except:
133 $0_1 = False
134 if (not $0_2($1_0)):
135 raise
136 finally:
137 if $0_1:
138 $0_2(None, None, None)
140 """, t)
143 if __name__ == "__main__":
144 import unittest
145 unittest.main()
