forked from json-quiz/json-quiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.js
151 lines (128 loc) · 3.79 KB
/
report.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
/**
* Custom reporter for Mocha, generating from the test suite
* a markdown spec to be rendered by jekyll.
*/
var fs = require('fs-extra');
var formatDir = __dirname + '/format';
var buildDir = __dirname + '/build';
var specDir = buildDir + '/_spec';
var dataDir = buildDir + '/_data';
var schemaDir = buildDir + '/schemas';
function makeMarkdownWrapper() {
var _level = 0;
function _getIndent() {
return Array.apply(null, new Array(_level - 3))
.map(function () { return ' '; })
.join('');
}
return {
setLevel: function (level) {
_level = level
},
fileName: function (suiteTitle) {
return suiteTitle.replace(' ', '-').toLowerCase() + '.md';
},
fileHeader: function (suiteTitle) {
return '---\n'
+ 'title: ' + suiteTitle + '\n'
+ 'layout: spec\n'
+ '---\n\n';
},
firstLevelTitle: function (title) {
return '# ' + title + '\n\n';
},
secondLevelTitle: function (title) {
return '## ' + title + '\n\n';
},
otherTitle: function (title) {
return _getIndent() + '* ' + title + ':\n\n';
},
spec: function (text) {
var matches = text.match(/#([a-zA-Z\-0-9_]+)#/);
if (matches) {
// make link to another schema
var schemaId = matches[1];
text = text.replace(
'#' + schemaId + '#',
'[' + schemaId + '](' + schemaId + '.html)'
);
}
return _getIndent() + '* ' + text + '\n\n';
},
example: function (title, content) {
// convert file name into plain text title
title = (title.charAt(0).toUpperCase() + title.slice(1))
.slice(0, -5)
.split('-')
.join(' ');
return '### ' + title + '\n\n'
+ '{% highlight json %}\n\n'
+ content + '\n\n'
+ '{% endhighlight %}\n\n';
}
};
}
function DocReporter(runner) {
var level = 0;
var output = '';
var suiteFile, parentSuite;
var wrapper = makeMarkdownWrapper();
runner.on('suite', function (suite) {
if (suite.root) {
return parentSuite = 'root';
}
wrapper.setLevel(++level);
if (level === 1) {
suiteFile = wrapper.fileName(suite.title);
output += wrapper.fileHeader(suite.title)
+ wrapper.firstLevelTitle(suite.title);
} else if (level === 2) {
output += wrapper.secondLevelTitle(suite.title);
} else {
output += wrapper.otherTitle(suite.title);
}
parentSuite = suite.title;
});
runner.on('suite end', function (suite) {
if (suite.root) {
return postReportActions();
}
wrapper.setLevel(--level);
if (level === 0) {
fs.mkdirsSync(specDir);
fs.writeFileSync(specDir + '/' + suiteFile, output);
output = '';
console.log('Doc file "%s" generated', suiteFile);
}
});
runner.on('pass', function (test) {
var exampleFile, exampleTitle, exampleContent;
wrapper.setLevel(++level);
if (parentSuite === 'Examples') {
exampleFile = __dirname + '/' + test.title;
exampleTitle = test.title.split('/').pop();
exampleContent = fs.readFileSync(exampleFile, { encoding: 'utf8' });
output += wrapper.example(exampleTitle, exampleContent);
} else {
output += wrapper.spec(test.title);
}
wrapper.setLevel(--level);
});
runner.on('fail', function (test, err) {
console.log('fail: %s -- error: %s', test.fullTitle(), err.message);
console.log('Aborting doc generation');
process.exit(1);
});
}
function postReportActions() {
fs.mkdirsSync(dataDir);
fs.copySync(formatDir + '/spec.yml', dataDir + '/spec.yml');
console.log('Spec plan copied');
fs.copySync(formatDir, schemaDir, {
filter: function (path) {
return path.indexOf('/examples') === -1;
}
});
console.log('Json schemas copied');
}
module.exports = DocReporter;