t_sed.sh revision 1.6 1 1.6 christos # $NetBSD: t_sed.sh,v 1.6 2016/04/05 00:48:53 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.1 jruoho atf_init_test_cases() {
137 1.4 jruoho atf_add_test_case c2048
138 1.1 jruoho atf_add_test_case emptybackref
139 1.3 jruoho atf_add_test_case longlines
140 1.2 dholland atf_add_test_case rangeselection
141 1.6 christos atf_add_test_case preserve_leading_ws_ia
142 1.1 jruoho }
143