-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
201 lines (177 loc) · 6.83 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
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
195
196
197
198
199
200
201
'use strict';
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const testit = require('testit');
function assertEqual(output, expected) {
try {
assert.strictEqual(output, expected);
} catch (error) {
console.log(' Output:\t' + JSON.stringify(output));
console.log(' Expected:\t' + JSON.stringify(expected));
throw error;
}
}
function assertDeepEqual(output, expected) {
try {
assert.deepStrictEqual(output, expected);
} catch (error) {
console.log(' Output:\t' + JSON.stringify(output));
console.log(' Expected:\t' + JSON.stringify(expected));
throw error;
}
}
function getFilename(filename) {
if (/\.\*$/.test(filename)) {
const files = fs.readdirSync(path.dirname(filename));
let result;
let gotResult = false;
for (const file of files) {
const p = filename.replace(/\.\*$/, path.extname(file));
if (file === path.basename(p)) {
if (gotResult) {
throw new Error('Multiple files were found matching ' + filename);
}
gotResult = true;
result = p;
}
}
if (gotResult) {
return path.resolve(result);
}
throw new Error('Could not find a file matching ' + filename);
}
return path.resolve(filename);
}
function read(filename) {
filename = getFilename(filename);
return fs.readFileSync(filename, 'utf8').trim();
}
module.exports = addTests;
function addTests(transform, testDirectory, test) {
test = test || testit;
function addTestCases(directory) {
const inputFile = getFilename(path.join(directory, 'input.*'));
const input = read(inputFile);
const options = require(path.join(directory, 'options'));
const locals = require(path.join(directory, 'locals'));
const dependencies = require(path.join(directory, 'dependencies')).map(dep => {
return path.resolve(directory, dep);
});
const expected = read(path.join(directory, 'expected.*'));
function checkFunctionOutput(template) {
if ((dependencies && dependencies.length > 0) || (typeof template === 'object' && template)) {
assert(typeof template === 'object' && template, ' template should be an object because this module tracks dependencies');
assert(typeof template.fn === 'function', 'template.fn should be a function');
assertEqual(template.fn(locals).trim(), expected);
assert(Array.isArray(template.dependencies), ' template.dependencies should be an array');
assert(template.dependencies.every(dependency => {
return typeof dependency === 'string';
}), ' template.dependencies should all be strings');
assertDeepEqual(template.dependencies.map(dependency => {
return path.resolve(dependency);
}), dependencies || []);
} else {
assert(typeof template === 'function', 'template should be a function, or an object with an "fn" property of type function and a "dependencies" property that is an array.');
assertEqual(template(locals).trim(), expected);
}
}
function checkOutput(output) {
if ((dependencies && dependencies.length > 0) || (typeof output === 'object' && output)) {
assert(typeof output === 'object' && output, ' output should be an object because this module tracks dependencies');
assert(typeof output.body === 'string', 'output.body should be a string');
assertEqual(output.body.trim(), expected);
assert(Array.isArray(output.dependencies), ' output.dependencies should be an array');
assertDeepEqual(output.dependencies.map(dependency => {
return path.resolve(dependency);
}), dependencies || []);
} else {
assert(typeof output === 'string', 'output should be a string, or an object with a "body" property of type string and a "dependencies" property that is an array.');
assertEqual(output.trim(), expected);
}
}
if (transform.compile) {
test(transform.name + '.compile()', () => {
const template = transform.compile(input, options);
checkFunctionOutput(template);
});
}
if (transform.compileAsync) {
test(transform.name + '.compileAsync()', () => {
return transform.compileAsync(input, options).then(template => {
checkFunctionOutput(template);
});
});
}
if (transform.compileFile) {
test(transform.name + '.compileFile()', () => {
const template = transform.compileFile(inputFile, options);
checkFunctionOutput(template);
});
}
if (transform.compileFileAsync) {
test(transform.name + '.compileFileAsync()', () => {
return transform.compileFileAsync(inputFile, options).then(template => {
checkFunctionOutput(template);
});
});
}
if (transform.render) {
test(transform.name + '.render()', () => {
const output = transform.render(input, options, locals);
checkOutput(output);
});
}
if (transform.renderAsync) {
test(transform.name + '.renderAsync()', () => {
return transform.renderAsync(input, options, locals).then(output => {
checkOutput(output);
});
});
}
if (transform.renderFile) {
test(transform.name + '.renderFile()', () => {
const output = transform.renderFile(inputFile, options, locals);
checkOutput(output);
});
}
if (transform.renderFileAsync) {
test(transform.name + '.renderFileAsync()', () => {
transform.renderFileAsync(inputFile, options, locals).then(output => {
checkOutput(output);
});
});
}
}
assert(transform && typeof transform === 'object', 'Transform must be an object');
assert(typeof transform.name === 'string' && transform.name, 'Transform must have a name');
assert(typeof testDirectory === 'string', 'Must specify a testDirectory');
test(transform.name, () => {
test('transform has an output format', () => {
assert(typeof transform.outputFormat === 'string' && transform.outputFormat);
});
if (transform.inputFormats) {
test('transform has input formats', () => {
assert(Array.isArray(transform.inputFormats), 'Expected transform.inputFormats to be an array');
assert(transform.inputFormats.every(format => {
return typeof format === 'string' && format;
}), 'Expected all inputFormats to be non-empty strings');
});
}
const dir = fs.readdirSync(testDirectory).filter(filename => {
return filename[0] !== '.';
});
const isMultiTest = dir.length > 0 && dir.every(file => {
return fs.statSync(path.join(testDirectory, file)).isDirectory();
});
if (isMultiTest) {
for (const subdir of dir) {
test(subdir, () => {
addTestCases(path.join(testDirectory, subdir));
});
}
} else {
addTestCases(testDirectory);
}
});
}