forked from jstransformers/test-jstransformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (114 loc) · 3.05 KB
/
index.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
'use strict';
const path = require('path');
const fs = require('fs');
const assert = require('assert');
const cp = require('child_process');
const test = require('testit');
const testJStransformer = require('..');
const resolve = path.resolve;
const simplePath = path.join(__dirname, '..', 'example', 'simple');
const multiPath = path.join(__dirname, '..', 'example', 'multi');
testJStransformer({
name: 'simple',
outputFormat: 'txt',
inputFormats: ['txt'],
compile: () => {
return () => {
return 'output text';
};
}
}, resolve(simplePath));
testJStransformer({
name: 'multi',
outputFormat: 'txt',
inputFormats: ['txt'],
compile: str => {
const secondCase = path.join(__dirname, '..', 'example', 'multi', 'second-case');
return {
fn: () => {
return 'output text';
},
dependencies: /load foo.js/.test(str) ? [resolve(secondCase, './foo.js')] : []
};
}
}, resolve(multiPath));
testJStransformer({
name: 'render',
outputFormat: 'txt',
inputFormats: ['txt'],
render: () => {
return 'output text';
}
}, resolve(simplePath));
testJStransformer({
name: 'renderAsync',
outputFormat: 'txt',
inputFormats: ['txt'],
renderAsync: () => {
return new Promise(resolve => {
resolve('output text');
});
}
}, resolve(simplePath));
testJStransformer({
name: 'renderFile',
outputFormat: 'txt',
inputFormats: ['txt'],
renderFile: () => {
return 'output text';
}
}, resolve(simplePath));
testJStransformer({
name: 'renderFileAsync',
outputFormat: 'txt',
inputFormats: ['txt'],
renderFileAsync: () => {
return new Promise(resolve => {
resolve('output text');
});
}
}, resolve(simplePath));
test('failures', () => {
const failuresPath = path.join(__dirname, 'failures');
fs.readdirSync(resolve(failuresPath)).forEach(testCase => {
if (!/\.js$/.test(testCase)) {
return;
}
test(testCase, () => {
return new Promise(resolve => {
let stdout = '';
let stderr = '';
const failPath = path.join('failures', testCase);
console.log(failPath);
const child = cp.fork(require.resolve('./' + failPath), {
silent: true
});
child.stdout.on('data', data => {
stdout += data;
});
child.stderr.on('data', data => {
stderr += data;
});
child.on('exit', code => {
resolve(
'code: ' + code +
'\n\nstdout:\n\n' + stdout.replace(/\d+[a-z]+/g, '#ms').replace(/\r\n/g, '\n') +
'\n\nstderr:\n\n' + stderr.replace(/\r\n/g, '\n')
);
});
}).then(result => {
let expected;
const testCasePath = path.join(__dirname, 'failures', testCase.replace(/\.js$/, '.txt'));
try {
expected = fs.readFileSync(resolve(testCasePath), 'utf8');
} catch (err) {
if (err.code === 'ENOENT') {
fs.writeFileSync(resolve(testCasePath), result);
}
throw err;
}
assert.strictEqual(result, expected);
});
});
});
});