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

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