1 # frozen_string_literal: false
4 class TestFnmatch < Test::Unit::TestCase
6 def bracket_test(s, t) # `s' should start with neither '!' nor '^'
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))
10 assert_equal(t.include?(i.chr), !File.fnmatch("[!#{s}]", i.chr, File::FNM_DOTMATCH))
15 assert_file.for("[ruby-dev:22819]").fnmatch('\[1\]' , '[1]')
16 assert_file.for("[ruby-dev:22815]").fnmatch('*?', 'a')
17 assert_file.fnmatch('*/', 'a/')
18 assert_file.fnmatch('\[1\]' , '[1]', File::FNM_PATHNAME)
19 assert_file.fnmatch('*?', 'a', File::FNM_PATHNAME)
20 assert_file.fnmatch('*/', 'a/', File::FNM_PATHNAME)
25 assert_file.fnmatch('cat', 'cat')
26 assert_file.not_fnmatch('cat', 'category')
27 assert_file.not_fnmatch('cat', 'wildcat')
31 # '?' matches any one character
32 assert_file.fnmatch('?at', 'cat')
33 assert_file.fnmatch('c?t', 'cat')
34 assert_file.fnmatch('ca?', 'cat')
35 assert_file.fnmatch('?a?', 'cat')
36 assert_file.not_fnmatch('c??t', 'cat')
37 assert_file.not_fnmatch('??at', 'cat')
38 assert_file.not_fnmatch('ca??', 'cat')
42 # '*' matches any number (including 0) of any characters
43 assert_file.fnmatch('c*', 'cats')
44 assert_file.fnmatch('c*ts', 'cats')
45 assert_file.fnmatch('*ts', 'cats')
46 assert_file.fnmatch('*c*a*t*s*', 'cats')
47 assert_file.not_fnmatch('c*t', 'cats')
48 assert_file.not_fnmatch('*abc', 'abcabz')
49 assert_file.fnmatch('*abz', 'abcabz')
50 assert_file.not_fnmatch('a*abc', 'abc')
51 assert_file.fnmatch('a*bc', 'abc')
52 assert_file.not_fnmatch('a*bc', 'abcd')
56 # [seq] : matches any character listed between bracket
57 # [!seq] or [^seq] : matches any character except those listed between bracket
58 bracket_test("bd-gikl-mosv-x", "bdefgiklmosvwx")
63 assert_file.fnmatch('\?', '?')
64 assert_file.not_fnmatch('\?', '\?')
65 assert_file.not_fnmatch('\?', 'a')
66 assert_file.not_fnmatch('\?', '\a')
67 assert_file.fnmatch('\*', '*')
68 assert_file.not_fnmatch('\*', '\*')
69 assert_file.not_fnmatch('\*', 'cats')
70 assert_file.not_fnmatch('\*', '\cats')
71 assert_file.fnmatch('\a', 'a')
72 assert_file.not_fnmatch('\a', '\a')
73 assert_file.fnmatch('[a\-c]', 'a')
74 assert_file.fnmatch('[a\-c]', '-')
75 assert_file.fnmatch('[a\-c]', 'c')
76 assert_file.not_fnmatch('[a\-c]', 'b')
77 assert_file.not_fnmatch('[a\-c]', '\\')
81 # escaping character loses its meaning if FNM_NOESCAPE is set
82 assert_file.not_fnmatch('\?', '?', File::FNM_NOESCAPE)
83 assert_file.fnmatch('\?', '\?', File::FNM_NOESCAPE)
84 assert_file.not_fnmatch('\?', 'a', File::FNM_NOESCAPE)
85 assert_file.fnmatch('\?', '\a', File::FNM_NOESCAPE)
86 assert_file.not_fnmatch('\*', '*', File::FNM_NOESCAPE)
87 assert_file.fnmatch('\*', '\*', File::FNM_NOESCAPE)
88 assert_file.not_fnmatch('\*', 'cats', File::FNM_NOESCAPE)
89 assert_file.fnmatch('\*', '\cats', File::FNM_NOESCAPE)
90 assert_file.not_fnmatch('\a', 'a', File::FNM_NOESCAPE)
91 assert_file.fnmatch('\a', '\a', File::FNM_NOESCAPE)
92 assert_file.fnmatch('[a\-c]', 'a', File::FNM_NOESCAPE)
93 assert_file.not_fnmatch('[a\-c]', '-', File::FNM_NOESCAPE)
94 assert_file.fnmatch('[a\-c]', 'c', File::FNM_NOESCAPE)
95 assert_file.fnmatch('[a\-c]', 'b', File::FNM_NOESCAPE) # '\\' < 'b' < 'c'
96 assert_file.fnmatch('[a\-c]', '\\', File::FNM_NOESCAPE)
100 # case is ignored if FNM_CASEFOLD is set
101 assert_file.not_fnmatch('cat', 'CAT')
102 assert_file.fnmatch('cat', 'CAT', File::FNM_CASEFOLD)
103 assert_file.not_fnmatch('[a-z]', 'D')
104 assert_file.fnmatch('[a-z]', 'D', File::FNM_CASEFOLD)
105 assert_file.not_fnmatch('[abc]', 'B')
106 assert_file.fnmatch('[abc]', 'B', File::FNM_CASEFOLD)
109 def test_fnm_pathname
110 # wildcard doesn't match '/' if FNM_PATHNAME is set
111 assert_file.fnmatch('foo?boo', 'foo/boo')
112 assert_file.fnmatch('foo*', 'foo/boo')
113 assert_file.not_fnmatch('foo?boo', 'foo/boo', File::FNM_PATHNAME)
114 assert_file.not_fnmatch('foo*', 'foo/boo', File::FNM_PATHNAME)
117 def test_fnm_dotmatch
118 # wildcard matches leading period if FNM_DOTMATCH is set
119 assert_file.not_fnmatch('*', '.profile')
120 assert_file.fnmatch('*', '.profile', File::FNM_DOTMATCH)
121 assert_file.fnmatch('.*', '.profile')
122 assert_file.fnmatch('*', 'dave/.profile')
123 assert_file.fnmatch('*/*', 'dave/.profile')
124 assert_file.not_fnmatch('*/*', 'dave/.profile', File::FNM_PATHNAME)
125 assert_file.fnmatch('*/*', 'dave/.profile', File::FNM_PATHNAME | File::FNM_DOTMATCH)
130 assert_file.fnmatch('**/foo', 'a/b/c/foo', File::FNM_PATHNAME)
131 assert_file.fnmatch('**/foo', '/foo', File::FNM_PATHNAME)
132 assert_file.not_fnmatch('**/foo', 'a/.b/c/foo', File::FNM_PATHNAME)
133 assert_file.fnmatch('**/foo', 'a/.b/c/foo', File::FNM_PATHNAME | File::FNM_DOTMATCH)
134 assert_file.fnmatch('**/foo', '/root/foo', File::FNM_PATHNAME)
135 assert_file.fnmatch('**/foo', 'c:/root/foo', File::FNM_PATHNAME)
139 feature5422 = '[ruby-core:40037]'
140 assert_file.for(feature5422).not_fnmatch?( "{.g,t}*", ".gem")
141 assert_file.for(feature5422).fnmatch?("{.g,t}*", ".gem", File::FNM_EXTGLOB)
144 def test_unmatched_encoding
145 bug7911 = '[ruby-dev:47069] [Bug #7911]'
147 pattern_ascii = 'a'.encode('US-ASCII')
148 pattern_eucjp = path.encode('EUC-JP')
149 assert_nothing_raised(ArgumentError, bug7911) do
150 assert_file.not_fnmatch(pattern_ascii, path)
151 assert_file.not_fnmatch(pattern_eucjp, path)
152 assert_file.not_fnmatch(pattern_ascii, path, File::FNM_CASEFOLD)
153 assert_file.not_fnmatch(pattern_eucjp, path, File::FNM_CASEFOLD)
154 assert_file.fnmatch("{*,#{pattern_ascii}}", path, File::FNM_EXTGLOB)
155 assert_file.fnmatch("{*,#{pattern_eucjp}}", path, File::FNM_EXTGLOB)
160 assert_file.fnmatch("[a-\u3042]*", "\u3042")
161 assert_file.not_fnmatch("[a-\u3042]*", "\u3043")
165 assert_raise(ArgumentError) {
166 File.fnmatch("a\0z", "a")