1 # This program is free software: you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation, either version 3 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 if (( $UID == 0 )); then
15 echo "new-exp.tests: the test suite should not be run as root" >&2
18 # must do this because posix mode causes process substitution to be disabled
19 # and flagged as a syntax error, which causes the shell to exit
27 HOME=/usr/homes/chet # to make the check against new-exp.right work
29 recho "${undef-"foo bar"}" # should be foo bar
31 recho "${und="foo"}" # should be foo
40 expect $0: 'HOME: }: syntax error: operand expected (error token is "}")'
41 recho "${HOME:`echo }`}" # should be a math error -- bad substring substitution
45 x=${_ENV[(_$-=0)+(_=1)-_${-%%*i*}]}
51 recho ${HOME:-`echo }`}
53 recho ${HOME:-`echo "}"`}
55 recho "${HOME:-`echo "}"`}"
57 recho "$(echo "${HOME}")"
59 recho "$(echo "$(echo ${HOME})")"
61 recho "$(echo "$(echo "${HOME}")")"
69 recho "${P#\*}" # should be @*
72 recho "$(echo ")")" # should be )
74 recho "$(echo "\")\"")" # should be ")"
78 recho -${foo}- # should be -abcd -
80 recho -${foo% *}- # should be -abcd -
82 recho -${foo%% *}- # should be -abcd-
86 echo -n $foo' ' ; echo foo
89 echo -n $foo" " ; echo foo
92 echo -n "$foo " ; echo foo
95 echo -e "$foo\c " ; echo foo
98 echo -e $foo"\c " ; echo foo
100 # make sure backslashes are preserved in front of characters that are not
101 # valid backslash escapes
111 recho ${z:4:3} ${z:${#z}-3:3}
114 recho ${z:4:3} ${z: -3:3}
119 expect '<abcdefghijklmnop>'
122 expect '<abcdefghijklmnop>'
125 set 'ab cd' 'ef' 'gh ij' 'kl mn' 'op'
126 expect '<ab cd> <ef>'
129 expect '<gh ij> <kl mn>'
132 expect '<gh ij> <kl mn> <op>'
135 expect '<ab cd> <ef> <gh ij> <kl mn> <op>'
138 # code to ad-hoc parse arithmetic expressions in substring expansions was
139 # broken until post-2.04
140 base=/home/chet/foo//bar
141 string1=$base/abcabcabc
144 expect '</home/chet/foo//bar/abcabcabc>'
147 expect '<home/chet/foo//bar/abcabcabc>'
151 recho ${string1:(j?1:0):j}
154 recho ${string1:j?1:0:j}
157 recho ${string1:(j?(x?1:0):0):j}
160 recho ${string1:j?(x?1:0):0:j}
162 unset base string1 x j
164 # indirect variable references
165 expect '<abcdefghijklmnop>'
177 expect '<abcdefghijklmnop>'
179 expect '<abcdefghijklmnop>'
190 expect '<a> <b> <c> <d>'
192 expect '<a> <b c> <d>'
196 expect $0: ABX: unbound variable
200 expect $0: '$6: cannot assign in this way'
205 # sed-like variable substitution
223 av=(abcd efgh ijkl mnop qrst uvwx)
232 recho ${av[1]/%ab/xx}
238 recho ${av[1]//??/za}
240 recho ${av[1]/#??/za}
242 recho ${av[1]/%??/za}
244 expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>'
246 expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>'
247 recho ${av[@]/#*/yyy}
248 expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>'
249 recho ${av[@]/%*/yyy}
250 expect '<yyy> <efgh> <ijkl> <mnop> <qrst> <uvwx>'
251 recho ${av[@]/a*/yyy}
252 expect '<abxx> <efxx> <ijxx> <mnxx> <qrxx> <uvxx>'
253 recho ${av[@]/%??/xx}
255 set abcd efgh ijkl mnop qrst uvwx
259 expect '<xxcd> <xxgh> <xxkl> <xxop> <xxst> <xxwx>'
261 expect '<xxcd> <xxgh> <xxkl> <xxop> <xxst> <xxwx>'
267 expect '<zaza> <zaza> <zaza> <zaza> <zaza> <zaza>'
269 expect '<zacd> <zagh> <zakl> <zaop> <zast> <zawx>'
271 expect '<yyy> <yyy> <yyy> <yyy> <yyy> <yyy>'
273 expect '<yyy> <efgh> <ijkl> <mnop> <qrst> <uvwx>'
275 expect '<abcd> <efgh> <ijkl> <mnop> <qrst> <uvwyyy>'
281 # sneaky way to replace a newline in a variable value with something else
282 AVAR=$'This\nstring\nhas\nmultiple\nlines.'
285 eval BVAR=\"\${AVAR//$'\n'/-}\"
290 # run process substitution tests in a subshell so that syntax errors
291 # caused by a shell not implementing process substitution (e.g., one
292 # built on a NeXT) will not cause the whole test to exit prematurely
293 ${THIS_SH} ./new-exp1.sub
295 # run the tests of $(<filename) in a subshell to avoid cluttering up
297 ${THIS_SH} ./new-exp2.sub
301 expect $0: '${#:}: bad substitution'
325 recho ${foo:-"$zbcd"}
347 # this disagrees with sh and ksh, but I think it is right according
350 recho "${foo:-"$z1"}"
353 expect '<ab> <cd> <ef> <gh>'
355 expect '<ab> <cd> <ef> <gh>'
357 expect '<ab> <cd> <ef> <gh>'
364 recho ${foo:-$xxx"$@"}
366 recho "${foo:-$xxx$@}"
368 recho "${foo:-$xxx"$@"}"
386 AB='abcdefghijklmnopqrstuvwxyz'
415 set $'\1' $'\2' $'\177'
437 # out-of-range substrings
445 # as of bush-4.2, negative LENGTH means offset from the end
456 # make sure null patterns work
467 recho ${xxx//%${zzz}/}
468 recho ${xxx//%${zzz}}
469 recho ${xxx//#${zzz}/}
470 recho ${xxx//#${zzz}}
472 # make sure null strings are replaced appropriately
485 # another case that caused a core dump in bush-2.0
486 XPATH=/usr/bin:/bin:/usr/local/bin:/usr/gnu/bin::/usr/bin/X11:/sbin:/usr/sbin
490 xx=(ar as at au av aw ax ay az)
504 set -- ar as at au av aw ax ay az
520 ( recho $9 ; echo after 1)
521 ( recho ${9} ; echo after 2)
522 ( recho $UNSET ; echo after 3)
523 ( recho ${UNSET} ; echo after 4)
524 ( recho "$UNSET" ; echo after 5)
525 ( recho "${UNSET}" ; echo after 6)
526 ( recho "${#UNSET}" ; echo after 7)
530 recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
532 recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
534 recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
536 recho "${RECEIVED:$((${#RECEIVED}-1)):1}"
538 # tests of new prefix expansion ${!prefix*}
539 ${THIS_SH} ./new-exp3.sub
541 # bug with indirect expansion through bush-2.05b
542 ${THIS_SH} ./new-exp4.sub
544 # these caused errors and core dumps in versions before bush-2.04
546 echo ${c//${$(($#-1))}/x/}
551 set -- ${@:1:$(($# - 2))}
555 recho ${@:1:$(($# - 2))}
563 # this is an error -- negative expression
565 recho ${@:1:$(($# - 2))}
569 XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:.:/sbin:/usr/sbin
570 set $( IFS=: ; echo $XPATH )
578 set /full/path/to/x16 /another/full/path
585 ${THIS_SH} ./new-exp5.sub
590 # these had better agree
599 var=(abcde abcfg abchi)
601 # problems with anchoring pattern replacements
602 echo ${var[*]//#abc/foo}
603 echo ${var[*]/#abc/foo}
606 ${THIS_SH} ./new-exp6.sub
608 ${THIS_SH} ./new-exp7.sub
610 ${THIS_SH} ./new-exp8.sub
612 # tests to check whether things like indirect expansion of a variable whose
613 # value is 'anothervar[@]' stop working
614 ${THIS_SH} ./new-exp9.sub
616 # new parameter transformation `@' expansion operator
617 ${THIS_SH} ./new-exp10.sub
619 # parameter substring replacement and removal operators with multibyte chars
620 ${THIS_SH} ./new-exp11.sub
622 # indirect expansion with arrays and local variables
623 ${THIS_SH} ./new-exp12.sub
625 # more indirect expansion and parameter transformation issues
626 ${THIS_SH} ./new-exp13.sub
628 # new K parameter transformation operator
629 ${THIS_SH} ./new-exp14.sub
631 # ongoing work with a/A parameter transformations and `nounset'
632 ${THIS_SH} ./new-exp15.sub
634 # problems with stray CTLNUL in bush-4.0-alpha
642 echo ${1##*"${1##*}"}
646 # caused core dumps because of bad bracket expression parsing in bush-5.0
647 eval : $'${x/#[0\xef\xbf\xbd\\Z[:]]}'
649 expect $0: 'ABXD: parameter unset'
650 ${THIS_SH} -c 'recho ${ABXD:?"parameter unset"}' $0