forked from Distributive-Network/PythonMonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_objects.py
More file actions
150 lines (98 loc) · 2.93 KB
/
test_objects.py
File metadata and controls
150 lines (98 loc) · 2.93 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
import pythonmonkey as pm
def test_eval_pyobjects():
class MyClass:
pass
o = MyClass()
proxy_o = pm.eval("(obj) => { return obj; }")(o)
assert o is proxy_o
def test_eval_pyobjects_subobjects():
class InnerClass:
def __init__(self):
self.c = 2
class OuterClass:
def __init__(self):
self.a = 1
self.b = InnerClass()
o = OuterClass()
assert pm.eval("(obj) => { return obj.a; }")(o) == 1.0
assert pm.eval("(obj) => { return obj.b; }")(o) is o.b
assert pm.eval("(obj) => { return obj.b.c; }")(o) == 2.0
def test_eval_pyobjects_cycle():
class MyClass:
def __init__(self):
self.a = 1
self.b = 2
self.recursive = self
o = MyClass()
assert pm.eval("(obj) => { return obj.a; }")(o) == 1.0
assert pm.eval("(obj) => { return obj.b; }")(o) == 2.0
assert pm.eval("(obj) => { return obj.recursive; }")(o) is o.recursive
def test_eval_pyobjects_proxy_get():
class MyClass:
def __init__(self):
self.a = 42
o = MyClass()
assert pm.eval("(obj) => { return obj.a}")(o) == 42.0
def test_eval_pyobjects_proxy_set():
class MyClass:
def __init__(self):
self.a = 42
o = MyClass()
pm.eval("(obj) => { obj.b = 43; }")(o)
assert o.b == 43.0
def test_eval_pyobjects_proxy_keys():
class MyClass:
def __init__(self):
self.a = 42
o = MyClass()
assert pm.eval("(obj) => { return Object.keys(obj)[0]; }")(o) == 'a'
def test_eval_pyobjects_proxy_delete():
class MyClass:
def __init__(self):
self.a = 42
o = MyClass()
pm.eval("(obj) => { delete obj.a; }")(o)
assert not hasattr(o, 'a')
def test_eval_pyobjects_proxy_has():
class MyClass:
def __init__(self):
self.a = 42
o = MyClass()
assert pm.eval("(obj) => { return 'a' in obj; }")(o)
def test_eval_pyobjects_proxy_not_extensible():
class MyClass:
def __init__(self):
self.a = 42
o = MyClass()
assert not pm.eval("(o) => Object.isExtensible(o)")(o)
assert pm.eval("(o) => Object.preventExtensions(o) === o")(o)
def test_instanceof_pyobject():
class MyClass:
def __init__(self):
self.a = 42
o = MyClass()
assert pm.eval("(obj) => { return obj instanceof Object; }")(o)
def test_pyobjects_not_instanceof_string():
class MyClass:
def __init__(self):
self.a = 42
o = MyClass()
assert not pm.eval("(obj) => { return obj instanceof String; }")(o)
def test_pyobjects_valueOf():
class MyClass:
def __init__(self):
self.a = 42
o = MyClass()
assert o is pm.eval("(obj) => { return obj.valueOf(); }")(o)
def test_pyobjects_toString():
class MyClass:
def __init__(self):
self.a = 42
o = MyClass()
assert '[object Object]' == pm.eval("(obj) => { return obj.toString(); }")(o)
def test_pyobjects_toLocaleString():
class MyClass:
def __init__(self):
self.a = 42
o = MyClass()
assert '[object Object]' == pm.eval("(obj) => { return obj.toLocaleString(); }")(o)