3e75cb0cbed87ced2611cbc2dd1d1adf9e4e7c71
1#!/bin/sh
2
3test_description='wildmatch tests'
4
5. ./test-lib.sh
6
7match_with_function() {
8 text=$1
9 pattern=$2
10 match_expect=$3
11 match_function=$4
12
13 if test "$match_expect" = 1
14 then
15 test_expect_success "$match_function: match '$text' '$pattern'" "
16 test-wildmatch $match_function '$text' '$pattern'
17 "
18 elif test "$match_expect" = 0
19 then
20 test_expect_success "$match_function: no match '$text' '$pattern'" "
21 test_must_fail test-wildmatch $match_function '$text' '$pattern'
22 "
23 else
24 test_expect_success "PANIC: Test framework error. Unknown matches value $match_expect" 'false'
25 fi
26
27}
28
29match() {
30 match_glob=$1
31 match_iglob=$2
32 match_pathmatch=$3
33 match_pathmatchi=$4
34 text=$5
35 pattern=$6
36
37 # $1: Case sensitive glob match: test-wildmatch & ls-files
38 match_with_function "$text" "$pattern" $match_glob "wildmatch"
39
40 # $2: Case insensitive glob match: test-wildmatch & ls-files
41 match_with_function "$text" "$pattern" $match_iglob "iwildmatch"
42
43 # $3: Case sensitive path match: test-wildmatch & ls-files
44 match_with_function "$text" "$pattern" $match_pathmatch "pathmatch"
45
46 # $4: Case insensitive path match: test-wildmatch & ls-files
47 match_with_function "$text" "$pattern" $match_pathmatchi "ipathmatch"
48}
49
50# Basic wildmatch features
51match 1 1 1 1 foo foo
52match 0 0 0 0 foo bar
53match 1 1 1 1 '' ""
54match 1 1 1 1 foo '???'
55match 0 0 0 0 foo '??'
56match 1 1 1 1 foo '*'
57match 1 1 1 1 foo 'f*'
58match 0 0 0 0 foo '*f'
59match 1 1 1 1 foo '*foo*'
60match 1 1 1 1 foobar '*ob*a*r*'
61match 1 1 1 1 aaaaaaabababab '*ab'
62match 1 1 1 1 'foo*' 'foo\*'
63match 0 0 0 0 foobar 'foo\*bar'
64match 1 1 1 1 'f\oo' 'f\\oo'
65match 1 1 1 1 ball '*[al]?'
66match 0 0 0 0 ten '[ten]'
67match 0 0 1 1 ten '**[!te]'
68match 0 0 0 0 ten '**[!ten]'
69match 1 1 1 1 ten 't[a-g]n'
70match 0 0 0 0 ten 't[!a-g]n'
71match 1 1 1 1 ton 't[!a-g]n'
72match 1 1 1 1 ton 't[^a-g]n'
73match 1 1 1 1 'a]b' 'a[]]b'
74match 1 1 1 1 a-b 'a[]-]b'
75match 1 1 1 1 'a]b' 'a[]-]b'
76match 0 0 0 0 aab 'a[]-]b'
77match 1 1 1 1 aab 'a[]a-]b'
78match 1 1 1 1 ']' ']'
79
80# Extended slash-matching features
81match 0 0 1 1 'foo/baz/bar' 'foo*bar'
82match 0 0 1 1 'foo/baz/bar' 'foo**bar'
83match 0 0 1 1 'foobazbar' 'foo**bar'
84match 1 1 1 1 'foo/baz/bar' 'foo/**/bar'
85match 1 1 0 0 'foo/baz/bar' 'foo/**/**/bar'
86match 1 1 1 1 'foo/b/a/z/bar' 'foo/**/bar'
87match 1 1 1 1 'foo/b/a/z/bar' 'foo/**/**/bar'
88match 1 1 0 0 'foo/bar' 'foo/**/bar'
89match 1 1 0 0 'foo/bar' 'foo/**/**/bar'
90match 0 0 1 1 'foo/bar' 'foo?bar'
91match 0 0 1 1 'foo/bar' 'foo[/]bar'
92match 0 0 1 1 'foo/bar' 'foo[^a-z]bar'
93match 0 0 1 1 'foo/bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'
94match 1 1 1 1 'foo-bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'
95match 1 1 0 0 'foo' '**/foo'
96match 1 1 1 1 'XXX/foo' '**/foo'
97match 1 1 1 1 'bar/baz/foo' '**/foo'
98match 0 0 1 1 'bar/baz/foo' '*/foo'
99match 0 0 1 1 'foo/bar/baz' '**/bar*'
100match 1 1 1 1 'deep/foo/bar/baz' '**/bar/*'
101match 0 0 1 1 'deep/foo/bar/baz/' '**/bar/*'
102match 1 1 1 1 'deep/foo/bar/baz/' '**/bar/**'
103match 0 0 0 0 'deep/foo/bar' '**/bar/*'
104match 1 1 1 1 'deep/foo/bar/' '**/bar/**'
105match 0 0 1 1 'foo/bar/baz' '**/bar**'
106match 1 1 1 1 'foo/bar/baz/x' '*/bar/**'
107match 0 0 1 1 'deep/foo/bar/baz/x' '*/bar/**'
108match 1 1 1 1 'deep/foo/bar/baz/x' '**/bar/*/*'
109
110# Various additional tests
111match 0 0 0 0 'acrt' 'a[c-c]st'
112match 1 1 1 1 'acrt' 'a[c-c]rt'
113match 0 0 0 0 ']' '[!]-]'
114match 1 1 1 1 'a' '[!]-]'
115match 0 0 0 0 '' '\'
116match 0 0 0 0 '\' '\'
117match 0 0 0 0 'XXX/\' '*/\'
118match 1 1 1 1 'XXX/\' '*/\\'
119match 1 1 1 1 'foo' 'foo'
120match 1 1 1 1 '@foo' '@foo'
121match 0 0 0 0 'foo' '@foo'
122match 1 1 1 1 '[ab]' '\[ab]'
123match 1 1 1 1 '[ab]' '[[]ab]'
124match 1 1 1 1 '[ab]' '[[:]ab]'
125match 0 0 0 0 '[ab]' '[[::]ab]'
126match 1 1 1 1 '[ab]' '[[:digit]ab]'
127match 1 1 1 1 '[ab]' '[\[:]ab]'
128match 1 1 1 1 '?a?b' '\??\?b'
129match 1 1 1 1 'abc' '\a\b\c'
130match 0 0 0 0 'foo' ''
131match 1 1 1 1 'foo/bar/baz/to' '**/t[o]'
132
133# Character class tests
134match 1 1 1 1 'a1B' '[[:alpha:]][[:digit:]][[:upper:]]'
135match 0 1 0 1 'a' '[[:digit:][:upper:][:space:]]'
136match 1 1 1 1 'A' '[[:digit:][:upper:][:space:]]'
137match 1 1 1 1 '1' '[[:digit:][:upper:][:space:]]'
138match 0 0 0 0 '1' '[[:digit:][:upper:][:spaci:]]'
139match 1 1 1 1 ' ' '[[:digit:][:upper:][:space:]]'
140match 0 0 0 0 '.' '[[:digit:][:upper:][:space:]]'
141match 1 1 1 1 '.' '[[:digit:][:punct:][:space:]]'
142match 1 1 1 1 '5' '[[:xdigit:]]'
143match 1 1 1 1 'f' '[[:xdigit:]]'
144match 1 1 1 1 'D' '[[:xdigit:]]'
145match 1 1 1 1 '_' '[[:alnum:][:alpha:][:blank:][:cntrl:][:digit:][:graph:][:lower:][:print:][:punct:][:space:][:upper:][:xdigit:]]'
146match 1 1 1 1 '.' '[^[:alnum:][:alpha:][:blank:][:cntrl:][:digit:][:lower:][:space:][:upper:][:xdigit:]]'
147match 1 1 1 1 '5' '[a-c[:digit:]x-z]'
148match 1 1 1 1 'b' '[a-c[:digit:]x-z]'
149match 1 1 1 1 'y' '[a-c[:digit:]x-z]'
150match 0 0 0 0 'q' '[a-c[:digit:]x-z]'
151
152# Additional tests, including some malformed wildmatch patterns
153match 1 1 1 1 ']' '[\\-^]'
154match 0 0 0 0 '[' '[\\-^]'
155match 1 1 1 1 '-' '[\-_]'
156match 1 1 1 1 ']' '[\]]'
157match 0 0 0 0 '\]' '[\]]'
158match 0 0 0 0 '\' '[\]]'
159match 0 0 0 0 'ab' 'a[]b'
160match 0 0 0 0 'a[]b' 'a[]b'
161match 0 0 0 0 'ab[' 'ab['
162match 0 0 0 0 'ab' '[!'
163match 0 0 0 0 'ab' '[-'
164match 1 1 1 1 '-' '[-]'
165match 0 0 0 0 '-' '[a-'
166match 0 0 0 0 '-' '[!a-'
167match 1 1 1 1 '-' '[--A]'
168match 1 1 1 1 '5' '[--A]'
169match 1 1 1 1 ' ' '[ --]'
170match 1 1 1 1 '$' '[ --]'
171match 1 1 1 1 '-' '[ --]'
172match 0 0 0 0 '0' '[ --]'
173match 1 1 1 1 '-' '[---]'
174match 1 1 1 1 '-' '[------]'
175match 0 0 0 0 'j' '[a-e-n]'
176match 1 1 1 1 '-' '[a-e-n]'
177match 1 1 1 1 'a' '[!------]'
178match 0 0 0 0 '[' '[]-a]'
179match 1 1 1 1 '^' '[]-a]'
180match 0 0 0 0 '^' '[!]-a]'
181match 1 1 1 1 '[' '[!]-a]'
182match 1 1 1 1 '^' '[a^bc]'
183match 1 1 1 1 '-b]' '[a-]b]'
184match 0 0 0 0 '\' '[\]'
185match 1 1 1 1 '\' '[\\]'
186match 0 0 0 0 '\' '[!\\]'
187match 1 1 1 1 'G' '[A-\\]'
188match 0 0 0 0 'aaabbb' 'b*a'
189match 0 0 0 0 'aabcaa' '*ba*'
190match 1 1 1 1 ',' '[,]'
191match 1 1 1 1 ',' '[\\,]'
192match 1 1 1 1 '\' '[\\,]'
193match 1 1 1 1 '-' '[,-.]'
194match 0 0 0 0 '+' '[,-.]'
195match 0 0 0 0 '-.]' '[,-.]'
196match 1 1 1 1 '2' '[\1-\3]'
197match 1 1 1 1 '3' '[\1-\3]'
198match 0 0 0 0 '4' '[\1-\3]'
199match 1 1 1 1 '\' '[[-\]]'
200match 1 1 1 1 '[' '[[-\]]'
201match 1 1 1 1 ']' '[[-\]]'
202match 0 0 0 0 '-' '[[-\]]'
203
204# Test recursion
205match 1 1 1 1 '-adobe-courier-bold-o-normal--12-120-75-75-m-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
206match 0 0 0 0 '-adobe-courier-bold-o-normal--12-120-75-75-X-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
207match 0 0 0 0 '-adobe-courier-bold-o-normal--12-120-75-75-/-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
208match 1 1 1 1 'XXX/adobe/courier/bold/o/normal//12/120/75/75/m/70/iso8859/1' 'XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*'
209match 0 0 0 0 'XXX/adobe/courier/bold/o/normal//12/120/75/75/X/70/iso8859/1' 'XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*'
210match 1 1 1 1 'abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txt' '**/*a*b*g*n*t'
211match 0 0 0 0 'abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txtz' '**/*a*b*g*n*t'
212match 0 0 0 0 foo '*/*/*'
213match 0 0 0 0 foo/bar '*/*/*'
214match 1 1 1 1 foo/bba/arr '*/*/*'
215match 0 0 1 1 foo/bb/aa/rr '*/*/*'
216match 1 1 1 1 foo/bb/aa/rr '**/**/**'
217match 1 1 1 1 abcXdefXghi '*X*i'
218match 0 0 1 1 ab/cXd/efXg/hi '*X*i'
219match 1 1 1 1 ab/cXd/efXg/hi '*/*X*/*/*i'
220match 1 1 1 1 ab/cXd/efXg/hi '**/*X*/**/*i'
221
222# Extra pathmatch tests
223match 0 0 0 0 foo fo
224match 1 1 1 1 foo/bar foo/bar
225match 1 1 1 1 foo/bar 'foo/*'
226match 0 0 1 1 foo/bba/arr 'foo/*'
227match 1 1 1 1 foo/bba/arr 'foo/**'
228match 0 0 1 1 foo/bba/arr 'foo*'
229match 0 0 1 1 foo/bba/arr 'foo**'
230match 0 0 1 1 foo/bba/arr 'foo/*arr'
231match 0 0 1 1 foo/bba/arr 'foo/**arr'
232match 0 0 0 0 foo/bba/arr 'foo/*z'
233match 0 0 0 0 foo/bba/arr 'foo/**z'
234match 0 0 1 1 foo/bar 'foo?bar'
235match 0 0 1 1 foo/bar 'foo[/]bar'
236match 0 0 1 1 foo/bar 'foo[^a-z]bar'
237match 0 0 1 1 ab/cXd/efXg/hi '*Xg*i'
238
239# Extra case-sensitivity tests
240match 0 1 0 1 'a' '[A-Z]'
241match 1 1 1 1 'A' '[A-Z]'
242match 0 1 0 1 'A' '[a-z]'
243match 1 1 1 1 'a' '[a-z]'
244match 0 1 0 1 'a' '[[:upper:]]'
245match 1 1 1 1 'A' '[[:upper:]]'
246match 0 1 0 1 'A' '[[:lower:]]'
247match 1 1 1 1 'a' '[[:lower:]]'
248match 0 1 0 1 'A' '[B-Za]'
249match 1 1 1 1 'a' '[B-Za]'
250match 0 1 0 1 'A' '[B-a]'
251match 1 1 1 1 'a' '[B-a]'
252match 0 1 0 1 'z' '[Z-y]'
253match 1 1 1 1 'Z' '[Z-y]'
254
255test_done