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