-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
expression.js
194 lines (168 loc) · 7.1 KB
/
expression.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import path from 'path';
import * as diff from 'diff';
import fs from 'fs';
import harness from './harness';
import compactStringify from 'json-stringify-pretty-compact';
// we have to handle this edge case here because we have test fixtures for this
// edge case, and we don't want UPDATE=1 to mess with them
function stringify(v) {
let s = compactStringify(v);
// http://timelessrepo.com/json-isnt-a-javascript-subset
if (s.indexOf('\u2028') >= 0) {
s = s.replace(/\u2028/g, '\\u2028');
}
if (s.indexOf('\u2029') >= 0) {
s = s.replace(/\u2029/g, '\\u2029');
}
return s;
}
const decimalSigFigs = 6;
function stripPrecision(x) {
// Intended for test output serialization:
// strips down to 6 decimal sigfigs but stops at decimal point
if (typeof x === 'number') {
if (x === 0) { return x; }
const multiplier = Math.pow(10,
Math.max(0,
decimalSigFigs - Math.ceil(Math.log10(Math.abs(x)))));
// We strip precision twice in a row here to avoid cases where
// stripping an already stripped number will modify its value
// due to bad floating point precision luck
// eg `Math.floor(8.16598 * 100000) / 100000` -> 8.16597
const firstStrip = Math.floor(x * multiplier) / multiplier;
return Math.floor(firstStrip * multiplier) / multiplier;
} else if (typeof x !== 'object') {
return x;
} else if (Array.isArray(x)) {
return x.map(stripPrecision);
} else {
const stripped = {};
for (const key of Object.keys(x)) {
stripped[key] = stripPrecision(x[key]);
}
return stripped;
}
}
function deepEqual(a, b) {
if (typeof a !== typeof b)
return false;
if (typeof a === 'number') {
return stripPrecision(a) === stripPrecision(b);
}
if (a === null || b === null || typeof a !== 'object')
return a === b;
const ka = Object.keys(a);
const kb = Object.keys(b);
if (ka.length !== kb.length)
return false;
ka.sort();
kb.sort();
for (let i = 0; i < ka.length; i++)
if (ka[i] !== kb[i] || !deepEqual(a[ka[i]], b[ka[i]]))
return false;
return true;
}
/**
* Run the expression suite.
*
* @param {string} implementation - identify the implementation under test; used to
* deal with implementation-specific test exclusions and fudge-factors
* @param {Object} options
* @param {Array<string>} [options.tests] - array of test names to run; tests not in the array will be skipped
* @param {Array<string>} [options.ignores] - array of test names to ignore.
* @param {} runExpressionTest - a function that runs a single expression test fixture
* @returns {undefined} terminates the process when testing is complete
*/
export function run(implementation, options, runExpressionTest) {
const directory = path.join(__dirname, '../expression-tests');
options.fixtureFilename = 'test.json';
harness(directory, implementation, options, (fixture, params, done) => {
try {
const result = runExpressionTest(fixture, params);
const dir = path.join(directory, params.id);
if (process.env.UPDATE) {
fixture.expected = {
compiled: result.compiled,
outputs: stripPrecision(result.outputs),
serialized: result.serialized
};
delete fixture.metadata;
fs.writeFile(path.join(dir, 'test.json'), `${stringify(fixture, null, 2)}\n`, done);
return;
}
const expected = fixture.expected;
const compileOk = deepEqual(result.compiled, expected.compiled);
const evalOk = compileOk && deepEqual(result.outputs, expected.outputs);
let recompileOk = true;
let roundTripOk = true;
let serializationOk = true;
if (expected.compiled.result !== 'error') {
serializationOk = compileOk && deepEqual(expected.serialized, result.serialized);
recompileOk = compileOk && deepEqual(result.recompiled, expected.compiled);
roundTripOk = recompileOk && deepEqual(result.roundTripOutputs, expected.outputs);
}
params.ok = compileOk && evalOk && recompileOk && roundTripOk && serializationOk;
const diffOutput = {
text: '',
html: ''
};
const diffJson = (label, expectedJson, actualJson) => {
let text = '';
let html = '';
diff.diffJson(expectedJson, actualJson)
.forEach((hunk) => {
if (hunk.added) {
text += `+ ${hunk.value}`;
html += `<ins> ${hunk.value}</ins>`;
} else if (hunk.removed) {
text += `- ${hunk.value}`;
html += `<del> ${hunk.value}</del>`;
} else {
text += ` ${hunk.value}`;
html += `<span> ${hunk.value}</span>`;
}
});
if (text) {
diffOutput.text += `${label}\n${text}`;
diffOutput.html += `<h3>${label}</h3>\n${html}`;
}
};
if (!compileOk) {
diffJson('Compiled', expected.compiled, result.compiled);
}
if (compileOk && !serializationOk) {
diffJson('Serialized', expected.serialized, result.serialized);
}
if (compileOk && !recompileOk) {
diffJson('Serialized and re-compiled', expected.compiled, result.recompiled);
}
const diffOutputs = (testOutputs) => {
return expected.outputs.map((expectedOutput, i) => {
if (!deepEqual(expectedOutput, testOutputs[i])) {
return `f(${JSON.stringify(fixture.inputs[i])})\nExpected: ${JSON.stringify(expectedOutput)}\nActual: ${JSON.stringify(testOutputs[i])}`;
}
return false;
})
.filter(Boolean)
.join('\n');
};
if (compileOk && !evalOk) {
const differences = `Original\n${diffOutputs(result.outputs)}\n`;
diffOutput.text += differences;
diffOutput.html += differences;
}
if (recompileOk && !roundTripOk) {
const differences = `\nRoundtripped through serialize()\n${diffOutputs(result.roundTripOutputs)}\n`;
diffOutput.text += differences;
diffOutput.html += differences;
}
params.difference = diffOutput.html;
if (diffOutput.text) { console.log(diffOutput.text); }
params.expression = compactStringify(fixture.expression);
params.serialized = compactStringify(result.serialized);
done();
} catch (e) {
done(e);
}
});
}