UNPKG

1.57 kBJavaScriptView Raw
1const rand = require('random-seed').create();
2const defaultSeed = 'CARMI';
3const loadBytecode = require('../bytecode/carmi-instance');
4
5function currentValues(inst) {
6 if (typeof inst !== 'object' || inst === null) {
7 return inst;
8 }
9 if (Array.isArray(inst)) {
10 return inst.map(currentValues);
11 }
12 return Object.keys(inst)
13 .sort()
14 .filter(k => typeof inst[k] !== 'function' && k.indexOf('$') !== 0)
15 .reduce((acc, k) => {
16 acc[k] = currentValues(inst[k]);
17 return acc;
18 }, {});
19}
20
21const funcLibrary = {
22 tap: x => x,
23 invoke: (fn, ...args) => fn && fn(...args)
24};
25
26function expectTapFunctionToHaveBeenCalled(n, compiler) {
27 if (typeof compiler === 'string' && (compiler === 'optimizing' || compiler === 'bytecode')) {
28 expect(funcLibrary.tap.mock.calls.length).toEqual(n);
29 }
30 funcLibrary.tap.mockClear();
31}
32
33beforeEach(() => {
34 rand.seed(defaultSeed);
35 jest.spyOn(funcLibrary, 'tap');
36});
37
38afterEach(() => {
39 jest.clearAllMocks();
40});
41
42function describeCompilers(compilers, tests) {
43 // compilers = compilers.filter(t => t !== 'bytecode')
44 compilers.forEach(compiler => {
45 describe(`compiler:${compiler}`, () => tests(compiler));
46 });
47}
48
49function evalOrLoad(src) {
50 if (typeof src === 'string') {
51 try { // eslint-disable-next-line no-eval
52 return eval(src);
53 } catch (e) {
54 require('fs').writeFileSync('./tmp.js', src);
55 throw e;
56 }
57 }
58 return loadBytecode(src.buffer);
59}
60
61module.exports = {currentValues, expectTapFunctionToHaveBeenCalled, funcLibrary, describeCompilers, rand, evalOrLoad};