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

356 lines, 185 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_ (id(b) === 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) ("new":<BuiltinFunc>,"name":"Obj") --> ("__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
179shopt -s ysh:upgrade
180
181redir >out.txt {
182 x=42
183 setvar y = {foo: x}
184
185 pp asdl_ (x)
186 pp asdl_ (y)
187
188 # TODO, this might be nice?
189 # pp asdl_ (x, y)
190}
191
192# Two lines with value.Str
193grep -n -o value.Str out.txt
194
195# Dict should have an address
196#grep -n -o 'Dict 0x' out.txt
197
198#cat out.txt
199
200## STDOUT:
2011:value.Str
2022:value.Str
203## END
204
205#### pp asdl_ can handle an object cycle
206
207shopt -s ysh:upgrade
208
209var d = {}
210setvar d.cycle = d
211
212pp test_ (d) | fgrep -o '{"cycle":'
213
214pp asdl_ (d) | fgrep -o 'cycle ...'
215
216## STDOUT:
217{"cycle":
218cycle ...
219## END
220
221
222#### pp gc-stats_
223
224pp gc-stats_
225
226## STDOUT:
227## END
228
229
230#### pp cell_
231x=42
232
233pp cell_ x
234echo status=$?
235
236pp -- cell_ x
237echo status=$?
238
239pp cell_ nonexistent
240echo status=$?
241## STDOUT:
242x = (Cell exported:F readonly:F nameref:F val:(value.Str s:42))
243status=0
244x = (Cell exported:F readonly:F nameref:F val:(value.Str s:42))
245status=0
246status=1
247## END
248
249#### pp cell_ on indexed array with hole
250declare -a array
251array[3]=42
252pp cell_ array
253## STDOUT:
254array = (Cell exported:F readonly:F nameref:F val:(value.BashArray strs:[_ _ _ 42]))
255## END
256
257
258#### pp proc
259shopt --set ysh:upgrade
260
261# This has to be a separate file because sh_spec.py strips comments!
262. $REPO_ROOT/spec/testdata/doc-comments.sh
263
264pp proc
265echo ---
266
267# print one
268pp proc f
269
270## STDOUT:
271proc_name doc_comment
272f "doc ' comment with \" quotes"
273g ""
274myproc "YSH-style proc"
275"true" "Special quoting rule"
276---
277proc_name doc_comment
278f "doc ' comment with \" quotes"
279## END
280
281#### pp (x) and pp [x] quote code
282
283pp (42)
284
285shopt --set ysh:upgrade
286
287pp [42] | sed 's/0x[a-f0-9]\+/[replaced]/'
288
289## STDOUT:
290
291 pp (42)
292 ^
293[ stdin ]:1: (Int) 42
294
295 pp [42] | sed 's/0x[a-f0-9]\+/[replaced]/'
296 ^
297[ stdin ]:5: <Expr [replaced]>
298## END
299
300#### pp test_ supports BashArray, BashAssoc
301
302declare -a array=(a b c)
303pp test_ (array)
304
305array[5]=z
306pp test_ (array)
307
308declare -A assoc=([k]=v [k2]=v2)
309pp test_ (assoc)
310
311# I think assoc arrays can never null / unset
312
313assoc['k3']=
314pp test_ (assoc)
315
316## STDOUT:
317{"type":"BashArray","data":{"0":"a","1":"b","2":"c"}}
318{"type":"BashArray","data":{"0":"a","1":"b","2":"c","5":"z"}}
319{"type":"BashAssoc","data":{"k":"v","k2":"v2"}}
320{"type":"BashAssoc","data":{"k":"v","k2":"v2","k3":""}}
321## END
322
323#### pp value (x) is like = keyword
324
325shopt --set ysh:upgrade
326source $LIB_YSH/list.ysh
327
328# It can be piped!
329
330pp value ('foo') | cat
331
332pp value ("isn't this sq") | cat
333
334pp value ('"dq $myvar"') | cat
335
336pp value (r'\ backslash \\') | cat
337
338pp value (u'one \t two \n') | cat
339
340# Without a terminal, default width is 80
341pp value (repeat([123], 40)) | cat
342
343## STDOUT:
344(Str) 'foo'
345(Str) b'isn\'t this sq'
346(Str) '"dq $myvar"'
347(Str) b'\\ backslash \\\\'
348(Str) b'one \t two \n'
349(List)
350[
351 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,
352 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,
353 123, 123, 123, 123, 123, 123, 123, 123, 123, 123
354]
355## END
356