File: FunctionTest.py

package info (click to toggle)
pyjamas 0.7~%2Bpre2-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 10,656 kB
  • ctags: 12,331
  • sloc: python: 74,493; php: 805; sh: 291; makefile: 59; xml: 9
file content (215 lines) | stat: -rw-r--r-- 6,168 bytes parent folder | download
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from UnitTest import UnitTest

from ClassTest import PassMeAClass
from ClassTest import ExampleChildClass
from ClassTest import ExampleMultiSuperclassParent1
import Factory2

import imports.decors # must be in this form

class Handler:

    def __init__(self, x):
        self._x = x

    def handle(self, y):
        return self._x is y

def aProcedure():
    x = 1
    if x is 2:
        # a return statement which is not reached
        return "something"
    #this is a comment

def aFunctionWithOnlyDoc():
    """Only a doc string"""

def aFunctionReturningNone():
    return None

def aFunctionReturningParam(param):
    return param

def aFunctionReturningFunction():
    return aFunctionReturningParam

def aFunctionReturningGlobalX1():
    return x

def aFunctionReturningGlobalX2():
    return x

def aFunctionReturningGlobalX3():
    a = x
    return a

def aFunctionReturningLocalX():
    x = 'local test'
    return x

def aFunctionReturningArgX(x):
    return x

x = 'global test'

name = 'mapping-test'
def call(default, arguments, this):
    return (name, default, arguments, this)

def functionDefaults(s = "", l = []):
    n = len(l)
    s = s + "%d" % n
    l.append(n)
    return s, l

class FunctionTest(UnitTest):

    def testLambda(self):
        f = lambda x: x
        self.assertEqual(f(1), 1)
        f = lambda x=1: x
        self.assertEqual(f(), 1)
        self.assertEqual(f(2), 2)

        f = lambda x, y: x*y
        self.assertEqual(f(2,3), 6)
        f = lambda x, y=4: x*y
        self.assertEqual(f(2), 8)
        h = Handler(5)
        f = lambda x: h.handle(x)
        self.assertTrue(f(5))
        self.assertFalse(f(4))

        f = lambda a, b=1, **kw: (a,b,kw)
        v = f(b = 2, c = 3, a = 1)
        self.assertEqual(v[0], 1)
        self.assertEqual(v[1], 2)
        self.assertEqual(v[2]['c'], 3)

        f = lambda a, b=1, *args: (a, b, args)
        v = f(1,2,3,4)
        self.assertEqual(v[2][0], 3)
        self.assertEqual(v[2][1], 4)

    def testProcedure(self):
        self.assertTrue(aFunctionReturningNone() is None,
                        "Function should return None")
        self.assertTrue(aProcedure() is None,
                        "Procedures should always return None")

    def testVariableFunction(self):
        self.assertEqual((aFunctionReturningParam)("foo"), "foo")
        self.assertEqual(aFunctionReturningFunction()("foo"), "foo")

    def testLookup(self):
        expected_result1 = 'global test'
        expected_result2 = 'local test'
        self.assertEqual(aFunctionReturningGlobalX1(), expected_result1)
        self.assertEqual(aFunctionReturningGlobalX2(), expected_result1)
        self.assertEqual(aFunctionReturningGlobalX3(), expected_result1)
        self.assertEqual(aFunctionReturningLocalX(), expected_result2)
        self.assertEqual(aFunctionReturningArgX('test'), 'test')

    def testNameMapping(self):
        r = call(1, 2, 3)
        self.assertEqual(r[0], 'mapping-test')
        self.assertEqual(r[1], 1)
        self.assertEqual(r[2], 2)
        self.assertEqual(r[3], 3)

    def testFactory(self):

        Factory2.gregister("passme", PassMeAClass)
        Factory2.gregister("exchild", ExampleChildClass)
        Factory2.gregister("mscp1", ExampleMultiSuperclassParent1)

        pmc = Factory2.ggetObject("passme")
        self.assertEqual(pmc.foo(), "foo in PassMeAClass")

        try:
            pmc = Factory2.ggetObject("mscp1", 5)
        except:
            self.assertEqual(False, True, "Exception indicates bug in compiler: 'Error: uncaught exception: ExampleMultiSuperclassParent1() arguments after ** must be a dictionary 5'")
        else:
            self.assertEqual(pmc.x, 5)
        try:
            pmc = Factory2.ggetObject("exchild", 5, 7) # 5 is ignored
        except:
            self.assertEqual(False, True, "Exception indicates bug in compiler: 'Error: uncaught exception: ExampleChildClass() arguments after ** must be a dictionary 7'")
        else:
            self.assertEqual(pmc.prop_a, 1)
            self.assertEqual(pmc.prop_b, 7)

    def testSliceFunc(self):
        s = "123 "
        s = s[1:].rstrip()
        self.assertEqual(s, "23")

    def testFunctionDefaults(self):
        s, l = functionDefaults()
        self.assertEqual(s, '0')
        self.assertTrue(l == [0], "First mutable default mismatch")

        s, l = functionDefaults()
        #self.assertEqual(s, '1') # can be enabled when the next line is fixed
        self.assertTrue(l == [0, 1], "Second mutable default mismatch bug #214")

        inittest = 1
        def f(inittest = inittest):
            return inittest
        self.assertEqual(f(), inittest)

    def testFunctionDecorating(self):
        log = []
        def deco1(f):
            def fn(*args, **kwargs):
                log.append("deco1 begin")
                res = f(*args, **kwargs)
                log.append("deco1 end")
                return res
            return fn

        def deco2(f):
            def fn(*args, **kwargs):
                log.append("deco2 begin")
                res = f(*args, **kwargs)
                log.append("deco2 end")
                return res
            return fn

        @deco1
        def fn1(a, b = 0):
            return a, b

        @deco1
        @deco2
        def fn2(a, b = 0):
            return a, b

        res = fn1(1,2)
        self.assertEqual(res[0], 1)
        self.assertEqual(res[1], 2)
        self.assertEqual(len(log), 2)
        self.assertEqual(log[0], "deco1 begin")
        self.assertEqual(log[1], "deco1 end")

        log = []
        res = fn2(a=3)
        self.assertEqual(res[0], 3)
        self.assertEqual(res[1], 0)
        self.assertEqual(len(log), 4)
        self.assertEqual(log[0], "deco1 begin")
        self.assertEqual(log[1], "deco2 begin")
        self.assertEqual(log[2], "deco2 end")
        self.assertEqual(log[3], "deco1 end")

        @imports.decors.othermoduledeco1
        def fn3(x):
            return "b"

        self.assertEqual(fn3("b"), "abc")

    def testTopLevelContionalFunction(self):
        self.assertEqual(imports.conditional_func(), "overridden")