1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var util = require('util');
|
10 | var Base = require('./base');
|
11 | var constants = require('../runner').constants;
|
12 | var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
|
13 | var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
|
14 | var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN;
|
15 | var EVENT_RUN_END = constants.EVENT_RUN_END;
|
16 | var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
|
17 | var EVENT_TEST_END = constants.EVENT_TEST_END;
|
18 | var inherits = require('../utils').inherits;
|
19 | var sprintf = util.format;
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | exports = module.exports = TAP;
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function TAP(runner, options) {
|
38 | Base.call(this, runner, options);
|
39 |
|
40 | var self = this;
|
41 | var n = 1;
|
42 |
|
43 | var tapVersion = '12';
|
44 | if (options && options.reporterOptions) {
|
45 | if (options.reporterOptions.tapVersion) {
|
46 | tapVersion = options.reporterOptions.tapVersion.toString();
|
47 | }
|
48 | }
|
49 |
|
50 | this._producer = createProducer(tapVersion);
|
51 |
|
52 | runner.once(EVENT_RUN_BEGIN, function() {
|
53 | self._producer.writeVersion();
|
54 | });
|
55 |
|
56 | runner.on(EVENT_TEST_END, function() {
|
57 | ++n;
|
58 | });
|
59 |
|
60 | runner.on(EVENT_TEST_PENDING, function(test) {
|
61 | self._producer.writePending(n, test);
|
62 | });
|
63 |
|
64 | runner.on(EVENT_TEST_PASS, function(test) {
|
65 | self._producer.writePass(n, test);
|
66 | });
|
67 |
|
68 | runner.on(EVENT_TEST_FAIL, function(test, err) {
|
69 | self._producer.writeFail(n, test, err);
|
70 | });
|
71 |
|
72 | runner.once(EVENT_RUN_END, function() {
|
73 | self._producer.writeEpilogue(runner.stats);
|
74 | });
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | inherits(TAP, Base);
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | function title(test) {
|
90 | return test.fullTitle().replace(/#/g, '');
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | function println(format, varArgs) {
|
101 | var vargs = Array.from(arguments);
|
102 | vargs[0] += '\n';
|
103 | process.stdout.write(sprintf.apply(null, vargs));
|
104 | }
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | function createProducer(tapVersion) {
|
115 | var producers = {
|
116 | '12': new TAP12Producer(),
|
117 | '13': new TAP13Producer()
|
118 | };
|
119 | var producer = producers[tapVersion];
|
120 |
|
121 | if (!producer) {
|
122 | throw new Error(
|
123 | 'invalid or unsupported TAP version: ' + JSON.stringify(tapVersion)
|
124 | );
|
125 | }
|
126 |
|
127 | return producer;
|
128 | }
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | function TAPProducer() {}
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | TAPProducer.prototype.writeVersion = function() {};
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | TAPProducer.prototype.writePlan = function(ntests) {
|
156 | println('%d..%d', 1, ntests);
|
157 | };
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | TAPProducer.prototype.writePass = function(n, test) {
|
167 | println('ok %d %s', n, title(test));
|
168 | };
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | TAPProducer.prototype.writePending = function(n, test) {
|
178 | println('ok %d %s # SKIP -', n, title(test));
|
179 | };
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | TAPProducer.prototype.writeFail = function(n, test, err) {
|
190 | println('not ok %d %s', n, title(test));
|
191 | };
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 | TAPProducer.prototype.writeEpilogue = function(stats) {
|
200 |
|
201 | println('# tests ' + (stats.passes + stats.failures));
|
202 | println('# pass ' + stats.passes);
|
203 |
|
204 | println('# fail ' + stats.failures);
|
205 | this.writePlan(stats.passes + stats.failures + stats.pending);
|
206 | };
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | function TAP12Producer() {
|
221 | |
222 |
|
223 |
|
224 |
|
225 | this.writeFail = function(n, test, err) {
|
226 | TAPProducer.prototype.writeFail.call(this, n, test, err);
|
227 | if (err.message) {
|
228 | println(err.message.replace(/^/gm, ' '));
|
229 | }
|
230 | if (err.stack) {
|
231 | println(err.stack.replace(/^/gm, ' '));
|
232 | }
|
233 | };
|
234 | }
|
235 |
|
236 |
|
237 |
|
238 |
|
239 | inherits(TAP12Producer, TAPProducer);
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 | function TAP13Producer() {
|
254 | |
255 |
|
256 |
|
257 |
|
258 | this.writeVersion = function() {
|
259 | println('TAP version 13');
|
260 | };
|
261 |
|
262 | |
263 |
|
264 |
|
265 |
|
266 | this.writeFail = function(n, test, err) {
|
267 | TAPProducer.prototype.writeFail.call(this, n, test, err);
|
268 | var emitYamlBlock = err.message != null || err.stack != null;
|
269 | if (emitYamlBlock) {
|
270 | println(indent(1) + '---');
|
271 | if (err.message) {
|
272 | println(indent(2) + 'message: |-');
|
273 | println(err.message.replace(/^/gm, indent(3)));
|
274 | }
|
275 | if (err.stack) {
|
276 | println(indent(2) + 'stack: |-');
|
277 | println(err.stack.replace(/^/gm, indent(3)));
|
278 | }
|
279 | println(indent(1) + '...');
|
280 | }
|
281 | };
|
282 |
|
283 | function indent(level) {
|
284 | return Array(level + 1).join(' ');
|
285 | }
|
286 | }
|
287 |
|
288 |
|
289 |
|
290 |
|
291 | inherits(TAP13Producer, TAPProducer);
|
292 |
|
293 | TAP.description = 'TAP-compatible output';
|