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