OILS / spec / ysh-builtin-meta.test.sh View on Github | oils.pub

353 lines, 191 significant
1## oils_failures_allowed: 1
2
3#### Type objects Bool, Int, Float, etc.
4
5pp test_ (Bool)
6pp test_ (Int)
7pp test_ (Float)
8pp test_ (Str)
9
10pp test_ (List)
11pp test_ (Dict)
12pp test_ (Obj)
13echo
14
15var b = Bool
16
17pp test_ (b is Bool)
18
19# Objects don't have equality, only identity
20#pp test_ (b === Bool)
21
22pp test_ (vm.id(b) === vm.id(Bool))
23
24## STDOUT:
25(Obj) ("name":"Bool") --> ("__index__":<BuiltinFunc>)
26(Obj) ("name":"Int") --> ("__index__":<BuiltinFunc>)
27(Obj) ("name":"Float") --> ("__index__":<BuiltinFunc>)
28(Obj) ("name":"Str") --> ("__index__":<BuiltinFunc>)
29(Obj) ("name":"List") --> ("__index__":<BuiltinFunc>)
30(Obj) ("name":"Dict") --> ("__index__":<BuiltinFunc>)
31(Obj) ("name":"Obj","new":<BuiltinFunc>) --> ("__index__":<BuiltinFunc>)
32
33(Bool) true
34(Bool) true
35## END
36
37#### Parameterized types - List[Int], Dict[Str, Int]
38shopt -s ysh:upgrade
39
40var li = List[Int]
41var dsi = Dict[Str, Int]
42
43pp test_ (li)
44pp test_ (dsi)
45
46# test identity
47for i in a b c {
48 assert [li is List[Int]]
49 assert [dsi is Dict[Str,Int]]
50}
51
52assert [li is not dsi]
53
54var lli = List[li]
55pp test_ (lli)
56
57pp test_ (Dict[Str, List[Int]])
58
59## STDOUT:
60(Obj) ("unique_id":"List[Int]")
61(Obj) ("unique_id":"Dict[Str,Int]")
62(Obj) ("unique_id":"List[List[Int]]")
63(Obj) ("unique_id":"Dict[Str,List[Int]]")
64## END
65
66#### Errors for parameterized types
67shopt -s ysh:upgrade
68
69# more in test/ysh-runtime-errors.sh test-obj-methods
70try {
71 pp test_ (Bool[Str])
72}
73echo $[_error.code]
74
75# I think this means
76# TODO: need very low precedence operation
77#
78# Func[Int, Str : Int]
79# Func[Int, Str -> Int]
80# Func[Int, Str --> Int]
81
82## STDOUT:
833
84## END
85
86#### runproc
87shopt --set parse_proc parse_at
88
89f() {
90 write -- f "$@"
91}
92proc p {
93 write -- p @ARGV
94}
95runproc f 1 2
96echo status=$?
97
98runproc p 3 4
99echo status=$?
100
101runproc invalid 5 6
102echo status=$?
103
104runproc
105echo status=$?
106
107## STDOUT:
108f
1091
1102
111status=0
112p
1133
1144
115status=0
116status=1
117status=2
118## END
119
120
121#### runproc typed args
122shopt --set parse_brace parse_proc
123
124proc p {
125 echo 'hi from p'
126}
127
128# The block is ignored for now
129runproc p {
130 echo myblock
131}
132echo
133
134proc ty (w; t; n; block) {
135 echo 'ty'
136 pp test_ (w)
137 pp test_ (t)
138 pp test_ (n)
139 echo $[type(block)]
140}
141
142ty a (42; n=99; ^(echo ty))
143echo
144
145runproc ty a (42; n=99; ^(echo ty))
146echo
147
148runproc ty a (42; n=99) {
149 echo 'ty gets literal'
150}
151
152# TODO: Command vs. Block vs. Literal Block should be unified
153
154## STDOUT:
155hi from p
156
157ty
158(Str) "a"
159(Int) 42
160(Int) 99
161Command
162
163ty
164(Str) "a"
165(Int) 42
166(Int) 99
167Command
168
169ty
170(Str) "a"
171(Int) 42
172(Int) 99
173Command
174## END
175
176
177#### pp asdl_
178
179x=42
180setvar y = {foo: x}
181
182pp asdl_ (x)
183pp asdl_ (y)
184
185pp asdl_ (io.stdin)
186
187## STDOUT:
188(value.Str s:42)
189(value.Dict d:{foo (value.Str s:42)})
190(value.Stdin)
191## END
192
193#### pp asdl_ can handle an object cycle
194
195shopt -s ysh:upgrade
196
197var d = {}
198setvar d.cycle = d
199
200pp test_ (d) | fgrep -o '{"cycle":'
201
202pp asdl_ (d) | fgrep -o 'cycle ...'
203
204## STDOUT:
205{"cycle":
206cycle ...
207## END
208
209
210#### pp gc-stats_
211
212pp gc-stats_
213
214## STDOUT:
215## END
216
217
218#### pp cell_
219x=42
220
221pp cell_ x
222echo status=$?
223
224pp -- cell_ x
225echo status=$?
226
227pp cell_ nonexistent
228echo status=$?
229## STDOUT:
230x = (Cell exported:F readonly:F nameref:F val:(value.Str s:42))
231status=0
232x = (Cell exported:F readonly:F nameref:F val:(value.Str s:42))
233status=0
234status=1
235## END
236
237#### pp cell_ on indexed array with hole
238declare -a array
239array[3]=42
240array[5]=99
241pp cell_ array
242
243## STDOUT:
244array = (Cell
245 exported:F
246 readonly:F
247 nameref:F
248 val:(value.BashArray d:{3 42 5 99} max_index:5)
249)
250## END
251
252
253#### pp proc
254shopt --set ysh:upgrade
255
256# This has to be a separate file because sh_spec.py strips comments!
257. $REPO_ROOT/spec/testdata/doc-comments.sh
258
259pp proc
260echo ---
261
262# print one
263pp proc f
264
265## STDOUT:
266proc_name doc_comment
267f "doc ' comment with \" quotes"
268g ""
269myproc "YSH-style proc"
270proc-none ""
271shfunc-none ""
272"true" "Special quoting rule"
273---
274proc_name doc_comment
275f "doc ' comment with \" quotes"
276## END
277
278#### pp (x) and pp [x] quote code
279
280pp (42)
281
282shopt --set ysh:upgrade
283
284pp [42] | sed 's/0x[a-f0-9]\+/[replaced]/'
285
286## STDOUT:
287
288 pp (42)
289 ^
290[ stdin ]:1: (Int) 42
291
292 pp [42] | sed 's/0x[a-f0-9]\+/[replaced]/'
293 ^
294[ stdin ]:5: <Expr [replaced]>
295## END
296
297#### pp test_ supports BashArray, BashAssoc
298
299declare -a array=(a b c)
300pp test_ (array)
301
302array[5]=z
303pp test_ (array)
304
305declare -A assoc=([k]=v [k2]=v2)
306pp test_ (assoc)
307
308# I think assoc arrays can never null / unset
309
310assoc['k3']=
311pp test_ (assoc)
312
313## STDOUT:
314{"type":"BashArray","data":{"0":"a","1":"b","2":"c"}}
315{"type":"BashArray","data":{"0":"a","1":"b","2":"c","5":"z"}}
316{"type":"BashAssoc","data":{"k":"v","k2":"v2"}}
317{"type":"BashAssoc","data":{"k":"v","k2":"v2","k3":""}}
318## END
319
320#### pp value (x) is like = keyword
321
322shopt --set ysh:upgrade
323source $LIB_YSH/list.ysh
324
325# It can be piped!
326
327pp value ('foo') | cat
328
329pp value ("isn't this sq") | cat
330
331pp value ('"dq $myvar"') | cat
332
333pp value (r'\ backslash \\') | cat
334
335pp value (u'one \t two \n') | cat
336
337# Without a terminal, default width is 80
338pp value (repeat([123], 40)) | cat
339
340## STDOUT:
341(Str) 'foo'
342(Str) b'isn\'t this sq'
343(Str) '"dq $myvar"'
344(Str) b'\\ backslash \\\\'
345(Str) b'one \t two \n'
346(List)
347[
348 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,
349 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,
350 123, 123, 123, 123, 123, 123, 123, 123, 123, 123
351]
352## END
353