OILS / spec / ysh-object.test.sh View on Github | oilshell.org

198 lines, 88 significant
1## our_shell: ysh
2## oils_failures_allowed: 2
3
4#### Object() creates prototype chain
5
6func Rect_area(this) {
7 return (this.x * this.y)
8}
9
10var Rect = Object(null, {area: Rect_area})
11
12var rect1 = Object(Rect, {x: 3, y: 4})
13var rect2 = Object(Rect, {x: 10, y: 20})
14
15# This could change to show the object?
16# pp test_ (rect)
17
18# TODO: This should be a bound function
19#pp asdl_ (rect)
20#pp (rect.area)
21#pp (rect->area)
22
23var area1 = rect1.area()
24var area2 = rect2.area()
25
26pp test_ ([rect1.x, rect1.y])
27echo "area1 = $area1"
28
29pp test_ ([rect2.x, rect2.y])
30echo "area2 = $area2"
31
32#pp test_ (rect1.nonexistent)
33
34## STDOUT:
35(List) [3,4]
36area1 = 12
37(List) [10,20]
38area2 = 200
39## END
40
41#### prototype()
42
43func Rect_area(this) {
44 return (this.x * this.y)
45}
46
47var Rect = Object(null, {area: Rect_area})
48
49var obj = Object(Rect, {x: 3, y: 4})
50
51pp test_ (prototype(Rect))
52pp test_ (prototype(obj))
53
54## STDOUT:
55(Null) null
56(Obj) {"area":<Func>}
57## END
58
59#### propView()
60
61var obj = Object(null, {x: 3, y: 4})
62var props = propView(obj)
63
64pp test_ (props)
65
66# object can be mutated
67setvar props.x = 99
68
69pp test_ (props)
70
71var e = propView(null) # error
72
73## status: 3
74## STDOUT:
75(Dict) {"x":3,"y":4}
76(Dict) {"x":99,"y":4}
77## END
78
79#### Mutating method lookup with ->
80
81func inc(self, n) {
82 setvar self.i += n
83}
84var Counter_methods = Object(null, {'M/inc': inc})
85
86var c = Object(Counter_methods, {i: 5})
87
88echo $[c.i]
89call c->inc(3)
90echo $[c.i]
91
92## STDOUT:
935
948
95## END
96
97#### Copy to Dict with dict(), and mutate
98
99var rect = Object(null, {x: 3, y: 4})
100var d = dict(rect)
101
102pp test_ (rect)
103pp test_ (d)
104
105# Right now, object attributes aren't mutable! Could change this.
106#
107setvar rect.x = 99
108setvar d.x = 100
109
110pp test_ (rect)
111pp test_ (d)
112## STDOUT:
113(Obj) {"x":3,"y":4}
114(Dict) {"x":3,"y":4}
115(Obj) {"x":99,"y":4}
116(Dict) {"x":100,"y":4}
117## END
118
119#### setvar obj.attr = and += and ...
120
121var rect = Object(null, {x: 3, y: 4})
122pp test_ (rect)
123
124setvar rect.y = 99
125pp test_ (rect)
126
127setvar rect.y += 3
128pp test_ (rect)
129
130setvar rect.x *= 5
131pp test_ (rect)
132
133## STDOUT:
134(Obj) {"x":3,"y":4}
135(Obj) {"x":3,"y":99}
136(Obj) {"x":3,"y":102}
137(Obj) {"x":15,"y":102}
138## END
139
140#### can't encode objects as JSON
141
142var Rect = Object(null, {})
143
144json write (Rect)
145echo 'nope'
146
147## status: 1
148## STDOUT:
149## END
150
151#### pretty printing of cycles
152
153var d = {k: 42}
154setvar d.cycle = d
155
156pp test_ (d)
157
158var o = Object(null, d)
159
160pp test_ (o)
161
162var o2 = Object(o, {z: 99})
163
164pp test_ (o2)
165
166## STDOUT:
167## END
168
169#### Can all builtin methods with s.upper()
170
171var s = 'foo'
172var x = s.upper()
173var y = "--$[x.lower()]"
174
175pp test_ (x)
176pp test_ (y)
177
178## STDOUT:
179(Str) "FOO"
180(Str) "--foo"
181## END
182
183
184#### Dict.keys(d), Dict.values(d), Dict.get(d, key)
185
186var d = {a: 42, b: 99}
187
188pp test_ (Dict.keys(d))
189pp test_ (Dict.values(d))
190
191pp test_ (Dict.get(d, 'key', 'default'))
192
193# mutating methods are OK?
194# call d->inc(x)
195
196## STDOUT:
197## END
198