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

354 lines, 182 significant
1## oils_failures_allowed: 2
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) ("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
67
68# TODO: errors
69
70pp test_ (Bool[Str])
71pp test_ (List[Str, Str])
72pp test_ (Dict[Str])
73
74# I think this means
75# TODO: need very low precedence operation
76#
77# Func[Int, Str : Int]
78# Func[Int, Str -> Int]
79# Func[Int, Str --> Int]
80
81## STDOUT:
82## END
83
84#### runproc
85shopt --set parse_proc parse_at
86
87f() {
88 write -- f "$@"
89}
90proc p {
91 write -- p @ARGV
92}
93runproc f 1 2
94echo status=$?
95
96runproc p 3 4
97echo status=$?
98
99runproc invalid 5 6
100echo status=$?
101
102runproc
103echo status=$?
104
105## STDOUT:
106f
1071
1082
109status=0
110p
1113
1124
113status=0
114status=1
115status=2
116## END
117
118
119#### runproc typed args
120shopt --set parse_brace parse_proc
121
122proc p {
123 echo 'hi from p'
124}
125
126# The block is ignored for now
127runproc p {
128 echo myblock
129}
130echo
131
132proc ty (w; t; n; block) {
133 echo 'ty'
134 pp test_ (w)
135 pp test_ (t)
136 pp test_ (n)
137 echo $[type(block)]
138}
139
140ty a (42; n=99; ^(echo ty))
141echo
142
143runproc ty a (42; n=99; ^(echo ty))
144echo
145
146runproc ty a (42; n=99) {
147 echo 'ty gets literal'
148}
149
150# TODO: Command vs. Block vs. Literal Block should be unified
151
152## STDOUT:
153hi from p
154
155ty
156(Str) "a"
157(Int) 42
158(Int) 99
159Command
160
161ty
162(Str) "a"
163(Int) 42
164(Int) 99
165Command
166
167ty
168(Str) "a"
169(Int) 42
170(Int) 99
171Command
172## END
173
174
175#### pp asdl_
176
177shopt -s ysh:upgrade
178
179redir >out.txt {
180 x=42
181 setvar y = {foo: x}
182
183 pp asdl_ (x)
184 pp asdl_ (y)
185
186 # TODO, this might be nice?
187 # pp asdl_ (x, y)
188}
189
190# Two lines with value.Str
191grep -n -o value.Str out.txt
192
193# Dict should have an address
194#grep -n -o 'Dict 0x' out.txt
195
196#cat out.txt
197
198## STDOUT:
1991:value.Str
2002:value.Str
201## END
202
203#### pp asdl_ can handle an object cycle
204
205shopt -s ysh:upgrade
206
207var d = {}
208setvar d.cycle = d
209
210pp test_ (d) | fgrep -o '{"cycle":'
211
212pp asdl_ (d) | fgrep -o 'cycle ...'
213
214## STDOUT:
215{"cycle":
216cycle ...
217## END
218
219
220#### pp gc-stats_
221
222pp gc-stats_
223
224## STDOUT:
225## END
226
227
228#### pp cell_
229x=42
230
231pp cell_ x
232echo status=$?
233
234pp -- cell_ x
235echo status=$?
236
237pp cell_ nonexistent
238echo status=$?
239## STDOUT:
240x = (Cell exported:F readonly:F nameref:F val:(value.Str s:42))
241status=0
242x = (Cell exported:F readonly:F nameref:F val:(value.Str s:42))
243status=0
244status=1
245## END
246
247#### pp cell_ on indexed array with hole
248declare -a array
249array[3]=42
250pp cell_ array
251## STDOUT:
252array = (Cell exported:F readonly:F nameref:F val:(value.BashArray strs:[_ _ _ 42]))
253## END
254
255
256#### pp proc
257shopt --set ysh:upgrade
258
259# This has to be a separate file because sh_spec.py strips comments!
260. $REPO_ROOT/spec/testdata/doc-comments.sh
261
262pp proc
263echo ---
264
265# print one
266pp proc f
267
268## STDOUT:
269proc_name doc_comment
270f "doc ' comment with \" quotes"
271g ""
272myproc "YSH-style proc"
273"true" "Special quoting rule"
274---
275proc_name doc_comment
276f "doc ' comment with \" quotes"
277## END
278
279#### pp (x) and pp [x] quote code
280
281pp (42)
282
283shopt --set ysh:upgrade
284
285pp [42] | sed 's/0x[a-f0-9]\+/[replaced]/'
286
287## STDOUT:
288
289 pp (42)
290 ^
291[ stdin ]:1: (Int) 42
292
293 pp [42] | sed 's/0x[a-f0-9]\+/[replaced]/'
294 ^
295[ stdin ]:5: <Expr [replaced]>
296## END
297
298#### pp test_ supports BashArray, BashAssoc
299
300declare -a array=(a b c)
301pp test_ (array)
302
303array[5]=z
304pp test_ (array)
305
306declare -A assoc=([k]=v [k2]=v2)
307pp test_ (assoc)
308
309# I think assoc arrays can never null / unset
310
311assoc['k3']=
312pp test_ (assoc)
313
314## STDOUT:
315{"type":"BashArray","data":{"0":"a","1":"b","2":"c"}}
316{"type":"BashArray","data":{"0":"a","1":"b","2":"c","5":"z"}}
317{"type":"BashAssoc","data":{"k":"v","k2":"v2"}}
318{"type":"BashAssoc","data":{"k":"v","k2":"v2","k3":""}}
319## END
320
321#### pp value (x) is like = keyword
322
323shopt --set ysh:upgrade
324source $LIB_YSH/list.ysh
325
326# It can be piped!
327
328pp value ('foo') | cat
329
330pp value ("isn't this sq") | cat
331
332pp value ('"dq $myvar"') | cat
333
334pp value (r'\ backslash \\') | cat
335
336pp value (u'one \t two \n') | cat
337
338# Without a terminal, default width is 80
339pp value (repeat([123], 40)) | cat
340
341## STDOUT:
342(Str) 'foo'
343(Str) b'isn\'t this sq'
344(Str) '"dq $myvar"'
345(Str) b'\\ backslash \\\\'
346(Str) b'one \t two \n'
347(List)
348[
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, 123, 123, 123, 123, 123,
351 123, 123, 123, 123, 123, 123, 123, 123, 123, 123
352]
353## END
354