-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESParser.test.js
105 lines (93 loc) · 2.44 KB
/
ESParser.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
const ESParser = require("./ESParser");
const parser = new ESParser();
function parse(expression) {
return parser.parse(expression);
}
describe("builds a boolio tree from a JS expression", () => {
it("simple or expression", () => {
expect(parse("a || b")).toEqual({
type: "or",
left: { type: "atom", name: "a" },
right: { type: "atom", name: "b" }
});
});
it("simple and expression", () => {
expect(parse("a && b")).toEqual({
type: "and",
left: { type: "atom", name: "a" },
right: { type: "atom", name: "b" }
});
});
it("simple not expression", () => {
expect(parse("!a")).toEqual({
type: "not",
argument: { type: "atom", name: "a" }
});
});
it("multiple operators", () => {
expect(parse("a || b && c")).toEqual({
type: "or",
left: { type: "atom", name: "a" },
right: {
type: "and",
left: { type: "atom", name: "b" },
right: { type: "atom", name: "c" }
}
});
});
it("grouping", () => {
expect(parse("(a || b)")).toEqual({
type: "or",
left: { type: "atom", name: "a" },
right: { type: "atom", name: "b" }
});
});
it("grouping with multiple operators", () => {
expect(parse("(a || b) && c")).toEqual({
type: "and",
left: {
type: "or",
left: { type: "atom", name: "a" },
right: { type: "atom", name: "b" }
},
right: { type: "atom", name: "c" }
});
});
it("call expression", () => {
expect(parse("foo() && bar(1,2,x)")).toEqual({
type: "and",
left: { type: "atom", name: "foo()" },
right: { type: "atom", name: "bar(1,2,x)" }
});
});
it("nested call expression", () => {
expect(parse("foo(bar(1)) && x")).toEqual({
type: "and",
left: { type: "atom", name: "foo(bar(1))" },
right: { type: "atom", name: "x" }
});
});
it("call with operator", () => {
expect(parse("foo(a && b)")).toEqual({
type: "atom",
name: "foo(a && b)"
});
});
it("member and binary expressions", () => {
expect(
parse(
"wire.frequency.type !== 'OneTime' && (!wire.beneficiary.isInternational)"
)
).toEqual({
type: "and",
left: { type: "atom", name: "wire.frequency.type !== 'OneTime'" },
right: {
type: "not",
argument: {
type: "atom",
name: "wire.beneficiary.isInternational"
}
}
});
});
});