3 class TestFnmatch < Test::Unit::TestCase
5 def bracket_test(s, t) # `s' should start with neither '!' nor '^'
7 assert_equal(t.include?(i.chr), File.fnmatch("[#{s}]", i.chr, File::FNM_DOTMATCH))
8 assert_equal(t.include?(i.chr), !File.fnmatch("[^#{s}]", i.chr, File::FNM_DOTMATCH))
9 assert_equal(t.include?(i.chr), !File.fnmatch("[!#{s}]", i.chr, File::FNM_DOTMATCH))
13 assert(File.fnmatch('\[1\]' , '[1]'), "[ruby-dev:22819]")
14 assert(File.fnmatch('*?', 'a'), "[ruby-dev:22815]")
15 assert(File.fnmatch('*/', 'a/'))
16 assert(File.fnmatch('\[1\]' , '[1]', File::FNM_PATHNAME))
17 assert(File.fnmatch('*?', 'a', File::FNM_PATHNAME))
18 assert(File.fnmatch('*/', 'a/', File::FNM_PATHNAME))
20 assert(File.fnmatch('cat', 'cat'))
21 assert(!File.fnmatch('cat', 'category'))
22 assert(!File.fnmatch('cat', 'wildcat'))
23 # '?' matches any one character
24 assert(File.fnmatch('?at', 'cat'))
25 assert(File.fnmatch('c?t', 'cat'))
26 assert(File.fnmatch('ca?', 'cat'))
27 assert(File.fnmatch('?a?', 'cat'))
28 assert(!File.fnmatch('c??t', 'cat'))
29 assert(!File.fnmatch('??at', 'cat'))
30 assert(!File.fnmatch('ca??', 'cat'))
31 # '*' matches any number (including 0) of any characters
32 assert(File.fnmatch('c*', 'cats'))
33 assert(File.fnmatch('c*ts', 'cats'))
34 assert(File.fnmatch('*ts', 'cats'))
35 assert(File.fnmatch('*c*a*t*s*', 'cats'))
36 assert(!File.fnmatch('c*t', 'cats'))
37 assert(!File.fnmatch('*abc', 'abcabz'))
38 assert(File.fnmatch('*abz', 'abcabz'))
39 assert(!File.fnmatch('a*abc', 'abc'))
40 assert(File.fnmatch('a*bc', 'abc'))
41 assert(!File.fnmatch('a*bc', 'abcd'))
42 # [seq] : matches any character listed between bracket
43 # [!seq] or [^seq] : matches any character except those listed between bracket
44 bracket_test("bd-gikl-mosv-x", "bdefgiklmosvwx")
46 assert(File.fnmatch('\?', '?'))
47 assert(!File.fnmatch('\?', '\?'))
48 assert(!File.fnmatch('\?', 'a'))
49 assert(!File.fnmatch('\?', '\a'))
50 assert(File.fnmatch('\*', '*'))
51 assert(!File.fnmatch('\*', '\*'))
52 assert(!File.fnmatch('\*', 'cats'))
53 assert(!File.fnmatch('\*', '\cats'))
54 assert(File.fnmatch('\a', 'a'))
55 assert(!File.fnmatch('\a', '\a'))
56 assert(File.fnmatch('[a\-c]', 'a'))
57 assert(File.fnmatch('[a\-c]', '-'))
58 assert(File.fnmatch('[a\-c]', 'c'))
59 assert(!File.fnmatch('[a\-c]', 'b'))
60 assert(!File.fnmatch('[a\-c]', '\\'))
61 # escaping character loses its meaning if FNM_NOESCAPE is set
62 assert(!File.fnmatch('\?', '?', File::FNM_NOESCAPE))
63 assert(File.fnmatch('\?', '\?', File::FNM_NOESCAPE))
64 assert(!File.fnmatch('\?', 'a', File::FNM_NOESCAPE))
65 assert(File.fnmatch('\?', '\a', File::FNM_NOESCAPE))
66 assert(!File.fnmatch('\*', '*', File::FNM_NOESCAPE))
67 assert(File.fnmatch('\*', '\*', File::FNM_NOESCAPE))
68 assert(!File.fnmatch('\*', 'cats', File::FNM_NOESCAPE))
69 assert(File.fnmatch('\*', '\cats', File::FNM_NOESCAPE))
70 assert(!File.fnmatch('\a', 'a', File::FNM_NOESCAPE))
71 assert(File.fnmatch('\a', '\a', File::FNM_NOESCAPE))
72 assert(File.fnmatch('[a\-c]', 'a', File::FNM_NOESCAPE))
73 assert(!File.fnmatch('[a\-c]', '-', File::FNM_NOESCAPE))
74 assert(File.fnmatch('[a\-c]', 'c', File::FNM_NOESCAPE))
75 assert(File.fnmatch('[a\-c]', 'b', File::FNM_NOESCAPE)) # '\\' < 'b' < 'c'
76 assert(File.fnmatch('[a\-c]', '\\', File::FNM_NOESCAPE))
77 # case is ignored if FNM_CASEFOLD is set
78 assert(!File.fnmatch('cat', 'CAT'))
79 assert(File.fnmatch('cat', 'CAT', File::FNM_CASEFOLD))
80 assert(!File.fnmatch('[a-z]', 'D'))
81 assert(File.fnmatch('[a-z]', 'D', File::FNM_CASEFOLD))
82 # wildcard doesn't match '/' if FNM_PATHNAME is set
83 assert(File.fnmatch('foo?boo', 'foo/boo'))
84 assert(File.fnmatch('foo*', 'foo/boo'))
85 assert(!File.fnmatch('foo?boo', 'foo/boo', File::FNM_PATHNAME))
86 assert(!File.fnmatch('foo*', 'foo/boo', File::FNM_PATHNAME))
87 # wildcard matches leading period if FNM_DOTMATCH is set
88 assert(!File.fnmatch('*', '.profile'))
89 assert(File.fnmatch('*', '.profile', File::FNM_DOTMATCH))
90 assert(File.fnmatch('.*', '.profile'))
91 assert(File.fnmatch('*', 'dave/.profile'))
92 assert(File.fnmatch('*/*', 'dave/.profile'))
93 assert(!File.fnmatch('*/*', 'dave/.profile', File::FNM_PATHNAME))
94 assert(File.fnmatch('*/*', 'dave/.profile', File::FNM_PATHNAME | File::FNM_DOTMATCH))
96 assert(File.fnmatch('**/foo', 'a/b/c/foo', File::FNM_PATHNAME))
97 assert(File.fnmatch('**/foo', '/foo', File::FNM_PATHNAME))
98 assert(!File.fnmatch('**/foo', 'a/.b/c/foo', File::FNM_PATHNAME))
99 assert(File.fnmatch('**/foo', 'a/.b/c/foo', File::FNM_PATHNAME | File::FNM_DOTMATCH))
100 assert(File.fnmatch('**/foo', '/root/foo', File::FNM_PATHNAME))
101 assert(File.fnmatch('**/foo', 'c:/root/foo', File::FNM_PATHNAME))