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

442 lines, 230 significant
1
2#### command sub $(echo hi)
3var x = $(echo hi)
4var y = $(echo '')
5# Make sure we can operate on these values
6echo x=${x:-default} y=${y:-default}
7## STDOUT:
8x=hi y=default
9## END
10
11
12#### Set $HOME using 'var' (i.e. Oil string var in word evaluator)
13var HOME = "foo"
14echo $HOME
15echo ~
16## STDOUT:
17foo
18foo
19## END
20
21#### Use shell var in Oil expression
22x='abc'
23var length = len(x) # length in BYTES, unlike ${#x}
24echo $length
25## STDOUT:
263
27## END
28
29#### Length doesn't apply to BashArray
30x=(a b c)
31x[10]=A
32x[20]=B
33
34# shell style: length is 5
35echo shell=${#x[@]}
36
37# Length could be 20, but we may change the representation to Dict[int, str]
38echo ysh=$[len(x)]
39
40## status: 3
41## STDOUT:
42shell=5
43## END
44
45#### $[len(x)] inside strings
46var s = "abc"
47echo -$[len(s)]-
48
49# This already has a meaning ...
50#echo "-$len(x)-"
51#echo "-${len}(x)-"
52
53## STDOUT:
54-3-
55## END
56
57#### Func with multiple args in multiple contexts
58shopt --set ysh:upgrade # needed for math.ysh
59
60source $LIB_YSH/math.ysh
61
62var x = max(1+2, 3+4)
63echo $x $[max(1+2, 3+4)]
64
65## STDOUT:
667 7
67## END
68
69
70#### Trailing Comma in Param list
71shopt --set ysh:upgrade # needed for math.ysh
72
73source $LIB_YSH/math.ysh
74
75var x = max(1+2, 3+4,)
76echo $x $[max(1+2, 3+4,)]
77
78## STDOUT:
797 7
80## END
81
82#### nested expr contexts
83var s = "123"
84
85# lex_mode_e.ShCommand -> Expr -> ShCommand -> Expr
86var x = $(echo $'len\n' $[len(s)])
87echo $x
88## STDOUT:
89len 3
90## END
91
92
93# TODO:
94# - test keyword args
95# - test splatting *args, **kwargs
96# - Multiline parsing
97#
98# var x = max(
99# 1+2,
100# 3+4,
101# )
102# echo $x $max(
103# 1+2,
104# 3+4,
105# )
106
107#### YSH var used with shell arithmetic
108var w = "3"
109echo lt=$(( w < 4 ))
110echo gt=$(( w > 4 ))
111
112var z = 3
113echo lt=$(( z < 4 ))
114echo gt=$(( z > 4 ))
115## STDOUT:
116lt=1
117gt=0
118lt=1
119gt=0
120## END
121
122#### Parse { var x = 42 }
123shopt -s ysh:upgrade
124g() { var x = 42 }
125
126var x = 1
127f() { var x = 42; setvar x = 43 }
128f
129echo x=$x
130## STDOUT:
131x=1
132## END
133
134#### double quoted
135var foo = "bar"
136var x = "-$foo-${foo}-${undef:-default}-"
137echo $x
138## STDOUT:
139-bar-bar-default-
140## END
141
142#### double quoted respects strict_array
143shopt -s strict:all
144declare -a a=(one two three)
145var x = "-${a[@]}-"
146echo $x
147## status: 1
148## stdout-json: ""
149
150#### simple var sub $name $0 $1 $? etc.
151( exit 42 )
152var status = $?
153echo status=$status
154
155set -- a b c
156var one = $1
157var two = $2
158echo $one $two
159
160var named = "$one" # equivalent to 'one'
161echo named=$named
162
163## STDOUT:
164status=42
165a b
166named=a
167## END
168
169#### braced var sub ${x:-default}
170
171# without double quotes
172
173var b = ${foo:-default}
174echo $b
175var c = ${bar:-"-$b-"}
176echo $c
177
178var d = "${bar:-"-$c-"}" # another one
179echo $d
180
181## STDOUT:
182default
183-default-
184--default--
185## END
186
187#### braced var sub respects strict_array
188set -- a b c
189var x = ${undef:-"$@"}
190echo $x
191shopt -s strict_array
192setvar x = ${undef:-"$@"}
193echo $x
194## status: 1
195## STDOUT:
196a b c
197## END
198
199
200#### null / true / false
201shopt -s ysh:upgrade
202var n = null
203if (n) {
204 echo yes
205} else {
206 echo no
207}
208var t = true
209if (t) {
210 echo yes
211} else {
212 echo no
213}
214var f = false
215if (f) {
216 echo yes
217} else {
218 echo no
219}
220## STDOUT:
221no
222yes
223no
224## END
225
226#### multiline dict
227
228# Note: a pair has to be all on one line. We could relax that but there isn't
229# a strong reason to now.
230
231var mydict = { a:1,
232 b: 2,
233}
234echo mydict=$[len(mydict)]
235## STDOUT:
236mydict=2
237## END
238
239#### multiline array and command sub (only here docs disallowed)
240var array = %(
241 one
242 two
243 three
244)
245echo array=$[len(array)]
246
247var comsub = $(
248echo hi
249echo bye
250)
251echo comsub=$[len(comsub)]
252
253## STDOUT:
254array=3
255comsub=6
256## END
257
258#### obj=>method() - remove?
259var s = 'hi'
260
261# TODO: This does a bound method thing we probably don't want
262var s2 = s=>upper()
263echo $s2
264## STDOUT:
265HI
266## END
267
268#### s->upper does NOT work, should be s.upper() or =>
269var s = 'hi'
270var method = s->upper
271echo $method
272## status: 3
273## stdout-json: ""
274
275#### d.key
276var d = {name: 'andy'}
277var x = d.name
278echo $x
279## STDOUT:
280andy
281## END
282
283#### a ++ b for string/list concatenation
284shopt -s parse_brace
285
286var i = 'abc'
287var j = 'de'
288var k = i ++ j
289echo string $k
290
291
292var a = [1, 2]
293var b = [3]
294var c = a ++ b
295echo list len=$[len(c)]
296
297echo ---
298
299try {
300 = 'ab' ++ 3
301}
302echo Str Int $_status
303
304try {
305 = [1, 2] ++ 3
306}
307echo List Int $_status
308
309try {
310 = 3 ++ 'ab'
311}
312echo Int Str $_status
313
314## STDOUT:
315string abcde
316list len=3
317---
318Str Int 3
319List Int 3
320Int Str 3
321## END
322
323#### s ~~ glob and s !~~ glob
324shopt -s ysh:all
325
326if ('foo.py' ~~ '*.py') {
327 echo yes
328}
329if ('foo.py' !~~ '*.sh') {
330 echo no
331}
332## STDOUT:
333yes
334no
335## END
336
337#### Type Errors
338shopt --set parse_brace
339
340# TODO: It might be nice to get a message
341try {
342 var x = {} + []
343}
344echo $_status
345
346try {
347 setvar x = {} + 3
348}
349echo $_status
350
351try {
352 = 'foo' ++ 3
353}
354echo $_status
355
356try {
357 = 'foo' ++ 3
358}
359echo $_status
360
361## STDOUT:
3623
3633
3643
3653
366## END
367
368
369#### can't use ++ on integers
370var x = 12 ++ 3
371echo $x
372## status: 3
373## STDOUT:
374## END
375
376#### can't do mystr ++ mylist
377= ["s"] + "t"
378## status: 3
379## STDOUT:
380## END
381
382
383#### expression literals
384var e = ^[1 + 2]
385
386echo type=$[type(e)]
387echo $[io->evalExpr(e)]
388
389var e = ^[2 < 1]
390echo $[io->evalExpr(e)]
391
392var x = 42
393var e = ^[42 === x and true]
394echo $[io->evalExpr(e)]
395
396var mylist = ^[3, 4]
397pp test_ (io->evalExpr(mylist))
398
399## STDOUT:
400type=Expr
4013
402false
403true
404(List) [3,4]
405## END
406
407#### No list comprehension in ^[]
408
409var mylist = ^[x for x in y]
410pp test_ (io->evalExpr(mylist))
411
412## status: 2
413## STDOUT:
414## END
415
416
417#### expression literals, evaluation failure
418var e = ^[1 / 0]
419call io->evalExpr(e)
420## status: 3
421## STDOUT:
422## END
423
424#### expression literals, lazy evaluation
425var x = 0
426var e = ^[x]
427
428setvar x = 1
429echo result=$[io->evalExpr(e)]
430## STDOUT:
431result=1
432## END
433
434#### expression literals, sugar for strings
435var x = 0
436var e = ^"x is $x"
437
438setvar x = 1
439echo result=$[io->evalExpr(e)]
440## STDOUT:
441result=x is 1
442## END