-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathPredicate.test.ts
More file actions
101 lines (85 loc) · 3.13 KB
/
Copy pathPredicate.test.ts
File metadata and controls
101 lines (85 loc) · 3.13 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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
import * as assert from 'assert';
import { ExactPredicate, PatternPredicate } from '../../src/view/Predicate';
describe('PatternPredicate', () => {
describe('asterisk match', () => {
it('should match anything', () => {
const predicate = new PatternPredicate('*');
assert.ok(predicate.match('foo'));
assert.ok(predicate.match(''));
});
it('should match trailing part', () => {
const predicate = new PatternPredicate('foo*');
assert.ok(predicate.match('foo'));
assert.ok(predicate.match('foobar'));
assert.ok(!predicate.match('_foo'));
assert.ok(!predicate.match('bar'));
assert.ok(!predicate.match(''));
});
it('should match leading part', () => {
const predicate = new PatternPredicate('*bar');
assert.ok(predicate.match('foobar'));
assert.ok(predicate.match('bar'));
assert.ok(!predicate.match('foo'));
assert.ok(!predicate.match('bar_'));
assert.ok(!predicate.match(''));
});
});
describe('question-mark match', () => {
it('should match exactly one character', () => {
const predicate = new PatternPredicate('fo?');
assert.ok(predicate.match('foo'));
assert.ok(predicate.match('fob'));
assert.ok(!predicate.match('fo'));
assert.ok(!predicate.match('fooo'));
assert.ok(!predicate.match(''));
});
it('should combine with asterisk', () => {
const predicate = new PatternPredicate('fo?*');
assert.ok(predicate.match('foo'));
assert.ok(predicate.match('foobar'));
assert.ok(!predicate.match('fo'));
assert.ok(!predicate.match(''));
});
});
describe('exact match', () => {
it('should match exactly', () => {
const predicate = new PatternPredicate('foobar');
assert.ok(predicate.match('foobar'));
assert.ok(!predicate.match('foo'));
assert.ok(!predicate.match('_foobar_'));
assert.ok(!predicate.match(''));
});
});
describe('escapePattern', () => {
it('should escape regexp elements', () => {
assert.strictEqual(
PatternPredicate.escapePattern('^$\\.+()[]{}|'),
'^\\^\\$\\\\\\.\\+\\(\\)\\[\\]\\{\\}\\|$'
);
assert.strictEqual(PatternPredicate.escapePattern('*'), '^.*$');
assert.strictEqual(PatternPredicate.escapePattern('foobar'), '^foobar$');
assert.strictEqual(PatternPredicate.escapePattern('foo*'), '^foo.*$');
assert.strictEqual(PatternPredicate.escapePattern('*bar'), '^.*bar$');
assert.strictEqual(PatternPredicate.escapePattern('fo?'), '^fo.$');
assert.strictEqual(PatternPredicate.escapePattern('?bar'), '^.bar$');
});
});
});
describe('ExactPredicate', () => {
it('should match all', () => {
const predicate = new ExactPredicate();
assert.ok(predicate.match('foo'));
assert.ok(predicate.match(''));
});
it('should exact match', () => {
const predicate = new ExactPredicate('foobar');
assert.ok(!predicate.match('foo'));
assert.ok(!predicate.match('bar'));
assert.ok(!predicate.match(''));
assert.ok(predicate.match('foobar'));
});
});