xref: /netbsd-src/tests/bin/sh/t_expand.sh (revision 23edd9e2798c3bc9d1341839a6e0f6de4f0babc0)
1*23edd9e2Skre# $NetBSD: t_expand.sh,v 1.23 2023/03/06 05:54:54 kre Exp $
228604916Sjruoho#
328604916Sjruoho# Copyright (c) 2007, 2009 The NetBSD Foundation, Inc.
428604916Sjruoho# All rights reserved.
528604916Sjruoho#
628604916Sjruoho# Redistribution and use in source and binary forms, with or without
728604916Sjruoho# modification, are permitted provided that the following conditions
828604916Sjruoho# are met:
928604916Sjruoho# 1. Redistributions of source code must retain the above copyright
1028604916Sjruoho#    notice, this list of conditions and the following disclaimer.
1128604916Sjruoho# 2. Redistributions in binary form must reproduce the above copyright
1228604916Sjruoho#    notice, this list of conditions and the following disclaimer in the
1328604916Sjruoho#    documentation and/or other materials provided with the distribution.
1428604916Sjruoho#
1528604916Sjruoho# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1628604916Sjruoho# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1728604916Sjruoho# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1828604916Sjruoho# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1928604916Sjruoho# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2028604916Sjruoho# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2128604916Sjruoho# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2228604916Sjruoho# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2328604916Sjruoho# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2428604916Sjruoho# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2528604916Sjruoho# POSSIBILITY OF SUCH DAMAGE.
2628604916Sjruoho#
272c3c3b3cSchristos# the implementation of "sh" to test
282c3c3b3cSchristos: ${TEST_SH:="/bin/sh"}
2928604916Sjruoho
3028604916Sjruoho#
3128604916Sjruoho# This file tests the functions in expand.c.
3228604916Sjruoho#
3328604916Sjruoho
3428604916Sjruohodelim_argv() {
3528604916Sjruoho	str=
3628604916Sjruoho	while [ $# -gt 0 ]; do
3728604916Sjruoho		if [ -z "${str}" ]; then
3828604916Sjruoho			str=">$1<"
3928604916Sjruoho		else
4028604916Sjruoho			str="${str} >$1<"
4128604916Sjruoho		fi
4228604916Sjruoho		shift
4328604916Sjruoho	done
4428604916Sjruoho	echo ${str}
4528604916Sjruoho}
4628604916Sjruoho
4728604916Sjruohoatf_test_case dollar_at
4828604916Sjruohodollar_at_head() {
49265b0617Skre	atf_set descr "Somewhere between 2.0.2 and 3.0 the expansion" \
5028604916Sjruoho	                "of the \$@ variable had been broken.  Check for" \
5128604916Sjruoho			"this behavior."
5228604916Sjruoho}
5328604916Sjruohodollar_at_body() {
5428604916Sjruoho	# This one should work everywhere.
552c3c3b3cSchristos	atf_check -s exit:0 -o inline:' EOL\n' -e empty \
562c3c3b3cSchristos		${TEST_SH} -c 'echo "" "" | '" sed 's,\$,EOL,'"
5728604916Sjruoho
5828604916Sjruoho	# This code triggered the bug.
592c3c3b3cSchristos	atf_check -s exit:0 -o inline:' EOL\n' -e empty \
602c3c3b3cSchristos		${TEST_SH} -c 'set -- "" ""; echo "$@" | '" sed 's,\$,EOL,'"
6128604916Sjruoho
622c3c3b3cSchristos	atf_check -s exit:0 -o inline:'0\n' -e empty ${TEST_SH} -c \
632c3c3b3cSchristos		'set -- -; shift; n_arg() { echo $#; }; n_arg "$@"'
6428604916Sjruoho}
6528604916Sjruoho
669addc446Skreatf_test_case dollar_at_unquoted_or_conditional
679addc446Skredollar_at_unquoted_or_conditional_head() {
68265b0617Skre	atf_set descr 'Sometime during 2013 the expansion of "${1+$@}"' \
699addc446Skre			' (where $1 and $2 (and maybe more) are set)' \
709addc446Skre			' seems to have broken.  Check for this bug.'
719addc446Skre}
729addc446Skredollar_at_unquoted_or_conditional_body() {
739addc446Skre
749addc446Skre	atf_check -s exit:0 -o inline:'a\na\nb\nb\n' -e empty \
759addc446Skre		${TEST_SH} -c 'set -- "a a" "b b"; printf %s\\n $@'
769addc446Skre	atf_check -s exit:0 -o inline:'a\na\nb\nb\n' -e empty \
779addc446Skre		${TEST_SH} -c 'set -- "a a" "b b"; printf %s\\n ${1+$@}'
789addc446Skre	atf_check -s exit:0 -o inline:'a a\nb b\n' -e empty \
799addc446Skre		${TEST_SH} -c 'set -- "a a" "b b"; printf %s\\n "$@"'
809addc446Skre	atf_check -s exit:0 -o inline:'a a\nb b\n' -e empty \
819addc446Skre		${TEST_SH} -c 'set -- "a a" "b b"; printf %s\\n ${1+"$@"}'
829addc446Skre
839addc446Skre	# This is the one that fails when the bug is present
849addc446Skre	atf_check -s exit:0 -o inline:'a a\nb b\n' -e empty \
859addc446Skre		${TEST_SH} -c 'set -- "a a" "b b"; printf %s\\n "${1+$@}"'
869addc446Skre}
879addc446Skre
8828604916Sjruohoatf_test_case dollar_at_with_text
8928604916Sjruohodollar_at_with_text_head() {
90265b0617Skre	atf_set descr "Test \$@ expansion when it is surrounded by text" \
9128604916Sjruoho	                "within the quotes.  PR bin/33956."
9228604916Sjruoho}
9328604916Sjruohodollar_at_with_text_body() {
9428604916Sjruoho
952c3c3b3cSchristos	cat <<'EOF' > h-f1
962c3c3b3cSchristos
972c3c3b3cSchristosdelim_argv() {
982c3c3b3cSchristos	str=
992c3c3b3cSchristos	while [ $# -gt 0 ]; do
1002c3c3b3cSchristos		if [ -z "${str}" ]; then
1012c3c3b3cSchristos			str=">$1<"
1022c3c3b3cSchristos		else
1032c3c3b3cSchristos			str="${str} >$1<"
1042c3c3b3cSchristos		fi
1052c3c3b3cSchristos		shift
1062c3c3b3cSchristos	done
1072c3c3b3cSchristos	echo "${str}"
1082c3c3b3cSchristos}
1092c3c3b3cSchristos
1102c3c3b3cSchristosEOF
1112c3c3b3cSchristos	cat <<'EOF' > h-f2
1122c3c3b3cSchristos
1132c3c3b3cSchristosdelim_argv() {
1142c3c3b3cSchristos	str=
1152c3c3b3cSchristos	while [ $# -gt 0 ]; do
1162c3c3b3cSchristos
1172c3c3b3cSchristos		str="${str}${str:+ }>$1<"
1182c3c3b3cSchristos		shift
1192c3c3b3cSchristos
1202c3c3b3cSchristos	done
1212c3c3b3cSchristos	echo "${str}"
1222c3c3b3cSchristos}
1232c3c3b3cSchristos
1242c3c3b3cSchristosEOF
1252c3c3b3cSchristos
1262c3c3b3cSchristos	chmod +x h-f1 h-f2
1272c3c3b3cSchristos
1282c3c3b3cSchristos	for f in 1 2
1292c3c3b3cSchristos	do
1302c3c3b3cSchristos		atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
1319addc446Skre			". ./h-f${f}; "'set -- ; delim_argv $@'
1329addc446Skre		atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
1332c3c3b3cSchristos			". ./h-f${f}; "'set -- ; delim_argv "$@"'
1342c3c3b3cSchristos		atf_check -s exit:0 -o inline:'>foobar<\n' -e empty \
1352c3c3b3cSchristos			${TEST_SH} -c \
1362c3c3b3cSchristos			". ./h-f${f}; "'set -- ; delim_argv "foo$@bar"'
1372c3c3b3cSchristos		atf_check -s exit:0 -o inline:'>foobar<\n' -e empty \
1382c3c3b3cSchristos			${TEST_SH} -c \
1399addc446Skre			". ./h-f${f}; "'set -- ; delim_argv foo"$@"bar'
1409addc446Skre		atf_check -s exit:0 -o inline:'>foo  bar<\n' -e empty \
1419addc446Skre			${TEST_SH} -c \
1422c3c3b3cSchristos			". ./h-f${f}; "'set -- ; delim_argv "foo $@ bar"'
1439addc446Skre		atf_check -s exit:0 -o inline:'>foo  bar<\n' -e empty \
1449addc446Skre			${TEST_SH} -c \
1459addc446Skre			". ./h-f${f}; "'set -- ; delim_argv foo" $@ "bar'
1462c3c3b3cSchristos
1472c3c3b3cSchristos		atf_check -s exit:0 -o inline:'>a< >b< >c<\n' -e empty \
1482c3c3b3cSchristos			${TEST_SH} -c \
1492c3c3b3cSchristos			". ./h-f${f}; "'set -- a b c; delim_argv "$@"'
1509addc446Skre
1512c3c3b3cSchristos		atf_check -s exit:0 -o inline:'>fooa< >b< >cbar<\n' -e empty \
1522c3c3b3cSchristos			${TEST_SH} -c \
1532c3c3b3cSchristos			". ./h-f${f}; "'set -- a b c; delim_argv "foo$@bar"'
1549addc446Skre
1552c3c3b3cSchristos		atf_check -s exit:0 -o inline:'>foo a< >b< >c bar<\n' -e empty \
1562c3c3b3cSchristos			${TEST_SH} -c \
1572c3c3b3cSchristos			". ./h-f${f}; "'set -- a b c; delim_argv "foo $@ bar"'
1582c3c3b3cSchristos	done
15928604916Sjruoho}
16028604916Sjruoho
1619addc446Skreatf_test_case dollar_at_empty_and_conditional
1629addc446Skredollar_at_empty_and_conditional_head() {
163265b0617Skre	atf_set descr 'Test $@ expansion when there are no args, and ' \
1649addc446Skre	                'when conditionally expanded.'
1659addc446Skre}
1669addc446Skredollar_at_empty_and_conditional_body() {
1679addc446Skre
1689addc446Skre	# same task, implementation different from previous,
1699addc446Skre	# that these work is also a test...
1709addc446Skre
1719addc446Skre	cat <<'EOF' > h-f3
1729addc446Skre
1739addc446Skredelim_argv() {
1749addc446Skre	str=
1759addc446Skre	for Arg; do
1769addc446Skre		str="${str:+${str} }>${Arg}<"
1779addc446Skre	done
1789addc446Skre	printf '%s\n' "${str}"
1799addc446Skre}
1809addc446Skre
1819addc446SkreEOF
1829addc446Skre
1839addc446Skre	chmod +x h-f3
1849addc446Skre
1859addc446Skre	# in these we give printf a first arg of "", which makes
1869addc446Skre	# the first output char be \n, then the $@ produces anything else
1879addc446Skre	# (we need to make sure we don't end up with:
1889addc446Skre	#	printf %s\\n
1899addc446Skre	# -- that is, no operands for the %s, that's unspecified)
1909addc446Skre
1919addc446Skre	atf_check -s exit:0 -o inline:'\na a\nb b\n' -e empty \
1929addc446Skre		${TEST_SH} -c 'set -- "a a" "b b"; printf %s\\n "" "$@"'
1939addc446Skre	atf_check -s exit:0 -o inline:'\n' -e empty \
1949addc446Skre		${TEST_SH} -c 'set -- ; printf %s\\n "" "$@"'
1959addc446Skre	atf_check -s exit:0 -o inline:'\n' -e empty \
1969addc446Skre		${TEST_SH} -c 'set -- ; printf %s\\n ""${1+"$@"}'
1979addc446Skre	atf_check -s exit:0 -o inline:'\n' -e empty \
1989addc446Skre		${TEST_SH} -c 'set -- ; printf %s\\n """${1+$@}"'
1999addc446Skre
2009addc446Skre	# in these we prefix (concat) the $@ expansion with "" to make
2019addc446Skre	# sure there is always at least one arg for the %s in printf
2029addc446Skre	# If there is anything else there, the prepended nothing vanishes
2039addc446Skre	atf_check -s exit:0 -o inline:'a a\nb b\n' -e empty \
2049addc446Skre		${TEST_SH} -c 'set -- "a a" "b b"; printf %s\\n """$@"'
2059addc446Skre	atf_check -s exit:0 -o inline:'\n' -e empty \
2069addc446Skre		${TEST_SH} -c 'set -- ; printf %s\\n """$@"'
2079addc446Skre	atf_check -s exit:0 -o inline:'\n' -e empty \
2089addc446Skre		${TEST_SH} -c 'set -- ; printf %s\\n ""${1+"$@"}'
2099addc446Skre	atf_check -s exit:0 -o inline:'\n' -e empty \
2109addc446Skre		${TEST_SH} -c 'set -- ; printf %s\\n """${1+$@}"'
2119addc446Skre
2129addc446Skre	atf_check -s exit:0 -o inline:'>a< >b< >c<\n' -e empty ${TEST_SH} -c \
2139addc446Skre		'. ./h-f3; set -- a b c; delim_argv "${1+$@}"'
2149addc446Skre	atf_check -s exit:0 -o inline:'>a< >b< >c<\n' -e empty ${TEST_SH} -c \
2159addc446Skre		'. ./h-f3; set -- a b c; delim_argv ${1+"$@"}'
2169addc446Skre	atf_check -s exit:0 -o inline:'>fooa< >b< >cbar<\n' -e empty \
2179addc446Skre		${TEST_SH} -c \
2189addc446Skre		    '. ./h-f3; set -- a b c; delim_argv "foo${1+$@}bar"'
2199addc446Skre	atf_check -s exit:0 -o inline:'>fooa< >b< >cbar<\n' -e empty \
2209addc446Skre		${TEST_SH} -c \
2219addc446Skre		    '. ./h-f3; set -- a b c; delim_argv foo${1+"$@"}bar'
2229addc446Skre	atf_check -s exit:0 -o inline:'>foo a< >b< >c bar<\n' -e empty \
2239addc446Skre		${TEST_SH} -c \
2249addc446Skre		     '. ./h-f3; set -- a b c; delim_argv "foo ${1+$@} bar"'
2259addc446Skre	atf_check -s exit:0 -o inline:'>foo a< >b< >c bar<\n' -e empty \
2269addc446Skre		${TEST_SH} -c \
2279addc446Skre		     '. ./h-f3; set -- a b c; delim_argv "foo "${1+"$@"}" bar"'
2289addc446Skre
2299addc446Skre	# since $1 is not set, we get nothing ($@ is irrelevant)
2309addc446Skre	# (note here we are not using printf, don't need to guarantee an arg)
2319addc446Skre	atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
2329addc446Skre		 '. ./h-f3; set -- ; delim_argv ${1+"$@"}'
2339addc446Skre
2349addc446Skre	# here since $1 is not set we get "" as the special $@ properties
2359addc446Skre	# do not apply, and ${foo+anything} generates nothing if foo is unset
2369addc446Skre	atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
2379addc446Skre		 '. ./h-f3; set -- ; delim_argv "${1+$@}"'
2389addc446Skre
2399addc446Skre	# in this one we get the initial "" followed by nothing
2409addc446Skre	atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
2419addc446Skre		 '. ./h-f3; set -- ; delim_argv ""${1+"$@"}'
2429addc446Skre	# which we verify by changing the "" to X, and including Y
2439addc446Skre	atf_check -s exit:0 -o inline:'>X<\n' -e empty ${TEST_SH} -c \
2449addc446Skre		 '. ./h-f3; set -- ; delim_argv X${1+"$@"Y}'
2459addc446Skre	# and again, done differently (the ${1+...} produces nothing at all
2469addc446Skre	atf_check -s exit:0 -o inline:'>X<\n' -e empty ${TEST_SH} -c \
2479addc446Skre		 '. ./h-f3; set -- ; delim_argv X ${1+"$@"}'
2489addc446Skre	# in these two we get the initial "" and then nothing
2499addc446Skre	atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
2509addc446Skre		 '. ./h-f3; set -- ; delim_argv """${1+$@}"'
2519addc446Skre	atf_check -s exit:0 -o inline:'>< ><\n' -e empty ${TEST_SH} -c \
2529addc446Skre		 '. ./h-f3; set -- ; delim_argv "" "${1+$@}"'
2539addc446Skre
2549addc446Skre	# now we repeat all those with $1 set (so we eval the $@)
2559addc446Skre	atf_check -s exit:0 -o inline:'>a<\n' -e empty ${TEST_SH} -c \
2569addc446Skre		 '. ./h-f3; set -- a ; delim_argv ""${1+"$@"}'
2579addc446Skre	atf_check -s exit:0 -o inline:'>XaY<\n' -e empty ${TEST_SH} -c \
2589addc446Skre		 '. ./h-f3; set -- a ; delim_argv X${1+"$@"Y}'
2599addc446Skre	atf_check -s exit:0 -o inline:'>X< >a<\n' -e empty ${TEST_SH} -c \
2609addc446Skre		 '. ./h-f3; set -- a ; delim_argv X ${1+"$@"}'
2619addc446Skre	atf_check -s exit:0 -o inline:'>a<\n' -e empty ${TEST_SH} -c \
2629addc446Skre		 '. ./h-f3; set -- a ; delim_argv """${1+$@}"'
2639addc446Skre	atf_check -s exit:0 -o inline:'>< >a<\n' -e empty ${TEST_SH} -c \
2649addc446Skre		 '. ./h-f3; set -- a ; delim_argv "" "${1+$@}"'
2659addc446Skre
2669addc446Skre	# now we do all of those again, but testing $X instead of $1 (X set)
2679addc446Skre	atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
2689addc446Skre		 '. ./h-f3; X=1; set -- ; delim_argv ${X+"$@"}'
2699addc446Skre	atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
2709addc446Skre		 '. ./h-f3; X=1; set -- ; delim_argv ""${X+"$@"}'
2719addc446Skre	atf_check -s exit:0 -o inline:'>X<\n' -e empty ${TEST_SH} -c \
2729addc446Skre		 '. ./h-f3; X=1; set -- ; delim_argv X${X+"$@"}'
2739addc446Skre	atf_check -s exit:0 -o inline:'>X<\n' -e empty ${TEST_SH} -c \
2749addc446Skre		 '. ./h-f3; X=1; set -- ; delim_argv X ${X+"$@"}'
2759addc446Skre	atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
2769addc446Skre		 '. ./h-f3; X=1; set -- ; delim_argv "${X+$@}"'
2779addc446Skre	atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
2789addc446Skre		 '. ./h-f3; X=1; set -- ; delim_argv """${X+$@}"'
2799addc446Skre	atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
2809addc446Skre		 '. ./h-f3; X=1; set -- ; delim_argv "" "${X+$@}"'
2819addc446Skre
2829addc446Skre	atf_check -s exit:0 -o inline:'>a<\n' -e empty ${TEST_SH} -c \
2839addc446Skre		 '. ./h-f3; X=1; set -- a; delim_argv ${X+"$@"}'
2849addc446Skre	atf_check -s exit:0 -o inline:'>a< >b<\n' -e empty ${TEST_SH} -c \
2859addc446Skre		 '. ./h-f3; X=1; set -- a b; delim_argv ${X+"$@"}'
2869addc446Skre	atf_check -s exit:0 -o inline:'>a<\n' -e empty ${TEST_SH} -c \
2879addc446Skre		 '. ./h-f3; X=1; set -- a ; delim_argv ""${X+"$@"}'
2889addc446Skre	atf_check -s exit:0 -o inline:'>Xa<\n' -e empty ${TEST_SH} -c \
2899addc446Skre		 '. ./h-f3; X=1; set -- a ; delim_argv X${X+"$@"}'
2909addc446Skre	atf_check -s exit:0 -o inline:'>X< >a<\n' -e empty ${TEST_SH} -c \
2919addc446Skre		 '. ./h-f3; X=1; set -- a ; delim_argv X ${X+"$@"}'
2929addc446Skre	atf_check -s exit:0 -o inline:'>a<\n' -e empty ${TEST_SH} -c \
2939addc446Skre		 '. ./h-f3; X=1; set -- a ; delim_argv """${X+$@}"'
2949addc446Skre	atf_check -s exit:0 -o inline:'>a< >b<\n' -e empty ${TEST_SH} -c \
2959addc446Skre		 '. ./h-f3; X=1; set -- a b ; delim_argv """${X+$@}"'
2969addc446Skre	atf_check -s exit:0 -o inline:'>< >a<\n' -e empty ${TEST_SH} -c \
2979addc446Skre		 '. ./h-f3; X=1; set -- a ; delim_argv "" "${X+$@}"'
2989addc446Skre	atf_check -s exit:0 -o inline:'>< >a< >b<\n' -e empty ${TEST_SH} -c \
2999addc446Skre		 '. ./h-f3; X=1; set -- a b ; delim_argv "" "${X+$@}"'
3009addc446Skre
3019addc446Skre	# and again, but testing $X where X is unset
3029addc446Skre	atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
3039addc446Skre		 '. ./h-f3; unset X; set -- ; delim_argv ${X+"$@"}'
3049addc446Skre	atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
3059addc446Skre		 '. ./h-f3; unset X; set -- ; delim_argv ""${X+"$@"}'
3069addc446Skre	atf_check -s exit:0 -o inline:'>X<\n' -e empty ${TEST_SH} -c \
3079addc446Skre		 '. ./h-f3; unset X; set -- ; delim_argv X${X+"$@"}'
3089addc446Skre	atf_check -s exit:0 -o inline:'>X<\n' -e empty ${TEST_SH} -c \
3099addc446Skre		 '. ./h-f3; unset X; set -- ; delim_argv X ${X+"$@"}'
3109addc446Skre	atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
3119addc446Skre		 '. ./h-f3; unset X; set -- ; delim_argv "${X+$@}"'
3129addc446Skre	atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
3139addc446Skre		 '. ./h-f3; unset X; set -- ; delim_argv """${X+$@}"'
3149addc446Skre	atf_check -s exit:0 -o inline:'>< ><\n' -e empty ${TEST_SH} -c \
3159addc446Skre		 '. ./h-f3; unset X; set -- ; delim_argv "" "${X+$@}"'
3169addc446Skre	atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
3179addc446Skre		 '. ./h-f3; unset X; set -- a; delim_argv ${X+"$@"}'
3189addc446Skre	atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
3199addc446Skre		 '. ./h-f3; unset X; set -- a b; delim_argv ${X+"$@"}'
3209addc446Skre	atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
3219addc446Skre		 '. ./h-f3; unset X; set -- a ; delim_argv ""${X+"$@"}'
3229addc446Skre	atf_check -s exit:0 -o inline:'>X<\n' -e empty ${TEST_SH} -c \
3239addc446Skre		 '. ./h-f3; unset X; set -- a ; delim_argv X${X+"$@"}'
3249addc446Skre	atf_check -s exit:0 -o inline:'>X<\n' -e empty ${TEST_SH} -c \
3259addc446Skre		 '. ./h-f3; unset X; set -- a ; delim_argv X ${X+"$@"}'
3269addc446Skre	atf_check -s exit:0 -o inline:'><\n' -e empty ${TEST_SH} -c \
3279addc446Skre		 '. ./h-f3; unset X; set -- a ; delim_argv """${X+$@}"'
3289addc446Skre	atf_check -s exit:0 -o inline:'>< ><\n' -e empty ${TEST_SH} -c \
3299addc446Skre		 '. ./h-f3; unset X; set -- a; delim_argv "" "${X+$@}"'
3309addc446Skre
3319addc446Skre	# a few that stretch belief...
3329addc446Skre
3339addc446Skre	atf_check -s exit:0 -o inline:'>a< >b<\n' -e empty ${TEST_SH} -c \
3349addc446Skre		 '. ./h-f3; X=1; set -- a b ; delim_argv ${X+${1+"$@"}}'
3359addc446Skre	atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
3369addc446Skre		 '. ./h-f3; X=1; set -- ; delim_argv ${X+${1+"$@"}}'
3379addc446Skre	atf_check -s exit:0 -o inline:'\n' -e empty ${TEST_SH} -c \
3389addc446Skre		 '. ./h-f3; unset X; set -- a b ; delim_argv ${X+${1+"$@"}}'
3399addc446Skre
3409addc446Skre	# and now for something completely different
3419addc446Skre
3429addc446Skre	atf_check -s exit:0 -o inline:'>a< >b< >ca< >b< >c<\n' -e empty \
3439addc446Skre		${TEST_SH} -c '. ./h-f3; set -- a b c; delim_argv "$@$@"'
3449addc446Skre	atf_check -s exit:0 -o inline:'>a< >b< >ca b c<\n' -e empty \
3459addc446Skre		${TEST_SH} -c '. ./h-f3; set -- a b c; delim_argv "$@$*"'
3469addc446Skre	atf_check -s exit:0 -o inline:'>a b ca< >b< >c<\n' -e empty \
3479addc446Skre		${TEST_SH} -c '. ./h-f3; set -- a b c; delim_argv "$*$@"'
3489addc446Skre	atf_check -s exit:0 -o inline:'>a a++b + ca a< >< >b < > c<\n'	\
3499addc446Skre		-e empty ${TEST_SH} -c					\
3509addc446Skre		'. ./h-f3; set -- "a a" "" "b " " c"; IFS=+; delim_argv "$*$@"'
3519addc446Skre	atf_check -s exit:0 -o inline:'>a< >a< >< >b < > ca+a< >< >b < > c<\n' \
3529addc446Skre		-e empty ${TEST_SH} -c					       \
3539addc446Skre		'. ./h-f3; set -- "a+a" "" "b " " c"; IFS=+; delim_argv $*"$@"'
3549addc446Skre}
3559addc446Skre
35628604916Sjruohoatf_test_case strip
35728604916Sjruohostrip_head() {
358265b0617Skre	atf_set descr "Checks that the %% operator works and strips" \
35928604916Sjruoho	                "the contents of a variable from the given point" \
3602c3c3b3cSchristos			"to the end"
36128604916Sjruoho}
36228604916Sjruohostrip_body() {
36328604916Sjruoho	line='#define bindir "/usr/bin" /* comment */'
36428604916Sjruoho	stripped='#define bindir "/usr/bin" '
3652c3c3b3cSchristos
3662c3c3b3cSchristos	# atf_expect_fail "PR bin/43469" -- now fixed
3672c3c3b3cSchristos	for exp in 				\
3682c3c3b3cSchristos		'${line%%/\**}'			\
3692c3c3b3cSchristos		'${line%%"/*"*}'		\
3702c3c3b3cSchristos		'${line%%'"'"'/*'"'"'*}'	\
3712c3c3b3cSchristos		'"${line%%/\**}"'		\
3722c3c3b3cSchristos		'"${line%%"/*"*}"'		\
3732c3c3b3cSchristos		'"${line%%'"'"'/*'"'"'*}"'	\
3742c3c3b3cSchristos		'${line%/\**}'			\
3752c3c3b3cSchristos		'${line%"/*"*}'			\
3762c3c3b3cSchristos		'${line%'"'"'/*'"'"'*}'		\
3772c3c3b3cSchristos		'"${line%/\**}"'		\
3782c3c3b3cSchristos		'"${line%"/*"*}"'		\
3792c3c3b3cSchristos		'"${line%'"'"'/*'"'"'*}"'
3802c3c3b3cSchristos	do
3812c3c3b3cSchristos		atf_check -o inline:":$stripped:\n" -e empty ${TEST_SH} -c \
3822c3c3b3cSchristos			"line='${line}'; echo :${exp}:"
3832c3c3b3cSchristos	done
38428604916Sjruoho}
38528604916Sjruoho
3866768e34bSkreatf_test_case wrap_strip
3876768e34bSkrewrap_strip_head() {
388265b0617Skre	atf_set descr "Checks that the %% operator works and strips" \
3896768e34bSkre	                "the contents of a variable from the given point" \
3906768e34bSkre			'to the end, and that \ \n sequences do not break it'
3916768e34bSkre}
3926768e34bSkrewrap_strip_body() {
3936768e34bSkre	line='#define bindir "/usr/bin" /* comment */'
3946768e34bSkre	stripped='#define bindir "/usr/bin" '
3956768e34bSkre
3966768e34bSkre	for exp in 				\
3976768e34bSkre		'${line\
3986768e34bSkre%%/\**}'					\
3996768e34bSkre		'${line%%"/\
4006768e34bSkre*"*}'						\
4016768e34bSkre		'${line%%'"'"'/*'"'"'\
4026768e34bSkre*}'						\
4036768e34bSkre		'"${li\
4046768e34bSkrene%%/\**}"'					\
4056768e34bSkre		'"${line%%"\
4066768e34bSkre/*"*}"'						\
4076768e34bSkre		'"${line%\
4086768e34bSkre%'"'"'/*'"'"'*}"'				\
4096768e34bSkre		'${line\
4106768e34bSkre%\
4116768e34bSkre/\*\
4126768e34bSkre*\
4136768e34bSkre}'						\
4146768e34bSkre		'${line%"/*\
4156768e34bSkre"*\
4166768e34bSkre}'						\
4176768e34bSkre		'${line\
4186768e34bSkre%\
4196768e34bSkre'"'"'/*'"'"'*}'					\
4206768e34bSkre		'"$\
4216768e34bSkre{li\
4226768e34bSkrene%\
4236768e34bSkre'"'"'/*'"'"'*}"'
4246768e34bSkre	do
4256768e34bSkre		atf_check -o inline:":$stripped:\n" -e empty ${TEST_SH} -c \
4266768e34bSkre			"line='${line}'; echo :${exp}:"
4276768e34bSkre	done
4286768e34bSkre}
4296768e34bSkre
4305c624ca9Skreatf_test_case tilde
4315c624ca9Skretilde_head() {
432265b0617Skre	atf_set descr "Checks that the ~ expansions work"
4335c624ca9Skre}
4345c624ca9Skretilde_body() {
435*23edd9e2Skre	for HOME in '' / /home/foo /home/foo/ \
4365c624ca9Skre/a/very/long/home/directory/path/that/might/push/the/tilde/expansion/code/beyond/what/it/would/normally/ever/see/on/any/sane/system/and/perhaps/expose/some/bugs
4375c624ca9Skre	do
4385c624ca9Skre		export HOME
4395c624ca9Skre
4405c624ca9Skre		atf_check -s exit:0 -e empty \
4415c624ca9Skre			-o inline:'HOME\t'"${HOME}"'
4425c624ca9Skre~\t'"${HOME}"'
443*23edd9e2Skre~/foobar\t'"${HOME%/}"'/foobar
4445c624ca9Skre"$V"\t'"${HOME}"'
4455c624ca9Skre"$X"\t~
4465c624ca9Skre"$Y"\t'"${HOME}"':'"${HOME}"'
447*23edd9e2Skre"$YY"\t'"${HOME%/}"'/foo:'"${HOME%/}"'/bar
448*23edd9e2Skre"$Z"\t'"${HOME%/}"'/~
4495c624ca9Skre${U:-~}\t'"${HOME}"'
4505c624ca9Skre$V\t'"${HOME}"'
4515c624ca9Skre$X\t~
4525c624ca9Skre$Y\t'"${HOME}"':'"${HOME}"'
453*23edd9e2Skre$YY\t'"${HOME%/}"'/foo:'"${HOME%/}"'/bar
454*23edd9e2Skre$Z\t'"${HOME%/}"'/~
4555c624ca9Skre${U:=~}\t'"${HOME}"'
4565c624ca9Skre${UU:=~:~}\t'"${HOME}"':'"${HOME}"'
457*23edd9e2Skre${UUU:=~/:~}\t'"${HOME%/}"'/:'"${HOME}"'
458*23edd9e2Skre${U4:=~/:~/}\t'"${HOME%/}"'/:'"${HOME%/}"'/\n' \
4595c624ca9Skre			${TEST_SH} -s <<- \EOF
4605c624ca9Skre				unset -v U UU UUU U4
4615c624ca9Skre				V=~
4625c624ca9Skre				X="~"
4635c624ca9Skre				Y=~:~ YY=~/foo:~/bar
4645c624ca9Skre				Z=~/~
4655c624ca9Skre				printf '%s\t%s\n' \
4665c624ca9Skre					'HOME' "${HOME}" \
4675c624ca9Skre					'~' ~ \
4685c624ca9Skre					'~/foobar' ~/foobar \
4695c624ca9Skre					'"$V"' "$V" \
4705c624ca9Skre					'"$X"' "$X" \
4715c624ca9Skre					'"$Y"' "$Y" \
4725c624ca9Skre					'"$YY"' "$YY" \
4735c624ca9Skre					'"$Z"' "$Z" \
4745c624ca9Skre					'${U:-~}' ''${U:-~} \
4755c624ca9Skre					'$V' ''$V \
4765c624ca9Skre					'$X' ''$X \
4775c624ca9Skre					'$Y' ''$Y \
4785c624ca9Skre					'$YY' ''$YY \
4795c624ca9Skre					'$Z' ''$Z \
4805c624ca9Skre					'${U:=~}' ''${U:=~} \
4815c624ca9Skre					'${UU:=~:~}' ''${UU:=~:~} \
4825c624ca9Skre					'${UUU:=~/:~}' ''${UUU:=~/:~} \
4835c624ca9Skre					'${U4:=~/:~/}' ''${U4:=~/:~/}
4845c624ca9Skre			EOF
4855c624ca9Skre	done
4865c624ca9Skre
487*23edd9e2Skre	# Verify that when HOME is "" expanding a bare ~
488*23edd9e2Skre	# makes an empty word, not nothing (or anything else)
489*23edd9e2Skre	HOME=""
490*23edd9e2Skre	export HOME
491*23edd9e2Skre	atf_check -s exit:0 -e empty -o inline:'1:<>\n' ${TEST_SH} -c \
492*23edd9e2Skre		'set -- ~ ; IFS=, ; printf '"'%d:<%s>\\n'"' "$#" "$*"'
493*23edd9e2Skre	atf_check -s exit:0 -e empty -o inline:'4:<,X,,/>\n' ${TEST_SH} -c \
494*23edd9e2Skre		'set -- ~ X ~ ~/ ; IFS=, ; printf '"'%d:<%s>\\n'"' "$#" "$*"'
495*23edd9e2Skre
4965c624ca9Skre	# Testing ~user is harder, so, perhaps later...
4975c624ca9Skre}
4985c624ca9Skre
49928604916Sjruohoatf_test_case varpattern_backslashes
50028604916Sjruohovarpattern_backslashes_head() {
501265b0617Skre	atf_set descr "Tests that protecting wildcards with backslashes" \
50228604916Sjruoho	                "works in variable patterns."
50328604916Sjruoho}
50428604916Sjruohovarpattern_backslashes_body() {
50528604916Sjruoho	line='/foo/bar/*/baz'
50628604916Sjruoho	stripped='/foo/bar/'
5072c3c3b3cSchristos	atf_check -o inline:'/foo/bar/\n' -e empty ${TEST_SH} -c \
5082c3c3b3cSchristos		'line="/foo/bar/*/baz"; echo ${line%%\**}'
50928604916Sjruoho}
51028604916Sjruoho
51128604916Sjruohoatf_test_case arithmetic
51228604916Sjruohoarithmetic_head() {
513265b0617Skre	atf_set descr "POSIX requires shell arithmetic to use signed" \
51428604916Sjruoho	                "long or a wider type.  We use intmax_t, so at" \
51528604916Sjruoho			"least 64 bits should be available.  Make sure" \
51628604916Sjruoho			"this is true."
51728604916Sjruoho}
51828604916Sjruohoarithmetic_body() {
5192c3c3b3cSchristos
5202c3c3b3cSchristos	atf_check -o inline:'3' -e empty ${TEST_SH} -c \
5212c3c3b3cSchristos		'printf %s $((1 + 2))'
5222c3c3b3cSchristos	atf_check -o inline:'2147483647' -e empty ${TEST_SH} -c \
5232c3c3b3cSchristos		'printf %s $((0x7fffffff))'
5242c3c3b3cSchristos	atf_check -o inline:'9223372036854775807' -e empty ${TEST_SH} -c \
5252c3c3b3cSchristos		'printf %s $(((1 << 63) - 1))'
52628604916Sjruoho}
52728604916Sjruoho
52883d9b545Sastatf_test_case iteration_on_null_parameter
52983d9b545Sastiteration_on_null_parameter_head() {
530265b0617Skre	atf_set descr "Check iteration of \$@ in for loop when set to null;" \
53183d9b545Sast	                "the error \"sh: @: parameter not set\" is incorrect." \
53283d9b545Sast	                "PR bin/48202."
53383d9b545Sast}
53483d9b545Sastiteration_on_null_parameter_body() {
5352c3c3b3cSchristos	atf_check -o empty -e empty ${TEST_SH} -c \
5362c3c3b3cSchristos		'N=; set -- ${N};   for X; do echo "[$X]"; done'
5372c3c3b3cSchristos}
5382c3c3b3cSchristos
5392c3c3b3cSchristosatf_test_case iteration_on_quoted_null_parameter
5402c3c3b3cSchristositeration_on_quoted_null_parameter_head() {
541265b0617Skre	atf_set descr \
5422c3c3b3cSchristos		'Check iteration of "$@" in for loop when set to null;'
5432c3c3b3cSchristos}
5442c3c3b3cSchristositeration_on_quoted_null_parameter_body() {
5452c3c3b3cSchristos	atf_check -o inline:'[]\n' -e empty ${TEST_SH} -c \
5462c3c3b3cSchristos		'N=; set -- "${N}"; for X; do echo "[$X]"; done'
5472c3c3b3cSchristos}
5482c3c3b3cSchristos
5492c3c3b3cSchristosatf_test_case iteration_on_null_or_null_parameter
5502c3c3b3cSchristositeration_on_null_or_null_parameter_head() {
551265b0617Skre	atf_set descr \
5522c3c3b3cSchristos		'Check expansion of null parameter as default for another null'
5532c3c3b3cSchristos}
5542c3c3b3cSchristositeration_on_null_or_null_parameter_body() {
5552c3c3b3cSchristos	atf_check -o empty -e empty ${TEST_SH} -c \
5562c3c3b3cSchristos		'N=; E=; set -- ${N:-${E}}; for X; do echo "[$X]"; done'
5572c3c3b3cSchristos}
5582c3c3b3cSchristos
5592c3c3b3cSchristosatf_test_case iteration_on_null_or_missing_parameter
5602c3c3b3cSchristositeration_on_null_or_missing_parameter_head() {
561265b0617Skre	atf_set descr \
5622c3c3b3cSchristos	    'Check expansion of missing parameter as default for another null'
5632c3c3b3cSchristos}
5642c3c3b3cSchristositeration_on_null_or_missing_parameter_body() {
5652c3c3b3cSchristos	# atf_expect_fail 'PR bin/50834'
5662c3c3b3cSchristos	atf_check -o empty -e empty ${TEST_SH} -c \
5672c3c3b3cSchristos		'N=; set -- ${N:-}; for X; do echo "[$X]"; done'
56883d9b545Sast}
56983d9b545Sast
57086dd15a6Skre####### The remaining tests use the following helper functions ...
57186dd15a6Skre
5723f904448Schristosnl='
5733f904448Schristos'
5743f904448Schristosreset()
5753f904448Schristos{
5763f904448Schristos	TEST_NUM=0
5773f904448Schristos	TEST_FAILURES=''
5783f904448Schristos	TEST_FAIL_COUNT=0
5793f904448Schristos	TEST_ID="$1"
5803f904448Schristos}
5813f904448Schristos
5823f904448Schristoscheck()
5833f904448Schristos{
5843f904448Schristos	fail=false
5853f904448Schristos	TEMP_FILE=$( mktemp OUT.XXXXXX )
5863f904448Schristos	TEST_NUM=$(( $TEST_NUM + 1 ))
5873f904448Schristos	MSG=
5883f904448Schristos
5893f904448Schristos	# our local shell (ATF_SHELL) better do quoting correctly...
5903f904448Schristos	# some of the tests expect us to expand $nl internally...
5913f904448Schristos	CMD="$1"
5923f904448Schristos
5933f904448Schristos	result="$( ${TEST_SH} -c "${CMD}" 2>"${TEMP_FILE}" )"
5943f904448Schristos	STATUS=$?
5953f904448Schristos
5963f904448Schristos	if [ "${STATUS}" -ne "$3" ]; then
5973f904448Schristos		MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
5983f904448Schristos		MSG="${MSG} expected exit code $3, got ${STATUS}"
5993f904448Schristos
6003f904448Schristos		# don't actually fail just because of wrong exit code
6013f904448Schristos		# unless we either expected, or received "good"
60222a6d2ddSkre		# or something else is detected as incorrect as well.
6033f904448Schristos		case "$3/${STATUS}" in
6043f904448Schristos		(*/0|0/*) fail=true;;
6053f904448Schristos		esac
6063f904448Schristos	fi
6073f904448Schristos
6083f904448Schristos	if [ "$3" -eq 0 ]; then
6093f904448Schristos		if [ -s "${TEMP_FILE}" ]; then
6103f904448Schristos			MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
6113f904448Schristos			MSG="${MSG} Messages produced on stderr unexpected..."
6123f904448Schristos			MSG="${MSG}${nl}$( cat "${TEMP_FILE}" )"
6133f904448Schristos			fail=true
6143f904448Schristos		fi
6153f904448Schristos	else
6163f904448Schristos		if ! [ -s "${TEMP_FILE}" ]; then
6173f904448Schristos			MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
6183f904448Schristos			MSG="${MSG} Expected messages on stderr,"
6193f904448Schristos			MSG="${MSG} nothing produced"
6203f904448Schristos			fail=true
6213f904448Schristos		fi
6223f904448Schristos	fi
6233f904448Schristos	rm -f "${TEMP_FILE}"
6243f904448Schristos
6253f904448Schristos	# Remove newlines (use local shell for this)
6263f904448Schristos	oifs="$IFS"
6273f904448Schristos	IFS="$nl"
6283f904448Schristos	result="$(echo $result)"
6293f904448Schristos	IFS="$oifs"
6303f904448Schristos	if [ "$2" != "$result" ]
6313f904448Schristos	then
6323f904448Schristos		MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
6333f904448Schristos		MSG="${MSG} Expected output '$2', received '$result'"
6343f904448Schristos		fail=true
6353f904448Schristos	fi
6363f904448Schristos
6373f904448Schristos	if $fail
6383f904448Schristos	then
6393f904448Schristos		MSG="${MSG}${MSG:+${nl}}[$TEST_NUM]"
6403f904448Schristos		MSG="${MSG} Full command: <<${CMD}>>"
6413f904448Schristos	fi
6423f904448Schristos
6433f904448Schristos	$fail && test -n "$TEST_ID" && {
6443f904448Schristos		TEST_FAILURES="${TEST_FAILURES}${TEST_FAILURES:+${nl}}"
6453f904448Schristos		TEST_FAILURES="${TEST_FAILURES}${TEST_ID}[$TEST_NUM]:"
6463f904448Schristos		TEST_FAILURES="${TEST_FAILURES} Test of '$1' failed.";
6473f904448Schristos		TEST_FAILURES="${TEST_FAILURES}${nl}${MSG}"
6483f904448Schristos		TEST_FAIL_COUNT=$(( $TEST_FAIL_COUNT + 1 ))
6493f904448Schristos		return 0
6503f904448Schristos	}
65122a6d2ddSkre	$fail && atf_fail "Test[$TEST_NUM] failed: $(
65222a6d2ddSkre	    # ATF does not like newlines in messages, so change them...
65322a6d2ddSkre		    printf '%s' "${MSG}" | tr '\n' ';'
65422a6d2ddSkre	    )"
6553f904448Schristos	return 0
6563f904448Schristos}
6573f904448Schristos
6583f904448Schristosresults()
6593f904448Schristos{
66086dd15a6Skre	test -n "$1" && atf_expect_fail "$1"
66186dd15a6Skre
6623f904448Schristos	test -z "${TEST_ID}" && return 0
6633f904448Schristos	test -z "${TEST_FAILURES}" && return 0
6643f904448Schristos
6653f904448Schristos	echo >&2 "=========================================="
6663f904448Schristos	echo >&2 "While testing '${TEST_ID}'"
6673f904448Schristos	echo >&2 " - - - - - - - - - - - - - - - - -"
6683f904448Schristos	echo >&2 "${TEST_FAILURES}"
66986dd15a6Skre
6703f904448Schristos	atf_fail \
671706a2d4fSkre "Test ${TEST_ID}: $TEST_FAIL_COUNT (of $TEST_NUM) subtests failed - see stderr"
6723f904448Schristos}
6733f904448Schristos
67486dd15a6Skre####### End helpers
67586dd15a6Skre
6763f904448Schristosatf_test_case shell_params
6773f904448Schristosshell_params_head() {
678265b0617Skre	atf_set descr "Test correct operation of the numeric parameters"
6793f904448Schristos}
6803f904448Schristosshell_params_body() {
6813f904448Schristos	atf_require_prog mktemp
6823f904448Schristos
6833f904448Schristos	reset shell_params
6843f904448Schristos
6853f904448Schristos	check 'set -- a b c; echo "$#: $1 $2 $3"' '3: a b c' 0
6863f904448Schristos	check 'set -- a b c d e f g h i j k l m; echo "$#: ${1}0 ${10} $10"' \
6873f904448Schristos		'13: a0 j a0' 0
6883f904448Schristos	check 'x="$0"; set -- a b; y="$0";
6893f904448Schristos	      [ "x${x}y" = "x${y}y" ] && echo OK || echo x="$x" y="$y"' \
6903f904448Schristos		'OK' 0
6913f904448Schristos	check "${TEST_SH} -c 'echo 0=\$0 1=\$1 2=\$2' a b c" '0=a 1=b 2=c' 0
6923f904448Schristos
6933f904448Schristos	echo 'echo 0="$0" 1="$1" 2="$2"' > helper.sh
694c6dfc962Schristos	check "${TEST_SH} helper.sh a b c" '0=helper.sh 1=a 2=b' 0
6953f904448Schristos
6963f904448Schristos	check 'set -- a bb ccc dddd eeeee ffffff ggggggg hhhhhhhh \
6973f904448Schristos		iiiiiiiii jjjjjjjjjj kkkkkkkkkkk
6983f904448Schristos	       echo "${#}: ${#1} ${#2} ${#3} ${#4} ... ${#9} ${#10} ${#11}"' \
6993f904448Schristos		 '11: 1 2 3 4 ... 9 10 11' 0
7003f904448Schristos
7013f904448Schristos	check 'set -- a b c; echo "$#: ${1-A} ${2-B} ${3-C} ${4-D} ${5-E}"' \
7023f904448Schristos		'3: a b c D E' 0
7033f904448Schristos	check 'set -- a "" c "" e
7043f904448Schristos	       echo "$#: ${1:-A} ${2:-B} ${3:-C} ${4:-D} ${5:-E}"' \
7053f904448Schristos		'5: a B c D e' 0
7063f904448Schristos	check 'set -- a "" c "" e
7073f904448Schristos	       echo "$#: ${1:+A} ${2:+B} ${3:+C} ${4:+D} ${5:+E}"' \
7083f904448Schristos		'5: A  C  E' 0
7093f904448Schristos	check 'set -- "abab*cbb"
7103f904448Schristos	       echo "${1} ${1#a} ${1%b} ${1##ab} ${1%%b} ${1#*\*} ${1%\**}"' \
7113f904448Schristos	       'abab*cbb bab*cbb abab*cb ab*cbb abab*cb cbb abab' 0
7123f904448Schristos	check 'set -- "abab?cbb"
7133f904448Schristos    echo "${1}:${1#*a}+${1%b*}-${1##*a}_${1%%b*}%${1#[ab]}=${1%?*}/${1%\?*}"' \
7143f904448Schristos	       'abab?cbb:bab?cbb+abab?cb-b?cbb_a%bab?cbb=abab?cb/abab' 0
7153f904448Schristos	check 'set -- a "" c "" e; echo "${2:=b}"' '' 1
7163f904448Schristos
7173782bd48Skre	check 'set -- a b c d; echo ${4294967297}' '' 0  # result 'a' => ${1}
7183782bd48Skre	check 'set -- a b c; echo ${01}' 'a' 0
7193782bd48Skre	check "${TEST_SH} -c 'echo 0=\${00} 1=\${01} 2=\${02}' a b c" \
7203782bd48Skre			'0=a 1=b 2=c' 0
7213782bd48Skre
72225950655Skre	# by special request, for PaulG...  (${0...} is not octal!)
723a12c6cfaSkre
724a12c6cfaSkre	# Posix XCU 2.5.1 (Issue 7 TC2 pg 2349 lines 74835..6):
725a12c6cfaSkre	#   The digits denoting the positional parameters shall always
726a12c6cfaSkre	#   be interpreted as a decimal value, even if there is a leading zero.
727a12c6cfaSkre
72825950655Skre	check \
72925950655Skre	    'set -- a b c d e f g h i j k l m; echo "$#: ${08} ${010} ${011}"' \
73025950655Skre		'13: h j k' 0
73125950655Skre
7323f904448Schristos	results
7333f904448Schristos}
7343f904448Schristos
735706a2d4fSkreatf_test_case var_with_embedded_cmdsub
736706a2d4fSkrevar_with_embedded_cmdsub_head() {
737265b0617Skre	atf_set descr "Test expansion of vars with embedded cmdsub"
738706a2d4fSkre}
739706a2d4fSkrevar_with_embedded_cmdsub_body() {
740706a2d4fSkre
741706a2d4fSkre	reset var_with_embedded_cmdsub
742706a2d4fSkre
743706a2d4fSkre	check 'unset x; echo ${x-$(echo a)}$(echo b)'  'ab' 0	#1
744706a2d4fSkre	check 'unset x; echo ${x:-$(echo a)}$(echo b)' 'ab' 0	#2
745706a2d4fSkre	check 'x=""; echo ${x-$(echo a)}$(echo b)'     'b'  0	#3
746706a2d4fSkre	check 'x=""; echo ${x:-$(echo a)}$(echo b)'    'ab' 0	#4
747706a2d4fSkre	check 'x=c; echo ${x-$(echo a)}$(echo b)'      'cb' 0	#5
748706a2d4fSkre	check 'x=c; echo ${x:-$(echo a)}$(echo b)'     'cb' 0	#6
749706a2d4fSkre
750706a2d4fSkre	check 'unset x; echo ${x+$(echo a)}$(echo b)'  'b'  0	#7
751706a2d4fSkre	check 'unset x; echo ${x:+$(echo a)}$(echo b)' 'b'  0	#8
752706a2d4fSkre	check 'x=""; echo ${x+$(echo a)}$(echo b)'     'ab' 0	#9
753706a2d4fSkre	check 'x=""; echo ${x:+$(echo a)}$(echo b)'    'b'  0	#10
754706a2d4fSkre	check 'x=c; echo ${x+$(echo a)}$(echo b)'      'ab' 0	#11
755706a2d4fSkre	check 'x=c; echo ${x:+$(echo a)}$(echo b)'     'ab' 0	#12
756706a2d4fSkre
757706a2d4fSkre	check 'unset x; echo ${x=$(echo a)}$(echo b)'  'ab' 0	#13
758706a2d4fSkre	check 'unset x; echo ${x:=$(echo a)}$(echo b)' 'ab' 0	#14
759706a2d4fSkre	check 'x=""; echo ${x=$(echo a)}$(echo b)'     'b'  0	#15
760706a2d4fSkre	check 'x=""; echo ${x:=$(echo a)}$(echo b)'    'ab' 0	#16
761706a2d4fSkre	check 'x=c; echo ${x=$(echo a)}$(echo b)'      'cb' 0	#17
762706a2d4fSkre	check 'x=c; echo ${x:=$(echo a)}$(echo b)'     'cb' 0	#18
763706a2d4fSkre
764706a2d4fSkre	check 'unset x; echo ${x?$(echo a)}$(echo b)'  ''   2	#19
765706a2d4fSkre	check 'unset x; echo ${x:?$(echo a)}$(echo b)' ''   2	#20
766706a2d4fSkre	check 'x=""; echo ${x?$(echo a)}$(echo b)'     'b'  0	#21
767706a2d4fSkre	check 'x=""; echo ${x:?$(echo a)}$(echo b)'    ''   2	#22
768706a2d4fSkre	check 'x=c; echo ${x?$(echo a)}$(echo b)'      'cb' 0	#23
769706a2d4fSkre	check 'x=c; echo ${x:?$(echo a)}$(echo b)'     'cb' 0	#24
770706a2d4fSkre
771706a2d4fSkre	check 'unset x; echo ${x%$(echo a)}$(echo b)'  'b'  0	#25
772706a2d4fSkre	check 'unset x; echo ${x%%$(echo a)}$(echo b)' 'b'  0	#26
773706a2d4fSkre	check 'x=""; echo ${x%$(echo a)}$(echo b)'     'b'  0	#27
774706a2d4fSkre	check 'x=""; echo ${x%%$(echo a)}$(echo b)'    'b'  0	#28
775706a2d4fSkre	check 'x=c; echo ${x%$(echo a)}$(echo b)'      'cb' 0	#29
776706a2d4fSkre	check 'x=c; echo ${x%%$(echo a)}$(echo b)'     'cb' 0	#30
777706a2d4fSkre	check 'x=aa; echo ${x%$(echo "*a")}$(echo b)'  'ab' 0	#31
778706a2d4fSkre	check 'x=aa; echo ${x%%$(echo "*a")}$(echo b)' 'b'  0	#32
779706a2d4fSkre
780706a2d4fSkre	check 'unset x; echo ${x#$(echo a)}$(echo b)'  'b'  0	#33
781706a2d4fSkre	check 'unset x; echo ${x##$(echo a)}$(echo b)' 'b'  0	#34
782706a2d4fSkre	check 'x=""; echo ${x#$(echo a)}$(echo b)'     'b'  0	#35
783706a2d4fSkre	check 'x=""; echo ${x##$(echo a)}$(echo b)'    'b'  0	#36
784706a2d4fSkre	check 'x=c; echo ${x#$(echo a)}$(echo b)'      'cb' 0	#37
785706a2d4fSkre	check 'x=c; echo ${x##$(echo a)}$(echo b)'     'cb' 0	#38
786706a2d4fSkre	check 'x=aa; echo ${x#$(echo "*a")}$(echo b)'  'ab' 0	#39
787706a2d4fSkre	check 'x=aa; echo ${x##$(echo "*a")}$(echo b)' 'b'  0	#40
788706a2d4fSkre
789706a2d4fSkre	results
790706a2d4fSkre}
791706a2d4fSkre
792a30f469fSkreatf_test_case dollar_hash
793a30f469fSkredollar_hash_head() {
794265b0617Skre	atf_set descr 'Test expansion of various aspects of $#'
795a30f469fSkre}
796a30f469fSkredollar_hash_body() {
797a30f469fSkre
798a30f469fSkre#
799a30f469fSkre#	$# looks like it should be so simple that it doesn't really
800a30f469fSkre#	need a test of its own, and used in that way, it really doesn't.
801a30f469fSkre#	But when we add braces ${#} we need to deal with the three
802a30f469fSkre#	(almost 4) different meanings of a # inside a ${} expansion...
803a30f469fSkre#
80422a6d2ddSkre#	Note that some of these are just how we treat expansions that
80522a6d2ddSkre#	are unspecified by posix (as noted below.)
80622a6d2ddSkre#
807a30f469fSkre#		1.   ${#} is just $# (number of params)
808a30f469fSkre#		1.a	${\#} is nothing at all (error: invalid expansion)
809a30f469fSkre#		1.b	${\#...} (anything after) is the same (invalid)
810a30f469fSkre#		2.   ${#VAR} is the length of the value VAR
811a30f469fSkre#		2.a	Including ${##} - the length of ${#}
812a30f469fSkre#		3    ${VAR#pat} is the value of VAR with leading pat removed
813a30f469fSkre#		3.a	Including ${VAR#} which just removes leading nothing
814a30f469fSkre#			This is relevant in case of ${VAR#${X}} with X=''
81522a6d2ddSkre#				nb: not required by posix, see XCU 2.6.2
816a30f469fSkre#		3.b	${##} is not a case of 3.a but rather 2.a
817a30f469fSkre#		3.c	Yet ${##pat} is a case of 3.a
818a30f469fSkre#			Including ${##${X}} where X='' or X='#'
81922a6d2ddSkre#				nb: not required by posix, see XCU 2.6.2
820a30f469fSkre#		3.d	And ${#\#} is invalid (error)
821a30f469fSkre#		3.e	But ${##\#} removes a leading # from the value of $#
822a30f469fSkre#			(so is just $# as there is no leading # there)
82322a6d2ddSkre#				nb: not required by posix, see XCU 2.6.2
824a30f469fSkre#		4    ${VAR##pat} is the value of VAR with longest pat removed
825a30f469fSkre#		4.a	Including ${VAR##} which removes the longest nothing
826a30f469fSkre#		4.b	Which in this case includes ${###} (so is == $#)
82722a6d2ddSkre#				nb: not required by posix, see XCU 2.6.2
828a30f469fSkre#		4.c	But not ${##\#} which is $# with a leading '#' removed
829a30f469fSkre#			(and so is also == $#), i.e.: like ${###} but different.
83022a6d2ddSkre#				nb: not required by posix, see XCU 2.6.2
831a30f469fSkre#		4.d	As is ${###\#} or just ${####} - remove  # (so just $#)
83222a6d2ddSkre#				nb: not required by posix, see XCU 2.6.2
833a30f469fSkre#
834a30f469fSkre
835a30f469fSkre	reset dollar_hash
836a30f469fSkre
837a30f469fSkre	check 'set -- ; echo $#'			'0'		0  # 1
838a30f469fSkre	check 'set -- a b c; echo $#'			'3'		0  # 2
839a30f469fSkre	check 'set -- a b c d e f g h i j; echo $#'	'10'		0  # 3
840a30f469fSkre# rule 1
841a30f469fSkre	check 'set -- ; echo ${#}'			'0'		0  # 4
842a30f469fSkre	check 'set -- a b c; echo ${#}'			'3'		0  # 5
843a30f469fSkre	check 'set -- a b c d e f g h i j; echo ${#}'	'10'		0  # 6
844a30f469fSkre# rule 1.a
845a30f469fSkre	check 'set -- a b c; echo ${\#}'		''		2  # 7
846a30f469fSkre# rule 1.b
847a30f469fSkre	check 'set -- a b c; echo ${\#:-foo}'		''		2  # 8
848a30f469fSkre# rule 2
849a30f469fSkre	check 'VAR=12345; echo ${#VAR}'			'5'		0  # 9
850a30f469fSkre	check 'VAR=123456789012; echo ${#VAR}'		'12'		0  #10
851a30f469fSkre# rule 2.a
852a30f469fSkre	check 'set -- ; echo ${##}'			'1'		0  #11
853a30f469fSkre	check 'set -- a b c; echo ${##}'		'1'		0  #12
854a30f469fSkre	check 'set -- a b c d e f g h i j; echo ${##}'	'2'		0  #13
855a30f469fSkre# rule 3
856a30f469fSkre	check 'VAR=12345; echo ${VAR#1}'		'2345'		0  #14
857a30f469fSkre	check 'VAR=12345; echo ${VAR#2}'		'12345'		0  #15
858a30f469fSkre	check 'VAR=#2345; echo ${VAR#\#}'		'2345'		0  #16
859a30f469fSkre	check 'X=1; VAR=12345; echo ${VAR#${X}}'	'2345'		0  #17
860a30f469fSkre	check 'X=1; VAR=#2345; echo ${VAR#${X}}'	'#2345'		0  #18
861a30f469fSkre# rule 3.a
862a30f469fSkre	check 'VAR=12345; echo ${VAR#}'			'12345'		0  #19
863a30f469fSkre	check 'X=; VAR=12345; echo ${VAR#${X}}'		'12345'		0  #20
864a30f469fSkre# rule 3.b (tested above, rule 2.a)
865a30f469fSkre# rule 3.c
866a30f469fSkre	check 'set -- ; echo ${##0}'			''		0  #21
867a30f469fSkre	check 'set -- a b c; echo ${##1}'		'3'		0  #22
868a30f469fSkre	check 'set -- a b c d e f g h i j; echo ${##1}'	'0'		0  #23
869a30f469fSkre	check 'X=0; set -- ; echo ${##${X}}'		''		0  #24
870a30f469fSkre	check 'X=; set -- ; echo ${##${X}}'		'0'		0  #25
871a30f469fSkre	check 'X=1; set -- a b c; echo ${##${X}}'	'3'		0  #26
872a30f469fSkre	check 'X=1; set -- a b c d e f g h i j; echo ${##${X}}'	'0'	0  #27
873a30f469fSkre	check 'X=; set -- a b c d e f g h i j; echo ${##${X}}'	'10'	0  #28
874a30f469fSkre	check 'X=#; VAR=#2345; echo ${VAR#${X}}'	'2345'		0  #29
875a30f469fSkre	check 'X=#; VAR=12345; echo ${VAR#${X}}'	'12345'		0  #30
876a30f469fSkre# rule 3.d
877a30f469fSkre	check 'set -- a b c; echo ${#\#}'		''		2  #31
878a30f469fSkre# rule 3.e
879a30f469fSkre	check 'set -- ; echo ${##\#}'			'0'		0  #32
880a30f469fSkre	check 'set -- a b c d e f g h i j; echo ${##\#}' '10'		0  #33
881a30f469fSkre
882a30f469fSkre# rule 4
883a30f469fSkre	check 'VAR=12345; echo ${VAR##1}'		'2345'		0  #34
884a30f469fSkre	check 'VAR=12345; echo ${VAR##\1}'		'2345'		0  #35
885a30f469fSkre# rule 4.a
886a30f469fSkre	check 'VAR=12345; echo ${VAR##}'		'12345'		0  #36
887a30f469fSkre# rule 4.b
888a30f469fSkre	check 'set -- ; echo ${###}'			'0'		0  #37
889a30f469fSkre	check 'set -- a b c d e f g h i j; echo ${###}'	'10'		0  #38
890a30f469fSkre# rule 4.c
891a30f469fSkre	check 'VAR=12345; echo ${VAR#\#}'		'12345'		0  #39
892a30f469fSkre	check 'VAR=12345; echo ${VAR#\#1}'		'12345'		0  #40
893a30f469fSkre	check 'VAR=#2345; echo ${VAR#\#}'		'2345'		0  #41
894a30f469fSkre	check 'VAR=#12345; echo ${VAR#\#1}'		'2345'		0  #42
895a30f469fSkre	check 'VAR=#2345; echo ${VAR#\#1}'		'#2345'		0  #43
896a30f469fSkre	check 'set -- ; echo ${####}'			'0'		0  #44
897a30f469fSkre	check 'set -- ; echo ${###\#}'			'0'		0  #45
898a30f469fSkre	check 'set -- a b c d e f g h i j; echo ${####}' '10'		0  #46
899a30f469fSkre	check 'set -- a b c d e f g h i j; echo ${###\#}' '10'		0  #47
900a30f469fSkre
901a30f469fSkre# now check for some more utter nonsense, not mentioned in the rules
902a30f469fSkre# above (doesn't need to be)
903a30f469fSkre
904a30f469fSkre	check 'x=hello; set -- a b c; echo ${#x:-1}'	''		2  #48
905a30f469fSkre	check 'x=hello; set -- a b c; echo ${#x-1}'	''		2  #49
906a30f469fSkre	check 'x=hello; set -- a b c; echo ${#x:+1}'	''		2  #50
907a30f469fSkre	check 'x=hello; set -- a b c; echo ${#x+1}'	''		2  #51
908a30f469fSkre	check 'x=hello; set -- a b c; echo ${#x+1}'	''		2  #52
909a30f469fSkre	check 'x=hello; set -- a b c; echo ${#x:?msg}'	''		2  #53
910a30f469fSkre	check 'x=hello; set -- a b c; echo ${#x?msg}'	''		2  #54
911a30f469fSkre	check 'x=hello; set -- a b c; echo ${#x:=val}'	''		2  #55
912a30f469fSkre	check 'x=hello; set -- a b c; echo ${#x=val}'	''		2  #56
913a30f469fSkre	check 'x=hello; set -- a b c; echo ${#x#h}'	''		2  #57
914a30f469fSkre	check 'x=hello; set -- a b c; echo ${#x#*l}'	''		2  #58
915a30f469fSkre	check 'x=hello; set -- a b c; echo ${#x##*l}'	''		2  #59
916a30f469fSkre	check 'x=hello; set -- a b c; echo ${#x%o}'	''		2  #60
917a30f469fSkre	check 'x=hello; set -- a b c; echo ${#x%l*}'	''		2  #61
918a30f469fSkre	check 'x=hello; set -- a b c; echo ${#x%%l*}'	''		2  #62
919a30f469fSkre
920a30f469fSkre# but just to be complete, these ones should work
921a30f469fSkre
922a30f469fSkre	check 'x=hello; set -- a b c; echo ${#%5}'	'3'		0  #63
923a30f469fSkre	check 'x=hello; set -- a b c; echo ${#%3}'	''		0  #64
924a30f469fSkre	check 'x=hello; set -- a b c; echo ${#%?}'	''		0  #65
925a30f469fSkre	check 'X=#; set -- a b c; echo ${#%${X}}'	'3'		0  #66
926a30f469fSkre	check 'X=3; set -- a b c; echo ${#%${X}}'	''		0  #67
927a30f469fSkre	check 'set -- a b c; echo ${#%%5}'		'3'		0  #68
928a30f469fSkre	check 'set -- a b c; echo ${#%%3}'		''		0  #69
929a30f469fSkre	check 'set -- a b c d e f g h i j k l; echo ${#%1}' '12'	0  #70
930a30f469fSkre	check 'set -- a b c d e f g h i j k l; echo ${#%2}' '1'		0  #71
931a30f469fSkre	check 'set -- a b c d e f g h i j k l; echo ${#%?}' '1'		0  #72
932a30f469fSkre	check 'set -- a b c d e f g h i j k l; echo ${#%[012]}' '1'	0  #73
933a30f469fSkre	check 'set -- a b c d e f g h i j k l; echo ${#%[0-4]}' '1'	0  #74
934a30f469fSkre	check 'set -- a b c d e f g h i j k l; echo ${#%?2}' ''		0  #75
935a30f469fSkre	check 'set -- a b c d e f g h i j k l; echo ${#%1*}' ''		0  #76
936a30f469fSkre	check 'set -- a b c d e f g h i j k l; echo ${#%%2}' '1'	0  #77
937a30f469fSkre	check 'set -- a b c d e f g h i j k l; echo ${#%%1*}' ''	0  #78
938a30f469fSkre
939a30f469fSkre# and this lot are stupid, as $# is never unset or null, but they do work...
940a30f469fSkre
941a30f469fSkre	check 'set -- a b c; echo ${#:-99}'		'3'		0  #79
942a30f469fSkre	check 'set -- a b c; echo ${#-99}'		'3'		0  #80
943a30f469fSkre	check 'set -- a b c; echo ${#:+99}'		'99'		0  #81
944a30f469fSkre	check 'set -- a b c; echo ${#+99}'		'99'		0  #82
945a30f469fSkre	check 'set -- a b c; echo ${#:?bogus}'		'3'		0  #83
946a30f469fSkre	check 'set -- a b c; echo ${#?bogus}'		'3'		0  #84
947a30f469fSkre
948a30f469fSkre# even this utter nonsense is OK, as while special params cannot be
949a30f469fSkre# set this way, here, as $# is not unset, or null, the assignment
950a30f469fSkre# never happens (isn't even attempted)
951a30f469fSkre
952a30f469fSkre	check 'set -- a b c; echo ${#:=bogus}'		'3'		0  #85
953a30f469fSkre	check 'set -- a b c; echo ${#=bogus}'		'3'		0  #86
954a30f469fSkre
95522a6d2ddSkre	for n in 0 1 10 25 100				#87 #88 #89 #90 #91
95622a6d2ddSkre	do
95722a6d2ddSkre		check "(exit $n)"'; echo ${#?}'		"${#n}"		0
95822a6d2ddSkre	done
95922a6d2ddSkre
96022a6d2ddSkre	results		# results so far anyway...
96122a6d2ddSkre
96222a6d2ddSkre# now we have some harder to verify cases, as they (must) check unknown values
96322a6d2ddSkre# and hence the resuls cannot just be built into the script, but we have
96422a6d2ddSkre# to use some tricks to validate them, so for these, just do regular testing
96522a6d2ddSkre# and don't attempt to use the check helper function, nor to include
96622a6d2ddSkre# these tests in the result summary.   If anything has already failed, we
96722a6d2ddSkre# do not get this far...   From here on, first failure ends this test.
96822a6d2ddSkre
96922a6d2ddSkre	for opts in '' '-a' '-au' '-auf' '-aufe' # options safe enough to set
97022a6d2ddSkre	do
97122a6d2ddSkre		# Note the shell might have other (or these) opts set already
97222a6d2ddSkre
97322a6d2ddSkre		RES=$(${TEST_SH} -c "test -n '${opts}' && set ${opts};
97422a6d2ddSkre			printf '%s' \"\$-\";printf ' %s\\n' \"\${#-}\"") ||
97522a6d2ddSkre			atf_fail '${#-} test exited with status '"$?"
97622a6d2ddSkre		LEN="${RES##* }"
97722a6d2ddSkre		DMINUS="${RES% ${LEN}}"
97822a6d2ddSkre		if [ "${#DMINUS}" != "${LEN}" ]
97922a6d2ddSkre		then
98022a6d2ddSkre			atf_fail \
98122a6d2ddSkre		   '${#-} test'" produced ${LEN} for opts ${DMINUS} (${RES})"
98222a6d2ddSkre		fi
98322a6d2ddSkre	done
98422a6d2ddSkre
98522a6d2ddSkre	for seq in a b c d e f g h i j
98622a6d2ddSkre	do
98722a6d2ddSkre		# now we are tryin to generate different pids for $$ and $!
98822a6d2ddSkre		# so we just run the test a number of times, and hope...
98922a6d2ddSkre		# On NetBSD pid randomisation will usually help the tests
99022a6d2ddSkre
99122a6d2ddSkre		eval "$(${TEST_SH} -c \
99222a6d2ddSkre		    '(exit 0)& BG=$! LBG=${#!};
99322a6d2ddSkre	    printf "SH=%s BG=%s LSH=%s LBG=%s" "$$" "$BG" "${#$}" "$LBG";
99422a6d2ddSkre		      wait')"
99522a6d2ddSkre
99622a6d2ddSkre		if [ "${#SH}" != "${LSH}" ] || [ "${#BG}" != "${LBG}" ]
99722a6d2ddSkre		then
99822a6d2ddSkre			atf_fail \
99922a6d2ddSkre	'${#!] of '"${BG} was ${LBG}, expected ${#BG}"'; ${#$} of '"${SH} was ${LSH}, expected ${#SH}"
100022a6d2ddSkre		fi
100122a6d2ddSkre	done
1002a30f469fSkre}
1003a30f469fSkre
100486dd15a6Skreatf_test_case dollar_star
100586dd15a6Skredollar_star_head() {
1006265b0617Skre	atf_set descr 'Test expansion of various aspects of $*'
100786dd15a6Skre}
100886dd15a6Skredollar_star_body() {
100986dd15a6Skre
1010a30f469fSkre	reset dollar_star
101186dd15a6Skre
101286dd15a6Skre	check 'set -- a b c; echo $# $*'		'3 a b c'	0  # 1
101386dd15a6Skre	check 'set -- a b c; echo $# "$*"'		'3 a b c'	0  # 2
101486dd15a6Skre	check 'set -- a "b c"; echo $# $*'		'2 a b c'	0  # 3
101586dd15a6Skre	check 'set -- a "b c"; echo $# "$*"'		'2 a b c'	0  # 4
101686dd15a6Skre	check 'set -- a b c; set -- $* ; echo $# $*'	'3 a b c'	0  # 5
101786dd15a6Skre	check 'set -- a b c; set -- "$*" ; echo $# $*'	'1 a b c'	0  # 6
101886dd15a6Skre	check 'set -- a "b c"; set -- $* ; echo $# $*'	'3 a b c'	0  # 7
101986dd15a6Skre	check 'set -- a "b c"; set -- "$*" ; echo $# $*' \
102086dd15a6Skre							'1 a b c'	0  # 8
102186dd15a6Skre
102286dd15a6Skre	check 'IFS=". "; set -- a b c; echo $# $*'	'3 a b c'	0  # 9
102386dd15a6Skre	check 'IFS=". "; set -- a b c; echo $# "$*"'	'3 a.b.c'	0  #10
102486dd15a6Skre	check 'IFS=". "; set -- a "b c"; echo $# $*'	'2 a b c'	0  #11
102586dd15a6Skre	check 'IFS=". "; set -- a "b c"; echo $# "$*"'	'2 a.b c'	0  #12
102686dd15a6Skre	check 'IFS=". "; set -- a "b.c"; echo $# $*'	'2 a b c'	0  #13
102786dd15a6Skre	check 'IFS=". "; set -- a "b.c"; echo $# "$*"'	'2 a.b.c'	0  #14
102886dd15a6Skre	check 'IFS=". "; set -- a b c; set -- $* ; echo $# $*' \
102986dd15a6Skre							'3 a b c'	0  #15
103086dd15a6Skre	check 'IFS=". "; set -- a b c; set -- "$*" ; echo $# $*' \
103186dd15a6Skre							'1 a b c'	0  #16
103286dd15a6Skre	check 'IFS=". "; set -- a "b c"; set -- $* ; echo $# $*' \
103386dd15a6Skre							'3 a b c'	0  #17
103486dd15a6Skre	check 'IFS=". "; set -- a "b c"; set -- "$*" ; echo $# $*' \
103586dd15a6Skre							'1 a b c'	0  #18
103686dd15a6Skre	check 'IFS=". "; set -- a b c; set -- $* ; echo $# "$*"' \
103786dd15a6Skre							'3 a.b.c'	0  #19
103886dd15a6Skre	check 'IFS=". "; set -- a b c; set -- "$*" ; echo $# "$*"' \
103986dd15a6Skre							'1 a.b.c'	0  #20
104086dd15a6Skre	check 'IFS=". "; set -- a "b c"; set -- $* ; echo $# "$*"' \
104186dd15a6Skre							'3 a.b.c'	0  #21
104286dd15a6Skre	check 'IFS=". "; set -- a "b c"; set -- "$*" ; echo $# "$*"' \
104386dd15a6Skre							'1 a.b c'	0  #22
104486dd15a6Skre
104586dd15a6Skre	results
104686dd15a6Skre}
104786dd15a6Skre
104886dd15a6Skreatf_test_case dollar_star_in_word
104986dd15a6Skredollar_star_in_word_head() {
1050265b0617Skre	atf_set descr 'Test expansion $* occurring in word of ${var:-word}'
105186dd15a6Skre}
105286dd15a6Skredollar_star_in_word_body() {
105386dd15a6Skre
105486dd15a6Skre	reset dollar_star_in_word
105586dd15a6Skre
105686dd15a6Skre	unset xXx			; # just in case!
105786dd15a6Skre
105886dd15a6Skre	# Note that the expected results for these tests are identical
105986dd15a6Skre	# to those from the dollar_star test.   It should never make
106086dd15a6Skre	# a difference whether we expand $* or ${unset:-$*}
106186dd15a6Skre
106286dd15a6Skre	# (note expanding ${unset:-"$*"} is different, that is not tested here)
106386dd15a6Skre
106486dd15a6Skre	check 'set -- a b c; echo $# ${xXx:-$*}'		'3 a b c' 0  # 1
106586dd15a6Skre	check 'set -- a b c; echo $# "${xXx:-$*}"'		'3 a b c' 0  # 2
106686dd15a6Skre	check 'set -- a "b c"; echo $# ${xXx:-$*}'		'2 a b c' 0  # 3
106786dd15a6Skre	check 'set -- a "b c"; echo $# "${xXx:-$*}"'		'2 a b c' 0  # 4
106886dd15a6Skre	check 'set -- a b c; set -- ${xXx:-$*} ; echo $# $*'	'3 a b c' 0  # 5
106986dd15a6Skre	check 'set -- a b c; set -- "${xXx:-$*}" ; echo $# $*'	'1 a b c' 0  # 6
107086dd15a6Skre	check 'set -- a "b c"; set -- ${xXx:-$*} ; echo $# $*'	'3 a b c' 0  # 7
107186dd15a6Skre	check 'set -- a "b c"; set -- "${xXx:-$*}" ; echo $# $*' \
107286dd15a6Skre								'1 a b c' 0  # 8
107386dd15a6Skre
107486dd15a6Skre	check 'IFS=". "; set -- a b c; echo $# ${xXx:-$*}'	'3 a b c' 0  # 9
107586dd15a6Skre	check 'IFS=". "; set -- a b c; echo $# "${xXx:-$*}"'	'3 a.b.c' 0  #10
107686dd15a6Skre	check 'IFS=". "; set -- a "b c"; echo $# ${xXx:-$*}'	'2 a b c' 0  #11
107786dd15a6Skre	check 'IFS=". "; set -- a "b c"; echo $# "${xXx:-$*}"'	'2 a.b c' 0  #12
107886dd15a6Skre	check 'IFS=". "; set -- a "b.c"; echo $# ${xXx:-$*}'	'2 a b c' 0  #13
107986dd15a6Skre	check 'IFS=". "; set -- a "b.c"; echo $# "${xXx:-$*}"'	'2 a.b.c' 0  #14
108086dd15a6Skre	check 'IFS=". ";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
108186dd15a6Skre								'3 a b c' 0  #15
108286dd15a6Skre	check 'IFS=". ";set -- a b c;set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
108386dd15a6Skre								'1 a b c' 0  #16
108486dd15a6Skre	check 'IFS=". ";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
108586dd15a6Skre								'3 a b c' 0  #17
108686dd15a6Skre	check 'IFS=". ";set -- a "b c";set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
108786dd15a6Skre								'1 a b c' 0  #18
108886dd15a6Skre	check 'IFS=". ";set -- a b c;set -- ${xXx:-$*};echo $# "${xXx:-$*}"' \
108986dd15a6Skre								'3 a.b.c' 0  #19
109086dd15a6Skre	check 'IFS=". ";set -- a b c;set -- "$*";echo $# "$*"' \
109186dd15a6Skre								'1 a.b.c' 0  #20
109286dd15a6Skre	check 'IFS=". ";set -- a "b c";set -- $*;echo $# "$*"' \
109386dd15a6Skre								'3 a.b.c' 0  #21
109486dd15a6Skre	check 'IFS=". ";set -- a "b c";set -- "$*";echo $# "$*"' \
109586dd15a6Skre								'1 a.b c' 0  #22
109686dd15a6Skre
109786dd15a6Skre	results
109886dd15a6Skre}
109986dd15a6Skre
110086dd15a6Skreatf_test_case dollar_star_with_empty_ifs
110186dd15a6Skredollar_star_with_empty_ifs_head() {
1102265b0617Skre	atf_set descr 'Test expansion of $* with IFS=""'
110386dd15a6Skre}
110486dd15a6Skredollar_star_with_empty_ifs_body() {
110586dd15a6Skre
110686dd15a6Skre	reset dollar_star_with_empty_ifs
110786dd15a6Skre
110886dd15a6Skre	check 'IFS=""; set -- a b c; echo $# $*'	'3 a b c'	0  # 1
110986dd15a6Skre	check 'IFS=""; set -- a b c; echo $# "$*"'	'3 abc'		0  # 2
111086dd15a6Skre	check 'IFS=""; set -- a "b c"; echo $# $*'	'2 a b c'	0  # 3
111186dd15a6Skre	check 'IFS=""; set -- a "b c"; echo $# "$*"'	'2 ab c'	0  # 4
111286dd15a6Skre	check 'IFS=""; set -- a "b.c"; echo $# $*'	'2 a b.c'	0  # 5
111386dd15a6Skre	check 'IFS=""; set -- a "b.c"; echo $# "$*"'	'2 ab.c'	0  # 6
111486dd15a6Skre	check 'IFS=""; set -- a b c; set -- $* ; echo $# $*' \
111586dd15a6Skre							'3 a b c'	0  # 7
111686dd15a6Skre	check 'IFS=""; set -- a b c; set -- "$*" ; echo $# $*' \
111786dd15a6Skre							'1 abc'		0  # 8
111886dd15a6Skre	check 'IFS=""; set -- a "b c"; set -- $* ; echo $# $*' \
111986dd15a6Skre							'2 a b c'	0  # 9
112086dd15a6Skre	check 'IFS=""; set -- a "b c"; set -- "$*" ; echo $# $*' \
112186dd15a6Skre							'1 ab c'	0  #10
112286dd15a6Skre	check 'IFS=""; set -- a b c; set -- $* ; echo $# "$*"' \
112386dd15a6Skre							'3 abc'		0  #11
112486dd15a6Skre	check 'IFS=""; set -- a b c; set -- "$*" ; echo $# "$*"' \
112586dd15a6Skre							'1 abc'		0  #12
112686dd15a6Skre	check 'IFS=""; set -- a "b c"; set -- $* ; echo $# "$*"' \
112786dd15a6Skre							'2 ab c'	0  #13
112886dd15a6Skre	check 'IFS=""; set -- a "b c"; set -- "$*" ; echo $# "$*"' \
112986dd15a6Skre							'1 ab c'	0  #14
113086dd15a6Skre
1131a787cac6Skre	results	  # FIXED: 'PR bin/52090 expect 7 of 14 subtests to fail'
113286dd15a6Skre}
113386dd15a6Skre
113486dd15a6Skreatf_test_case dollar_star_in_word_empty_ifs
113586dd15a6Skredollar_star_in_word_empty_ifs_head() {
1136265b0617Skre	atf_set descr 'Test expansion of ${unset:-$*} with IFS=""'
113786dd15a6Skre}
113886dd15a6Skredollar_star_in_word_empty_ifs_body() {
113986dd15a6Skre
114086dd15a6Skre	reset dollar_star_in_word_empty_ifs
114186dd15a6Skre
114286dd15a6Skre	unset xXx			; # just in case
114386dd15a6Skre
114486dd15a6Skre	# Note that the expected results for these tests are identical
114586dd15a6Skre	# to those from the dollar_star_with_empty_ifs test.   It should
114686dd15a6Skre	# never make a difference whether we expand $* or ${unset:-$*}
114786dd15a6Skre
114886dd15a6Skre	# (note expanding ${unset:-"$*"} is different, that is not tested here)
114986dd15a6Skre
115086dd15a6Skre	check 'IFS="";set -- a b c;echo $# ${xXx:-$*}'		'3 a b c' 0  # 1
115186dd15a6Skre	check 'IFS="";set -- a b c;echo $# "${xXx:-$*}"'	'3 abc'	  0  # 2
115286dd15a6Skre	check 'IFS="";set -- a "b c";echo $# ${xXx:-$*}'	'2 a b c' 0  # 3
115386dd15a6Skre	check 'IFS="";set -- a "b c";echo $# "${xXx:-$*}"'	'2 ab c'  0  # 4
115486dd15a6Skre	check 'IFS="";set -- a "b.c";echo $# ${xXx:-$*}'	'2 a b.c' 0  # 5
115586dd15a6Skre	check 'IFS="";set -- a "b.c";echo $# "${xXx:-$*}"'	'2 ab.c'  0  # 6
115686dd15a6Skre	check 'IFS="";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
115786dd15a6Skre								'3 a b c' 0  # 7
115886dd15a6Skre	check 'IFS="";set -- a b c;set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
115986dd15a6Skre								'1 abc'   0  # 8
116086dd15a6Skre	check 'IFS="";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-$*}' \
116186dd15a6Skre								'2 a b c' 0  # 9
116286dd15a6Skre	check 'IFS="";set -- a "b c";set -- "${xXx:-$*}";echo $# ${xXx:-$*}' \
116386dd15a6Skre								'1 ab c'  0  #10
116486dd15a6Skre	check 'IFS="";set -- a b c;set -- ${xXx:-$*};echo $# "${xXx:-$*}"' \
116586dd15a6Skre								'3 abc'	  0  #11
116686dd15a6Skre	check 'IFS="";set -- a b c;set -- "${xXx:-$*}";echo $# "${xXx:-$*}"' \
116786dd15a6Skre								'1 abc'	  0  #12
116886dd15a6Skre	check 'IFS="";set -- a "b c";set -- ${xXx:-$*};echo $# "${xXx:-$*}"' \
116986dd15a6Skre								'2 ab c'  0  #13
117086dd15a6Skre	check 'IFS="";set -- a "b c";set -- "${xXx:-$*}";echo $# "${xXx:-$*}"' \
117186dd15a6Skre								'1 ab c'  0  #14
117286dd15a6Skre
1173a787cac6Skre	results	  # FIXED: 'PR bin/52090 expect 7 of 14 subtests to fail'
117486dd15a6Skre}
117586dd15a6Skre
117686dd15a6Skreatf_test_case dollar_star_in_quoted_word
117786dd15a6Skredollar_star_in_quoted_word_head() {
1178265b0617Skre	atf_set descr 'Test expansion $* occurring in word of ${var:-"word"}'
117986dd15a6Skre}
118086dd15a6Skredollar_star_in_quoted_word_body() {
118186dd15a6Skre
118286dd15a6Skre	reset dollar_star_in_quoted_word
118386dd15a6Skre
118486dd15a6Skre	unset xXx			; # just in case!
118586dd15a6Skre
118686dd15a6Skre	check 'set -- a b c; echo $# ${xXx:-"$*"}'		'3 a b c' 0  # 1
118786dd15a6Skre	check 'set -- a "b c"; echo $# ${xXx:-"$*"}'		'2 a b c' 0  # 2
118886dd15a6Skre	check 'set -- a b c; set -- ${xXx:-"$*"} ; echo $# ${xXx-"$*"}' \
118986dd15a6Skre								'1 a b c' 0  # 3
119086dd15a6Skre	check 'set -- a "b c"; set -- ${xXx:-"$*"} ; echo $# ${xXx-"$*"}' \
119186dd15a6Skre								'1 a b c' 0  # 4
119286dd15a6Skre	check 'set -- a b c; set -- ${xXx:-"$*"} ; echo $# ${xXx-"$*"}' \
119386dd15a6Skre								'1 a b c' 0  # 5
119486dd15a6Skre	check 'set -- a "b c"; set -- ${xXx:-"$*"} ; echo $# ${xXx-$*}' \
119586dd15a6Skre								'1 a b c' 0  # 6
119686dd15a6Skre	check 'set -- a b c; set -- ${xXx:-$*} ; echo $# ${xXx-"$*"}' \
119786dd15a6Skre								'3 a b c' 0  # 7
119886dd15a6Skre	check 'set -- a "b c"; set -- ${xXx:-$*} ; echo $# ${xXx-"$*"}' \
119986dd15a6Skre								'3 a b c' 0  # 8
120086dd15a6Skre
120186dd15a6Skre	check 'IFS=". "; set -- a b c; echo $# ${xXx:-"$*"}'	'3 a.b.c' 0  # 9
120286dd15a6Skre	check 'IFS=". "; set -- a "b c"; echo $# ${xXx:-"$*"}'	'2 a.b c' 0  #10
120386dd15a6Skre	check 'IFS=". "; set -- a "b.c"; echo $# ${xXx:-"$*"}'	'2 a.b.c' 0  #11
120486dd15a6Skre	check 'IFS=". ";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
120586dd15a6Skre								'1 a.b.c' 0  #12
120686dd15a6Skre      check 'IFS=". ";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
120786dd15a6Skre								'1 a.b c' 0  #13
120886dd15a6Skre	check 'IFS=". ";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
120986dd15a6Skre								'3 a.b.c' 0  #14
121086dd15a6Skre	check 'IFS=". ";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
121186dd15a6Skre								'3 a.b.c' 0  #15
121286dd15a6Skre	check 'IFS=". ";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
121386dd15a6Skre								'1 a b c' 0  #16
121486dd15a6Skre	check 'IFS=". ";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
121586dd15a6Skre								'1 a b c' 0  #17
121686dd15a6Skre
121786dd15a6Skre	check 'IFS="";set -- a b c;echo $# ${xXx:-"$*"}'	'3 abc'   0  #18
121886dd15a6Skre	check 'IFS="";set -- a "b c";echo $# ${xXx:-"$*"}'	'2 ab c'  0  #19
121986dd15a6Skre	check 'IFS="";set -- a "b.c";echo $# ${xXx:-"$*"}'	'2 ab.c'  0  #20
122086dd15a6Skre	check 'IFS="";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
122186dd15a6Skre								'1 abc'   0  #21
122286dd15a6Skre	check 'IFS="";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-"$*"}' \
122386dd15a6Skre								'1 ab c'  0  #22
122486dd15a6Skre	check 'IFS="";set -- a b c;set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
122586dd15a6Skre								'3 abc'   0  #23
122686dd15a6Skre	check 'IFS="";set -- a "b c";set -- ${xXx:-$*};echo $# ${xXx:-"$*"}' \
122786dd15a6Skre								'2 ab c'  0  #24
122886dd15a6Skre	check 'IFS="";set -- a b c;set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
122986dd15a6Skre								'1 abc'   0  #25
123086dd15a6Skre	check 'IFS="";set -- a "b c";set -- ${xXx:-"$*"};echo $# ${xXx:-$*}' \
123186dd15a6Skre								'1 ab c'  0  #26
123286dd15a6Skre
1233a787cac6Skre	results	  # FIXED: 'PR bin/52090 - 2 of 26 subtests expected to fail'
123486dd15a6Skre}
123586dd15a6Skre
1236265b0617Skreatf_test_case dollar_at_in_field_split_context
1237265b0617Skredollar_at_in_field_split_context_head() {
1238265b0617Skre	atf_set descr 'Test "$@" wth field splitting -- PR bin/54112'
1239265b0617Skre}
1240265b0617Skredollar_at_in_field_split_context_body() {
1241265b0617Skre	reset dollar_at_in_field_split_context
1242265b0617Skre
1243265b0617Skre		# the simple case (no field split) which always worked
1244265b0617Skre	check 'set -- ""; set -- ${0+"$@"}; echo $#'		1	0   #1
1245265b0617Skre
1246265b0617Skre		# The original failure case from the bash-bug list
1247265b0617Skre	check 'set -- ""; set -- ${0+"$@" "$@"}; echo $#'	2	0   #2
1248265b0617Skre
1249265b0617Skre		# slightly simpler cases that triggered the same issue
1250265b0617Skre	check 'set -- ""; set -- ${0+"$@" }; echo $#'		1	0   #3
1251265b0617Skre	check 'set -- ""; set -- ${0+ "$@"}; echo $#'		1	0   #4
1252265b0617Skre	check 'set -- ""; set -- ${0+ "$@" }; echo $#'		1	0   #5
1253265b0617Skre
1254265b0617Skre		# and the bizarre
1255265b0617Skre	check 'set -- ""; set -- ${0+"$@" "$@" "$@"}; echo $#'	3	0   #6
1256265b0617Skre
1257265b0617Skre	# repeat tests when there is more than one set empty numeric param
1258265b0617Skre
1259265b0617Skre	check 'set -- "" ""; set -- ${0+"$@"}; echo $#'		2	0   #7
1260265b0617Skre	check 'set -- "" ""; set -- ${0+"$@" "$@"}; echo $#'	4	0   #8
1261265b0617Skre	check 'set -- "" ""; set -- ${0+"$@" }; echo $#'	2	0   #9
1262265b0617Skre	check 'set -- "" ""; set -- ${0+ "$@"}; echo $#'	2	0   #10
1263265b0617Skre	check 'set -- "" ""; set -- ${0+ "$@" }; echo $#'	2	0   #11
1264265b0617Skre	check 'set -- "" ""; set -- ${0+"$@" "$@" "$@"}; echo $#' \
1265265b0617Skre								6	0   #12
1266265b0617Skre
1267265b0617Skre		# Next some checks of the way the NetBSD shell
1268265b0617Skre		# interprets some expressions that are POSIX unspecified.
1269265b0617Skre		# Other shells might fail these tests, without that
1270265b0617Skre		# being a problem.   We retain these tests so accidental
1271265b0617Skre		# changes in our behaviour can be detected.
1272265b0617Skre
1273265b0617Skre	check 'set --; X=; set -- "$X$@"; echo $#'		0	0   #13
1274265b0617Skre	check 'set --; X=; set -- "$@$X"; echo $#'		0	0   #14
1275265b0617Skre	check 'set --; X=; set -- "$X$@$X"; echo $#'		0	0   #15
1276265b0617Skre	check 'set --; X=; set -- "$@$@"; echo $#'		0	0   #16
1277265b0617Skre
1278265b0617Skre	check 'set -- ""; X=; set -- "$X$@"; echo $#'		1	0   #17
1279265b0617Skre	check 'set -- ""; X=; set -- "$@$X"; echo $#'		1	0   #19
1280265b0617Skre	check 'set -- ""; X=; set -- "$X$@$X"; echo $#'		1	0   #19
1281265b0617Skre	check 'set -- ""; X=; set -- "$@$@"; echo $#'		1	0   #20
1282265b0617Skre
1283265b0617Skre	check 'set -- "" ""; X=; set -- "$X$@"; echo $#'	2	0   #21
1284265b0617Skre	check 'set -- "" ""; X=; set -- "$@$X"; echo $#'	2	0   #22
1285265b0617Skre	check 'set -- "" ""; X=; set -- "$X$@$X"; echo $#'	2	0   #23
1286265b0617Skre		# Yes, this next one really is (and should be) 3...
1287265b0617Skre	check 'set -- "" ""; X=; set -- "$@$@"; echo $#'	3	0   #24
1288265b0617Skre
1289265b0617Skre	results
1290265b0617Skre}
1291265b0617Skre
12927fea2a9dSkreatf_test_case embedded_nl
12937fea2a9dSkreembedded_nl_head() {
1294265b0617Skre	atf_set descr 'Test literal \n in xxx string in ${var-xxx}'
12957fea2a9dSkre}
12967fea2a9dSkreembedded_nl_body() {
12977fea2a9dSkre
12987fea2a9dSkre	atf_check -s exit:0 -o inline:'a\nb\n' -e empty ${TEST_SH} <<- 'EOF'
12997fea2a9dSkre		unset V
13007fea2a9dSkre		X="${V-a
13017fea2a9dSkre		b}"
13027fea2a9dSkre		printf '%s\n' "${X}"
13037fea2a9dSkre		EOF
13047fea2a9dSkre
13057fea2a9dSkre	atf_check -s exit:0 -o inline:'a\nb\n' -e empty ${TEST_SH} <<- 'EOF'
13067fea2a9dSkre		unset V
13077fea2a9dSkre		X=${V-"a
13087fea2a9dSkre		b"}
13097fea2a9dSkre		printf '%s\n' "${X}"
13107fea2a9dSkre		EOF
13117fea2a9dSkre
13127fea2a9dSkre	# This should not generate a syntax error, see PR bin/53201
13137fea2a9dSkre	atf_check -s exit:0 -o inline:'abc\n' -e empty ${TEST_SH} <<- 'EOF'
13147fea2a9dSkre		V=abc
13157fea2a9dSkre		X=${V-a
13167fea2a9dSkre		b}
13177fea2a9dSkre		printf '%s\n' "${X}"
13187fea2a9dSkre		EOF
13197fea2a9dSkre
13207fea2a9dSkre	# Nor should any of these...
13217fea2a9dSkre	atf_check -s exit:0 -o inline:'a\nb\n' -e empty ${TEST_SH} <<- 'EOF'
13227fea2a9dSkre		unset V
13237fea2a9dSkre		X=${V-a
13247fea2a9dSkre		b}
13257fea2a9dSkre		printf '%s\n' "${X}"
13267fea2a9dSkre		EOF
13277fea2a9dSkre
13287fea2a9dSkre	atf_check -s exit:0 -o inline:'a\nb\n' -e empty ${TEST_SH} <<- 'EOF'
13297fea2a9dSkre		unset V
13307fea2a9dSkre		X=${V:=a
13317fea2a9dSkre		b}
13327fea2a9dSkre		printf '%s\n' "${X}"
13337fea2a9dSkre		EOF
13347fea2a9dSkre
13357fea2a9dSkre	atf_check -s exit:0 -o inline:'xa\nby\na\nb\n' -e empty \
13367fea2a9dSkre	    ${TEST_SH} <<- 'EOF'
13377fea2a9dSkre		unset V
13387fea2a9dSkre		X=x${V:=a
13397fea2a9dSkre		b}y
13407fea2a9dSkre		printf '%s\n' "${X}" "${V}"
13417fea2a9dSkre		EOF
13427fea2a9dSkre}
13437fea2a9dSkre
1344b7fc669eSkrecheck3()
1345b7fc669eSkre{
1346b7fc669eSkre	check "X=foo; ${1}"		"$2" 0
1347b7fc669eSkre	check "X=; ${1}"		"$3" 0
1348b7fc669eSkre	check "unset X; ${1}"		"$4" 0
1349b7fc669eSkre}
1350b7fc669eSkre
1351b7fc669eSkreatf_test_case alternative
1352b7fc669eSkrealternative_head() {
1353b7fc669eSkre	atf_set descr 'Test various possibilities for ${var+xxx}'
1354b7fc669eSkre}
1355b7fc669eSkrealternative_body() {
1356b7fc669eSkre	reset alternative
1357b7fc669eSkre
1358b7fc669eSkre	# just to verify (validate) that the test method works as expected
1359b7fc669eSkre	# (this is currently the very first test performed in this test set)
1360b7fc669eSkre	check	'printf %s a b'				ab	0	#  1
1361b7fc669eSkre
1362b7fc669eSkre	check3	'set -- ${X+bar}; echo "$#:$1"'		1:bar 1:bar 0:  #  4
1363b7fc669eSkre	check3	'set -- ${X+}; echo "$#:$1"'		0: 0: 0:	#  7
1364b7fc669eSkre	check3	'set -- ${X+""}; echo "$#:$1"'		1: 1: 0:	# 10
1365b7fc669eSkre	check3	'set -- "${X+}"; echo "$#:$1"'		1: 1: 1:	# 13
1366b7fc669eSkre	check3	'set -- "${X+bar}"; echo "$#:$1"'	1:bar 1:bar 1:	# 16
1367b7fc669eSkre
1368b7fc669eSkre	check3	'set -- ${X+a b c}; echo "$#:$1"'	3:a 3:a 0:	# 19
1369b7fc669eSkre	check3	'set -- ${X+"a b c"}; echo "$#:$1"'	'1:a b c' '1:a b c' 0:
1370b7fc669eSkre	check3	'set -- "${X+a b c}"; echo "$#:$1"'	'1:a b c' '1:a b c' 1:
1371b7fc669eSkre	check3	'set -- ${X+a b\ c}; echo "$#:$1"'	2:a 2:a 0:	# 28
1372b7fc669eSkre	check3	'set -- ${X+"a b" c}; echo "$#:$1"'	'2:a b' '2:a b' 0:
1373b7fc669eSkre
1374b7fc669eSkre	check3	'printf %s "" ${X+}'			''  ''  ''	# 34
1375b7fc669eSkre	check3	'printf %s ""${X+bar}'			bar bar ''	# 37
1376b7fc669eSkre
1377b7fc669eSkre	check3	'Y=bar; printf %s ${X+x}${Y+y}'		xy  xy  y	# 40
1378b7fc669eSkre	check3	'Y=bar; printf %s ""${X+${Y+z}}'	z   z   ''	# 43
1379b7fc669eSkre	check3	'Y=; printf %s ""${X+${Y+z}}'		z   z   ''	# 46
1380b7fc669eSkre	check3	'unset Y; printf %s ""${X+${Y+z}}'	''  ''  ''	# 49
1381b7fc669eSkre	check3	'Y=1; printf %s a ${X+"${Y+z}"}'	az  az	a	# 52
1382b7fc669eSkre
1383b7fc669eSkre	check3	'printf %s ${X+}x}'			x}  x}  x}	# 55
1384b7fc669eSkre	check3	'printf %s ${X+}}'			 }   }   }	# 58
1385b7fc669eSkre	check3	'printf %s "" ${X+"}"x}'		}x  }x  ''	# 61
1386b7fc669eSkre	check3	'printf %s "" ${X+\}x}'			}x  }x  ''	# 64
1387b7fc669eSkre	check3	'printf %s "${X+\}x}"'			}x  }x  ''	# 67
1388b7fc669eSkre	check3	'printf %s "${X+\}}"'			 }  }   ''	# 70
1389b7fc669eSkre
1390b7fc669eSkre	check3	'set -- ${X:+bar}; echo "$#:$1"'	1:bar 0: 0:	# 73
1391b7fc669eSkre	check3	'set -- ${X:+}; echo "$#:$1"'		0: 0: 0:	# 76
1392b7fc669eSkre	check3	'set -- ${X:+""}; echo "$#:$1"'		1: 0: 0:	# 79
1393b7fc669eSkre	check3	'set -- "${X:+}"; echo "$#:$1"'		1: 1: 1:	# 80
1394b7fc669eSkre	check3	'set -- "${X:+bar}"; echo "$#:$1"'	1:bar 1: 1:	# 83
1395b7fc669eSkre
1396b7fc669eSkre	check3	'set -- ${X:+a b c}; echo "$#:$1"'	3:a 0: 0:	# 86
1397b7fc669eSkre	check3	'set -- ${X:+"a b c"}; echo "$#:$1"'	'1:a b c' 0: 0:	# 89
1398b7fc669eSkre	check3	'set -- "${X:+a b c}"; echo "$#:$1"'	'1:a b c' 1: 1:	# 92
1399b7fc669eSkre	check3	'set -- ${X:+a b\ c}; echo "$#:$1"'	2:a 0: 0:	# 95
1400b7fc669eSkre	check3	'set -- ${X:+"a b" c}; echo "$#:$1"'	'2:a b' 0: 0:	# 98
1401b7fc669eSkre
1402b7fc669eSkre	check3	'printf %s "" ${X:+}'			''  ''  ''	#101
1403b7fc669eSkre	check3	'printf %s ""${X:+bar}'			bar ''  ''	#104
1404b7fc669eSkre
1405b7fc669eSkre	check3	'Y=bar; printf %s ${X:+x}${Y:+y}'	xy  y   y	#107
1406b7fc669eSkre	check3	'Y=bar; printf %s ""${X:+${Y:+z}}'	z   ''  ''	#110
1407b7fc669eSkre	check3	'Y=; printf %s ""${X:+${Y+z}}'		z   ''  ''	#113
1408b7fc669eSkre	check3	'Y=; printf %s ""${X:+${Y:+z}}'		''  ''  ''	#116
1409b7fc669eSkre	check3	'unset Y; printf %s ""${X:+${Y:+z}}'	''  ''  ''	#119
1410b7fc669eSkre	check3	'Y=1; printf %s a ${X:+"${Y:+z}"}'	az  a	a	#122
1411b7fc669eSkre
1412b7fc669eSkre	check3	'printf %s ${X:+}x}'			x}  x}  x}	#125
1413b7fc669eSkre	check3	'printf %s ${X:+}}'			 }   }   }	#128
1414b7fc669eSkre	check3	'printf %s "" ${X:+"}"x}'		}x  ''  ''	#131
1415b7fc669eSkre	check3	'printf %s "" ${X:+\}x}'		}x  ''  ''	#134
1416b7fc669eSkre	check3	'printf %s "${X:+\}x}"'			}x  ''  ''	#137
1417b7fc669eSkre	check3	'printf %s "${X:+\}}"'			 }  ''  ''	#140
1418b7fc669eSkre
1419b7fc669eSkre	results
1420b7fc669eSkre}
1421b7fc669eSkre
1422b7fc669eSkreatf_test_case default
1423b7fc669eSkredefault_head() {
1424b7fc669eSkre	atf_set descr 'Test various possibilities for ${var-xxx}'
1425b7fc669eSkre}
1426b7fc669eSkredefault_body() {
1427b7fc669eSkre	reset default
1428b7fc669eSkre
1429b7fc669eSkre	check3	'set -- ${X-bar}; echo "$#:$1"'		1:foo 0: 1:bar	#  3
1430b7fc669eSkre	check3	'set -- ${X-}; echo "$#:$1"'		1:foo 0: 0:	#  6
1431b7fc669eSkre	check3	'set -- ${X-""}; echo "$#:$1"'		1:foo 0: 1:	#  9
1432b7fc669eSkre	check3	'set -- "${X-}"; echo "$#:$1"'		1:foo 1: 1:	# 12
1433b7fc669eSkre	check3	'set -- "${X-bar}"; echo "$#:$1"'	1:foo 1: 1:bar	# 15
1434b7fc669eSkre
1435b7fc669eSkre	check3	'set -- ${X-a b c}; echo "$#:$1"'	1:foo 0: 3:a	# 18
1436b7fc669eSkre	check3	'set -- ${X-"a b c"}; echo "$#:$1"'	1:foo 0: '1:a b c' #21
1437b7fc669eSkre	check3	'set -- "${X-a b c}"; echo "$#:$1"'	1:foo 1: '1:a b c' #24
1438b7fc669eSkre	check3	'set -- ${X-a b\ c}; echo "$#:$1"'	1:foo 0: 2:a	# 27
1439b7fc669eSkre	check3	'set -- ${X-"a b" c}; echo "$#:$1"'	1:foo 0: '2:a b'   #30
1440b7fc669eSkre
1441b7fc669eSkre	check3	'printf %s "" ${X-}'			foo '' ''	# 33
1442b7fc669eSkre	check3	'printf %s ""${X-bar}'			foo '' bar	# 36
1443b7fc669eSkre
1444b7fc669eSkre	check3	'Y=bar; printf %s ${X-x}${Y-y}'		foobar bar xbar	# 39
1445b7fc669eSkre	check3	'Y=bar; printf %s ""${X-${Y-z}}'	foo '' bar	# 42
1446b7fc669eSkre	check3	'Y=; printf %s ""${X-${Y-z}}'		foo '' ''	# 45
1447b7fc669eSkre	check3	'unset Y; printf %s ""${X-${Y-z}}'	foo '' z	# 48
1448b7fc669eSkre	check3	'Y=1; printf %s a ${X-"${Y-z}"}'	afoo a a1	# 51
1449b7fc669eSkre
1450b7fc669eSkre	check3	'printf %s ${X-}x}'			foox} x} x}	# 54
1451b7fc669eSkre	check3	'printf %s ${X-}}'			 foo}  }  }	# 57
1452b7fc669eSkre	check3	'printf %s ${X-{}}'			 foo}  } {}	# 60
1453b7fc669eSkre	check3	'printf %s "" ${X-"}"x}'		foo ''  }x	# 63
1454b7fc669eSkre	check3	'printf %s "" ${X-\}x}'			foo ''  }x	# 66
1455b7fc669eSkre	check3	'printf %s "${X-\}x}"'			foo ''  }x	# 69
1456b7fc669eSkre	check3	'printf %s "${X-\}}"'			foo ''  }	# 72
1457b7fc669eSkre
1458b7fc669eSkre	check3	'set -- ${X:-bar}; echo "$#:$1"'	1:foo 1:bar 1:bar  #75
1459b7fc669eSkre	check3	'set -- ${X:-}; echo "$#:$1"'		1:foo 0: 0:	# 78
1460b7fc669eSkre	check3	'set -- ${X:-""}; echo "$#:$1"'		1:foo 1: 1:	# 81
1461b7fc669eSkre	check3	'set -- "${X:-}"; echo "$#:$1"'		1:foo 1: 1:	# 84
1462b7fc669eSkre	check3	'set -- "${X:-bar}"; echo "$#:$1"'	1:foo 1:bar 1:bar  #87
1463b7fc669eSkre
1464b7fc669eSkre	check3	'set -- ${X:-a b c}; echo "$#:$1"'	1:foo 3:a 3:a	# 90
1465b7fc669eSkre	check3	'set -- ${X:-"a b c"}; echo "$#:$1"' 1:foo '1:a b c' '1:a b c'
1466b7fc669eSkre	check3	'set -- "${X:-a b c}"; echo "$#:$1"' 1:foo '1:a b c' '1:a b c'
1467b7fc669eSkre	check3	'set -- ${X:-a b\ c}; echo "$#:$1"'	1:foo 2:a 2:a	# 99
1468b7fc669eSkre	check3	'set -- ${X:-"a b" c}; echo "$#:$1"'	1:foo '2:a b' '2:a b'
1469b7fc669eSkre
1470b7fc669eSkre	check3	'printf %s "" ${X:-}'			foo ''  ''	#105
1471b7fc669eSkre	check3	'printf %s ""${X:-bar}'			foo bar bar	#108
1472b7fc669eSkre
1473b7fc669eSkre	check3	'Y=bar; printf %s ${X:-x}${Y:-y}'	foobar xbar xbar #111
1474b7fc669eSkre	check3	'Y=bar; printf %s ""${X:-${Y:-z}}'	foo  bar bar	#114
1475b7fc669eSkre	check3	'Y=; printf %s ""${X:-${Y-z}}'		foo  ''  ''	#117
1476b7fc669eSkre	check3	'Y=; printf %s ""${X:-${Y:-z}}'		foo  z   z	#120
1477b7fc669eSkre	check3	'unset Y; printf %s ""${X:-${Y:-z}}'	foo  z   z	#123
1478b7fc669eSkre	check3	'Y=1; printf %s a ${X:-"${Y:-z}"}'	afoo a1	 a1	#126
1479b7fc669eSkre
1480b7fc669eSkre	check3	'printf %s ${X:-}x}'			foox} x}  x}	#129
1481b7fc669eSkre	check3	'printf %s ${X:-}}'			 foo}  }   }	#132
1482b7fc669eSkre	check3	'printf %s ${X:-{}}'			 foo} {}  {}	#135
1483b7fc669eSkre	check3	'printf %s "" ${X:-"}"x}'		 foo  }x  }x	#138
1484b7fc669eSkre	check3	'printf %s "" ${X:-\}x}'		 foo  }x  }x	#141
1485b7fc669eSkre	check3	'printf %s "${X:-\}x}"'			 foo  }x  }x	#144
1486b7fc669eSkre	check3	'printf %s "${X:-\}}"'			 foo  }   }	#147
1487b7fc669eSkre
1488b7fc669eSkre	results
1489b7fc669eSkre}
1490b7fc669eSkre
1491b7fc669eSkreatf_test_case assign
1492b7fc669eSkreassign_head() {
1493b7fc669eSkre	atf_set descr 'Test various possibilities for ${var=xxx}'
1494b7fc669eSkre}
1495b7fc669eSkreassign_body() {
1496b7fc669eSkre	reset assign
1497b7fc669eSkre
1498b7fc669eSkre	check3	'set -- ${X=bar}; echo "$#:$1"'		1:foo 0: 1:bar	#  3
1499b7fc669eSkre	check3	'set -- ${X=}; echo "$#:$1"'		1:foo 0: 0:	#  6
1500b7fc669eSkre	check3	'set -- ${X=""}; echo "$#:$1"'		1:foo 0: 0:	#  9
1501b7fc669eSkre	check3	'set -- "${X=}"; echo "$#:$1"'		1:foo 1: 1:	# 12
1502b7fc669eSkre	check3	'set -- "${X=bar}"; echo "$#:$1"'	1:foo 1: 1:bar	# 15
1503b7fc669eSkre
1504b7fc669eSkre	check3	'set -- ${X=a b c}; echo "$#:$1"'	1:foo 0: 3:a	# 18
1505b7fc669eSkre	check3	'set -- ${X="a b c"}; echo "$#:$1"'	1:foo 0: 3:a	# 21
1506b7fc669eSkre	check3	'set -- "${X=a b c}"; echo "$#:$1"'	1:foo 1: '1:a b c' #24
1507b7fc669eSkre	check3	'set -- ${X=a b\ c}; echo "$#:$1"'	1:foo 0: 3:a	# 27
1508b7fc669eSkre	check3	'set -- ${X="a b" c}; echo "$#:$1"'	1:foo 0: 3:a	# 30
1509b7fc669eSkre
1510b7fc669eSkre	check3	'printf %s "" ${X=}'			foo '' ''	# 33
1511b7fc669eSkre	check3	'printf %s ""${X=bar}'			foo '' bar	# 36
1512b7fc669eSkre
1513b7fc669eSkre	check3	'Y=bar; printf %s ${X=x}${Y=y}'		foobar bar xbar	# 39
1514b7fc669eSkre	check3	'Y=bar; printf %s ""${X=${Y=z}}'	foo '' bar	# 42
1515b7fc669eSkre	check3	'Y=; printf %s ""${X=${Y=z}}'		foo '' ''	# 45
1516b7fc669eSkre	check3	'unset Y; printf %s ""${X=${Y=z}}'	foo '' z	# 48
1517b7fc669eSkre	check3	'Y=1; printf %s a ${X="${Y=z}"}'	afoo a a1	# 51
1518b7fc669eSkre
1519b7fc669eSkre	check3	'printf %s ${X=}x}'			foox} x} x}	# 54
1520b7fc669eSkre	check3	'printf %s ${X=}}'			 foo}  }  }	# 57
1521b7fc669eSkre	check3	'printf %s ${X={}}'			 foo}  } {}	# 60
1522b7fc669eSkre	check3	'printf %s "" ${X="}"x}'		foo ''  }x	# 63
1523b7fc669eSkre	check3	'printf %s "" ${X=\}x}'			foo ''  }x	# 66
1524b7fc669eSkre	check3	'printf %s "${X=\}x}"'			foo ''  }x	# 69
1525b7fc669eSkre	check3	'printf %s "${X=\}}"'			foo ''  }	# 72
1526b7fc669eSkre
1527b7fc669eSkre	check3	'set -- ${X=a b c}; echo "$#:$1:$X"'  1:foo:foo 0:: '3:a:a b c'
1528b7fc669eSkre	check3	'set -- ${X="a b c"}; echo "$#:$1:$X"' 1:foo:foo 0:: '3:a:a b c'
1529b7fc669eSkre	check3	'set -- "${X=a b c}"; echo "$#:$1:$X"' \
1530b7fc669eSkre						1:foo:foo 1:: '1:a b c:a b c'
1531b7fc669eSkre	check3	'set -- ${X=a b\ c}; echo "$#:$1:$X"' 1:foo:foo 0:: '3:a:a b c'
1532b7fc669eSkre	check3	'set -- ${X="a b" c}; echo "$#:$1:$X"' 1:foo:foo 0:: '3:a:a b c'
1533b7fc669eSkre
1534b7fc669eSkre	check3	'printf %s ${X=}x}; printf :%s "${X-U}"' foox}:foo x}: x}: #90
1535b7fc669eSkre	check3	'printf %s ${X=}}; printf :%s "${X-U}"'  foo}:foo }:  }:   #93
1536b7fc669eSkre	check3	'printf %s ${X={}}; printf :%s "${X-U}"' foo}:foo }: {}:{  #96
1537b7fc669eSkre
1538b7fc669eSkre	check3	'set -- ${X:=bar}; echo "$#:$1"'	1:foo 1:bar 1:bar # 99
1539b7fc669eSkre	check3	'set -- ${X:=}; echo "$#:$1"'		1:foo 0: 0:	#102
1540b7fc669eSkre	check3	'set -- ${X:=""}; echo "$#:$1"'		1:foo 0: 0:	#105
1541b7fc669eSkre	check3	'set -- "${X:=}"; echo "$#:$1"'		1:foo 1: 1:	#108
1542b7fc669eSkre	check3	'set -- "${X:=bar}"; echo "$#:$1"'	1:foo 1:bar 1:bar #111
1543b7fc669eSkre
1544b7fc669eSkre	check3	'set -- ${X:=a b c}; echo "$#:$1"'	1:foo 3:a 3:a	#114
1545b7fc669eSkre	check3	'set -- ${X:="a b c"}; echo "$#:$1"' 1:foo 3:a 3:a	#117
1546b7fc669eSkre	check3	'set -- "${X:=a b c}"; echo "$#:$1"' 1:foo '1:a b c' '1:a b c'
1547b7fc669eSkre	check3	'set -- ${X:=a b\ c}; echo "$#:$1"'	1:foo 3:a 3:a	#123
1548b7fc669eSkre	check3	'set -- ${X:="a b" c}; echo "$#:$1"'	1:foo 3:a 3:a	#126
1549b7fc669eSkre
1550b7fc669eSkre	check3	'printf %s "" ${X:=}'			foo ''  ''	#129
1551b7fc669eSkre	check3	'printf %s ""${X:=bar}'			foo bar bar	#132
1552b7fc669eSkre
1553b7fc669eSkre	check3	'Y=bar; printf %s ${X:=x}${Y:=y}'	foobar xbar xbar #135
1554b7fc669eSkre	check3	'Y=bar; printf %s ""${X:=${Y:=z}}'	foo  bar bar	#138
1555b7fc669eSkre	check3	'Y=; printf %s ""${X:=${Y=z}}'		foo  ''  ''	#141
1556b7fc669eSkre	check3	'Y=; printf %s ""${X:=${Y:=z}}'		foo  z   z	#144
1557b7fc669eSkre	check3	'unset Y; printf %s ""${X:=${Y:=z}}'	foo  z   z	#147
1558b7fc669eSkre	check3	'Y=1; printf %s a ${X:="${Y:=z}"}'	afoo a1	 a1	#150
1559b7fc669eSkre
1560b7fc669eSkre	check3	'printf %s ${X:=}x}'			foox} x}  x}	#153
1561b7fc669eSkre	check3	'printf %s ${X:=}}'			 foo}  }   }	#156
1562b7fc669eSkre	check3	'printf %s ${X:={}}'			 foo} {}  {}	#159
1563b7fc669eSkre	check3	'printf %s "" ${X:="}"x}'		 foo  }x  }x	#162
1564b7fc669eSkre	check3	'printf %s "" ${X:=\}x}'		 foo  }x  }x	#165
1565b7fc669eSkre	check3	'printf %s "${X:=\}x}"'			 foo  }x  }x	#168
1566b7fc669eSkre	check3	'printf %s "${X:=\}}"'			 foo  }   }	#171
1567b7fc669eSkre
1568b7fc669eSkre	check3	'set -- ${X:=a b c}; echo "$#:$1:$X"' \
1569b7fc669eSkre				1:foo:foo '3:a:a b c' '3:a:a b c'	#174
1570b7fc669eSkre	check3	'set -- ${X:="a b c"}; echo "$#:$1:$X"' \
1571b7fc669eSkre				1:foo:foo '3:a:a b c' '3:a:a b c'	#177
1572b7fc669eSkre	check3	'set -- "${X:=a b c}"; echo "$#:$1:$X"' \
1573b7fc669eSkre				1:foo:foo '1:a b c:a b c' '1:a b c:a b c' #180
1574b7fc669eSkre	check3	'set -- ${X:=a b\ c}; echo "$#:$1:$X"' \
1575b7fc669eSkre				1:foo:foo '3:a:a b c' '3:a:a b c'	#183
1576b7fc669eSkre	check3	'set -- ${X:="a b" c}; echo "$#:$1:$X"' \
1577b7fc669eSkre				1:foo:foo '3:a:a b c' '3:a:a b c'	#186
1578b7fc669eSkre
1579b7fc669eSkre	check3	'printf %s ${X:=}x}; printf :%s "${X-U}"' foox}:foo x}: x}:
1580b7fc669eSkre	check3	'printf %s ${X:=}}; printf :%s "${X-U}"'  foo}:foo }:  }:
1581b7fc669eSkre	check3	'printf %s ${X:=\}}; printf :%s "${X-U}"' foo:foo }:}  }:}
1582b7fc669eSkre	check3	'printf %s ${X:={}}; printf :%s "${X-U}"' foo}:foo {}:{ {}:{
1583b7fc669eSkre									#198
1584b7fc669eSkre
1585b7fc669eSkre	results
1586b7fc669eSkre}
1587b7fc669eSkre
1588b7fc669eSkreatf_test_case error
1589b7fc669eSkreerror_head() {
1590b7fc669eSkre	atf_set descr 'Test various possibilities for ${var?xxx}'
1591b7fc669eSkre}
1592b7fc669eSkreerror_body() {
1593b7fc669eSkre	reset error
1594b7fc669eSkre
1595b7fc669eSkre	check 'X=foo; printf %s ${X?X is not set}'	foo	0	#1
1596b7fc669eSkre	check 'X=; printf %s ${X?X is not set}'		''	0	#2
1597b7fc669eSkre	check 'unset X; printf %s ${X?X is not set}'	''	2	#3
1598b7fc669eSkre
1599b7fc669eSkre	check 'X=foo; printf %s ${X?}'			foo	0	#4
1600b7fc669eSkre	check 'X=; printf %s ${X?}'			''	0	#5
1601b7fc669eSkre	check 'unset X; printf %s ${X?}'		''	2	#6
1602b7fc669eSkre
1603b7fc669eSkre	check 'X=foo; printf %s ${X:?X is not set}'	foo	0	#7
1604b7fc669eSkre	check 'X=; printf %s ${X:?X is not set}'	''	2	#8
1605b7fc669eSkre	check 'unset X; printf %s ${X:?X is not set}'	''	2	#9
1606b7fc669eSkre
1607b7fc669eSkre	check 'X=foo; printf %s ${X:?}'			foo	0	#10
1608b7fc669eSkre	check 'X=; printf %s ${X:?}'			''	2	#11
1609b7fc669eSkre	check 'unset X; printf %s ${X:?}'		''	2	#12
1610b7fc669eSkre
1611b7fc669eSkre	results
1612b7fc669eSkre}
1613b7fc669eSkre
161428604916Sjruohoatf_init_test_cases() {
161586dd15a6Skre	# Listed here in the order ATF runs them, not the order from above
161686dd15a6Skre
1617b7fc669eSkre	atf_add_test_case alternative
161886dd15a6Skre	atf_add_test_case arithmetic
1619b7fc669eSkre	atf_add_test_case assign
1620b7fc669eSkre	atf_add_test_case default
162128604916Sjruoho	atf_add_test_case dollar_at
1622265b0617Skre	atf_add_test_case dollar_at_empty_and_conditional
1623265b0617Skre	atf_add_test_case dollar_at_in_field_split_context
16249addc446Skre	atf_add_test_case dollar_at_unquoted_or_conditional
162528604916Sjruoho	atf_add_test_case dollar_at_with_text
1626a30f469fSkre	atf_add_test_case dollar_hash
162786dd15a6Skre	atf_add_test_case dollar_star
162886dd15a6Skre	atf_add_test_case dollar_star_in_quoted_word
162986dd15a6Skre	atf_add_test_case dollar_star_in_word
163086dd15a6Skre	atf_add_test_case dollar_star_in_word_empty_ifs
163186dd15a6Skre	atf_add_test_case dollar_star_with_empty_ifs
16327fea2a9dSkre	atf_add_test_case embedded_nl
1633b7fc669eSkre	atf_add_test_case error
163483d9b545Sast	atf_add_test_case iteration_on_null_parameter
16352c3c3b3cSchristos	atf_add_test_case iteration_on_quoted_null_parameter
16362c3c3b3cSchristos	atf_add_test_case iteration_on_null_or_null_parameter
16372c3c3b3cSchristos	atf_add_test_case iteration_on_null_or_missing_parameter
16383f904448Schristos	atf_add_test_case shell_params
163986dd15a6Skre	atf_add_test_case strip
16405c624ca9Skre	atf_add_test_case tilde
16416768e34bSkre	atf_add_test_case wrap_strip
1642706a2d4fSkre	atf_add_test_case var_with_embedded_cmdsub
164386dd15a6Skre	atf_add_test_case varpattern_backslashes
164428604916Sjruoho}
1645