1 ## oils_failures_allowed: 2
2 ## our_shell: ysh
3
4 #### join()
5 var x = :|a b 'c d'|
6
7 var y = join(x)
8 argv.py $y
9
10 var z = join(x, ":")
11 argv.py $z
12 ## STDOUT:
13 ['abc d']
14 ['a:b:c d']
15 ## END
16
17 #### @[split(x)] respects IFS
18 setvar IFS = ":"
19 var x = "one:two:three"
20 argv.py @[split(x)]
21 ## STDOUT:
22 ['one', 'two', 'three']
23 ## END
24
25 #### @[maybe(x)]
26 setvar empty = ''
27 setvar x = 'X'
28 argv.py a @[maybe(empty)] @[maybe(x)] b
29
30 setvar n = null
31 argv.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
41 setvar marray = :||
42 argv.py a @[maybe(marray)] b
43 echo done
44 ## status: 3
45 ## STDOUT:
46 ## END
47
48 #### split() on invalid type is fatal error
49 var myarray = :| --all --long |
50 write -- @[myarray]
51 write -- @[split(myarray)]
52 ## status: 3
53 ## STDOUT:
54 --all
55 --long
56 ## END
57
58 #### @[glob(x)]
59
60 # empty glob
61 write -- A @[glob('__nope__')] B
62 echo ___
63
64 touch -- a.z b.z -.z
65 write -- @[glob('?.z')]
66 echo ___
67
68 # add it back
69 shopt -s dashglob
70 write -- @[glob('?.z')]
71
72 ## STDOUT:
73 A
74 B
75 ___
76 a.z
77 b.z
78 ___
79 -.z
80 a.z
81 b.z
82 ## END
83
84 #### shSplit() respects IFS
85
86 var s = ' aa a bb b '
87
88 argv.py @[shSplit(s)]
89
90 setvar IFS = 'a'
91
92 argv.py @[shSplit(s)]
93
94 setvar IFS = 'b'
95
96 argv.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
109 proc 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
122 proc main {
123 var not_global_or_local = 42
124 my-proc
125 }
126
127 var g = 'global'
128
129 main
130
131 ## STDOUT:
132 g=global
133 mylocal=43
134 mylocal=43
135 not_global_or_local=null
136 ## END
137
138 #### ASCII upper() lower()
139
140 var x = 'abc-'
141 var y = 'XYZ!'
142
143 echo $x
144 echo $y
145 echo
146
147 echo $[x => upper()]
148 echo $[x => lower()]
149 echo $[y => upper()]
150 echo $[y => lower()]
151
152 ## STDOUT:
153 abc-
154 XYZ!
155
156 ABC-
157 abc-
158 XYZ!
159 xyz!
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
168 var x = u'\u{C0}\u{C8}' # upper grave
169 var y = u'\u{E1}\u{E9}' # lower acute
170
171 echo $x
172 echo $y
173 echo
174
175 echo $[x => upper()]
176 echo $[x => lower()]
177 echo $[y => upper()]
178 echo $[y => lower()]
179
180 ## STDOUT:
181 ÀÈ
182 áé
183 ## END
184
185 #### getFrame()
186
187 var fr = vm.getFrame(0)
188 pp test_ (fr)
189 var d = dict(fr)
190 pp test_ (d.ARGV)
191 echo
192
193 proc p1 {
194 var p1_var = 'x'
195 p2
196 }
197
198 proc 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
217 p1
218
219 var fr = vm.getFrame(99) # fails
220
221 ## status: 3
222 ## STDOUT:
223 <Frame>
224 (List) []
225
226 p2 frame -1
227 <Frame>
228 (Dict) {"ARGV":[],"fr":<Frame>}
229 (List) ["ARGV","fr"]
230
231 p2 frame -2
232 <Frame>
233 (List) ["ARGV","p1_var"]
234
235 ## END
236
237
238 #### bindFrame()
239
240 var frag = ^(echo $i)
241
242 # TODO: should be fragment
243 pp test_ (frag)
244
245 var cmd = bindFrame(frag, getFrame(0))
246
247 pp test_ (cmd)
248
249 ## STDOUT:
250 ## END