-
Notifications
You must be signed in to change notification settings - Fork 39
/
test.js
202 lines (191 loc) · 7.88 KB
/
test.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
'use strict';
let anymatch = require('./');
let assert = require('assert');
let path = require('path');
describe('anymatch', () => {
var matchers = [
'path/to/file.js',
'path/anyjs/**/*.js',
/foo.js$/,
(string => string.indexOf('/bar') !== -1 && string.length > 10)
];
it('should resolve string matchers', () => {
assert(anymatch(matchers, 'path/to/file.js'));
assert(anymatch(matchers[0], 'path/to/file.js'));
assert(!anymatch(matchers[0], 'bar.js'));
});
it('should resolve glob matchers', () => {
assert.equal(true, anymatch(matchers, 'path/anyjs/baz.js'));
assert.equal(true, anymatch(matchers[1], 'path/anyjs/baz.js'));
assert.equal(false, anymatch(matchers[1], 'bar.js'));
});
it('should resolve regexp matchers', () => {
assert.equal(true, anymatch(matchers, 'path/to/foo.js'));
assert.equal(true, anymatch(matchers[2], 'path/to/foo.js'));
assert.equal(false, anymatch(matchers[2], 'bar.js'));
});
it('should resolve function matchers', () => {
assert.equal(true, anymatch(matchers, 'path/to/bar.js'));
assert.equal(true, anymatch(matchers[3], 'path/to/bar.js'));
assert.equal(false, anymatch(matchers[3], 'bar.js'));
});
it('should return false for unmatched strings', () => {
assert.equal(false, anymatch(matchers, 'bar.js'));
});
it('should ignore improperly typed matchers', () => {
var emptyObj = {};
assert.equal(false, anymatch(emptyObj, ''));
assert.equal(false, anymatch(Infinity, ''));
});
it('should keep trailing separators on paths to match /*/ globs', () => {
assert.equal(true, anymatch('path/to/*/', 'path/to/dir/'));
});
describe('with returnIndex = true', () => {
it('should return the array index of first positive matcher', () => {
var result = anymatch(matchers, 'foo.js', true);
assert.equal(result, 2);
});
it('should return 0 if provided non-array matcher', () => {
var result = anymatch(matchers[2], 'foo.js', true);
assert.equal(result, 0);
});
it('should return -1 if no match', () => {
var result = anymatch(matchers, 'bar.js', true);
assert.equal(result, -1);
});
});
describe('curried matching function', () => {
var matchFn;
before(() => {
matchFn = anymatch(matchers);
});
it('should resolve matchers', () => {
assert.equal(true, matchFn('path/to/file.js'));
assert.equal(true, matchFn('path/anyjs/baz.js'));
assert.equal(true, matchFn('path/to/foo.js'));
assert.equal(true, matchFn('path/to/bar.js'));
assert.equal(false, matchFn('bar.js'));
});
it('should be usable as an Array.prototype.filter callback', () => {
var arr = [
'path/to/file.js',
'path/anyjs/baz.js',
'path/to/foo.js',
'path/to/bar.js',
'bar.js',
'foo.js'
];
var expected = arr.slice();
expected.splice(arr.indexOf('bar.js'), 1);
assert.deepEqual(arr.filter(matchFn), expected);
});
it('should bind individual criterion', () => {
assert(anymatch(matchers[0])('path/to/file.js'));
assert(!anymatch(matchers[0])('path/to/other.js'));
assert(anymatch(matchers[1])('path/anyjs/baz.js'));
assert(!anymatch(matchers[1])('path/to/baz.js'));
assert(anymatch(matchers[2])('path/to/foo.js'));
assert(!anymatch(matchers[2])('path/to/foo.js.bak'));
assert(anymatch(matchers[3])('path/to/bar.js'));
assert(!anymatch(matchers[3])('bar.js'));
});
});
describe('extra args', () => {
it('should not allow no args', () => {
assert.throws(() => anymatch());
})
it('should not allow bad testString', () => {
assert.throws(() => anymatch(matchers, { path: 'path/to/bar.js' }));
});
it('should allow string to be passed as first member of an array', () => {
assert.doesNotThrow(() => anymatch(matchers, ['path/to/bar.js']));
});
it('should pass extra args to function matchers', () => {
matchers.push((string, arg1, arg2) => arg1 || arg2);
assert(!anymatch(matchers, 'bar.js'), '1');
assert(!anymatch(matchers, ['bar.js', 0]), '2');
assert(anymatch(matchers, ['bar.js', true]), '3');
assert(anymatch(matchers, ['bar.js', 0, true]), '4');
// with returnIndex
assert.equal(anymatch(matchers, ['bar.js', 1], true), 4, '5');
// curried versions
var matchFn1 = anymatch(matchers);
var matchFn2 = anymatch(matchers[4]);
assert(!matchFn1(['bar.js', 0]), '6');
assert(!matchFn2(['bar.js', 0]), '7');
assert(matchFn1(['bar.js', true]), '8');
assert(matchFn2(['bar.js', true]), '9');
assert(matchFn1(['bar.js', 0, true]), '10');
assert(matchFn2(['bar.js', 0, true]), '11');
// curried with returnIndex
assert.equal(matchFn1(['bar.js', 1], true), 4, '12');
assert.equal(matchFn2(['bar.js', 1], true), 0, '13');
assert.equal(matchFn1(['bar.js', 0], true), -1, '14');
assert.equal(matchFn2(['bar.js', 0], true), -1, '15');
matchers.pop();
});
});
describe('glob negation', () => {
after(matchers.splice.bind(matchers, 4, 3));
it('should respect negated globs included in a matcher array', () => {
assert(anymatch(matchers, 'path/anyjs/no/no.js'), 'matches existing glob');
matchers.push('!path/anyjs/no/*.js');
assert(!anymatch(matchers, 'path/anyjs/no/no.js'), 'should be negated');
assert(!anymatch(matchers)('path/anyjs/no/no.js'), 'should be negated (curried)');
});
it('should not break returnIndex option', () => {
assert.equal(anymatch(matchers, 'path/anyjs/yes.js', true), 1);
assert.equal(anymatch(matchers)('path/anyjs/yes.js', true), 1);
assert.equal(anymatch(matchers, 'path/anyjs/no/no.js', true), -1);
assert.equal(anymatch(matchers)('path/anyjs/no/no.js', true), -1);
});
it('should allow negated globs to negate non-glob matchers', () => {
assert.equal(anymatch(matchers, 'path/to/bar.js', true), 3);
matchers.push('!path/to/bar.*');
assert(!anymatch(matchers, 'path/to/bar.js'));
});
it('should not match negated matchers if positive matchers do not match', () => {
assert(!anymatch(matchers, 'path/anyjs/no/no.ts'), 'does not match existing glob');
matchers.push('!path/anyjs/ts/*.js');
assert(!anymatch(matchers, 'path/anyjs/no/no.ts'), 'should not be negated');
assert(!anymatch(matchers)('path/anyjs/no/no.ts'), 'should not be negated (curried)');
})
});
describe('windows paths', () => {
var origSep = path.sep;
before(() => {
path.sep = '\\';
});
after(() => {
path.sep = origSep;
});
it('should resolve backslashes against string matchers', () => {
assert(anymatch(matchers, 'path\\to\\file.js'));
assert(anymatch(matchers)('path\\to\\file.js'));
});
it('should resolve backslashes against glob matchers', () => {
assert(anymatch(matchers, 'path\\anyjs\\file.js'));
assert(anymatch(matchers)('path\\anyjs\\file.js'));
});
it('should resolve backslashes against regex matchers', () => {
assert(anymatch(/path\/to\/file\.js/, 'path\\to\\file.js'));
assert(anymatch(/path\/to\/file\.js/)('path\\to\\file.js'));
});
it('should resolve backslashes against function matchers', () => {
assert(anymatch(matchers, 'path\\to\\bar.js'));
assert(anymatch(matchers)('path\\to\\bar.js'));
});
it('should still correctly handle forward-slash paths', () => {
assert(anymatch(matchers, 'path/to/file.js'));
assert(anymatch(matchers)('path/to/file.js'));
assert(!anymatch(matchers, 'path/no/no.js'));
assert(!anymatch(matchers)('path/no/no.js'));
});
});
describe('picomatch options', () => {
it('should support picomatch options', () => {
assert.equal(false, anymatch('path/to/?dotfile', 'path/to/.dotfile'));
assert.equal(true, anymatch('path/to/?dotfile', 'path/to/.dotfile', { dot: true }));
});
});
});