1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
local helpers = require('test.functional.helpers')(after_each)
local eq = helpers.eq
local exec_lua = helpers.exec_lua
local has_err = require('luassert').has.errors
describe('vim.lsp._watchfiles', function()
before_each(helpers.clear)
after_each(helpers.clear)
local match = function(...)
return exec_lua('return require("vim.lsp._watchfiles")._match(...)', ...)
end
describe('glob matching', function()
it('should match literal strings', function()
eq(true, match('', ''))
eq(false, match('', 'a'))
eq(true, match('a', 'a'))
eq(true, match('abc', 'abc'))
eq(false, match('abc', 'abcdef'))
eq(false, match('abc', 'a'))
eq(false, match('a', 'b'))
eq(false, match('.', 'a'))
eq(true, match('$', '$'))
eq(false, match('dir/subdir', 'dir/subdir/file'))
end)
it('should match * wildcards', function()
-- eq(false, match('*', '')) -- TODO: this fails
eq(true, match('*', 'a'))
eq(false, match('*', '/a'))
eq(false, match('*', 'a/'))
eq(true, match('*', 'aaa'))
eq(true, match('*.txt', 'file.txt'))
eq(false, match('*.txt', 'file.txtxt'))
eq(false, match('*.txt', 'dir/file.txt'))
eq(false, match('*.txt', '/dir/file.txt'))
eq(false, match('*.txt', 'C:/dir/file.txt'))
eq(false, match('*.dir', 'test.dir/file'))
eq(true, match('file.*', 'file.txt'))
eq(false, match('file.*', 'not-file.txt'))
eq(false, match('dir/*.txt', 'file.txt'))
eq(true, match('dir/*.txt', 'dir/file.txt'))
eq(false, match('dir/*.txt', 'dir/subdir/file.txt'))
end)
it('should match ? wildcards', function()
eq(false, match('?', ''))
eq(true, match('?', 'a'))
eq(false, match('??', 'a'))
eq(false, match('?', 'ab'))
eq(true, match('??', 'ab'))
eq(true, match('a?c', 'abc'))
eq(false, match('a?c', 'a/c'))
end)
it('should match ** wildcards', function()
eq(true, match('**', ''))
eq(true, match('**', 'a'))
eq(true, match('**', 'a/'))
eq(true, match('**', '/a'))
eq(true, match('**', 'C:/a'))
eq(true, match('**', 'a/a'))
eq(true, match('**', 'a/a/a'))
eq(false, match('a**', ''))
eq(true, match('a**', 'a'))
eq(true, match('a**', 'abcd'))
eq(false, match('a**', 'ba'))
eq(false, match('a**', 'a/b'))
eq(false, match('**a', ''))
eq(true, match('**a', 'a'))
eq(true, match('**a', 'dcba'))
eq(false, match('**a', 'ab'))
eq(false, match('**a', 'b/a'))
eq(false, match('a/**', ''))
eq(true, match('a/**', 'a'))
eq(true, match('a/**', 'a/b'))
eq(false, match('a/**', 'b/a'))
eq(false, match('a/**', '/a'))
eq(false, match('**/a', ''))
eq(true, match('**/a', 'a'))
eq(false, match('**/a', 'a/b'))
eq(true, match('**/a', '/a'))
eq(false, match('a/**/c', 'a'))
eq(false, match('a/**/c', 'c'))
eq(true, match('a/**/c', 'a/c'))
eq(true, match('a/**/c', 'a/b/c'))
eq(true, match('a/**/c', 'a/b/b/c'))
eq(true, match('**/a/**', 'a'))
eq(true, match('**/a/**', '/dir/a'))
eq(true, match('**/a/**', 'a/dir'))
eq(true, match('**/a/**', 'dir/a/dir'))
eq(true, match('**/a/**', '/a/dir'))
eq(true, match('**/a/**', 'C:/a/dir'))
-- eq(false, match('**/a/**', 'a.txt')) -- TODO: this fails
end)
it('should match {} groups', function()
eq(false, match('{}', ''))
eq(true, match('{,}', ''))
eq(false, match('{}', 'a'))
eq(true, match('{a}', 'a'))
eq(false, match('{a}', 'aa'))
eq(false, match('{a}', 'ab'))
eq(false, match('{ab}', 'a'))
eq(true, match('{ab}', 'ab'))
eq(true, match('{a,b}', 'a'))
eq(true, match('{a,b}', 'b'))
eq(false, match('{a,b}', 'ab'))
eq(true, match('{ab,cd}', 'ab'))
eq(false, match('{ab,cd}', 'a'))
eq(true, match('{ab,cd}', 'cd'))
eq(true, match('{a,b,c}', 'c'))
eq(false, match('{a,{b,c}}', 'c')) -- {} can't nest
end)
it('should match [] groups', function()
eq(true, match('[]', ''))
eq(false, match('[a-z]', ''))
eq(true, match('[a-z]', 'a'))
eq(false, match('[a-z]', 'ab'))
eq(true, match('[a-z]', 'z'))
eq(true, match('[a-z]', 'j'))
eq(false, match('[a-f]', 'j'))
eq(false, match('[a-z]', '`')) -- 'a' - 1
eq(false, match('[a-z]', '{')) -- 'z' + 1
eq(false, match('[a-z]', 'A'))
eq(false, match('[a-z]', '5'))
eq(true, match('[A-Z]', 'A'))
eq(true, match('[A-Z]', 'Z'))
eq(true, match('[A-Z]', 'J'))
eq(false, match('[A-Z]', '@')) -- 'A' - 1
eq(false, match('[A-Z]', '[')) -- 'Z' + 1
eq(false, match('[A-Z]', 'a'))
eq(false, match('[A-Z]', '5'))
eq(true, match('[a-zA-Z0-9]', 'z'))
eq(true, match('[a-zA-Z0-9]', 'Z'))
eq(true, match('[a-zA-Z0-9]', '9'))
eq(false, match('[a-zA-Z0-9]', '&'))
end)
it('should match [!...] groups', function()
has_err(function() match('[!]', '') end) -- not a valid pattern
eq(false, match('[!a-z]', ''))
eq(false, match('[!a-z]', 'a'))
eq(false, match('[!a-z]', 'z'))
eq(false, match('[!a-z]', 'j'))
eq(true, match('[!a-f]', 'j'))
eq(false, match('[!a-f]', 'jj'))
eq(true, match('[!a-z]', '`')) -- 'a' - 1
eq(true, match('[!a-z]', '{')) -- 'z' + 1
eq(false, match('[!a-zA-Z0-9]', 'a'))
eq(false, match('[!a-zA-Z0-9]', 'A'))
eq(false, match('[!a-zA-Z0-9]', '0'))
eq(true, match('[!a-zA-Z0-9]', '!'))
end)
it('should match complex patterns', function()
eq(false, match('**/*.{c,h}', ''))
eq(false, match('**/*.{c,h}', 'c'))
eq(true, match('**/*.{c,h}', 'file.c'))
eq(true, match('**/*.{c,h}', 'file.h'))
eq(true, match('**/*.{c,h}', '/file.c'))
eq(true, match('**/*.{c,h}', 'dir/subdir/file.c'))
eq(true, match('**/*.{c,h}', 'dir/subdir/file.h'))
eq(true, match('{[0-9],[a-z]}', '0'))
eq(true, match('{[0-9],[a-z]}', 'a'))
eq(false, match('{[0-9],[a-z]}', 'A'))
end)
end)
end)
|