1 from Cython
.TestUtils
import CythonTest
2 from Cython
.Compiler
.TreeFragment
import *
3 from Cython
.Compiler
.Nodes
import *
4 from Cython
.Compiler
.UtilNodes
import *
5 import Cython
.Compiler
.Naming
as Naming
7 class TestTreeFragments(CythonTest
):
10 F
= self
.fragment(u
"x = 4")
12 self
.assertCode(u
"x = 4", T
)
14 def test_copy_is_taken(self
):
15 F
= self
.fragment(u
"if True: x = 4")
18 self
.assertEqual("x", T2
.stats
[0].if_clauses
[0].body
.lhs
.name
)
19 T2
.stats
[0].if_clauses
[0].body
.lhs
.name
= "other"
20 self
.assertEqual("x", T1
.stats
[0].if_clauses
[0].body
.lhs
.name
)
22 def test_substitutions_are_copied(self
):
23 T
= self
.fragment(u
"y + y").substitute({"y": NameNode(pos
=None, name
="x")})
24 self
.assertEqual("x", T
.stats
[0].expr
.operand1
.name
)
25 self
.assertEqual("x", T
.stats
[0].expr
.operand2
.name
)
26 self
.assert_(T
.stats
[0].expr
.operand1
is not T
.stats
[0].expr
.operand2
)
28 def test_substitution(self
):
29 F
= self
.fragment(u
"x = 4")
30 y
= NameNode(pos
=None, name
=u
"y")
31 T
= F
.substitute({"x" : y
})
32 self
.assertCode(u
"y = 4", T
)
34 def test_exprstat(self
):
35 F
= self
.fragment(u
"PASS")
36 pass_stat
= PassStatNode(pos
=None)
37 T
= F
.substitute({"PASS" : pass_stat
})
38 self
.assert_(isinstance(T
.stats
[0], PassStatNode
), T
)
40 def test_pos_is_transferred(self
):
41 F
= self
.fragment(u
"""
45 T
= F
.substitute({"v" : NameNode(pos
=None, name
="a")})
46 v
= F
.root
.stats
[1].rhs
.operand2
.operand1
47 a
= T
.stats
[1].rhs
.operand2
.operand1
48 self
.assertEquals(v
.pos
, a
.pos
)
51 TemplateTransform
.temp_name_counter
= 0
52 F
= self
.fragment(u
"""
56 T
= F
.substitute(temps
=[u
"TMP"])
58 self
.assert_(isinstance(s
[0].expr
, TempRefNode
))
59 self
.assert_(isinstance(s
[1].rhs
, TempRefNode
))
60 self
.assert_(s
[0].expr
.handle
is s
[1].rhs
.handle
)
62 if __name__
== "__main__":