-
Notifications
You must be signed in to change notification settings - Fork 620
/
Copy pathTreePattern.test.ts
67 lines (58 loc) · 1.24 KB
/
TreePattern.test.ts
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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { TreePattern, type TreeNode } from './TreePattern';
export interface IMyPattern {
branch?: string;
}
const pattern1: TreePattern = new TreePattern({
a: [
1,
2,
TreePattern.tag('branch', {
b: []
})
]
});
const pattern2: TreePattern = new TreePattern({
c: TreePattern.oneOf([
123,
{
d: 1
}
])
});
describe(TreePattern.name, () => {
it('matches using a tag', () => {
const tree1: TreeNode = {
a: [
1,
2,
{
b: [],
extra: 'hi'
}
],
b: 123
};
const captures: IMyPattern = {};
expect(pattern1.match(tree1, captures)).toBe(true);
expect(captures.branch).toMatchObject({
b: [],
extra: 'hi'
});
});
it('matches alternatives', () => {
const tree2a: TreeNode = {
c: 123
};
expect(pattern2.match(tree2a)).toBe(true);
const tree2b: TreeNode = {
c: { d: 1 }
};
expect(pattern2.match(tree2b)).toBe(true);
const tree2c: TreeNode = {
c: 321
};
expect(pattern2.match(tree2c)).toBe(false);
});
});