forked from donmccurdy/expression-eval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
156 lines (133 loc) · 4.71 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
require('source-map-support').install();
const expr = require('./dist/expression-eval.js');
const tape = require('tape');
const fixtures = [
// array expression
{expr: '([1,2,3])[0]', expected: 1 },
{expr: '(["one","two","three"])[1]', expected: 'two' },
{expr: '([true,false,true])[2]', expected: true },
{expr: '([1,true,"three"]).length', expected: 3 },
{expr: 'isArray([1,2,3])', expected: true },
{expr: 'list[3]', expected: 4 },
{expr: 'numMap[1 + two]', expected: 'three'},
// binary expression
{expr: '1+2', expected: 3},
{expr: '2-1', expected: 1},
{expr: '2*2', expected: 4},
{expr: '6/3', expected: 2},
{expr: '5|3', expected: 7},
{expr: '5&3', expected: 1},
{expr: '5^3', expected: 6},
{expr: '4<<2', expected: 16},
{expr: '256>>4', expected: 16},
{expr: '-14>>>2', expected: 1073741820},
{expr: '10%6', expected: 4},
{expr: '"a"+"b"', expected: 'ab'},
{expr: 'one + three', expected: 4},
// call expression
{expr: 'func(5)', expected: 6},
{expr: 'func(1+2)', expected: 4},
// conditional expression
{expr: '(true ? "true" : "false")', expected: 'true' },
{expr: '( ( bool || false ) ? "true" : "false")', expected: 'true' },
{expr: '( true ? ( 123*456 ) : "false")', expected: 123*456 },
{expr: '( false ? "true" : one + two )', expected: 3 },
// identifier
{expr: 'string', expected: 'string' },
{expr: 'number', expected: 123 },
{expr: 'bool', expected: true },
// literal
{expr: '"foo"', expected: 'foo' }, // string literal
{expr: "'foo'", expected: 'foo' }, // string literal
{expr: '123', expected: 123 }, // numeric literal
{expr: 'true', expected: true }, // boolean literal
// logical expression
{expr: 'true || false', expected: true },
{expr: 'true && false', expected: false },
{expr: '1 == "1"', expected: true },
{expr: '2 != "2"', expected: false },
{expr: '1.234 === 1.234', expected: true },
{expr: '123 !== "123"', expected: true },
{expr: '1 < 2', expected: true },
{expr: '1 > 2', expected: false },
{expr: '2 <= 2', expected: true },
{expr: '1 >= 2', expected: false },
// logical expression lazy evaluation
{expr: 'true || throw()', expected: true },
{expr: 'false || true', expected: true },
{expr: 'false && throw()', expected: false },
{expr: 'true && false', expected: false },
// member expression
{expr: 'foo.bar', expected: 'baz' },
{expr: 'foo["bar"]', expected: 'baz' },
{expr: 'foo[foo.bar]', expected: 'wow' },
// call expression with member
{expr: 'foo.func("bar")', expected: 'baz'},
// unary expression
{expr: '-one', expected: -1 },
{expr: '+two', expected: 2 },
{expr: '!false', expected: true },
{expr: '!!true', expected: true },
{expr: '~15', expected: -16 },
{expr: '+[]', expected: 0 },
// 'this' context
{expr: 'this.three', expected: 3 },
// custom operators
{expr: '@2', expected: 'two' },
{expr: '3#4', expected: 3.4 },
{expr: '(1 # 2 # 3)', expected: 1.5 }, // Fails with undefined precedence, see issue #45
{expr: '1 + 2 ~ 3', expected: 9 }, // ~ is * but with low precedence
];
const context = {
string: 'string',
number: 123,
bool: true,
one: 1,
two: 2,
three: 3,
foo: {bar: 'baz', baz: 'wow', func: function(x) { return this[x]; }},
numMap: {10: 'ten', 3: 'three'},
list: [1,2,3,4,5],
func: function(x) { return x + 1; },
isArray: Array.isArray,
throw: () => { throw new Error('Should not be called.'); }
};
expr.addUnaryOp('@', (a) => {
if (a === 2) {
return 'two';
}
throw new Error('Unexpected value: ' + a);
});
expr.addBinaryOp('#', (a, b) => a + b / 10);
expr.addBinaryOp('~', 1, (a, b) => a * b);
tape('sync', (t) => {
fixtures.forEach((o) => {
const val = expr.compile(o.expr)(context);
t.equal(val, o.expected, `${o.expr} (${val}) === ${o.expected}`);
});
t.end();
});
tape('async', async (t) => {
const asyncContext = context;
asyncContext.asyncFunc = async function(a, b) {
return await a + b;
};
asyncContext.promiseFunc = function(a, b) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(a + b), 1000);
})
}
const asyncFixtures = fixtures;
asyncFixtures.push({
expr: 'asyncFunc(one, two)',
expected: 3,
}, {
expr: 'promiseFunc(one, two)',
expected: 3,
});
for (let o of asyncFixtures) {
const val = await expr.compileAsync(o.expr)(asyncContext);
t.equal(val, o.expected, `${o.expr} (${val}) === ${o.expected}`);
}
t.end();
});