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

109 lines, 42 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#### can't encode objects as JSON
42
43var Rect = Object(null, {})
44
45json write (Rect)
46echo 'nope'
47
48## status: 1
49## STDOUT:
50## END
51
52#### pretty printing of cycles
53
54var d = {k: 42}
55setvar d.cycle = d
56
57pp test_ (d)
58
59var o = Object(null, d)
60
61pp test_ (o)
62
63var o2 = Object(o, {z: 99})
64
65pp test_ (o2)
66
67## STDOUT:
68## END
69
70#### setvar obj.attr
71
72func Rect_area(this) {
73 return (this.x * this.y)
74}
75
76var Rect = Object(null, {area: Rect_area})
77
78var rect1 = Object(Rect, {x: 3, y: 4})
79
80pp test_ (rect1)
81
82# Right now it's not mutable
83setvar rect1.x = 99
84
85pp test_ (rect1)
86
87## STDOUT:
88(Obj) {"x":3,"y":4} ==> {"area":<Func>}
89## END
90
91#### Can all builtin methods with s.upper()
92
93var s = 'foo'
94var x = s.upper()
95var y = "--$[x.lower()]"
96
97pp test_ (x)
98pp test_ (y)
99
100# TODO:
101# keys(d) values(d) instead of d.keys() and d.values()
102#
103# mutating methods are OK?
104# call d->inc(x)
105
106## STDOUT:
107(Str) "FOO"
108(Str) "--foo"
109## END