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

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