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