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

351 lines, 189 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 strs:[_ _ _ 42 _ 99])
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"
270"true" "Special quoting rule"
271---
272proc_name doc_comment
273f "doc ' comment with \" quotes"
274## END
275
276#### pp (x) and pp [x] quote code
277
278pp (42)
279
280shopt --set ysh:upgrade
281
282pp [42] | sed 's/0x[a-f0-9]\+/[replaced]/'
283
284## STDOUT:
285
286 pp (42)
287 ^
288[ stdin ]:1: (Int) 42
289
290 pp [42] | sed 's/0x[a-f0-9]\+/[replaced]/'
291 ^
292[ stdin ]:5: <Expr [replaced]>
293## END
294
295#### pp test_ supports BashArray, BashAssoc
296
297declare -a array=(a b c)
298pp test_ (array)
299
300array[5]=z
301pp test_ (array)
302
303declare -A assoc=([k]=v [k2]=v2)
304pp test_ (assoc)
305
306# I think assoc arrays can never null / unset
307
308assoc['k3']=
309pp test_ (assoc)
310
311## STDOUT:
312{"type":"BashArray","data":{"0":"a","1":"b","2":"c"}}
313{"type":"BashArray","data":{"0":"a","1":"b","2":"c","5":"z"}}
314{"type":"BashAssoc","data":{"k":"v","k2":"v2"}}
315{"type":"BashAssoc","data":{"k":"v","k2":"v2","k3":""}}
316## END
317
318#### pp value (x) is like = keyword
319
320shopt --set ysh:upgrade
321source $LIB_YSH/list.ysh
322
323# It can be piped!
324
325pp value ('foo') | cat
326
327pp value ("isn't this sq") | cat
328
329pp value ('"dq $myvar"') | cat
330
331pp value (r'\ backslash \\') | cat
332
333pp value (u'one \t two \n') | cat
334
335# Without a terminal, default width is 80
336pp value (repeat([123], 40)) | cat
337
338## STDOUT:
339(Str) 'foo'
340(Str) b'isn\'t this sq'
341(Str) '"dq $myvar"'
342(Str) b'\\ backslash \\\\'
343(Str) b'one \t two \n'
344(List)
345[
346 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,
347 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,
348 123, 123, 123, 123, 123, 123, 123, 123, 123, 123
349]
350## END
351