1 # $NetBSD: t_fsplit.sh,v 1.1 2007/11/12 15:14:03 jmmv Exp $
3 # Copyright (c) 2007 The NetBSD Foundation, Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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.
29 # http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html
30 # explains (section 2.6) that Field splitting should be performed on the
31 # result of variable expansions.
32 # In particular this means that in ${x-word}, 'word' must be expanded as if
33 # the "${x-" and "}" were absent from the input line.
35 # So: sh -c 'set ${x-a b c}; echo $#' should give 3.
47 result
="$(echo $result)"
49 if [ "$2" != "$result" ]
51 atf_fail
"expected [$2], found [$result]"
57 atf_set
"descr" "Checks field splitting in for loops"
62 # Since I managed to break this, leave the test in
63 check
'for f in $x; do echo x${f}y; done' ''
66 atf_test_case default_val
68 atf_set
"descr" "Checks field splitting in variable default values"
73 # Check that IFS is applied to text from ${x-...} unless it is inside
75 check
'set ${x-a b c}; echo $#' 3
76 check
'for i in ${x-a b c}; do echo "z${i}z"; done' 'zaz zbz zcz'
77 check
'for i in ${x-"a b" c}; do echo "z${i}z"; done' 'za bz zcz'
78 check
'for i in ${x-"a ${x-b c}" d}; do echo "z${i}z"; done' 'za b cz zdz'
79 check
'for i in ${x-"a ${x-"b c"}" d}; do echo "z${i}z"; done' 'za b cz zdz'
80 check
'for i in ${x-a ${x-"b c"} d}; do echo "z${i}z"; done' 'zaz zb cz zdz'
81 check
'for i in ${x-a ${x-b c} d}; do echo "z${i}z"; done' 'zaz zbz zcz zdz'
84 atf_test_case ifs_alpha
86 atf_set
"descr" "Checks that field splitting works with alphabetic" \
92 # repeat with an alphabetic in IFS
93 check
'IFS=q; set ${x-aqbqc}; echo $#' 3
94 check
'IFS=q; for i in ${x-aqbqc}; do echo "z${i}z"; done' 'zaz zbz zcz'
95 check
'IFS=q; for i in ${x-"aqb"qc}; do echo "z${i}z"; done' 'zaqbz zcz'
96 check
'IFS=q; for i in ${x-"aq${x-bqc}"qd}; do echo "z${i}z"; done' 'zaqbqcz zdz'
97 check
'IFS=q; for i in ${x-"aq${x-"bqc"}"qd}; do echo "z${i}z"; done' 'zaqbqcz zdz'
98 check
'IFS=q; for i in ${x-aq${x-"bqc"}qd}; do echo "z${i}z"; done' 'zaz zbqcz zdz'
103 atf_set
"descr" "Checks that field splitting works with multi-word" \
109 # Some quote propagation checks
110 check
'set "${x-a b c}"; echo $#' 1
111 check
'set "${x-"a b" c}"; echo $1' 'a b c'
112 check
'for i in "${x-a b c}"; do echo "z${i}z"; done' 'za b cz'
115 atf_test_case dollar_at
117 atf_set
"descr" "Checks that field splitting works when expanding" \
123 # Check we get "$@" right
124 check
'set ""; for i; do echo "z${i}z"; done' 'zz'
125 check
'set ""; for i in "$@"; do echo "z${i}z"; done' 'zz'
126 check
'set "" ""; for i; do echo "z${i}z"; done' 'zz zz'
127 check
'set "" ""; for i in "$@"; do echo "z${i}z"; done' 'zz zz'
128 check
'set "" ""; for i in $@; do echo "z${i}z"; done' ''
129 check
'set "a b" c; for i; do echo "z${i}z"; done' 'za bz zcz'
130 check
'set "a b" c; for i in "$@"; do echo "z${i}z"; done' 'za bz zcz'
131 check
'set "a b" c; for i in $@; do echo "z${i}z"; done' 'zaz zbz zcz'
132 check
'set " a b " c; for i in "$@"; do echo "z${i}z"; done' 'z a b z zcz'
133 check
'set --; for i in x"$@"x; do echo "z${i}z"; done' 'zxxz'
134 check
'set a; for i in x"$@"x; do echo "z${i}z"; done' 'zxaxz'
135 check
'set a b; for i in x"$@"x; do echo "z${i}z"; done' 'zxaz zbxz'
140 atf_set
"descr" "Checks that IFS correctly configures field" \
147 check
't="-- "; IFS=" "; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '0'
148 check
't=" x"; IFS=" x"; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '1'
149 check
't=" x "; IFS=" x"; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '1'
150 check
't=axb; IFS="x"; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '2 a:b'
151 check
't="a x b"; IFS="x"; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '2 a : b'
152 check
't="a xx b"; IFS="x"; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '3 a :: b'
153 check
't="a xx b"; IFS="x "; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '3 a::b'
154 # A recent 'clarification' means that a single trailing IFS non-whitespace
155 # doesn't generate an empty parameter
156 check
't="xax"; IFS="x"; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '2 :a'
157 check
't="xax "; IFS="x "; set $t; IFS=":"; r="$*"; IFS=; echo $# $r' '2 :a'
158 # Verify that IFS isn't being applied where it shouldn't be.
159 check
'IFS="x"; set axb; IFS=":"; r="$*"; IFS=; echo $# $r' '1 axb'
162 atf_test_case var_length
164 atf_set
"descr" "Checks that field splitting works when expanding" \
165 "a variable's length"
170 # Check that we apply IFS to ${#var}
171 long
=12345678123456781234567812345678
172 long
=$long$long$long$long
173 check
'echo ${#long}; IFS=2; echo ${#long}; set 1 ${#long};echo $#' '128 1 8 3'
174 check
'IFS=2; set ${x-${#long}}; IFS=" "; echo $* $#' '1 8 2'
175 check
'IFS=2; set ${x-"${#long}"}; IFS=" "; echo $* $#' '128 1'
178 atf_init_test_cases
() {
179 atf_add_test_case
for
180 atf_add_test_case default_val
181 atf_add_test_case ifs_alpha
182 atf_add_test_case quote
183 atf_add_test_case dollar_at
184 atf_add_test_case ifs
185 atf_add_test_case var_length