File size: 3,927 Bytes
16a8ff1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from Cython.TestUtils import CythonTest

class TestCodeWriter(CythonTest):
    # CythonTest uses the CodeWriter heavily, so do some checking by
    # roundtripping Cython code through the test framework.

    # Note that this test is dependent upon the normal Cython parser
    # to generate the input trees to the CodeWriter. This save *a lot*
    # of time; better to spend that time writing other tests than perfecting
    # this one...

    # Whitespace is very significant in this process:
    #  - always newline on new block (!)
    #  - indent 4 spaces
    #  - 1 space around every operator

    def t(self, codestr):
        self.assertCode(codestr, self.fragment(codestr).root)

    def test_print(self):
        self.t(u"""

                    print(x + y ** 2)

                    print(x, y, z)

                    print(x + y, x + y * z, x * (y + z))

               """)

    def test_if(self):
        self.t(u"if x:\n    pass")

    def test_ifelifelse(self):
        self.t(u"""

                    if x:

                        pass

                    elif y:

                        pass

                    elif z + 34 ** 34 - 2:

                        pass

                    else:

                        pass

                """)

    def test_def(self):
        self.t(u"""

                    def f(x, y, z):

                        pass

                    def f(x = 34, y = 54, z):

                        pass

               """)

    def test_cdef(self):
        self.t(u"""

                    cdef f(x, y, z):

                        pass

                    cdef public void (x = 34, y = 54, z):

                        pass

                    cdef f(int *x, void *y, Value *z):

                        pass

                    cdef f(int **x, void **y, Value **z):

                        pass

                    cdef inline f(int &x, Value &z):

                        pass

               """)

    def test_longness_and_signedness(self):
        self.t(u"def f(unsigned long long long long long int y):\n    pass")

    def test_signed_short(self):
        self.t(u"def f(signed short int y):\n    pass")

    def test_typed_args(self):
        self.t(u"def f(int x, unsigned long int y):\n    pass")

    def test_cdef_var(self):
        self.t(u"""

                    cdef int hello

                    cdef int hello = 4, x = 3, y, z

                """)

    def test_for_loop(self):
        self.t(u"""

                    for x, y, z in f(g(h(34) * 2) + 23):

                        print(x, y, z)

                    else:

                        print(43)

                """)
        self.t(u"""

                    for abc in (1, 2, 3):

                        print(x, y, z)

                    else:

                        print(43)

                """)

    def test_while_loop(self):
        self.t(u"""

                    while True:

                        while True:

                            while True:

                                continue

                """)

    def test_inplace_assignment(self):
        self.t(u"x += 43")

    def test_cascaded_assignment(self):
        self.t(u"x = y = z = abc = 43")

    def test_attribute(self):
        self.t(u"a.x")

    def test_return_none(self):
        self.t(u"""

                    def f(x, y, z):

                        return

                    cdef f(x, y, z):

                        return

                    def f(x, y, z):

                        return None

                    cdef f(x, y, z):

                        return None

                    def f(x, y, z):

                        return 1234

                    cdef f(x, y, z):

                        return 1234

               """)

if __name__ == "__main__":
    import unittest
    unittest.main()