-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathJohnnyGenerator.js
77 lines (68 loc) · 3.18 KB
/
JohnnyGenerator.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
require('core-os');
const tests = require('./tests_scenarios.js');
const fs = require('fs');
const path = require('path');
Core.registerRequestPoint('JohnnyGenerator_GenerateRequest');
classes.JohnnyGenerator = {
checkGenerateCommand: function () {
var request = CatchRequest(Johnny_CommandRequest);
return function (success, fail) {
if (request.params[0] == 'generate') {
if (!request.params[1]) {
console.error('Please, specify the file to generate. Refer test_scenarios.js.');
} else {
(function parseTests(base, fpath) {
if (base instanceof Array) {
for (var i = 0; i < base.length; i++) {
var
matches = base[i].match(/^(unit|func|e2e)\s+([^:\s]+)\s*:\s*(.+)$/)
, type = matches[1]
, filename = matches[2] + '.' + type + '-test.js'
, description = matches[3];
if (filename == request.params[1]) {
if (fs.existsSync(filename)) {
console.error('The test file already exists.');
} else {
var dirname = path.dirname(filename);
if (!fs.statSync(dirname).isDirectory()) {
fs.mkdirSync(dirname);
}
FireRequest(
new JohnnyGenerator_GenerateRequest({
cases: description,
filename: filename
}),
function () {
},
function () {
}
)
}
}
}
} else {
Object.keys(base).map(function (key) {
parseTests(base[key], fpath.concat([key]))
})
}
})(tests, []);
}
success();
} else {
fail();
}
}
},
generateTests: function () {
var request = CatchRequest(JohnnyGenerator_GenerateRequest);
return function (success, fail) {
var cases = request.cases.split(',');
var its = cases.map(function (it) {
return '\nit("should ' + it.trimLeft() + '", function(done) {\n\n});';
});
fs.writeFileSync(request.filename, its.join('\n'));
console.log('File ' + request.filename + ' has been generated.');
success();
}
}
};