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

254 lines, 133 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 line (w)
54 pp line (t)
55 pp line (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 line (d) | fgrep -o '{"cycle":'
130
131pp asdl (d) | fgrep -o 'cycle ...'
132
133## STDOUT:
134{"cycle":
135cycle ...
136## END
137
138#### pp line supports BashArray, BashAssoc
139
140declare -a array=(a b c)
141pp line (array)
142
143array[5]=z
144pp line (array)
145
146declare -A assoc=([k]=v [k2]=v2)
147pp line (assoc)
148
149# I think assoc arrays can never null / unset
150
151assoc['k3']=
152pp line (assoc)
153
154## STDOUT:
155(BashArray) ["a","b","c"]
156(BashArray) ["a","b","c",null,null,"z"]
157(BashAssoc) {"k":"v","k2":"v2"}
158(BashAssoc) {"k":"v","k2":"v2","k3":""}
159## END
160
161
162#### pp gc-stats
163
164pp gc-stats
165
166## STDOUT:
167## END
168
169
170#### pp cell
171x=42
172
173pp cell x
174echo status=$?
175
176pp -- cell :x
177echo status=$?
178
179pp cell nonexistent
180echo status=$?
181## STDOUT:
182x = (Cell exported:F readonly:F nameref:F val:(value.Str s:42))
183status=0
184x = (Cell exported:F readonly:F nameref:F val:(value.Str s:42))
185status=0
186status=1
187## END
188
189#### pp cell on indexed array with hole
190declare -a array
191array[3]=42
192pp cell array
193## STDOUT:
194array = (Cell exported:F readonly:F nameref:F val:(value.BashArray strs:[_ _ _ 42]))
195## END
196
197
198#### pp proc
199shopt --set ysh:upgrade
200
201# This has to be a separate file because sh_spec.py strips comments!
202. $REPO_ROOT/spec/testdata/doc-comments.sh
203
204pp proc
205echo ---
206
207# print one
208pp proc f
209
210## STDOUT:
211proc_name doc_comment
212f "doc ' comment with \" quotes"
213g ""
214myproc "YSH-style proc"
215"true" "Special quoting rule"
216---
217proc_name doc_comment
218f "doc ' comment with \" quotes"
219## END
220
221
222#### pp (x) is like = keyword
223
224shopt --set ysh:upgrade
225source $LIB_YSH/list.ysh
226
227# It can be piped!
228
229pp ('foo') | cat
230
231pp ("isn't this sq") | cat
232
233pp ('"dq $myvar"') | cat
234
235pp (r'\ backslash \\') | cat
236
237pp (u'one \t two \n') | cat
238
239# Without a terminal, default width is 80
240pp (repeat([123], 40)) | cat
241
242## STDOUT:
243(Str) 'foo'
244(Str) b'isn\'t this sq'
245(Str) '"dq $myvar"'
246(Str) b'\\ backslash \\\\'
247(Str) b'one \t two \n'
248(List)
249[
250 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,
251 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,
252 123, 123, 123, 123, 123, 123, 123, 123, 123, 123
253]
254## END