This repository was archived by the owner on Nov 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgentjs.js
More file actions
executable file
·338 lines (277 loc) · 9.52 KB
/
gentjs.js
File metadata and controls
executable file
·338 lines (277 loc) · 9.52 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#! /usr/bin/env node
var path = require('path');
var fs = require('fs');
var format = require('util').format;
var mkdirp = require('mkdirp');
var deasync = require('deasync');
var configFile = 'gentjs.json';
var customConfig = false;
// Try load the .gentest.js config file
try {
// Custom config file
if (process.argv.length > 2 && process.argv[2][0] !== '-') {
configFile = process.argv[2];
customConfig = true;
}
// Make sure config file address is absolute
if (!path.isAbsolute(configFile))
configFile = path.join(process.cwd(), configFile);
// Read gentjs.json file
var config = fs.readFileSync(
configFile, {
encoding: 'UTF-8'
}
);
} catch (e) {
console.error("Cannot find '%s' file.", customConfig ? process.argv[2] : configFile);
process.exit(1);
}
var CWD = path.dirname(configFile);
// Parse the config file's content
try {
config = JSON.parse(config);
} catch (e) {
console.error(
"Error in parsing '%s' json file:\n\n%s",
customConfig ? process.argv[2] : configFile,
e
);
process.exit(1);
}
// Run test command
if (process.argv.slice(2).includes('--run')) {
try {
var Mocha = require(path.join(CWD, 'node_modules/mocha/'));
} catch (e) {
try {
var Mocha = require('mocha');
} catch (e) {
console.error("Error in loading mocha. Please install mocha in config file's directory or globally.");
process.exit(1);
}
}
var mochaOptions = config.mocha;
if (mochaOptions === undefined)
mochaOptions = {};
mochaOptions.ui = 'tdd';
var mocha = new Mocha(mochaOptions);
// Add test code pathes to mocha
if (Array.isArray(config.functions))
config.functions.forEach(function(fn) {
var fnDir = path.join(CWD, fn.directory === undefined ? config.directory : '' + fn.directory);
// Function's test case code path
var fnCodePath = path.join(fnDir, fn.file === undefined ? fn.name + '.js' : '' + fn.file);
mocha.addFile(fnCodePath);
});
var data = null;
var done = false;
mocha.run(function(failures) {
data = failures;
done = true;
});
deasync.loopWhile(function() {
return !done;
});
process.exit(data);
}
var overwrite = false;
if (process.argv.length > 2) {
var argvSlice = process.argv.slice(customConfig ? 3 : 2);
overwrite = argvSlice.includes('-o');
if (!overwrite)
overwrite = argvSlice.includes('--overwrite');
}
// Check if there is an array of functions defined in config file
if (!Array.isArray(config.functions))
process.exit(0);
var typeMap = null;
// If custom type module is specified
if (config.customTypes) {
try {
typeMap = require(path.join(CWD, String(config.customTypes)));
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND')
console.error("'%s' customTypes file does not exists.");
else
console.error("'%s' customTypes file loading error:\n\n%s", e);
process.exit(1);
}
} else {
typeMap = {
'str': [null, undefined, 'foo bar', 'FOO BAR', 113, 23.23, '', ' ', '\n\n\t\r'],
'char': [null, undefined, '', ' ', 'foo bar', 'a', 'A', 113, 23.23],
'bool': [null, undefined, true, false, '', 113, 23.23],
'num': function(param) {
var params = [];
// If min is defined
if (param.min !== undefined) {
// Check if min is a valid number
if (isNaN(param.min = Number(param.min)))
throw "'num'.'min' is not a number";
params.push(param.min - 1, param.min, param.min + 1);
}
// If max is defined
if (param.max !== undefined) {
// Check if max is a valid number
if (isNaN(param.max = Number(param.max)))
throw "'num'.'max' is not a number";
params.push(param.max - 1, param.max, param.max + 1);
}
// Check if mid is defined
if (param.mid === undefined)
throw "'num'.'mid' is not defined";
// Check if mid is a valid number
if (isNaN(param.mid = Number(param.mid)))
throw "'num'.'mid' is not a number";
params.push(param.mid);
return params;
}
};
}
// Check if directory config is defined
if (config.directory === undefined)
config.directory = 'tests';
else
// Convert it to string
config.directory += '';
// Check if indent config is defined
if (config.indent === undefined)
config.indent = 2;
config.indent = ' '.repeat(config.indent);
if (config.moduleDirectory !== undefined)
// Convert it to string
config.moduleDirectory += '';
config.functions.forEach(function(fn) {
// Check if functions has module property
if (fn.module !== undefined) {
fn.module += '';
}
// Check if functions has name property
if (!fn.name) {
console.error("All functions must have 'name' property.");
process.exit(1);
} else {
// Convert it to string
fn.name += '';
}
// Check if function's param property is defined
if (fn.params === undefined) {
console.error("'%s' must have 'params' property.", fn.module);
process.exit(1);
}
// Check if function's params property is an array
if (!Array.isArray(fn.params)) {
console.error("'%s' params property must be an array.", fn.module);
process.exit(1);
}
var testCases = [];
function cartesianProduct(arr) {
// There is only one set
if (arr.length === 1)
return arr[0].map(JSON.stringify);
var result = [];
arr[0].forEach(function(item) {
item = JSON.stringify(item);
cartesianProduct(arr.slice(1)).forEach(function(other) {
result.push(item + ', ' + other);
});
});
return result;
}
var params = [];
for (var i = 0; i < fn.params.length; i++) {
var param = fn.params[i];
if (typeof param === 'object') {
// Check if param has type property
if (param.type === undefined) {
console.error(
"'%s' function's '%s' param property must have type property",
fn.module,
param
);
process.exit(1);
}
// Check if param type is defined
if (!typeMap.hasOwnProperty(param.type)) {
console.error(
"'%s' function's '%s' param type is unknown.",
fn.module,
param.type
);
process.exit(1);
}
// Check if param type is an object definable type
if (typeof typeMap[param.type] !== 'function') {
console.error(
"'%s' function's '%s' param type is not object definable.",
fn.module,
param.type
);
process.exit(1);
}
var returnedParams = null;
try {
returnedParams = typeMap[param.type](param);
} catch (e) {
console.error(
"'%s' function's '%s' param type %s.",
fn.module,
param.type,
e
);
process.exit(1);
}
params.push(returnedParams);
continue;
}
// Check if param is a defined type
if (!typeMap.hasOwnProperty(param)) {
console.error(
"'%s' function's '%s' param type is unknown.",
fn.module,
param
);
process.exit(1);
}
params.push(typeMap[param]);
}
if (fn.params.length)
testCases = testCases.concat(cartesianProduct(params));
else
testCases.push('');
// Path of module to be loaded inside test source code
var moduleAddr = fn.module;
if (fn.module === undefined && config.moduleDirectory === undefined)
moduleAddr = path.join(CWD, fn.name);
else if (config.moduleDirectory !== undefined)
moduleAddr = path.join(config.moduleDirectory, fn.name);
var fnDir = path.join(CWD, fn.directory === undefined ? config.directory : '' + fn.directory);
moduleAddr = path.relative(
fnDir,
moduleAddr
);
var testCode = "var equal = require('assert').equal;\n" +
format("var %s = require('%s')", fn.name, moduleAddr) +
(fn.export !== undefined ? '.' + fn.export : '') + ";\n\n\n" +
format("test('#%s', function() {\n", fn.name);
testCases.forEach(function(testCase) {
testCode += config.indent + format(
"equal(%s(%s), /* Expected value */);\n",
fn.name,
testCase
);
});
testCode += '});\n';
// Create directory if not exists
if (!fs.existsSync(fnDir))
mkdirp.sync(fnDir);
// Function's test case code path
var fnCodePath = path.join(fnDir, fn.file === undefined ? fn.name + '.js' : '' + fn.file);
// Check if the file exists
if (fs.existsSync(fnCodePath))
if (!overwrite) {
console.warn("WARNING: File '%s' already exists. Use '-o' too overwrite.", path.relative(process.cwd(), fnCodePath));
return;
}
fs.writeFileSync(fnCodePath, testCode);
});