3 # SUSv3 compliant sed tests.
4 # Copyright 2005 by Rob Landley <rob@landley.net>
5 # Licensed under GPL v2, see file LICENSE for details.
7 [ -z "$COMMAND" ] && COMMAND=sed
10 # testing "description" "arguments" "result" "infile" "stdin"
13 testing "sed as cat" '"" -' "hello\n" "" "hello\n"
14 testing "sed handles empty lines" "-e 's/\$/@/'" "@\n" "" "\n"
18 # mix files and stdin (various orders)
21 # Multiple files: first no EOF, second length 0.
22 # Match $, at end of each file or all files?
23 # First no EOF, second no matches at all.
34 # Just make sure it works.
42 testing "sed accepts blanks before command" "-e '1 d'" "" "" ""
43 testing "sed accepts newlines in -e" "-e 'i\
46 3'" "1\n2\n3\n" "" "2\n"
47 testing "sed accepts multiple -e" "-e 'i\' -e '1' -e 'a\' -e '3'" \
51 testing "sed -n" "-n -e s/foo/bar/ -e s/bar/baz/" "" "" "foo\n"
52 testing "sed s//p" "-e s/foo/bar/p -e s/bar/baz/p" "bar\nbaz\nbaz\n" \
54 testing "sed -n s//p" "-ne s/abc/def/p" "def\n" "" "abc\n"
55 testing "sed s//g (exhaustive)" "-e 's/[[:space:]]*/,/g'" ",1,2,3,4,5,\n" \
57 testing "sed s arbitrary delimiter" "-e 's woo boing '" "boing\n" "" "woo\n"
58 testing "sed s chains" "-e s/foo/bar/ -e s/bar/baz/" "baz\n" "" "foo\n"
59 testing "sed s chains2" "-e s/foo/bar/ -e s/baz/nee/" "bar\n" "" "foo\n"
60 testing "sed s [delimiter]" "-e 's@[@]@@'" "onetwo" "" "one@two"
63 testing "sed b (branch)" "-e 'b one;p;: one'" "foo\n" "" "foo\n"
64 testing "sed b (branch with no label jumps to end)" "-e 'b;p'" \
68 testing "sed t (test/branch)" "-e 's/a/1/;t one;p;: one;p'" \
69 "1\n1\nb\nb\nb\nc\nc\nc\n" "" "a\nb\nc\n"
70 testing "sed t (test/branch clears test bit)" "-e 's/a/b/;:loop;t loop'" \
71 "b\nb\nc\n" "" "a\nb\nc\n"
72 testing "sed T (!test/branch)" "-e 's/a/1/;T notone;p;: notone;p'" \
73 "1\n1\n1\nb\nb\nc\nc\n" "" "a\nb\nc\n"
75 # Normal sed end-of-script doesn't print "c" because n flushed the pattern
76 # space. If n hits EOF, pattern space is empty when script ends.
77 # Query: how does this interact with no newline at EOF?
78 testing "sed n (flushes pattern space, terminates early)" "-e 'n;p'" \
79 "a\nb\nb\nc\n" "" "a\nb\nc\n"
80 # N does _not_ flush pattern space, therefore c is still in there @ script end.
81 testing "sed N (doesn't flush pattern space when terminating)" "-e 'N;p'" \
82 "a\nb\na\nb\nc\n" "" "a\nb\nc\n"
83 testing "sed address match newline" '"/b/N;/b\\nc/i woo"' "a\nwoo\nb\nc\nd\n" \
86 # Multiple lines in pattern space
87 testing "sed N (stops at end of input) and P (prints to first newline only)" \
88 "-n 'N;P;p'" "a\na\nb\n" "" "a\nb\nc\n"
91 testing "sed G (append hold space to pattern space)" 'G' "a\n\nb\n\nc\n\n" \
93 #testing "sed g/G (swap/append hold and patter space)"
94 #testing "sed g (swap hold/pattern space)"
96 testing "sed d ends script iteration" \
97 "-e '/ook/d;s/ook/ping/p;i woot'" "" "" "ook\n"
98 testing "sed d ends script iteration (2)" \
99 "-e '/ook/d;a\' -e 'bang'" "woot\nbang\n" "" "ook\nwoot\n"
101 # Ponder this a bit more, why "woo not found" from gnu version?
102 #testing "sed doesn't substitute in deleted line" \
103 # "-e '/ook/d;s/ook//;t woo;a bang;'" "bang" "" "ook\n"
105 # This makes both seds very unhappy. Why?
106 #testing "sed -g (exhaustive)" "sed -e 's/[[:space:]]*/,/g'" ",1,2,3,4,5," \