1from Cython.TestUtils import CythonTest
2
3class TestCodeWriter(CythonTest):
4    # CythonTest uses the CodeWriter heavily, so do some checking by
5    # roundtripping Cython code through the test framework.
6
7    # Note that this test is dependant upon the normal Cython parser
8    # to generate the input trees to the CodeWriter. This save *a lot*
9    # of time; better to spend that time writing other tests than perfecting
10    # this one...
11
12    # Whitespace is very significant in this process:
13    #  - always newline on new block (!)
14    #  - indent 4 spaces
15    #  - 1 space around every operator
16
17    def t(self, codestr):
18        self.assertCode(codestr, self.fragment(codestr).root)
19
20    def test_print(self):
21        self.t(u"""
22                    print x, y
23                    print x + y ** 2
24                    print x, y, z,
25               """)
26
27    def test_if(self):
28        self.t(u"if x:\n    pass")
29
30    def test_ifelifelse(self):
31        self.t(u"""
32                    if x:
33                        pass
34                    elif y:
35                        pass
36                    elif z + 34 ** 34 - 2:
37                        pass
38                    else:
39                        pass
40                """)
41
42    def test_def(self):
43        self.t(u"""
44                    def f(x, y, z):
45                        pass
46                    def f(x = 34, y = 54, z):
47                        pass
48               """)
49
50    def test_longness_and_signedness(self):
51        self.t(u"def f(unsigned long long long long long int y):\n    pass")
52
53    def test_signed_short(self):
54        self.t(u"def f(signed short int y):\n    pass")
55
56    def test_typed_args(self):
57        self.t(u"def f(int x, unsigned long int y):\n    pass")
58
59    def test_cdef_var(self):
60        self.t(u"""
61                    cdef int hello
62                    cdef int hello = 4, x = 3, y, z
63                """)
64
65    def test_for_loop(self):
66        self.t(u"""
67                    for x, y, z in f(g(h(34) * 2) + 23):
68                        print x, y, z
69                    else:
70                        print 43
71                """)
72
73    def test_inplace_assignment(self):
74        self.t(u"x += 43")
75
76    def test_attribute(self):
77        self.t(u"a.x")
78
79if __name__ == "__main__":
80    import unittest
81    unittest.main()
82
83