t_sed.sh revision 1.7 1 1.7 christos # $NetBSD: t_sed.sh,v 1.7 2019/10/05 20:24:16 christos Exp $
2 1.1 jruoho #
3 1.1 jruoho # Copyright (c) 2012 The NetBSD Foundation, Inc.
4 1.1 jruoho # All rights reserved.
5 1.1 jruoho #
6 1.1 jruoho # This code is derived from software contributed to The NetBSD Foundation
7 1.2 dholland # by Jukka Ruohonen and David A. Holland.
8 1.1 jruoho #
9 1.1 jruoho # Redistribution and use in source and binary forms, with or without
10 1.1 jruoho # modification, are permitted provided that the following conditions
11 1.1 jruoho # are met:
12 1.1 jruoho # 1. Redistributions of source code must retain the above copyright
13 1.1 jruoho # notice, this list of conditions and the following disclaimer.
14 1.1 jruoho # 2. Redistributions in binary form must reproduce the above copyright
15 1.1 jruoho # notice, this list of conditions and the following disclaimer in the
16 1.1 jruoho # documentation and/or other materials provided with the distribution.
17 1.1 jruoho #
18 1.1 jruoho # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 jruoho # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 jruoho # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 jruoho # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 jruoho # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 jruoho # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 jruoho # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 jruoho # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 jruoho # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 jruoho # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 jruoho # POSSIBILITY OF SUCH DAMAGE.
29 1.1 jruoho #
30 1.1 jruoho
31 1.4 jruoho atf_test_case c2048
32 1.4 jruoho c2048_head() {
33 1.4 jruoho atf_set "descr" "Test that sed(1) does not fail when the " \
34 1.4 jruoho "2048'th character is a backslash (PR bin/25899)"
35 1.4 jruoho }
36 1.4 jruoho
37 1.4 jruoho c2048_body() {
38 1.4 jruoho
39 1.5 jmmv atf_check -s exit:0 -o inline:'foo\n' -e empty \
40 1.5 jmmv -x "echo foo | sed -f $(atf_get_srcdir)/d_c2048.in"
41 1.4 jruoho }
42 1.4 jruoho
43 1.1 jruoho atf_test_case emptybackref
44 1.1 jruoho emptybackref_head() {
45 1.1 jruoho atf_set "descr" "Test that sed(1) handles " \
46 1.1 jruoho "empty back references (PR bin/28126)"
47 1.1 jruoho }
48 1.1 jruoho
49 1.1 jruoho emptybackref_body() {
50 1.1 jruoho
51 1.1 jruoho atf_check -o inline:"foo1bar1\n" \
52 1.1 jruoho -x "echo foo1bar1 | sed -ne '/foo\(.*\)bar\1/p'"
53 1.1 jruoho
54 1.1 jruoho atf_expect_fail "PR bin/28126"
55 1.1 jruoho
56 1.1 jruoho atf_check -o inline:"foobar\n" \
57 1.1 jruoho -x "echo foobar | sed -ne '/foo\(.*\)bar\1/p'"
58 1.1 jruoho }
59 1.1 jruoho
60 1.3 jruoho atf_test_case longlines
61 1.3 jruoho longlines_head() {
62 1.3 jruoho atf_set "descr" "Test that sed(1) handles " \
63 1.3 jruoho "long lines correctly (PR bin/42261)"
64 1.3 jruoho }
65 1.3 jruoho
66 1.3 jruoho longlines_body() {
67 1.3 jruoho
68 1.3 jruoho str=$(awk 'BEGIN {while(x<2043){printf "x";x++}}')
69 1.3 jruoho echo $str > input
70 1.3 jruoho
71 1.3 jruoho atf_check -o save:output -x "echo x | sed s,x,${str},g"
72 1.3 jruoho atf_check -s exit:0 -o empty -e empty -x "diff input output"
73 1.3 jruoho }
74 1.3 jruoho
75 1.2 dholland atf_test_case rangeselection
76 1.2 dholland rangeselection_head() {
77 1.2 dholland atf_set "descr" "Test that sed(1) handles " \
78 1.2 dholland "range selection correctly"
79 1.2 dholland }
80 1.2 dholland
81 1.2 dholland rangeselection_body() {
82 1.2 dholland # basic cases
83 1.2 dholland atf_check -o inline:"D\n" \
84 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '1,3d'"
85 1.2 dholland atf_check -o inline:"A\n" \
86 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '2,4d'"
87 1.2 dholland # two nonoverlapping ranges
88 1.2 dholland atf_check -o inline:"C\n" \
89 1.2 dholland -x "printf 'A\nB\nC\nD\nE\n' | sed '1,2d;4,5d'"
90 1.2 dholland # overlapping ranges; the first prevents the second from being entered
91 1.2 dholland atf_check -o inline:"D\nE\n" \
92 1.2 dholland -x "printf 'A\nB\nC\nD\nE\n' | sed '1,3d;3,5d'"
93 1.2 dholland # the 'n' command can also prevent ranges from being entered
94 1.2 dholland atf_check -o inline:"B\nB\nC\nD\n" \
95 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '1,3s/A/B/;1,3n;1,3s/B/C/'"
96 1.2 dholland atf_check -o inline:"B\nC\nC\nD\n" \
97 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '1,3s/A/B/;1,3n;2,3s/B/C/'"
98 1.2 dholland
99 1.2 dholland # basic cases using regexps
100 1.2 dholland atf_check -o inline:"D\n" \
101 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '/A/,/C/d'"
102 1.2 dholland atf_check -o inline:"A\n" \
103 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '/B/,/D/d'"
104 1.2 dholland # two nonoverlapping ranges
105 1.2 dholland atf_check -o inline:"C\n" \
106 1.2 dholland -x "printf 'A\nB\nC\nD\nE\n' | sed '/A/,/B/d;/D/,/E/d'"
107 1.2 dholland # two overlapping ranges; the first blocks the second as above
108 1.2 dholland atf_check -o inline:"D\nE\n" \
109 1.2 dholland -x "printf 'A\nB\nC\nD\nE\n' | sed '/A/,/C/d;/C/,/E/d'"
110 1.2 dholland # the 'n' command makes some lines invisible to downstreap regexps
111 1.2 dholland atf_check -o inline:"B\nC\nC\nD\n" \
112 1.2 dholland -x "printf 'A\nB\nC\nD\n' | sed '/A/,/C/s/A/B/;1,3n;/B/,/C/s/B/C/'"
113 1.2 dholland
114 1.2 dholland # a range ends at the *first* matching end line
115 1.2 dholland atf_check -o inline:"D\nC\n" \
116 1.2 dholland -x "printf 'A\nB\nC\nD\nC\n' | sed '/A/,/C/d'"
117 1.2 dholland # another matching start line within the range has no effect
118 1.2 dholland atf_check -o inline:"D\nC\n" \
119 1.2 dholland -x "printf 'A\nB\nA\nC\nD\nC\n' | sed '/A/,/C/d'"
120 1.2 dholland }
121 1.2 dholland
122 1.6 christos atf_test_case preserve_leading_ws_ia
123 1.6 christos preserve_leading_ws_ia_head() {
124 1.6 christos atf_set "descr" "Test that sed(1) preserves leading whitespace " \
125 1.6 christos "in insert and append (PR bin/49872)"
126 1.6 christos }
127 1.6 christos
128 1.6 christos preserve_leading_ws_ia_body() {
129 1.6 christos atf_check -o inline:" 1 2 3\n4 5 6\n 7 8 9\n\n" \
130 1.6 christos -x 'echo | sed -e "/^$/i\\
131 1.6 christos 1 2 3\\
132 1.6 christos 4 5 6\\
133 1.6 christos 7 8 9"'
134 1.6 christos }
135 1.6 christos
136 1.7 christos atf_test_case escapes_in_subst
137 1.7 christos escapes_in_subst_head() {
138 1.7 christos atf_set "descr" "Test that sed(1) expands \x \d \o escapes " \
139 1.7 christos "in substition strings"
140 1.7 christos }
141 1.7 christos
142 1.7 christos escapes_in_subst_body() {
143 1.7 christos atf_check -o inline:"fooXbar\n" \
144 1.7 christos -x 'echo "foo bar" | sed -e "s/ /\x58/"'
145 1.7 christos atf_check -o inline:"fooXbar\n" \
146 1.7 christos -x 'echo "foo bar" | sed -e "s/ /\o130/"'
147 1.7 christos atf_check -o inline:"fooXbar\n" \
148 1.7 christos -x 'echo "foo bar" | sed -e "s/ /\d88/"'
149 1.7 christos }
150 1.7 christos
151 1.7 christos atf_test_case escapes_in_re
152 1.7 christos escapes_in_re_head() {
153 1.7 christos atf_set "descr" "Test that sed(1) expands \x \d \o escapes " \
154 1.7 christos "in regex strings"
155 1.7 christos }
156 1.7 christos
157 1.7 christos escapes_in_re_body() {
158 1.7 christos atf_check -o inline:"foo bar\n" \
159 1.7 christos -x 'echo "fooXbar" | sed -e "s/\x58/ /"'
160 1.7 christos atf_check -o inline:"foo bar\n" \
161 1.7 christos -x 'echo "fooXbar" | sed -e "s/\o130/ /"'
162 1.7 christos atf_check -o inline:"foo bar\n" \
163 1.7 christos -x 'echo "fooXbar" | sed -e "s/\d88/ /"'
164 1.7 christos }
165 1.7 christos
166 1.7 christos atf_test_case escapes_in_re_bracket
167 1.7 christos escapes_in_re_bracket_head() {
168 1.7 christos atf_set "descr" "Test that sed(1) does not expand \x \d \o escapes " \
169 1.7 christos "in regex strings inside braces"
170 1.7 christos }
171 1.7 christos
172 1.7 christos escapes_in_re_bracket_body() {
173 1.7 christos atf_check -o inline:"foo bar\n" \
174 1.7 christos -x 'echo "foo\\x58bar" | sed -e "s/[\x58]/ /g"'
175 1.7 christos atf_check -o inline:"f bar\n" \
176 1.7 christos -x 'echo "fooo\\130bar" | sed -e "s/[\o130]/ /g"'
177 1.7 christos atf_check -o inline:"foo bar\n" \
178 1.7 christos -x 'echo "foo\\d88bar" | sed -e "s/[\d88]/ /g"'
179 1.7 christos }
180 1.1 jruoho atf_init_test_cases() {
181 1.4 jruoho atf_add_test_case c2048
182 1.1 jruoho atf_add_test_case emptybackref
183 1.3 jruoho atf_add_test_case longlines
184 1.2 dholland atf_add_test_case rangeselection
185 1.6 christos atf_add_test_case preserve_leading_ws_ia
186 1.7 christos atf_add_test_case escapes_in_subst
187 1.7 christos atf_add_test_case escapes_in_re
188 1.7 christos atf_add_test_case escapes_in_re_bracket
189 1.1 jruoho }
190