OILS / spec / var-op-bash.test.sh View on Github | oilshell.org

310 lines, 167 significant
1## compare_shells: bash
2## oils_failures_allowed: 5
3
4#### Lower Case with , and ,,
5x='ABC DEF'
6echo ${x,}
7echo ${x,,}
8echo empty=${empty,}
9echo empty=${empty,,}
10## STDOUT:
11aBC DEF
12abc def
13empty=
14empty=
15## END
16
17#### Upper Case with ^ and ^^
18x='abc def'
19echo ${x^}
20echo ${x^^}
21echo empty=${empty^}
22echo empty=${empty^^}
23## STDOUT:
24Abc def
25ABC DEF
26empty=
27empty=
28## END
29
30#### Case Folding of Unicode Characters
31
32# https://www.utf8-chartable.de/unicode-utf8-table.pl
33
34x=$'\u00C0\u00C8' # upper grave
35y=$'\u00E1\u00E9' # lower acute
36
37echo u ${x^}
38echo U ${x^^}
39
40echo l ${x,}
41echo L ${x,,}
42
43echo u ${y^}
44echo U ${y^^}
45
46echo l ${y,}
47echo L ${y,,}
48
49## STDOUT:
50u ÀÈ
51U ÀÈ
52l àÈ
53L àè
54u Áé
55U ÁÉ
56l áé
57L áé
58## END
59
60#### Lower Case with constant string (VERY WEIRD)
61x='AAA ABC DEF'
62echo ${x,A}
63echo ${x,,A} # replaces every A only?
64## STDOUT:
65aAA ABC DEF
66aaa aBC DEF
67## END
68
69#### Lower Case glob
70
71# Hm with C.UTF-8, this does no case folding?
72export LC_ALL=en_US.UTF-8
73
74x='ABC DEF'
75echo ${x,[d-f]}
76echo ${x,,[d-f]} # This seems buggy, it doesn't include F?
77## STDOUT:
78ABC DEF
79ABC deF
80## END
81
82#### ${x@u} U l L upper / lower case (bash 5.1 feature)
83
84# https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
85
86x='abc def'
87echo "${x@u}"
88
89# TODO: we need to upgrade the spec tests to bash 5.1 (or bash 5.2 is coming
90# out soon)
91
92## N-I bash status: 1
93## N-I bash STDOUT:
94## END
95
96
97#### ${x@Q}
98x="FOO'BAR spam\"eggs"
99eval "new=${x@Q}"
100test "$x" = "$new" && echo OK
101## STDOUT:
102OK
103## END
104
105#### ${array@Q} and ${array[@]@Q}
106array=(x 'y\nz')
107echo ${array[@]@Q}
108echo ${array@Q}
109echo ${array@Q}
110## STDOUT:
111'x' 'y\nz'
112'x'
113'x'
114## END
115## OK osh STDOUT:
116x $'y\\nz'
117x
118x
119## END
120
121#### ${!prefix@} ${!prefix*} yields sorted array of var names
122ZOO=zoo
123ZIP=zip
124ZOOM='one two'
125Z='three four'
126
127z=lower
128
129argv.py ${!Z*}
130argv.py ${!Z@}
131argv.py "${!Z*}"
132argv.py "${!Z@}"
133for i in 1 2; do argv.py ${!Z*} ; done
134for i in 1 2; do argv.py ${!Z@} ; done
135for i in 1 2; do argv.py "${!Z*}"; done
136for i in 1 2; do argv.py "${!Z@}"; done
137## STDOUT:
138['Z', 'ZIP', 'ZOO', 'ZOOM']
139['Z', 'ZIP', 'ZOO', 'ZOOM']
140['Z ZIP ZOO ZOOM']
141['Z', 'ZIP', 'ZOO', 'ZOOM']
142['Z', 'ZIP', 'ZOO', 'ZOOM']
143['Z', 'ZIP', 'ZOO', 'ZOOM']
144['Z', 'ZIP', 'ZOO', 'ZOOM']
145['Z', 'ZIP', 'ZOO', 'ZOOM']
146['Z ZIP ZOO ZOOM']
147['Z ZIP ZOO ZOOM']
148['Z', 'ZIP', 'ZOO', 'ZOOM']
149['Z', 'ZIP', 'ZOO', 'ZOOM']
150## END
151
152#### ${!prefix@} matches var name (regression)
153hello1=1 hello2=2 hello3=3
154echo ${!hello@}
155hello=()
156echo ${!hello@}
157## STDOUT:
158hello1 hello2 hello3
159hello hello1 hello2 hello3
160## END
161
162#### ${var@a} for attributes
163array=(one two)
164echo ${array@a}
165declare -r array=(one two)
166echo ${array@a}
167declare -rx PYTHONPATH=hi
168echo ${PYTHONPATH@a}
169
170# bash and osh differ here
171#declare -rxn x=z
172#echo ${x@a}
173## STDOUT:
174a
175ar
176rx
177## END
178
179#### ${var@a} error conditions
180echo [${?@a}]
181## STDOUT:
182[]
183## END
184
185#### undef and @P @Q @a
186$SH -c 'echo ${undef@P}'
187echo status=$?
188$SH -c 'echo ${undef@Q}'
189echo status=$?
190$SH -c 'echo ${undef@a}'
191echo status=$?
192## STDOUT:
193
194status=0
195
196status=0
197
198status=0
199## END
200## OK osh STDOUT:
201
202status=0
203''
204status=0
205
206status=0
207## END
208
209
210#### argv array and @P @Q @a
211$SH -c 'echo ${@@P}' dummy a b c
212echo status=$?
213$SH -c 'echo ${@@Q}' dummy a 'b\nc'
214echo status=$?
215$SH -c 'echo ${@@a}' dummy a b c
216echo status=$?
217## STDOUT:
218a b c
219status=0
220'a' 'b\nc'
221status=0
222
223status=0
224## END
225## OK osh STDOUT:
226status=1
227a $'b\\nc'
228status=0
229a
230status=0
231## END
232
233#### assoc array and @P @Q @a
234
235# note: "y z" causes a bug!
236$SH -c 'declare -A A=(["x"]="y"); echo ${A@P} - ${A[@]@P}'
237echo status=$?
238
239# note: "y z" causes a bug!
240$SH -c 'declare -A A=(["x"]="y"); echo ${A@Q} - ${A[@]@Q}'
241echo status=$?
242
243$SH -c 'declare -A A=(["x"]=y); echo ${A@a} - ${A[@]@a}'
244echo status=$?
245## STDOUT:
246- y
247status=0
248- 'y'
249status=0
250A - A
251status=0
252## END
253## OK osh STDOUT:
254status=1
255status=1
256A - A
257status=0
258## END
259
260#### ${!var[@]@X}
261# note: "y z" causes a bug!
262$SH -c 'declare -A A=(["x"]="y"); echo ${!A[@]@P}'
263if test $? -ne 0; then echo fail; fi
264
265# note: "y z" causes a bug!
266$SH -c 'declare -A A=(["x y"]="y"); echo ${!A[@]@Q}'
267if test $? -ne 0; then echo fail; fi
268
269$SH -c 'declare -A A=(["x"]=y); echo ${!A[@]@a}'
270if test $? -ne 0; then echo fail; fi
271# STDOUT:
272
273
274
275# END
276## OK osh STDOUT:
277fail
278'x y'
279a
280## END
281
282#### ${#var@X} is a parse error
283# note: "y z" causes a bug!
284$SH -c 'declare -A A=(["x"]="y"); echo ${#A[@]@P}'
285if test $? -ne 0; then echo fail; fi
286
287# note: "y z" causes a bug!
288$SH -c 'declare -A A=(["x"]="y"); echo ${#A[@]@Q}'
289if test $? -ne 0; then echo fail; fi
290
291$SH -c 'declare -A A=(["x"]=y); echo ${#A[@]@a}'
292if test $? -ne 0; then echo fail; fi
293## STDOUT:
294fail
295fail
296fail
297## END
298
299#### ${!A@a} and ${!A[@]@a}
300declare -A A=(["x"]=y)
301echo x=${!A[@]@a}
302echo x=${!A@a}
303
304# OSH prints 'a' for indexed array because the AssocArray with ! turns into
305# it. Disallowing it would be the other reasonable behavior.
306
307## STDOUT:
308x=
309x=
310## END