forked from donmccurdy/expression-eval
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
179 lines (159 loc) · 5.69 KB
/
test.js
File metadata and controls
179 lines (159 loc) · 5.69 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
const expr = require('./');
const assert = require('assert');
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, idents: ['list'] },
{expr: 'numMap[1 + two]', expected: 'three', idents: ['numMap', 'two' ] },
// 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, idents: ['one', 'three']},
// 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, idents: [ 'one', 'two' ] },
{expr: '( true ? "true" : one + two )', expected: "true", idents: [ 'one', 'two' ] },
// identifier
{expr: 'string', expected: 'string', idents: ['string'] },
{expr: 'number', expected: 123, idents: ['number'] },
{expr: 'bool', expected: true, idents: ['bool'] },
// 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', idents: ['foo'] },
{expr: 'foo["bar"]', expected: 'baz', idents: ['foo'] },
{expr: 'foo[foo.bar]', expected: 'wow', idents: ['foo'] },
// call expression with member
{expr: 'foo.func("bar")', expected: 'baz', idents: ['foo']},
// unary expression
{expr: '-one', expected: -1, idents: ['one'] },
{expr: '+two', expected: 2, idents: ['two'] },
{expr: '!false', expected: true },
{expr: '!!true', expected: true },
{expr: '~15', expected: -16 },
// 'this' context
{expr: 'this.three', expected: 3 },
// getValue()
{ expr: 'my_val', expected: 1.234, idents: ['my_val'] },
{ expr: 'number', expected: 123, idents: ['number'] },
{ expr: 'getValue( "number" )', expected: 234, idents: ['number'] },
{ expr: 'getValue( "calc_" + "ident" )', expected: undefined, idents: ['calc_ident'] },
];
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.'); },
getValue: function( id ) {
if( id === 'my_val' ) {
return 1.234;
} else if( id == "number" ) {
return 234;
}
}
};
var tests = 0;
var passed = 0;
fixtures.forEach( ( o ) => {
tests++;
try {
var val = expr.compile(o.expr)(context);
} catch (e) {
console.error(`Error: ${o.expr}, expected ${o.expected}`);
throw e;
}
assert.equal(val, o.expected, `Failed: ${o.expr} (${val}) === ${o.expected}`);
passed++;
} );
fixtures.forEach( ( o ) => {
tests++;
try {
var node = expr.parse(o.expr);
var idents = expr.readIdentifiers( node );
} catch( e ) {
console.error(`Error: ${o.expr}`);
throw e;
}
let expected = o.idents || [];
assert.equal(Array.from( new Set(idents) ).join(','), Array.from( new Set(expected) ).join(','), `Failed: ${o.expr} identifiers (${idents}) === ${expected}`);
++passed;
} );
async function testAsync() {
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) {
tests++;
try {
var val = await expr.compileAsync(o.expr)(asyncContext);
} catch (e) {
console.error(`Error: ${o.expr}, expected ${o.expected}`);
throw e;
}
assert.equal(val, o.expected, `Failed: ${o.expr} (${val}) === ${o.expected}`);
passed++;
}
}
testAsync().then(() => {
console.log('%s/%s tests passed.', passed, tests);
})