-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTestFile.js
244 lines (198 loc) · 6.27 KB
/
TestFile.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
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
'use strict';
/**
* Event Emitter of Node.JS
*
* @external EventEmitter
*
* @see {@link https://nodejs.org/dist/latest-v4.x/docs/api/events.html#events_class_eventemitter}
*/
const EventEmitter = require('events'),
Path = require('path'),
Beautify = require('js-beautify');
/**
* **Test script** class
*
* @extends external:EventEmitter
*/
class TestScript extends EventEmitter {
/**
* @author TechQuery <shiy007@qq.com>
*
* @param {string} URI - Path of a **Source module**
* @param {string} sourcePath - Path of the **Source directory**
* @param {string} testPath - Path of the **Test directory**
*/
constructor(URI, sourcePath, testPath) {
super().length = 0;
this.sourcePath = Path.join(process.cwd(), sourcePath);
this.testPath = Path.join(process.cwd(), testPath);
this.URI = (
Path.isAbsolute( URI ) ?
Path.relative(this.sourcePath, URI) : URI
).replace(/\\/g, '/');
this.testURI = Path.join(this.testPath, this.URI);
this.sourceURI = Path.relative(
this.testURI, Path.join(this.sourcePath, this.URI)
).replace(/\\/g, '/');
this.ID = TestScript.toBigCamel( Path.basename(URI, '.js') );
this.header = [ ];
}
static toBigCamel(raw) {
return raw.replace(/^\w|[_\-](\w)/g, function (A, B) {
return (B || A).toUpperCase();
});
}
addHeader(raw) {
if (raw && (this.header.indexOf( raw ) < 0))
this.header.push( raw );
}
/**
* Add Test unit
*
* @author TechQuery
* @since 0.2.0
*
* @param {object} Doclet
*
* @throws {SyntaxError} While the `title`, `script` or `expected` of
* an Example is missing
*/
addUnit(Doclet) {
if (! (Doclet.examples instanceof Array)) return;
var case_key = ['title', 'script', 'expected'];
this[ this.length++ ] = {
title: Doclet.longname,
item: Doclet.examples.map(function (item) {
item = item.match(
/^\/\/\s*(.+?)(?:\r|\n|\r\n){2,}([\s\S]+)\s+\/\/([\s\S]+)/
) || [ ];
/**
* **Test item** object
*
* @typedef {object} TestItem
*
* @property {string} title - **Comment text** in the
* same line of `@example`
* @property {string} script - **Source code** to be tested
* @property {string} expected - Expected result
* @property {string} [source=''] - Full source code of
* this test
*/
var test_case = {source: ''};
for (let i = 0; case_key[i]; i++)
if (! (
test_case[ case_key[i] ] = (item[i + 1] || '').trim()
.replace(/\r\n|\r/g, "\n")
))
throw SyntaxError(
`The ${case_key[i]} of ${Doclet.longname} is missing`
);
return test_case;
})
};
}
getHeader() {
/**
* **Test header** object
*
* @typedef {object} TestHeader
*
* @property {string} URI - Module URI relative to
* the **Source directory**
* @property {string} sourceURI - Module URI relative to
* this **Test scirpt**
* @property {string} [source=''] - The header's source code of
* this **Test script**
*/
var header = {
URI: this.URI,
sourceURI: this.sourceURI,
source: ''
};
/**
* Before the **Test script** writed
*
* @event TestScript#fileWrite
*/
this.emit('fileWrite', header);
if ( header.source ) this.addHeader( header.source );
/**
* Before the header of **Test script** writed
*
* @event TestScript#headerWrite
*/
header.source = '';
this.emit('headerWrite', header);
return header.source;
}
/**
* @author TechQuery <shiy007@qq.com>
* @since 0.2.0
*
* @return {string} Source code of this **Test script**
*/
toString() {
var _this_ = this, header = this.getHeader();
return Beautify(`'use strict';
require('should');
${this.header.join("\n\n")}
describe('${this.URI}', function () {
${header}
${Array.from(this, function (test) {
return `describe('${test.title}', function () {
${test.item.map(function (item) {
/**
* Before **Test item** writed
*
* @event TestScript#itemWrite
*/
_this_.emit('itemWrite', item);
return item.source || `it('${item.title}', function () {
var result = ${item.script};
result.should.be.deepEqual( ${item.expected} );
});`;
}).join('')}
});
`;
}).join("\n")}
});`
);
}
}
/**
* @callback fileWrite
*
* @param {TestHeader} header
*
* @example // TestHook.js
*
* exports.fileWrite = function (header) {
*
* header.source = 'custom test script';
* };
*/
/**
* @callback headerWrite
*
* @param {TestHeader} header
*
* @example // TestHook.js
*
* exports.headerWrite = function (header) {
*
* header.source = 'custom test script';
* };
*/
/**
* @callback itemWrite
*
* @param {TestItem} item
*
* @example // TestHook.js
*
* exports.itemWrite = function (item) {
*
* item.source = 'custom test script';
* };
*/
module.exports = TestScript;