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