Home | History | Annotate | Download | only in Tests
      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
      6 
      7 class TestTreeFragments(CythonTest):
      8 
      9     def test_basic(self):
     10         F = self.fragment(u"x = 4")
     11         T = F.copy()
     12         self.assertCode(u"x = 4", T)
     13 
     14     def test_copy_is_taken(self):
     15         F = self.fragment(u"if True: x = 4")
     16         T1 = F.root
     17         T2 = F.copy()
     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)
     21 
     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)
     27 
     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)
     33 
     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)
     39 
     40     def test_pos_is_transferred(self):
     41         F = self.fragment(u"""
     42         x = y
     43         x = u * v ** w
     44         """)
     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)
     49 
     50     def test_temps(self):
     51         TemplateTransform.temp_name_counter = 0
     52         F = self.fragment(u"""
     53             TMP
     54             x = TMP
     55         """)
     56         T = F.substitute(temps=[u"TMP"])
     57         s = T.body.stats
     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)
     61 
     62 if __name__ == "__main__":
     63     import unittest
     64     unittest.main()
     65