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 #### attributes()
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 #### Copy to Dict with dict(), and mutate
80
81 var rect = Object(null, {x: 3, y: 4})
82 var d = dict(rect)
83
84 pp test_ (rect)
85 pp test_ (d)
86
87 # Right now, object attributes aren't mutable! Could change this.
88 #
89 setvar rect.x = 99
90 setvar d.x = 100
91
92 pp test_ (rect)
93 pp test_ (d)
94 ## STDOUT:
95 (Obj) {"x":3,"y":4}
96 (Dict) {"x":3,"y":4}
97 (Obj) {"x":99,"y":4}
98 (Dict) {"x":100,"y":4}
99 ## END
100
101 #### setvar obj.attr = and += and ...
102
103 var rect = Object(null, {x: 3, y: 4})
104 pp test_ (rect)
105
106 setvar rect.y = 99
107 pp test_ (rect)
108
109 setvar rect.y += 3
110 pp test_ (rect)
111
112 setvar rect.x *= 5
113 pp test_ (rect)
114
115 ## STDOUT:
116 (Obj) {"x":3,"y":4}
117 (Obj) {"x":3,"y":99}
118 (Obj) {"x":3,"y":102}
119 (Obj) {"x":15,"y":102}
120 ## END
121
122 #### can't encode objects as JSON
123
124 var Rect = Object(null, {})
125
126 json write (Rect)
127 echo 'nope'
128
129 ## status: 1
130 ## STDOUT:
131 ## END
132
133 #### pretty printing of cycles
134
135 var d = {k: 42}
136 setvar d.cycle = d
137
138 pp test_ (d)
139
140 var o = Object(null, d)
141
142 pp test_ (o)
143
144 var o2 = Object(o, {z: 99})
145
146 pp test_ (o2)
147
148 ## STDOUT:
149 ## END
150
151 #### Can all builtin methods with s.upper()
152
153 var s = 'foo'
154 var x = s.upper()
155 var y = "--$[x.lower()]"
156
157 pp test_ (x)
158 pp test_ (y)
159
160 ## STDOUT:
161 (Str) "FOO"
162 (Str) "--foo"
163 ## END
164
165
166 #### Dict.keys(d), Dict.values(d), Dict.get(d, key)
167
168 var d = {a: 42, b: 99}
169
170 pp test_ (Dict.keys(d))
171 pp test_ (Dict.values(d))
172
173 pp test_ (Dict.get(d, 'key', 'default'))
174
175 # mutating methods are OK?
176 # call d->inc(x)
177
178 ## STDOUT:
179 ## END
180