This repository was archived by the owner on May 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtest.ts
137 lines (129 loc) · 3.51 KB
/
test.ts
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
import fs from 'fs';
import path from 'path';
import assert from 'assert';
import degenerator, { compile } from '../src';
describe('degenerator()', () => {
describe('"async" output', () => {
it('should support "async" output functions', () => {
function aPlusB(a: () => string, b: () => string): string {
return a() + b();
}
const compiled = degenerator('' + aPlusB, ['a'], {
output: 'async'
});
assert.equal(
compiled.replace(/\s+/g, ' '),
'async function aPlusB(a, b) { return await a() + b(); }'
);
});
it('should be the default "output" mode (with options)', () => {
function foo(a: () => string): string {
return a();
}
const compiled = degenerator('' + foo, ['a'], {});
assert.equal(
compiled.replace(/\s+/g, ' '),
'async function foo(a) { return await a(); }'
);
});
it('should be the default "output" mode (without options)', () => {
function foo(a: () => string): string {
return a();
}
const compiled = degenerator('' + foo, ['a']);
assert.equal(
compiled.replace(/\s+/g, ' '),
'async function foo(a) { return await a(); }'
);
});
});
describe('"generator" output', () => {
it('should support "generator" output functions', () => {
function aPlusB(a: () => string, b: () => string): string {
return a() + b();
}
const compiled = degenerator('' + aPlusB, ['a'], {
output: 'generator'
});
assert.equal(
compiled.replace(/\s+/g, ' '),
'function* aPlusB(a, b) { return (yield a()) + b(); }'
);
});
});
describe('"expected" fixture tests', () => {
fs.readdirSync(__dirname)
.sort()
.forEach(n => {
if (n === 'test.js') return;
if (/\.expected\.js$/.test(n)) return;
if (/\.ts$/.test(n)) return;
if (/\.map/.test(n)) return;
const expectedName = `${path.basename(n, '.js')}.expected.js`;
it(`${n} → ${expectedName}`, function() {
const sourceName = path.resolve(__dirname, n);
const compiledName = path.resolve(__dirname, expectedName);
const js = fs.readFileSync(sourceName, 'utf8');
const expected = fs.readFileSync(compiledName, 'utf8');
// the test case can define the `names` to use as a
// comment on the first line of the file
const m = js.match(/\/\/\s*(.*)/);
let names;
if (m) {
// the comment should be a comma-separated list of function names
names = m[1].split(/,\s*/);
} else {
// if no function names were passed in then convert them all
names = [/.*/];
}
const compiled = degenerator(js, names, {
output: 'generator'
});
assert.equal(
compiled.trim().replace(/\r/g, ''),
expected.trim().replace(/\r/g, '')
);
});
});
});
describe('`compile()`', () => {
it('should compile code into an invocable async function', done => {
const a = () => Promise.resolve('a');
const b = () => 'b';
function aPlusB(): string {
return a() + b();
}
const fn = compile<() => Promise<string>>(
'' + aPlusB,
'aPlusB',
['a'],
{
sandbox: { a, b }
}
);
fn().then((val: string) => {
assert.equal(val, 'ab');
done();
}, done);
});
it('should be able to await non-promises', done => {
const a = () => 'a';
const b = () => 'b';
function aPlusB(): string {
return a() + b();
}
const fn = compile<() => Promise<string>>(
'' + aPlusB,
'aPlusB',
['a'],
{
sandbox: { a, b }
}
);
fn().then((val: string) => {
assert.equal(val, 'ab');
done();
}, done);
});
});
});