-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathtest_dicts.py
More file actions
381 lines (272 loc) · 9.83 KB
/
test_dicts.py
File metadata and controls
381 lines (272 loc) · 9.83 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import pythonmonkey as pm
import sys
import subprocess
def test_eval_dicts():
d = {"a": 1}
proxy_d = pm.eval("(dict) => { return dict; }")(d)
assert d is proxy_d
def test_eval_dicts_subdicts():
d = {"a": 1, "b": {"c": 2}}
assert pm.eval("(dict) => { return dict.a; }")(d) == 1.0
assert pm.eval("(dict) => { return dict.b; }")(d) is d["b"]
assert pm.eval("(dict) => { return dict.b.c; }")(d) == 2.0
def test_eval_dicts_cycle():
d: dict = {"a": 1, "b": 2}
d["recursive"] = d
assert pm.eval("(dict) => { return dict.a; }")(d) == 1.0
assert pm.eval("(dict) => { return dict.b; }")(d) == 2.0
assert pm.eval("(dict) => { return dict.recursive; }")(d) is d["recursive"]
def test_eval_objects():
pyObj = pm.eval("Object({a:1.0})")
assert pyObj == {'a': 1.0}
def test_eval_objects_subobjects():
pyObj = pm.eval("Object({a:1.0, b:{c:2.0}})")
assert pyObj['a'] == 1.0
assert pyObj['b'] == {'c': 2.0}
assert pyObj['b']['c'] == 2.0
def test_eval_objects_cycle():
pyObj = pm.eval("Object({a:1.0, b:2.0, recursive: function() { this.recursive = this; return this; }}.recursive())")
assert pyObj['a'] == 1.0
assert pyObj['b'] == 2.0
assert pyObj['recursive'] == pyObj
def test_eval_objects_proxy_get():
f = pm.eval("(obj) => { return obj.a}")
assert f({'a': 42.0}) == 42.0
def test_eval_objects_proxy_set():
f = pm.eval("(obj) => { obj.a = 42.0; return;}")
pyObj = {}
f(pyObj)
assert pyObj['a'] == 42.0
def test_eval_objects_proxy_keys():
f = pm.eval("(obj) => { return Object.keys(obj)[0]}")
assert f({'a': 42.0}) == 'a'
def test_eval_objects_proxy_delete():
f = pm.eval("(obj) => { delete obj.a }")
pyObj = {'a': 42.0}
f(pyObj)
assert 'a' not in pyObj
def test_eval_objects_proxy_has():
f = pm.eval("(obj) => { return 'a' in obj }")
pyObj = {'a': 42.0}
assert (f(pyObj))
def test_eval_objects_proxy_not_extensible():
assert not pm.eval("(o) => Object.isExtensible(o)")({})
assert not pm.eval("(o) => Object.isExtensible(o)")({"abc": 1})
assert pm.eval("(o) => Object.preventExtensions(o) === o")({})
def test_eval_objects_jsproxy_contains():
a = pm.eval("({'c':5})")
assert 'c' in a
def test_eval_objects_jsproxy_does_not_contain():
a = pm.eval("({'c':5})")
assert not (4 in a)
def test_eval_objects_jsproxy_does_not_contain_value():
a = pm.eval("({'c':5})")
assert not (5 in a)
def test_eval_objects_jsproxy_or():
if sys.version_info[0] >= 3 and sys.version_info[1] >= 9: # | is not implemented for dicts in 3.8 or less
a = pm.eval("({'c':5})")
b = pm.eval("({'d':6})")
c = a | b
assert a == {'c': 5.0}
assert c == {'c': 5.0, 'd': 6.0}
assert b == {'d': 6.0}
def test_eval_objects_jsproxy_or_true_dict_right():
if sys.version_info[0] >= 3 and sys.version_info[1] >= 9: # | is not implemented for dicts in 3.8 or less
a = pm.eval("({'c':5})")
b = {'d': 6.0}
c = a | b
assert a == {'c': 5.0}
assert c == {'c': 5.0, 'd': 6.0}
assert b == {'d': 6.0}
def test_eval_objects_jsproxy_or_true_dict_left():
if sys.version_info[0] >= 3 and sys.version_info[1] >= 9: # | is not implemented for dicts in 3.8 or less
a = {'c': 5}
b = pm.eval("({'d':6})")
c = a | b
assert a == {'c': 5.0}
assert c == {'c': 5.0, 'd': 6.0}
assert b == {'d': 6.0}
def test_eval_objects_jsproxy_inplace_or():
if sys.version_info[0] >= 3 and sys.version_info[1] >= 9: # | is not implemented for dicts in 3.8 or less
a = pm.eval("({'c':5})")
b = pm.eval("({'d':6})")
a |= b
assert a == {'c': 5.0, 'd': 6.0}
assert b == {'d': 6.0}
def test_eval_objects_jsproxy_inplace_or_true_dict_right():
if sys.version_info[0] >= 3 and sys.version_info[1] >= 9: # | is not implemented for dicts in 3.8 or less
a = pm.eval("({'c':5})")
b = {'d': 6.0}
a |= b
assert a == {'c': 5.0, 'd': 6.0}
assert b == {'d': 6.0}
def test_eval_objects_jsproxy_inplace_or_true_dict_left():
if sys.version_info[0] >= 3 and sys.version_info[1] >= 9: # | is not implemented for dicts in 3.8 or less
a = {'c': 5.0}
b = pm.eval("({'d':6})")
a |= b
assert a == {'c': 5.0, 'd': 6.0}
assert b == {'d': 6.0}
assert pm.eval("(o) => Object.preventExtensions(o) === o")({"abc": 1})
def test_instanceof_object():
a = {'c': 5.0}
result = [None]
pm.eval("(result, obj) => {result[0] = obj instanceof Object}")(result, a)
assert result[0]
# iter
def test_eval_objects_proxy_iterate():
obj = pm.eval("({ a: 123, b: 'test' })")
result = []
for i in obj:
result.append(i)
assert result == ['a', 'b']
def test_eval_objects_proxy_min():
obj = pm.eval("({ a: 123, b: 'test' })")
assert min(obj) == 'a'
def test_eval_objects_proxy_max():
obj = pm.eval("({ a: 123, b: 'test' })")
assert max(obj) == 'b'
def test_eval_objects_proxy_repr():
obj = pm.eval("({ a: 123, b: 'test' , c: { d: 1 }})")
obj.e = obj # supporting circular references
expected = "{'a': 123.0, 'b': 'test', 'c': {'d': 1.0}, 'e': {...}}"
assert repr(obj) == expected
assert str(obj) == expected
def test_eval_objects_proxy_dict_conversion():
obj = pm.eval("({ a: 123, b: 'test' , c: { d: 1 }})")
d = dict(obj)
assert type(obj) is not dict # dict subclass
assert type(d) is dict # strict dict
assert repr(d) == "{'a': 123.0, 'b': 'test', 'c': {'d': 1.0}}"
assert str(obj.keys()) == "dict_keys(['a', 'b', 'c'])"
assert obj == d
def test_eval_objects_jsproxy_get():
proxy = pm.eval("({a: 1})")
assert 1.0 == proxy['a']
assert 1.0 == proxy.a
def test_eval_objects_jsproxy_set():
proxy = pm.eval("({a: 1})")
proxy.a = 2.0
assert 2.0 == proxy['a']
proxy['a'] = 3.0
assert 3.0 == proxy.a
proxy.b = 1.0
assert 1.0 == proxy['b']
proxy['b'] = 2.0
assert 2.0 == proxy.b
def test_eval_objects_jsproxy_length():
proxy = pm.eval("({a: 1, b:2})")
assert 2 == len(proxy)
def test_eval_objects_jsproxy_delete():
proxy = pm.eval("({a: 1})")
del proxy.a
assert None is proxy.a
assert None is proxy['a']
def test_eval_objects_jsproxy_compare():
proxy = pm.eval("({a: 1, b:2})")
assert proxy == {'a': 1.0, 'b': 2.0}
def test_eval_objects_jsproxy_contains():
a = pm.eval("({'c':5})")
assert 'c' in a
def test_eval_objects_jsproxy_does_not_contain():
a = pm.eval("({'c':5})")
assert not (4 in a)
def test_eval_objects_jsproxy_does_not_contain_value():
a = pm.eval("({'c':5})")
assert not (5 in a)
def test_eval_objects_jsproxy_or():
if sys.version_info[0] >= 3 and sys.version_info[1] >= 9: # | is not implemented for dicts in 3.8 or less
a = pm.eval("({'c':5})")
b = pm.eval("({'d':6})")
c = a | b
assert a == {'c': 5.0}
assert c == {'c': 5.0, 'd': 6.0}
assert b == {'d': 6.0}
def test_eval_objects_jsproxy_or_true_dict_right():
if sys.version_info[0] >= 3 and sys.version_info[1] >= 9: # | is not implemented for dicts in 3.8 or less
a = pm.eval("({'c':5})")
b = {'d': 6.0}
c = a | b
assert a == {'c': 5.0}
assert c == {'c': 5.0, 'd': 6.0}
assert b == {'d': 6.0}
def test_eval_objects_jsproxy_or_true_dict_left():
if sys.version_info[0] >= 3 and sys.version_info[1] >= 9: # | is not implemented for dicts in 3.8 or less
a = {'c': 5}
b = pm.eval("({'d':6})")
c = a | b
assert a == {'c': 5.0}
assert c == {'c': 5.0, 'd': 6.0}
assert b == {'d': 6.0}
def test_eval_objects_jsproxy_inplace_or():
if sys.version_info[0] >= 3 and sys.version_info[1] >= 9: # | is not implemented for dicts in 3.8 or less
a = pm.eval("({'c':5})")
b = pm.eval("({'d':6})")
a |= b
assert a == {'c': 5.0, 'd': 6.0}
assert b == {'d': 6.0}
def test_eval_objects_jsproxy_inplace_or_true_dict_right():
if sys.version_info[0] >= 3 and sys.version_info[1] >= 9: # | is not implemented for dicts in 3.8 or less
a = pm.eval("({'c':5})")
b = {'d': 6.0}
a |= b
assert a == {'c': 5.0, 'd': 6.0}
assert b == {'d': 6.0}
def test_eval_objects_jsproxy_inplace_or_true_dict_left():
if sys.version_info[0] >= 3 and sys.version_info[1] >= 9: # | is not implemented for dicts in 3.8 or less
a = {'c': 5.0}
b = pm.eval("({'d':6})")
a |= b
assert a == {'c': 5.0, 'd': 6.0}
assert b == {'d': 6.0}
def test_eval_objects_jsproxy_inplace_or_true_dict_left_iterable_right():
if sys.version_info[0] >= 3 and sys.version_info[1] >= 9: # | is not implemented for dicts in 3.8 or less
a = {'c': 5}
a |= [('y', 3), ('z', 0)]
assert a == {'c': 5, 'y': 3, 'z': 0}
def test_update_inplace_or_iterable_wrong_type():
car = pm.eval('({"brand": "Ford","model": "Mustang","year": 1964})')
a = [1, 2]
try:
car |= a
assert (False)
except Exception as e:
assert str(type(e)) == "<class 'TypeError'>"
assert str(e) == "cannot convert dictionary update sequence element #0 to a sequence"
def test_instanceof_object():
items = {'a': 10}
result = [None]
pm.eval("(result, obj) => {result[0] = obj instanceof Object}")(result, items)
assert result[0]
def test_not_instanceof_string():
items = {'a': 10}
result = [None]
pm.eval("(result, obj) => {result[0] = obj instanceof String}")(result, items)
assert not result[0]
# valueOf
def test_valueOf():
items = {'a': 10}
result = [0]
pm.eval("(result, obj) => {result[0] = obj.valueOf()}")(result, items)
assert items == {'a': 10}
assert result[0] is items
# toString
def test_toString():
items = {'a': 10}
result = [0]
pm.eval("(result, obj) => {result[0] = obj.toString()}")(result, items)
assert result[0] == '[object Object]'
# toLocaleString
def test_toLocaleString():
items = {'a': 10}
result = [0]
pm.eval("(result, obj) => {result[0] = obj.toLocaleString()}")(result, items)
assert result[0] == '[object Object]'
# __class__
def test___class__attribute():
items = pm.eval("({'a': 10})")
assert repr(items.__class__) == "<class 'dict'>"
# none value attribute
def test___none__attribute():
a = pm.eval("({'0': 1, '1': 2})")
assert a[2] is None