1 | const rand = require('random-seed').create();
|
2 | const defaultSeed = 'CARMI';
|
3 | const loadBytecode = require('../bytecode/carmi-instance');
|
4 |
|
5 | function 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 |
|
21 | const funcLibrary = {
|
22 | tap: x => x,
|
23 | invoke: (fn, ...args) => fn && fn(...args)
|
24 | };
|
25 |
|
26 | function 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 |
|
33 | beforeEach(() => {
|
34 | rand.seed(defaultSeed);
|
35 | jest.spyOn(funcLibrary, 'tap');
|
36 | });
|
37 |
|
38 | afterEach(() => {
|
39 | jest.clearAllMocks();
|
40 | });
|
41 |
|
42 | function describeCompilers(compilers, tests) {
|
43 |
|
44 | compilers.forEach(compiler => {
|
45 | describe(`compiler:${compiler}`, () => tests(compiler));
|
46 | });
|
47 | }
|
48 |
|
49 | function evalOrLoad(src) {
|
50 | if (typeof src === 'string') {
|
51 | try {
|
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 |
|
61 | module.exports = {currentValues, expectTapFunctionToHaveBeenCalled, funcLibrary, describeCompilers, rand, evalOrLoad};
|