xref: /minix/tests/bin/sh/t_expand.sh (revision 84d9c625)
1# $NetBSD: t_expand.sh,v 1.2 2013/10/06 21:05:50 ast Exp $
2#
3# Copyright (c) 2007, 2009 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27
28#
29# This file tests the functions in expand.c.
30#
31
32delim_argv() {
33	str=
34	while [ $# -gt 0 ]; do
35		if [ -z "${str}" ]; then
36			str=">$1<"
37		else
38			str="${str} >$1<"
39		fi
40		shift
41	done
42	echo ${str}
43}
44
45atf_test_case dollar_at
46dollar_at_head() {
47	atf_set "descr" "Somewhere between 2.0.2 and 3.0 the expansion" \
48	                "of the \$@ variable had been broken.  Check for" \
49			"this behavior."
50}
51dollar_at_body() {
52	# This one should work everywhere.
53	got=`echo "" "" | sed 's,$,EOL,'`
54	atf_check_equal ' EOL' '$got'
55
56	# This code triggered the bug.
57	set -- "" ""
58	got=`echo "$@" | sed 's,$,EOL,'`
59	atf_check_equal ' EOL' '$got'
60
61	set -- -
62	shift
63	n_arg() { echo $#; }
64	n_args=`n_arg "$@"`
65	atf_check_equal '0' '$n_args'
66}
67
68atf_test_case dollar_at_with_text
69dollar_at_with_text_head() {
70	atf_set "descr" "Test \$@ expansion when it is surrounded by text" \
71	                "within the quotes.  PR bin/33956."
72}
73dollar_at_with_text_body() {
74	set --
75	atf_check_equal '' "$(delim_argv "$@")"
76	atf_check_equal '>foobar<' "$(delim_argv "foo$@bar")"
77	atf_check_equal '>foo  bar<' "$(delim_argv "foo $@ bar")"
78
79	set -- a b c
80	atf_check_equal '>a< >b< >c<' "$(delim_argv "$@")"
81	atf_check_equal '>fooa< >b< >cbar<' "$(delim_argv "foo$@bar")"
82	atf_check_equal '>foo a< >b< >c bar<' "$(delim_argv "foo $@ bar")"
83}
84
85atf_test_case strip
86strip_head() {
87	atf_set "descr" "Checks that the %% operator works and strips" \
88	                "the contents of a variable from the given point" \
89			"to the end"
90}
91strip_body() {
92	line='#define bindir "/usr/bin" /* comment */'
93	stripped='#define bindir "/usr/bin" '
94	atf_expect_fail "PR bin/43469"
95	atf_check_equal '$stripped' '${line%%/\**}'
96}
97
98atf_test_case varpattern_backslashes
99varpattern_backslashes_head() {
100	atf_set "descr" "Tests that protecting wildcards with backslashes" \
101	                "works in variable patterns."
102}
103varpattern_backslashes_body() {
104	line='/foo/bar/*/baz'
105	stripped='/foo/bar/'
106	atf_check_equal $stripped ${line%%\**}
107}
108
109atf_test_case arithmetic
110arithmetic_head() {
111	atf_set "descr" "POSIX requires shell arithmetic to use signed" \
112	                "long or a wider type.  We use intmax_t, so at" \
113			"least 64 bits should be available.  Make sure" \
114			"this is true."
115}
116arithmetic_body() {
117	atf_check_equal '3' '$((1 + 2))'
118	atf_check_equal '2147483647' '$((0x7fffffff))'
119	atf_check_equal '9223372036854775807' '$(((1 << 63) - 1))'
120}
121
122atf_test_case iteration_on_null_parameter
123iteration_on_null_parameter_head() {
124	atf_set "descr" "Check iteration of \$@ in for loop when set to null;" \
125	                "the error \"sh: @: parameter not set\" is incorrect." \
126	                "PR bin/48202."
127}
128iteration_on_null_parameter_body() {
129	s1=`/bin/sh -uc 'N=; set -- ${N};   for X; do echo "[$X]"; done' 2>&1`
130	s2=`/bin/sh -uc 'N=; set -- ${N:-}; for X; do echo "[$X]"; done' 2>&1`
131	atf_check_equal ''   '$s1'
132	atf_check_equal '[]' '$s2'
133}
134
135atf_init_test_cases() {
136	atf_add_test_case dollar_at
137	atf_add_test_case dollar_at_with_text
138	atf_add_test_case strip
139	atf_add_test_case varpattern_backslashes
140	atf_add_test_case arithmetic
141	atf_add_test_case iteration_on_null_parameter
142}
143