-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathtest.js
More file actions
600 lines (498 loc) · 21.1 KB
/
test.js
File metadata and controls
600 lines (498 loc) · 21.1 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
import test from 'ava';
import {matcher, isMatch} from './index.js';
test('matcher()', t => {
t.deepEqual(matcher(['foo', 'bar'], ['foo']), ['foo']);
t.deepEqual(matcher(['foo', 'bar'], ['bar']), ['bar']);
t.deepEqual(matcher(['foo', 'bar'], ['fo*', 'ba*', '!bar']), ['foo']);
t.deepEqual(matcher(['foo', 'bar', 'moo'], ['!*o']), ['bar']);
t.deepEqual(matcher(['moo', 'MOO'], ['*oo'], {caseSensitive: true}), ['moo']);
t.deepEqual(matcher(['moo', 'MOO'], ['*oo'], {caseSensitive: false}), ['moo', 'MOO']);
t.notThrows(() => {
matcher([], []);
});
});
test('isMatch()', t => {
t.true(isMatch('unicorn', 'unicorn'));
t.true(isMatch('MOO', 'MOO'));
t.true(isMatch('unicorn', 'uni*'));
t.true(isMatch('UNICORN', 'unicorn', {caseSensitive: false}));
t.true(isMatch('unicorn', '*corn'));
t.true(isMatch('unicorn', 'un*rn'));
t.true(isMatch('foo unicorn bar', '*unicorn*'));
t.true(isMatch('unicorn', '*'));
t.true(isMatch('UNICORN', 'UNI*', {caseSensitive: true}));
t.false(isMatch('UNICORN', 'unicorn', {caseSensitive: true}));
t.false(isMatch('unicorn', ''));
t.false(isMatch('unicorn', '!unicorn'));
t.false(isMatch('unicorn', '!uni*'));
t.false(isMatch('unicorn', String.raw`uni\*`));
t.true(isMatch('unicorn', '!tricorn'));
t.true(isMatch('unicorn', '!tri*'));
t.true(isMatch(['foo', 'bar', 'moo'], '*oo'));
t.true(isMatch(['foo', 'bar', 'moo'], ['*oo', '!f*']));
t.true(isMatch('moo', ['*oo', '!f*']));
t.true(isMatch('UNICORN', ['!*oo', 'UNI*'], {caseSensitive: true}));
t.false(isMatch(['unicorn', 'bar', 'wizard'], '*oo'));
t.false(isMatch(['foo', 'bar', 'unicorn'], ['*horn', '!b*']));
t.false(isMatch('moo', ['*oo', '!m*']));
t.false(isMatch('UNICORN', ['!*oo', 'uni*'], {caseSensitive: true}));
});
test('matches across newlines', t => {
t.deepEqual(matcher(['foo\nbar'], ['foo*']), ['foo\nbar']);
t.deepEqual(matcher(['foo\nbar'], ['foo*r']), ['foo\nbar']);
t.true(isMatch(['foo\nbar'], ['foo*']));
t.true(isMatch(['foo\nbar'], ['foo*r']));
});
test('handles empty arguments consistently', t => {
t.deepEqual(matcher(['phoenix'], ['bar', '']), []);
t.deepEqual(matcher(['phoenix'], ['', 'bar']), []);
t.deepEqual(matcher(['phoenix'], ['', 'bar', '']), []);
t.deepEqual(matcher(['phoenix'], ['bar', '', 'bar']), []);
t.deepEqual(matcher(['phoenix'], [undefined, '']), []);
t.deepEqual(matcher(['phoenix'], ['', undefined]), []);
t.deepEqual(matcher(['phoenix'], ['', undefined, '']), []);
t.deepEqual(matcher(['phoenix'], [undefined, '', undefined]), []);
t.deepEqual(matcher(['phoenix'], ['', '']), []);
t.deepEqual(matcher(['phoenix'], ['']), []);
t.deepEqual(matcher(['phoenix'], ''), []);
t.deepEqual(matcher(['phoenix'], []), []);
t.deepEqual(matcher(['phoenix'], [undefined]), []);
t.deepEqual(matcher(['phoenix'], undefined), []);
t.deepEqual(matcher(['phoenix', ''], ['bar']), []);
t.deepEqual(matcher(['', 'phoenix'], ['bar']), []);
t.deepEqual(matcher(['', 'phoenix', ''], ['bar']), []);
t.deepEqual(matcher(['phoenix', '', 'phoenix'], ['bar']), []);
t.deepEqual(matcher([undefined, ''], ['bar']), []);
t.deepEqual(matcher(['', undefined], ['bar']), []);
t.deepEqual(matcher(['', undefined, ''], ['bar']), []);
t.deepEqual(matcher([undefined, '', undefined], ['bar']), []);
t.deepEqual(matcher(['', ''], ['bar']), []);
t.deepEqual(matcher([''], ['bar']), []);
t.deepEqual(matcher('', ['bar']), []);
t.deepEqual(matcher([], ['bar']), []);
t.deepEqual(matcher([undefined], ['bar']), []);
t.deepEqual(matcher(undefined, ['bar']), []);
t.false(isMatch(['phoenix'], ['bar', '']));
t.false(isMatch(['phoenix'], ['', 'bar']));
t.false(isMatch(['phoenix'], ['', 'bar', '']));
t.false(isMatch(['phoenix'], ['bar', '', 'bar']));
t.false(isMatch(['phoenix'], [undefined, '']));
t.false(isMatch(['phoenix'], ['', undefined]));
t.false(isMatch(['phoenix'], ['', undefined, '']));
t.false(isMatch(['phoenix'], [undefined, '', undefined]));
t.false(isMatch(['phoenix'], ['', '']));
t.false(isMatch(['phoenix'], ['']));
t.false(isMatch(['phoenix'], ''));
t.false(isMatch(['phoenix'], []));
t.false(isMatch(['phoenix'], [undefined]));
t.false(isMatch(['phoenix'], undefined));
t.false(isMatch(['phoenix', ''], ['bar']));
t.false(isMatch(['', 'phoenix'], ['bar']));
t.false(isMatch(['', 'phoenix', ''], ['bar']));
t.false(isMatch(['phoenix', '', 'phoenix'], ['bar']));
t.false(isMatch([undefined, ''], ['bar']));
t.false(isMatch(['', undefined], ['bar']));
t.false(isMatch(['', undefined, ''], ['bar']));
t.false(isMatch([undefined, '', undefined], ['bar']));
t.false(isMatch(['', ''], ['bar']));
t.false(isMatch([''], ['bar']));
t.false(isMatch('', ['bar']));
t.false(isMatch([], ['bar']));
t.false(isMatch([undefined], ['bar']));
t.false(isMatch(undefined, ['bar']));
t.deepEqual(matcher([''], ['bar', '']), ['']);
t.deepEqual(matcher([''], ['', 'bar']), ['']);
t.deepEqual(matcher([''], [undefined, '']), ['']);
t.deepEqual(matcher([''], ['', undefined]), ['']);
t.deepEqual(matcher([''], ['', '']), ['']);
t.deepEqual(matcher(['phoenix', ''], ['']), ['']);
t.deepEqual(matcher(['', 'phoenix'], ['']), ['']);
t.deepEqual(matcher([undefined, ''], ['']), ['']);
t.deepEqual(matcher(['', undefined], ['']), ['']);
t.deepEqual(matcher(['', ''], ['']), ['', '']);
t.deepEqual(matcher([''], ['']), ['']);
t.deepEqual(matcher([''], ['*']), ['']);
t.deepEqual(matcher([undefined], ['bar', undefined]), []);
t.deepEqual(matcher([undefined], [undefined, 'bar']), []);
t.deepEqual(matcher([undefined], ['', undefined]), []);
t.deepEqual(matcher([undefined], [undefined, '']), []);
t.deepEqual(matcher([undefined], [undefined, undefined]), []);
t.deepEqual(matcher([undefined], [undefined]), []);
t.deepEqual(matcher([undefined], undefined), []);
t.deepEqual(matcher(['phoenix', undefined], [undefined]), []);
t.deepEqual(matcher([undefined, 'phoenix'], [undefined]), []);
t.deepEqual(matcher(['', undefined], [undefined]), []);
t.deepEqual(matcher([undefined, ''], [undefined]), []);
t.deepEqual(matcher([undefined, undefined], [undefined]), []);
t.deepEqual(matcher([undefined], [undefined]), []);
t.deepEqual(matcher(undefined, [undefined]), []);
t.deepEqual(matcher([], []), []);
t.deepEqual(matcher([], ['*']), []);
t.true(isMatch([''], [undefined, '']));
t.true(isMatch([''], ['', undefined]));
t.true(isMatch([''], ['', '']));
t.true(isMatch(['phoenix', ''], ['']));
t.true(isMatch(['', 'phoenix'], ['']));
t.true(isMatch([undefined, ''], ['']));
t.true(isMatch(['', undefined], ['']));
t.true(isMatch(['', ''], ['']));
t.true(isMatch([''], ['']));
t.true(isMatch([''], ['*']));
t.false(isMatch([undefined], ['bar', undefined]));
t.false(isMatch([undefined], [undefined, 'bar']));
t.false(isMatch([undefined], ['', undefined]));
t.false(isMatch([undefined], [undefined, '']));
t.false(isMatch([undefined], [undefined, undefined]));
t.false(isMatch([undefined], [undefined]));
t.false(isMatch([undefined], undefined));
t.false(isMatch(['phoenix', undefined], [undefined]));
t.false(isMatch([undefined, 'phoenix'], [undefined]));
t.false(isMatch(['', undefined], [undefined]));
t.false(isMatch([undefined, ''], [undefined]));
t.false(isMatch([undefined, undefined], [undefined]));
t.false(isMatch([undefined], [undefined]));
t.false(isMatch(undefined, [undefined]));
t.false(isMatch([], []));
t.false(isMatch([], ['*']));
t.throws(() => {
matcher(['phoenix'], [0]);
});
t.throws(() => {
matcher(['phoenix'], [null]);
});
t.throws(() => {
matcher(['phoenix'], [false]);
});
t.throws(() => {
matcher(['phoenix'], 0);
});
t.throws(() => {
matcher(['phoenix'], null);
});
t.throws(() => {
matcher(['phoenix'], false);
});
t.throws(() => {
matcher([0], ['bar']);
});
t.throws(() => {
matcher([null], ['bar']);
});
t.throws(() => {
matcher([false], ['bar']);
});
t.throws(() => {
matcher(0, ['bar']);
});
t.throws(() => {
matcher(null, ['bar']);
});
t.throws(() => {
matcher(false, ['bar']);
});
t.throws(() => {
isMatch(['phoenix'], [0]);
});
t.throws(() => {
isMatch(['phoenix'], [null]);
});
t.throws(() => {
isMatch(['phoenix'], [false]);
});
t.throws(() => {
isMatch(['phoenix'], 0);
});
t.throws(() => {
isMatch(['phoenix'], null);
});
t.throws(() => {
isMatch(['phoenix'], false);
});
t.throws(() => {
isMatch([0], ['bar']);
});
t.throws(() => {
isMatch([null], ['bar']);
});
t.throws(() => {
isMatch([false], ['bar']);
});
t.throws(() => {
isMatch(0, ['bar']);
});
t.throws(() => {
isMatch(null, ['bar']);
});
t.throws(() => {
isMatch(false, ['bar']);
});
});
test('matcher() negated pattern placement', t => {
t.deepEqual(matcher(['foo', 'bar'], ['fo*', '!bar', 'ba*']), ['foo']);
t.deepEqual(matcher(['foo', 'bar'], ['!bar', 'fo*', 'ba*']), ['foo']);
t.deepEqual(matcher(['foo', 'bar'], ['!bar']), ['foo']);
t.deepEqual(matcher(['foo', 'bar'], ['!bar', 'fu']), []);
});
test('isMatch() negated pattern placement', t => {
t.true(isMatch(['foo', 'bar'], ['fo*', '*oo', '!bar']));
t.true(isMatch(['foo', 'bar'], ['!bar', 'fo*', '*oo']));
t.true(isMatch(['foo', 'bar'], ['!bar']));
});
test('matcher() with allPatterns option', t => {
const flags = {allPatterns: true};
t.deepEqual(matcher('foo', '!x*', flags), ['foo']);
t.deepEqual(matcher(['foo', 'bar', 'for'], ['f*', 'b*'], flags), []);
t.deepEqual(matcher(['foo', 'bar', 'for'], ['f*', 'x*'], flags), []);
t.deepEqual(matcher(['foo', 'bar', 'for'], ['f*', '!b*'], flags), ['foo', 'for']);
t.deepEqual(matcher(['foo', 'bar', 'for'], ['f*', '!x*'], flags), ['foo', 'for']);
t.deepEqual(
matcher(
['Hey, tiger!', 'tiger has edge over hyenas', 'pushing a tiger over the edge is a stunt'],
['*edge*', '*tiger*', '!*stunt*'],
flags,
),
['tiger has edge over hyenas'],
);
});
test('isMatch() with allPatterns option', t => {
const flags = {allPatterns: true};
t.true(isMatch('foo', '!x*', flags));
t.false(isMatch(['foo', 'bar', 'for'], ['f*', 'b*'], flags));
t.false(isMatch(['foo', 'bar', 'for'], ['f*', 'x*'], flags));
t.true(isMatch(['foo', 'bar', 'for'], ['f*', '!b*'], flags));
t.true(isMatch(['foo', 'bar', 'for'], ['f*', '!x*'], flags));
t.true(isMatch(['foo', 'bar'], ['!bar'], flags));
t.true(isMatch(
['Hey, tiger!', 'tiger has edge over hyenas', 'pushing a tiger over the edge is a stunt'],
['*edge*', '*tiger*', '!*stunt*'],
flags,
));
});
test('isMatch() uses OR logic by default (matches ANY pattern)', t => {
// Default behavior: input matches if it matches ANY of the patterns (OR logic)
// Single input, multiple patterns
t.true(isMatch('foo', ['f*', 'b*'])); // Matches first pattern
t.true(isMatch('bar', ['f*', 'b*'])); // Matches second pattern
t.false(isMatch('zoo', ['f*', 'b*'])); // Matches neither pattern
// Multiple inputs, multiple patterns
t.true(isMatch(['foo', 'bar'], ['f*', 'b*'])); // Both inputs match at least one pattern
t.true(isMatch(['foo', 'zoo'], ['f*', 'b*'])); // At least one input matches a pattern
t.false(isMatch(['zoo', 'moo'], ['f*', 'b*'])); // No input matches any pattern
// Issue #31 use case - CORS origin matching
const allowedOrigins = ['*.example.com', '*.dev.example.com'];
t.true(isMatch('https://my.example.com', allowedOrigins)); // Matches first pattern
t.true(isMatch('https://my.dev.example.com', allowedOrigins)); // Matches second pattern
t.false(isMatch('https://my.other.com', allowedOrigins)); // Matches neither pattern
});
test('isMatch() with allPatterns option uses AND logic (matches ALL patterns)', t => {
// With allPatterns: true, input must match ALL non-negated patterns (AND logic)
// Single input must match all patterns
t.true(isMatch('foobar', ['f*', '*bar'], {allPatterns: true})); // Matches both
t.false(isMatch('foo', ['f*', '*bar'], {allPatterns: true})); // Matches only first
t.false(isMatch('bar', ['f*', '*bar'], {allPatterns: true})); // Matches only second
// Multiple inputs - at least one must match all patterns
t.true(isMatch(['foobar', 'zoo'], ['f*', '*bar'], {allPatterns: true})); // Foobar matches both
t.false(isMatch(['foo', 'bar'], ['f*', '*bar'], {allPatterns: true})); // No single input matches both
// Issue #31 scenario with allPatterns would require matching both patterns
const allowedOrigins = ['*.example.com', '*.dev.example.com'];
t.false(isMatch('https://my.example.com', allowedOrigins, {allPatterns: true})); // Doesn't match both
t.true(isMatch('https://my.dev.example.com', allowedOrigins, {allPatterns: true})); // Actually matches both because * matches any prefix
});
test('isMatch() documentation examples with allPatterns', t => {
// Test the exact examples from the documentation
t.true(isMatch('foobar', ['foo*', '*bar'], {allPatterns: true}));
t.false(isMatch('foo', ['foo*', '*bar'], {allPatterns: true}));
});
test('special regex characters are escaped properly', t => {
// Dots should be literal, not regex wildcard
t.true(isMatch('a.b', 'a.b'));
t.false(isMatch('axb', 'a.b'));
// Other special regex chars should be literal
t.true(isMatch('a+b', 'a+b'));
t.true(isMatch('a?b', 'a?b'));
t.true(isMatch('a(b)', 'a(b)'));
t.true(isMatch('a[b]', 'a[b]'));
t.true(isMatch('a{b}', 'a{b}'));
t.true(isMatch('a^b$', 'a^b$'));
// But * should still work as wildcard
t.true(isMatch('axb', 'a*b'));
});
test('complex allPatterns scenarios with negations', t => {
// Multiple negations - all must not match
t.true(isMatch('abc', ['a*', '!*x', '!*y'], {allPatterns: true}));
t.false(isMatch('abx', ['a*', '!*x', '!*y'], {allPatterns: true}));
t.false(isMatch('aby', ['a*', '!*x', '!*y'], {allPatterns: true}));
// Only negations - should match if none match
t.true(isMatch('foo', ['!bar', '!baz'], {allPatterns: true}));
t.false(isMatch('bar', ['!bar', '!baz'], {allPatterns: true}));
});
test('issue #32 regression test', t => {
// This was a bug in v4.0.0 that returned false instead of true
t.true(isMatch(['foo', 'bar'], ['a*', 'b*'])); // 'bar' matches 'b*'
t.true(isMatch(['apple', 'zoo'], ['a*', 'b*'])); // 'Apple' matches 'a*'
t.false(isMatch(['foo', 'zoo'], ['a*', 'b*'])); // Neither matches
});
test('escaped characters handling', t => {
// Escaped asterisks must stay literal (critical correctness)
t.false(isMatch('unicorn', String.raw`uni\*`)); // Per README promise
t.true(isMatch('uni*', String.raw`uni\*`)); // Should match literal asterisk
t.false(isMatch('unixcorn', String.raw`uni\*`)); // Should not wildcard
// Escaped spaces
t.true(isMatch('a b', String.raw`a\ b`)); // Should match literal space
t.false(isMatch('ab', String.raw`a\ b`)); // Should require space
t.false(isMatch('axb', String.raw`a\ b`)); // Should not wildcard
// Escaped backslashes
t.true(isMatch('test\\', 'test\\\\'));
t.false(isMatch('test', 'test\\\\'));
// Multiple escapes
t.true(isMatch(String.raw`a\*b`, String.raw`a\\\*b`));
t.false(isMatch('axb', String.raw`a\\\*b`));
});
test('newlines with dotAll flag', t => {
// The README promises foo*r matches foo\nbar (critical correctness)
t.true(isMatch('foo\nbar', 'foo*r'));
t.true(isMatch('foo\nbar', 'foo*'));
t.true(isMatch('foo\n\nbar', 'foo*bar'));
t.true(isMatch('foo\r\nbar', 'foo*bar')); // Windows line endings
});
test('regex cache with different flags', t => {
// Test potential cache collision bug
t.true(isMatch('FOO', 'foo', {caseSensitive: false}));
t.false(isMatch('FOO', 'foo', {caseSensitive: true}));
// If cache is broken, second call might return wrong result
t.false(isMatch('FOO', 'foo', {caseSensitive: true}));
t.true(isMatch('FOO', 'foo', {caseSensitive: false}));
});
test('unicode case handling', t => {
// Standard JS case insensitive behavior - Turkish İ lowercases to i̇, not i
t.false(isMatch('İstanbul', 'i*', {caseSensitive: false})); // İ ≠ i in Unicode
t.false(isMatch('İstanbul', 'i*', {caseSensitive: true}));
// But ASCII case insensitivity works as expected
t.true(isMatch('Istanbul', 'i*', {caseSensitive: false}));
t.false(isMatch('Istanbul', 'i*', {caseSensitive: true}));
});
test('allPatterns edge cases', t => {
// Only negations - should match if none match
t.true(isMatch('foo', ['!bar', '!baz'], {allPatterns: true}));
t.false(isMatch('bar', ['!bar', '!baz'], {allPatterns: true}));
// Mixed order should work the same
t.true(isMatch('foo', ['!bar', 'f*'], {allPatterns: true}));
t.true(isMatch('foo', ['f*', '!bar'], {allPatterns: true}));
t.false(isMatch('foobar', ['f*', '!*bar'], {allPatterns: true}));
// No patterns at all
t.false(isMatch('test', [], {allPatterns: true}));
});
test('multiple escaped stars in one pattern', t => {
t.true(isMatch('a*b*c', String.raw`a\*b\*c`));
t.false(isMatch('axbxc', String.raw`a\*b\*c`));
t.true(isMatch('a*b*c*d', String.raw`a\*b\*c\*d`));
});
test('mixed escaped and unescaped wildcards', t => {
t.true(isMatch('a*bcd', String.raw`a\*b*`)); // Literal * then wildcard
t.true(isMatch('a*bxd', String.raw`a\*b*`)); // Literal * then wildcard matching x
t.false(isMatch('axbcd', String.raw`a\*b*`)); // Missing literal *
t.true(isMatch('test*end', String.raw`*\*end`)); // Wildcard then literal *
t.false(isMatch('testend', String.raw`*\*end`)); // Missing literal *
});
test('consecutive wildcards optimization', t => {
// Multiple * should work like single *
t.true(isMatch('test', '**'));
t.true(isMatch('test', '*****'));
t.true(isMatch('a/b/c', '***'));
t.deepEqual(matcher(['foo', 'bar'], '**'), ['foo', 'bar']);
});
test('empty string with wildcards', t => {
t.true(isMatch('', '*'));
t.false(isMatch('', '!*'));
t.true(isMatch('', ''));
t.false(isMatch('', 'a*')); // Requires 'a' at start
t.false(isMatch('', '*a*')); // Requires 'a' somewhere
t.true(isMatch('', '**')); // Multiple wildcards still match empty
});
test('pattern ending with escape character', t => {
t.false(isMatch('test', 'test\\'));
t.true(isMatch('test\\', 'test\\\\'));
t.false(isMatch(String.raw`test\x`, 'test\\'));
});
test('real-world file matching scenarios', t => {
// Common gitignore patterns
t.true(isMatch('node_modules/foo', 'node_modules/*'));
t.true(isMatch('.env.local', '.env*'));
t.false(isMatch('src/.env', '.env*'));
// File extensions
t.true(isMatch('test.js', '*.js'));
t.false(isMatch('test.jsx', '*.js'));
t.true(isMatch('test.test.js', '*.test.js'));
t.true(isMatch('component.test.tsx', '*.test.*'));
});
test('performance with many patterns', t => {
const manyPatterns = Array.from({length: 100}, (_, i) => `pattern${i}*`);
manyPatterns.push('test*'); // Add one that matches
t.true(isMatch('test123', manyPatterns));
t.false(isMatch('nomatch', manyPatterns));
// With allPatterns - should be false as not all patterns match
t.false(isMatch('test123', manyPatterns, {allPatterns: true}));
});
test('whitespace in patterns', t => {
t.true(isMatch('hello world', 'hello world'));
t.true(isMatch('hello world', 'hello*world'));
t.true(isMatch('hello world', 'hello*world'));
t.false(isMatch('helloworld', 'hello world'));
// Tabs and other whitespace
t.true(isMatch('hello\tworld', 'hello*world'));
t.true(isMatch('hello\nworld', 'hello*world'));
});
test('negation edge cases', t => {
// Negation with no positive patterns
t.true(isMatch('foo', ['!bar']));
t.false(isMatch('bar', ['!bar']));
// Multiple negations
t.true(isMatch('foo', ['!bar', '!baz', '!qux']));
t.false(isMatch('bar', ['!bar', '!baz', '!qux']));
// Negation that would match everything
t.false(isMatch('anything', ['!*']));
t.false(isMatch('', ['!*']));
});
test('case sensitivity edge cases', t => {
// Patterns with mixed case
t.true(isMatch('FooBar', 'foo*', {caseSensitive: false}));
t.false(isMatch('FooBar', 'foo*', {caseSensitive: true}));
t.true(isMatch('FooBar', 'Foo*', {caseSensitive: true}));
// Numbers and special chars (should not be affected by case sensitivity)
t.true(isMatch('test123', 'test*', {caseSensitive: false}));
t.true(isMatch('test123', 'test*', {caseSensitive: true}));
t.true(isMatch('test-123', '*-123', {caseSensitive: false}));
t.true(isMatch('test-123', '*-123', {caseSensitive: true}));
});
test('spaces in single pattern (README example)', t => {
// Prove the README "spaces in a single pattern" example works
t.true(isMatch('foo bar baz', 'foo b* b*'));
t.false(isMatch('foo bar', 'foo b* b*'));
t.true(isMatch('foo bx bz', 'foo b* b*'));
t.true(isMatch('foo b b', 'foo b* b*'));
});
test('allPatterns with only negations', t => {
// Should include items not matching any negation
t.true(isMatch('foo', ['!bar', '!baz'], {allPatterns: true}));
t.false(isMatch('bar', ['!bar', '!baz'], {allPatterns: true}));
t.false(isMatch('baz', ['!bar', '!baz'], {allPatterns: true}));
t.true(isMatch('qux', ['!bar', '!baz'], {allPatterns: true}));
// With multiple inputs
t.true(isMatch(['foo', 'qux'], ['!bar', '!baz'], {allPatterns: true}));
t.false(isMatch(['foo', 'bar'], ['!bar', '!baz'], {allPatterns: true})); // Bar matches !bar
});
test('massive pattern set with duplicates', t => {
// Test cache behavior and performance with duplicates
const patterns = [
...Array.from({length: 50}, () => 'test*'), // 50 duplicates
...Array.from({length: 50}, () => 'foo*'), // 50 more duplicates
'unique*',
];
t.true(isMatch('test123', patterns));
t.true(isMatch('foo123', patterns));
t.true(isMatch('unique123', patterns));
t.false(isMatch('nomatch', patterns));
// Ensure it works with matcher() too
const results = matcher(['test1', 'foo2', 'unique3', 'nomatch'], patterns);
t.deepEqual(results, ['test1', 'foo2', 'unique3']);
});