1 | #! /bin/sh
|
---|
2 | # Test various combinations of command-line options.
|
---|
3 | #
|
---|
4 | # Copyright (C) 2001, 2006, 2009-2012 Free Software Foundation, Inc.
|
---|
5 | #
|
---|
6 | # Copying and distribution of this file, with or without modification,
|
---|
7 | # are permitted in any medium without royalty provided the copyright
|
---|
8 | # notice and this notice are preserved.
|
---|
9 | #
|
---|
10 | # This set of tests was started by Julian Foad.
|
---|
11 |
|
---|
12 | . "${srcdir=.}/init.sh"; path_prepend_ ../src
|
---|
13 |
|
---|
14 | failures=0
|
---|
15 |
|
---|
16 | # grep_test INPUT EXPECTED_OUTPUT PATTERN_AND_OPTIONS...
|
---|
17 | # Run "grep" with the given INPUT, pattern and options, and check that
|
---|
18 | # the output is EXPECTED_OUTPUT. If not, print a message and set 'failures'.
|
---|
19 | # "/" represents a newline within INPUT and EXPECTED_OUTPUT.
|
---|
20 | grep_test ()
|
---|
21 | {
|
---|
22 | INPUT="$1"
|
---|
23 | EXPECT="$2"
|
---|
24 | shift 2
|
---|
25 | OUTPUT=$(printf %s "$INPUT" | tr "/" "\n" | grep "$@" | tr "\n" "/")
|
---|
26 | if test "$OUTPUT" != "$EXPECT" || test "$VERBOSE" = "yes"; then
|
---|
27 | echo "Testing: grep $@"
|
---|
28 | test "$LC_ALL" != C && test "$LC_ALL" != "" && echo " LC_ALL: \"$LC_ALL\""
|
---|
29 | echo " input: \"$INPUT\""
|
---|
30 | echo " output: \"$OUTPUT\""
|
---|
31 | fi
|
---|
32 | if test "$OUTPUT" != "$EXPECT"; then
|
---|
33 | echo " expect: \"$EXPECT\""
|
---|
34 | echo "FAIL"
|
---|
35 | failures=1
|
---|
36 | fi
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 | # Test "--only-matching" ("-o") option
|
---|
41 |
|
---|
42 | # "-o" with "-i" should output an exact copy of the matching input text.
|
---|
43 | grep_test "WordA/wordB/WORDC/" "Word/word/WORD/" "word" -o -i
|
---|
44 | grep_test "WordA/wordB/WORDC/" "Word/word/WORD/" "Word" -o -i
|
---|
45 | grep_test "WordA/wordB/WORDC/" "Word/word/WORD/" "WORD" -o -i
|
---|
46 |
|
---|
47 | # Should display the line number (-n), octet offset (-b), or file name
|
---|
48 | # (-H) of every match, not just of the first match on each input line.
|
---|
49 | # Check it both with and without -i because of the separate code paths.
|
---|
50 | # Also check what it does when lines of context are specified.
|
---|
51 | grep_test "wA wB/wC/" "1:wA/1:wB/2:wC/" "w." -o -n
|
---|
52 | grep_test "wA wB/wC/" "1:wA/1:wB/2:wC/" "w." -o -n -i
|
---|
53 | grep_test "wA wB/wC/" "1:wA/1:wB/2:wC/" "w." -o -n -3 2>/dev/null
|
---|
54 | grep_test "XwA YwB/ZwC/" "1:wA/5:wB/9:wC/" "w." -o -b
|
---|
55 | grep_test "XwA YwB/ZwC/" "1:wA/5:wB/9:wC/" "w." -o -b -i
|
---|
56 | grep_test "XwA YwB/ZwC/" "1:wA/5:wB/9:wC/" "w." -o -b -3 2>/dev/null
|
---|
57 | grep_test "XwA YwB/ZwC/" "1:w/5:w/9:w/" "w" -F -o -b
|
---|
58 | grep_test "XwA YwB/ZwC/" "1:w/5:w/9:w/" "w" -F -o -b -i
|
---|
59 | grep_test "XwA YwB/ZwC/" "1:w/5:w/9:w/" "w" -F -o -b -3 2>/dev/null
|
---|
60 | grep_test "wA wB/" "(standard input):wA/(standard input):wB/" "w." -o -H
|
---|
61 | grep_test "wA wB/" "(standard input):wA/(standard input):wB/" "w." -o -H -i
|
---|
62 | grep_test "wA wB/" "(standard input):wA/(standard input):wB/" "w." -o -H -3 2>/dev/null
|
---|
63 |
|
---|
64 | # Combination of -h and -H
|
---|
65 | grep_test "wA wB/" "wA wB/" "w."
|
---|
66 | grep_test "wA wB/" "wA wB/" "w." -h
|
---|
67 | grep_test "wA wB/" "wA wB/" "w." -H -h
|
---|
68 | grep_test "wA wB/" "(standard input):wA wB/" "w." -H
|
---|
69 | grep_test "wA wB/" "(standard input):wA wB/" "w." -h -H
|
---|
70 |
|
---|
71 | # End of a previous match should not match a "start of ..." expression.
|
---|
72 | grep_test "word_word/" "word_/" "^word_*" -o
|
---|
73 | grep_test "wordword/" "word/" "\<word" -o
|
---|
74 |
|
---|
75 |
|
---|
76 | # Test "--color" option
|
---|
77 |
|
---|
78 | CB="[01;31m[K"
|
---|
79 | CE="[m[K"
|
---|
80 |
|
---|
81 | # "--color" with "-i" should output an exact copy of the matching input text.
|
---|
82 | grep_test "WordA/wordb/WORDC/" "${CB}Word${CE}A/${CB}word${CE}b/${CB}WORD${CE}C/" "word" --color=always -i
|
---|
83 | grep_test "WordA/wordb/WORDC/" "${CB}Word${CE}A/${CB}word${CE}b/${CB}WORD${CE}C/" "Word" --color=always -i
|
---|
84 | grep_test "WordA/wordb/WORDC/" "${CB}Word${CE}A/${CB}word${CE}b/${CB}WORD${CE}C/" "WORD" --color=always -i
|
---|
85 |
|
---|
86 | # End of a previous match should not match a "start of ..." expression.
|
---|
87 | grep_test "word_word/" "${CB}word_${CE}word/" "^word_*" --color=always
|
---|
88 | grep_test "wordword/" "${CB}word${CE}word/" "\<word" --color=always
|
---|
89 |
|
---|
90 |
|
---|
91 | # Test combination of "-m" with "-A" and anchors.
|
---|
92 | # Based on a report from Pavol Gono.
|
---|
93 | grep_test "4/40/" "4/40/" "^4$" -m1 -A99
|
---|
94 | grep_test "4/04/" "4/04/" "^4$" -m1 -A99
|
---|
95 | grep_test "4/444/" "4/444/" "^4$" -m1 -A99
|
---|
96 | grep_test "4/40/" "4/" "^4" -m1 -A99
|
---|
97 | grep_test "4/04/" "4/04/" "^4" -m1 -A99
|
---|
98 | grep_test "4/444/" "4/" "^4" -m1 -A99
|
---|
99 | grep_test "4/40/" "4/40/" "4$" -m1 -A99
|
---|
100 | grep_test "4/04/" "4/" "4$" -m1 -A99
|
---|
101 | grep_test "4/444/" "4/" "4$" -m1 -A99
|
---|
102 |
|
---|
103 |
|
---|
104 | # Test for "-F -w" bugs. Thanks to Gordon Lack for these two.
|
---|
105 | grep_test "A/CX/B/C/" "A/B/C/" -wF -e A -e B -e C
|
---|
106 | grep_test "LIN7C 55327/" "" -wF -e 5327 -e 5532
|
---|
107 |
|
---|
108 | # Test for non-empty matches following empty ones.
|
---|
109 | grep_test 'xyz/' 'y/' -o 'y*'
|
---|
110 | grep_test 'xyz/' "x${CB}y${CE}z/" --color=always 'y*'
|
---|
111 |
|
---|
112 | # Test for increasing/decreasing-length word matches,
|
---|
113 | # for independence from pattern order within the pattern list,
|
---|
114 | # and for preferring the longest match at a given position.
|
---|
115 | x0='a bb ccc dddd/'
|
---|
116 | x1='dddd ccc bb a/'
|
---|
117 | x2='bcd abcd abc bc bcd abc/'
|
---|
118 | x3='bc abcd bc/'
|
---|
119 | y0="a ${CB}bb${CE} ${CB}ccc${CE} dddd/"
|
---|
120 | y1="dddd ${CB}ccc${CE} ${CB}bb${CE} a/"
|
---|
121 | y2="bcd abcd abc ${CB}bc${CE} bcd abc/"
|
---|
122 | y3="${CB}bc${CE} abcd ${CB}bc${CE}/"
|
---|
123 | grep_test "$x0" "$y0" -E --color=always -e bb -e cc -e ccc
|
---|
124 | grep_test "$x0" "$y0" -F --color=always -e bb -e cc -e ccc
|
---|
125 | grep_test "$x0" "$y0" -E --color=always -e bb -e ccc -e cc
|
---|
126 | grep_test "$x0" "$y0" -F --color=always -e bb -e ccc -e cc
|
---|
127 | grep_test "$x0" "$y0" -E -w --color=always -e bb -e ccc
|
---|
128 | grep_test "$x0" "$y0" -F -w --color=always -e bb -e ccc
|
---|
129 | grep_test "$x0" "$y0" -E -w --color=always -e ccc -e bb
|
---|
130 | grep_test "$x0" "$y0" -F -w --color=always -e ccc -e bb
|
---|
131 | grep_test "$x1" "$y1" -E -w --color=always -e bb -e ccc
|
---|
132 | grep_test "$x1" "$y1" -F -w --color=always -e bb -e ccc
|
---|
133 | grep_test "$x1" "$y1" -E -w --color=always -e ccc -e bb
|
---|
134 | grep_test "$x1" "$y1" -F -w --color=always -e ccc -e bb
|
---|
135 | grep_test "$x2" "$y2" -E -w --color=always bc
|
---|
136 | grep_test "$x2" "$y2" -F -w --color=always bc
|
---|
137 | grep_test "$x3" "$y3" -E -w --color=always bc
|
---|
138 | grep_test "$x3" "$y3" -F -w --color=always bc
|
---|
139 |
|
---|
140 | # Skip the rest of the tests - known to fail. TAA.
|
---|
141 | Exit $failures
|
---|
142 |
|
---|
143 | # The rest of this file is meant to be executed under this locale.
|
---|
144 | LC_ALL=cs_CZ.UTF-8; export LC_ALL
|
---|
145 | # If the UTF-8 locale doesn't work, skip these tests silently.
|
---|
146 | locale -k LC_CTYPE 2>/dev/null | grep -q "charmap.*UTF-8" || Exit $failures
|
---|
147 |
|
---|
148 | # Test character class erroneously matching a '[' character.
|
---|
149 | grep_test "[/" "" "[[:alpha:]]" -E
|
---|
150 |
|
---|
151 | for mode in F G E; do
|
---|
152 | # Hint: pipe the output of these tests in
|
---|
153 | # "| LESS= LESSCHARSET=ascii less".
|
---|
154 | # LETTER N WITH TILDE is U+00F1 and U+00D1.
|
---|
155 | # LETTER Y WITH DIAERESIS is U+00FF and U+0178.
|
---|
156 | grep_test 'añÿb/AÑŸB/' 'ñÿ/ÑŸ/' 'ñÿ' -o -i -$mode
|
---|
157 | grep_test 'añÿb/AÑŸB/' 'ñÿ/ÑŸ/' 'ÑŸ' -o -i -$mode
|
---|
158 | grep_test 'añÿb/AÑŸB/' "a${CB}ñÿ${CE}b/A${CB}ÑŸ${CE}B/" 'ñÿ' --color=always -i -$mode
|
---|
159 | grep_test 'añÿb/AÑŸB/' "a${CB}ñÿ${CE}b/A${CB}ÑŸ${CE}B/" 'ÑŸ' --color=always -i -$mode
|
---|
160 |
|
---|
161 | # POSIX (about -i): ... each character in the string is matched
|
---|
162 | # against the pattern, not only the character, but also its case
|
---|
163 | # counterpart (if any), shall be matched.
|
---|
164 | # The following were chosen because of their trickiness due to the
|
---|
165 | # differing UTF-8 octet length of their counterpart and to the
|
---|
166 | # non-reflexivity of their mapping.
|
---|
167 | # Beware of homographs! Look carefully at the actual octets.
|
---|
168 |
|
---|
169 | # lc(U+0130 LATIN CAPITAL LETTER I WITH DOT ABOVE) = U+0069 LATIN SMALL LETTER I
|
---|
170 | grep_test 'aİb/' "a${CB}İ${CE}b/" 'i' --color=always -i -$mode
|
---|
171 | grep_test 'aib/' '' 'İ' --color=always -i -$mode
|
---|
172 | grep_test 'aİb/' '' 'I' --color=always -i -$mode
|
---|
173 | # uc(U+0131 LATIN SMALL LETTER DOTLESS I) = U+0049 LATIN CAPITAL LETTER I
|
---|
174 | grep_test 'aıb/' "a${CB}ı${CE}b/" 'I' --color=always -i -$mode
|
---|
175 | grep_test 'aIb/' '' 'ı' --color=always -i -$mode
|
---|
176 | grep_test 'aıb/' '' 'i' --color=always -i -$mode
|
---|
177 | # uc(U+017F LATIN SMALL LETTER LONG S) = U+0053 LATIN CAPITAL LETTER S
|
---|
178 | grep_test 'aſb/' "a${CB}ſ${CE}b/" 'S' --color=always -i -$mode
|
---|
179 | grep_test 'aSb/' '' 'ſ' --color=always -i -$mode
|
---|
180 | grep_test 'aſb/' '' 's' --color=always -i -$mode
|
---|
181 | # uc(U+1FBE GREEK PROSGEGRAMMENI) = U+0399 GREEK CAPITAL LETTER IOTA
|
---|
182 | grep_test 'aιb/' "a${CB}ι${CE}b/" 'Ι' --color=always -i -$mode
|
---|
183 | grep_test 'aΙb/' '' 'ι' --color=always -i -$mode
|
---|
184 | grep_test 'aιb/' '' 'ι' --color=always -i -$mode
|
---|
185 | # lc(U+2126 OHM SIGN) = U+03C9 GREEK SMALL LETTER OMEGA
|
---|
186 | grep_test 'aΩb/' "a${CB}Ω${CE}b/" 'ω' --color=always -i -$mode
|
---|
187 | grep_test 'aωb/' '' 'Ω' --color=always -i -$mode
|
---|
188 | grep_test 'aΩb/' '' 'Ω' --color=always -i -$mode
|
---|
189 | # lc(U+212A KELVIN SIGN) = U+006B LATIN SMALL LETTER K
|
---|
190 | grep_test 'aKb/' "a${CB}K${CE}b/" 'k' --color=always -i -$mode
|
---|
191 | grep_test 'akb/' '' 'K' --color=always -i -$mode
|
---|
192 | grep_test 'aKb/' '' 'K' --color=always -i -$mode
|
---|
193 | # lc(U+212B ANGSTROM SIGN) = U+00E5 LATIN SMALL LETTER A WITH RING ABOVE
|
---|
194 | grep_test 'aÅb/' "a${CB}Å${CE}b/" 'å' --color=always -i -$mode
|
---|
195 | grep_test 'aåb/' '' 'Å' --color=always -i -$mode
|
---|
196 | grep_test 'aÅb/' '' 'Å' --color=always -i -$mode
|
---|
197 | done
|
---|
198 |
|
---|
199 |
|
---|
200 | # Any tests inserted right here will be performed under an UTF-8 locale.
|
---|
201 | # Insert them before LC_ALL is set above to avoid this.
|
---|
202 | # Leave this comment last.
|
---|
203 |
|
---|
204 | Exit $failures
|
---|