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

250 lines, 134 significant
1## oils_failures_allowed: 2
2## our_shell: ysh
3
4#### join()
5var x = :|a b 'c d'|
6
7var y = join(x)
8argv.py $y
9
10var z = join(x, ":")
11argv.py $z
12## STDOUT:
13['abc d']
14['a:b:c d']
15## END
16
17#### @[split(x)] respects IFS
18setvar IFS = ":"
19var x = "one:two:three"
20argv.py @[split(x)]
21## STDOUT:
22['one', 'two', 'three']
23## END
24
25#### @[maybe(x)]
26setvar empty = ''
27setvar x = 'X'
28argv.py a @[maybe(empty)] @[maybe(x)] b
29
30setvar n = null
31argv.py a @[maybe(n)] b
32
33## STDOUT:
34['a', 'X', 'b']
35['a', 'b']
36## END
37
38#### maybe() on invalid type is fatal error
39
40# not allowed
41setvar marray = :||
42argv.py a @[maybe(marray)] b
43echo done
44## status: 3
45## STDOUT:
46## END
47
48#### split() on invalid type is fatal error
49var myarray = :| --all --long |
50write -- @[myarray]
51write -- @[split(myarray)]
52## status: 3
53## STDOUT:
54--all
55--long
56## END
57
58#### @[glob(x)]
59
60# empty glob
61write -- A @[glob('__nope__')] B
62echo ___
63
64touch -- a.z b.z -.z
65write -- @[glob('?.z')]
66echo ___
67
68# add it back
69shopt -s dashglob
70write -- @[glob('?.z')]
71
72## STDOUT:
73A
74B
75___
76a.z
77b.z
78___
79-.z
80a.z
81b.z
82## END
83
84#### shSplit() respects IFS
85
86var s = ' aa a bb b '
87
88argv.py @[shSplit(s)]
89
90setvar IFS = 'a'
91
92argv.py @[shSplit(s)]
93
94setvar IFS = 'b'
95
96argv.py @[shSplit(s)]
97
98## STDOUT:
99['aa', 'a', 'bb', 'b']
100[' ', '', ' ', ' bb b ']
101[' aa a ', '', ' ', ' ']
102## END
103
104
105#### getVar() gets global or local vars
106
107# compare with shvarGet(), which does dynamic scope
108
109proc my-proc {
110 var mylocal = 43
111
112 echo g=$[getVar('g')]
113 echo mylocal=$[getVar('mylocal')]
114
115 # the whole purpose is dynamic variable names / dynamic binding
116 var prefix = 'my'
117 echo mylocal=$[getVar(prefix ++ 'local')]
118
119 echo not_global_or_local=$[getVar('not_global_or_local')]
120}
121
122proc main {
123 var not_global_or_local = 42
124 my-proc
125}
126
127var g = 'global'
128
129main
130
131## STDOUT:
132g=global
133mylocal=43
134mylocal=43
135not_global_or_local=null
136## END
137
138#### ASCII upper() lower()
139
140var x = 'abc-'
141var y = 'XYZ!'
142
143echo $x
144echo $y
145echo
146
147echo $[x => upper()]
148echo $[x => lower()]
149echo $[y => upper()]
150echo $[y => lower()]
151
152## STDOUT:
153abc-
154XYZ!
155
156ABC-
157abc-
158XYZ!
159xyz!
160## END
161
162#### Unicode upper() lower()
163
164# Adapted from spec/var-op-bash
165
166# https://www.utf8-chartable.de/unicode-utf8-table.pl
167
168var x = u'\u{C0}\u{C8}' # upper grave
169var y = u'\u{E1}\u{E9}' # lower acute
170
171echo $x
172echo $y
173echo
174
175echo $[x => upper()]
176echo $[x => lower()]
177echo $[y => upper()]
178echo $[y => lower()]
179
180## STDOUT:
181ÀÈ
182áé
183## END
184
185#### getFrame()
186
187var fr = vm.getFrame(0)
188pp test_ (fr)
189var d = dict(fr)
190pp test_ (d.ARGV)
191echo
192
193proc p1 {
194 var p1_var = 'x'
195 p2
196}
197
198proc p2 {
199 echo 'p2 frame -1'
200 var fr = vm.getFrame(-1)
201 var d = dict(fr)
202
203 pp test_ (fr)
204 pp test_ (d)
205 pp test_ (keys(d))
206 echo
207
208 echo 'p2 frame -2'
209 setvar fr = vm.getFrame(-2)
210 setvar d = dict(fr)
211
212 pp test_ (fr)
213 pp test_ (keys(d))
214 echo
215}
216
217p1
218
219var fr = vm.getFrame(99) # fails
220
221## status: 3
222## STDOUT:
223<Frame>
224(List) []
225
226p2 frame -1
227<Frame>
228(Dict) {"ARGV":[],"fr":<Frame>}
229(List) ["ARGV","fr"]
230
231p2 frame -2
232<Frame>
233(List) ["ARGV","p1_var"]
234
235## END
236
237
238#### bindFrame()
239
240var frag = ^(echo $i)
241
242# TODO: should be fragment
243pp test_ (frag)
244
245var cmd = bindFrame(frag, getFrame(0))
246
247pp test_ (cmd)
248
249## STDOUT:
250## END