forked from beeware/voc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_methods.py
239 lines (192 loc) · 7.07 KB
/
test_methods.py
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
from unittest import expectedFailure
from ..utils import TranspileTestCase
class MethodTests(TranspileTestCase):
def test_method(self):
self.assertCodeExecution("""
class TestObj:
def myfunc(self, value):
print(value * 3)
return value + 5
obj = TestObj()
print("value =", obj.myfunc(5))
""")
def test_noargs_method(self):
self.assertCodeExecution("""
class TestObj:
def myfunc(self, ):
print('Hello')
return 5
obj = TestObj()
print("value =", obj.myfunc())
""")
def test_void_method(self):
self.assertCodeExecution("""
class TestObj:
def myfunc(self, value):
print(value * 3)
obj = TestObj()
obj.myfunc(5)
""")
def test_inner_function(self):
self.assertCodeExecution("""
class TestObj:
def myfunc(self, value):
print(value * 3)
def myinner(value2):
print(value2 * 4)
return value2 + 6
print("inner value =", myinner(10))
return value + 5
obj = TestObj()
print("outer =", obj.myfunc(5))
""", run_in_function=False)
def test_closure(self):
self.assertCodeExecution("""
class TestObj:
def myfunc(self, value):
print(value * 3)
def myinner(value2):
print(value2 * value)
return value2 + 6
print("inner value =", myinner(10))
return value + 5
obj = TestObj()
print("outer =", obj.myfunc(5))
""")
def test_default_args(self):
self.assertCodeExecution("""
class TestObj:
def myfunc(self, x, y=1, z=2):
print("x =", x)
print("y =", y)
print("z =", z)
obj = TestObj()
obj.myfunc(37)
""", run_in_function=False)
def test_override_some_default_args(self):
self.assertCodeExecution("""
class TestObj:
def myfunc(self, x, y=1, z=2):
print("x =", x)
print("y =", y)
print("z =", z)
obj = TestObj()
obj.myfunc(37, 42)
""", run_in_function=False)
def test_overide_all_default_args(self):
self.assertCodeExecution("""
class TestObj:
def myfunc(self, x, y=1, z=2):
print("x =", x)
print("y =", y)
print("z =", z)
obj = TestObj()
obj.myfunc(37, 42, 99)
""", run_in_function=False)
def test_use_kwargs(self):
self.assertCodeExecution("""
class TestObj:
def myfunc(self, x, y=1, z=2):
print("x =", x)
print("y =", y)
print("z =", z)
obj = TestObj()
obj.myfunc(37, y=42)
""", run_in_function=False)
def test_use_kwargs_non_sequential(self):
self.assertCodeExecution("""
class TestObj:
def myfunc(self, x, y=1, z=2):
print("x =", x)
print("y =", y)
print("z =", z)
obj = TestObj()
obj.myfunc(37, z=42)
""", run_in_function=False)
def test_use_all_kwargs(self):
self.assertCodeExecution("""
class TestObj:
def myfunc(self, x, y=1, z=2):
print("x =", x)
print("y =", y)
print("z =", z)
obj = TestObj()
obj.myfunc(x=37, y=42, z=99)
""", run_in_function=False)
def test_use_all_kwargs_different_order(self):
self.assertCodeExecution("""
class TestObj:
def myfunc(self, x, y=1, z=2):
print("x =", x)
print("y =", y)
print("z =", z)
obj = TestObj()
obj.myfunc(z=99, y=42, x=37)
""", run_in_function=False)
def test_call_method_with_var_args(self):
self.assertCodeExecution("""
class TestObj:
def myfunc(self, *args):
print(args)
return args[0]
obj = TestObj()
print("first arg =", obj.myfunc(1, 2, 3, 4, 5))
""", run_in_function=False)
def test_call_method_with_kw(self):
self.assertCodeExecution("""
class TestObj:
def myfunc(self, **kwargs):
print(kwargs['first'] * 3)
print(kwargs['second'] * 3)
return kwargs['first'] + kwargs['second']
obj = TestObj()
print("values sum =", obj.myfunc(first=1, second=2))
""", run_in_function=False)
def test_call_method_kw(self):
self.assertCodeExecution("""
class TestObj:
def myfunc(self, **kwargs):
print(kwargs['first'] * 3)
print(kwargs['second'] * 3)
return kwargs['first'] + kwargs['second']
obj = TestObj()
values = {'first': 1, 'second': 2}
print("values sum =", obj.myfunc(**values))
""", run_in_function=False)
def test_call_method_var_kw(self):
self.assertCodeExecution("""
class TestObj:
def myfunc(self, *args, **kwargs):
print(kwargs['first'] * 3)
print(kwargs['second'] * 3)
print(args)
return kwargs['first'] + kwargs['second']
obj = TestObj()
values_tuple = (1, 2, 3, 4)
values_dict = {'first': 1, 'second': 2}
print("values sum =", obj.myfunc(*values_tuple, **values_dict))
""", run_in_function=False)
def test_call_method_var(self):
self.assertCodeExecution("""
class TestObj:
def myfunc(self, *args):
print(args)
return len(args)
obj = TestObj()
values_tuple = (1, 2, 3, 4)
print("values count =", obj.myfunc(*values_tuple))
""", run_in_function=False)
@expectedFailure
def test_method_caching_not_visible_in_dict(self):
# Method caching is a performance optimization that should not affect __dict__ inspection
self.assertCodeExecution("""
class TestObj:
def myfunc(self):
print(0)
obj = TestObj()
obj.myfunc()
try:
print(obj.__dict__)
except AttributeError as err:
print(err)
""", run_in_function=False)