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

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