xref: /freebsd/contrib/netbsd-tests/bin/sh/t_fsplit.sh (revision c6db8143eda5c775467145ac73e8ebec47afdd8f)
1# $NetBSD: t_fsplit.sh,v 1.1 2012/03/17 16:33:11 jruoho Exp $
2#
3# Copyright (c) 2007 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# The standard
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.
34#
35# So: sh -c 'set ${x-a b c}; echo $#' should give 3.
36#
37
38nl='
39'
40
41check()
42{
43	result="$(eval $1)"
44	# Remove newlines
45	oifs="$IFS"
46	IFS="$nl"
47	result="$(echo $result)"
48	IFS="$oifs"
49	if [ "$2" != "$result" ]
50	then
51		atf_fail "expected [$2], found [$result]"
52	fi
53}
54
55atf_test_case for
56for_head() {
57	atf_set "descr" "Checks field splitting in for loops"
58}
59for_body() {
60	unset x
61
62	# Since I managed to break this, leave the test in
63	check 'for f in $x; do echo x${f}y; done' ''
64}
65
66atf_test_case default_val
67default_val_head() {
68	atf_set "descr" "Checks field splitting in variable default values"
69}
70default_val_body() {
71	unset x
72
73	# Check that IFS is applied to text from ${x-...} unless it is inside
74	# any set of "..."
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'
82}
83
84atf_test_case ifs_alpha
85ifs_alpha_head() {
86	atf_set "descr" "Checks that field splitting works with alphabetic" \
87	                "characters"
88}
89ifs_alpha_body() {
90	unset x
91
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'
99}
100
101atf_test_case quote
102quote_head() {
103	atf_set "descr" "Checks that field splitting works with multi-word" \
104	                "fields"
105}
106quote_body() {
107	unset x
108
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'
113}
114
115atf_test_case dollar_at
116dollar_at_head() {
117	atf_set "descr" "Checks that field splitting works when expanding" \
118	                "\$@"
119}
120dollar_at_body() {
121	unset x
122
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'
136}
137
138atf_test_case ifs
139ifs_head() {
140	atf_set "descr" "Checks that IFS correctly configures field" \
141	                "splitting behavior"
142}
143ifs_body() {
144	unset x
145
146	# Some IFS tests
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'
160}
161
162atf_test_case var_length
163var_length_head() {
164	atf_set "descr" "Checks that field splitting works when expanding" \
165	                "a variable's length"
166}
167var_length_body() {
168	unset x
169
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'
176}
177
178atf_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
186}
187