sed.tests 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/bin/sh
  2. # SUSv3 compliant sed tests.
  3. # Copyright 2005 by Rob Landley <rob@landley.net>
  4. # Licensed under GPL v2, see file LICENSE for details.
  5. . testing.sh
  6. # testing "description" "arguments" "result" "infile" "stdin"
  7. # Corner cases
  8. testing "sed no files (stdin)" 'sed ""' "hello\n" "" "hello\n"
  9. testing "sed explicit stdin" 'sed "" -' "hello\n" "" "hello\n"
  10. testing "sed handles empty lines" "sed -e 's/\$/@/'" "@\n" "" "\n"
  11. testing "sed stdin twice" 'sed "" - -' "hello" "" "hello"
  12. # Trailing EOF.
  13. # Match $, at end of each file or all files?
  14. # -e corner cases
  15. # without -e
  16. # multiple -e
  17. # interact with a
  18. # -eee arg1 arg2 arg3
  19. # -f corner cases
  20. # -e -f -e
  21. # -n corner cases
  22. # no newline at EOF?
  23. # -r corner cases
  24. # Just make sure it works.
  25. # -i corner cases:
  26. # sed -i -
  27. # permissions
  28. # -i on a symlink
  29. # on a directory
  30. # With $ last-line test
  31. # Continue with \
  32. # End of script with trailing \
  33. # command list
  34. testing "sed accepts blanks before command" "sed -e '1 d'" "" "" ""
  35. testing "sed accepts newlines in -e" "sed -e 'i\
  36. 1
  37. a\
  38. 3'" "1\n2\n3\n" "" "2\n"
  39. testing "sed accepts multiple -e" "sed -e 'i\' -e '1' -e 'a\' -e '3'" \
  40. "1\n2\n3\n" "" "2\n"
  41. # substitutions
  42. testing "sed -n" "sed -n -e s/foo/bar/ -e s/bar/baz/" "" "" "foo\n"
  43. testing "sed s//p" "sed -e s/foo/bar/p -e s/bar/baz/p" "bar\nbaz\nbaz\n" \
  44. "" "foo\n"
  45. testing "sed -n s//p" "sed -ne s/abc/def/p" "def\n" "" "abc\n"
  46. testing "sed s//g (exhaustive)" "sed -e 's/[[:space:]]*/,/g'" ",1,2,3,4,5,\n" \
  47. "" "12345\n"
  48. testing "sed s arbitrary delimiter" "sed -e 's woo boing '" "boing\n" "" "woo\n"
  49. testing "sed s chains" "sed -e s/foo/bar/ -e s/bar/baz/" "baz\n" "" "foo\n"
  50. testing "sed s chains2" "sed -e s/foo/bar/ -e s/baz/nee/" "bar\n" "" "foo\n"
  51. testing "sed s [delimiter]" "sed -e 's@[@]@@'" "onetwo" "" "one@two"
  52. # branch
  53. testing "sed b (branch)" "sed -e 'b one;p;: one'" "foo\n" "" "foo\n"
  54. testing "sed b (branch with no label jumps to end)" "sed -e 'b;p'" \
  55. "foo\n" "" "foo\n"
  56. # test and branch
  57. testing "sed t (test/branch)" "sed -e 's/a/1/;t one;p;: one;p'" \
  58. "1\n1\nb\nb\nb\nc\nc\nc\n" "" "a\nb\nc\n"
  59. testing "sed t (test/branch clears test bit)" "sed -e 's/a/b/;:loop;t loop'" \
  60. "b\nb\nc\n" "" "a\nb\nc\n"
  61. testing "sed T (!test/branch)" "sed -e 's/a/1/;T notone;p;: notone;p'" \
  62. "1\n1\n1\nb\nb\nc\nc\n" "" "a\nb\nc\n"
  63. # Normal sed end-of-script doesn't print "c" because n flushed the pattern
  64. # space. If n hits EOF, pattern space is empty when script ends.
  65. # Query: how does this interact with no newline at EOF?
  66. testing "sed n (flushes pattern space, terminates early)" "sed -e 'n;p'" \
  67. "a\nb\nb\nc\n" "" "a\nb\nc\n"
  68. # N does _not_ flush pattern space, therefore c is still in there @ script end.
  69. testing "sed N (doesn't flush pattern space when terminating)" "sed -e 'N;p'" \
  70. "a\nb\na\nb\nc\n" "" "a\nb\nc\n"
  71. testing "sed address match newline" 'sed "/b/N;/b\\nc/i woo"' \
  72. "a\nwoo\nb\nc\nd\n" "" "a\nb\nc\nd\n"
  73. # Multiple lines in pattern space
  74. testing "sed N (stops at end of input) and P (prints to first newline only)" \
  75. "sed -n 'N;P;p'" "a\na\nb\n" "" "a\nb\nc\n"
  76. # Hold space
  77. testing "sed G (append hold space to pattern space)" 'sed G' "a\n\nb\n\nc\n\n" \
  78. "" "a\nb\nc\n"
  79. #testing "sed g/G (swap/append hold and patter space)"
  80. #testing "sed g (swap hold/pattern space)"
  81. testing "sed d ends script iteration" \
  82. "sed -e '/ook/d;s/ook/ping/p;i woot'" "" "" "ook\n"
  83. testing "sed d ends script iteration (2)" \
  84. "sed -e '/ook/d;a\' -e 'bang'" "woot\nbang\n" "" "ook\nwoot\n"
  85. # Multiple files, with varying newlines and NUL bytes
  86. testing "sed embedded NUL" "sed -e 's/woo/bang/'" "\0bang\0woo\0" "" \
  87. "\0woo\0woo\0"
  88. testing "sed embedded NUL g" "sed -e 's/woo/bang/g'" "bang\0bang\0" "" \
  89. "woo\0woo\0"
  90. echo -e "/woo/a he\0llo" > sed.commands
  91. testing "sed NUL in command" "sed -f sed.commands" "woo\nhe\0llo\n" "" "woo"
  92. rm sed.commands
  93. # sed has funky behavior with newlines at the end of file. Test lots of
  94. # corner cases with the optional newline appending behavior.
  95. testing "sed normal newlines" "sed -e 's/woo/bang/' input -" "bang\nbang\n" \
  96. "woo\n" "woo\n"
  97. testing "sed leave off trailing newline" "sed -e 's/woo/bang/' input -" \
  98. "bang\nbang" "woo\n" "woo"
  99. testing "sed autoinsert newline" "sed -e 's/woo/bang/' input -" "bang\nbang" \
  100. "woo" "woo"
  101. testing "sed empty file plus cat" "sed -e 's/nohit//' input -" "one\ntwo" \
  102. "" "one\ntwo"
  103. testing "sed cat plus empty file" "sed -e 's/nohit//' input -" "one\ntwo" \
  104. "one\ntwo" ""
  105. testing "sed append autoinserts newline" "sed -e '/woot/a woo' -" \
  106. "woot\nwoo\n" "" "woot"
  107. testing "sed insert doesn't autoinsert newline" "sed -e '/woot/i woo' -" \
  108. "woo\nwoot" "" "woot"
  109. testing "sed print autoinsert newlines" "sed -e 'p' -" "one\none" "" "one"
  110. testing "sed print autoinsert newlines two files" "sed -e 'p' input -" \
  111. "one\none\ntwo\ntwo" "one" "two"
  112. testing "sed noprint, no match, no newline" "sed -ne 's/woo/bang/' input" \
  113. "" "no\n" ""
  114. testing "sed selective matches with one nl" "sed -ne 's/woo/bang/p' input -" \
  115. "a bang\nc bang\n" "a woo\nb no" "c woo\nd no"
  116. testing "sed selective matches insert newline" \
  117. "sed -ne 's/woo/bang/p' input -" "a bang\nb bang\nd bang" \
  118. "a woo\nb woo" "c no\nd woo"
  119. testing "sed selective matches noinsert newline" \
  120. "sed -ne 's/woo/bang/p' input -" "a bang\nb bang" "a woo\nb woo" \
  121. "c no\nd no"
  122. testing "sed clusternewline" \
  123. "sed -e '/one/a 111' -e '/two/i 222' -e p input -" \
  124. "one\none\n111\n222\ntwo\ntwo" "one" "two"
  125. # Test end-of-file matching behavior
  126. testing "sed match EOF" "sed -e '"'$p'"'" "hello\nthere\nthere" "" \
  127. "hello\nthere"
  128. testing "sed match EOF two files" "sed -e '"'$p'"' input -" \
  129. "one\ntwo\nthree\nfour\nfour" "one\ntwo" "three\nfour"
  130. echo -ne "three\nfour" > input2
  131. testing "sed match EOF inline" \
  132. "sed -e '"'$i ook'"' -i input input2 && cat input input2" \
  133. "one\nook\ntwothree\nook\nfour" "one\ntwo" ""
  134. rm input2
  135. # Test lie-to-autoconf
  136. testing "sed lie-to-autoconf" "sed --version | grep -o 'GNU sed version '" \
  137. "GNU sed version \n" "" ""
  138. # Jump to nonexistent label
  139. testing "sed nonexistent label" "sed -e 'b walrus' 2> /dev/null || echo yes" \
  140. "yes\n" "" ""
  141. testing "sed backref from empty s uses range regex" \
  142. "sed -e '/woot/s//eep \0 eep/'" "eep woot eep" "" "woot"
  143. testing "sed backref from empty s uses range regex with newline" \
  144. "sed -e '/woot/s//eep \0 eep/'" "eep woot eep\n" "" "woot\n"
  145. # -i with no filename
  146. touch ./- # Detect gnu failure mode here.
  147. testing "sed -i with no arg [GNUFAIL]" "sed -e '' -i 2> /dev/null || echo yes" \
  148. "yes\n" "" ""
  149. rm ./- # Clean up
  150. testing "sed s/xxx/[/" "sed -e 's/xxx/[/'" "[\n" "" "xxx\n"
  151. # Ponder this a bit more, why "woo not found" from gnu version?
  152. #testing "sed doesn't substitute in deleted line" \
  153. # "sed -e '/ook/d;s/ook//;t woo;a bang;'" "bang" "" "ook\n"
  154. # This makes both seds very unhappy. Why?
  155. #testing "sed -g (exhaustive)" "sed -e 's/[[:space:]]*/,/g'" ",1,2,3,4,5," \
  156. # "" "12345"
  157. exit $FAILCOUNT