| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
var Duplex = require('stream').Duplex |
| 7 |
var util = require('util') |
| •8 |
var { Parser } = require('tap-parser') |
| 9 |
util.inherits(Formatter, Duplex) |
| 10 |
module.exports = Formatter |
| 11 |
|
| 12 |
function Formatter(options, parser, parent) { |
| 13 |
if (!(this instanceof Formatter)) |
| 14 |
return new Formatter(options, parser, parent) |
| 15 |
|
| 16 |
if (!parser) |
| 17 |
parser = new Parser() |
| 18 |
|
| 19 |
Duplex.call(this, options) |
| 20 |
this.child = null |
| 21 |
this.parent = parent || null |
| 22 |
this.level = parser.level |
| 23 |
this.parser = parser |
| 24 |
|
| 25 |
attachEvents(this, parser, options) |
| 26 |
|
| 27 |
if (options.init) |
| 28 |
options.init.call(this) |
| 29 |
} |
| 30 |
|
| 31 |
function attachEvents (self, parser, options) { |
| 32 |
var events = [ |
| 33 |
'version', 'plan', 'assert', 'comment', |
| 34 |
'complete', 'extra', 'bailout' |
| 35 |
] |
| 36 |
|
| 37 |
parser.on('child', function (childparser) { |
| 38 |
self.child = new Formatter(options, childparser, self) |
| 39 |
if (options.child) |
| 40 |
options.child.call(self, self.child) |
| 41 |
}) |
| 42 |
|
| 43 |
events.forEach(function (ev) { |
| 44 |
if (typeof options[ev] === 'function') |
| 45 |
parser.on(ev, options[ev].bind(self)) |
| 46 |
}) |
| 47 |
|
| 48 |
|
| 49 |
var streamEvents = [ |
| 50 |
'pipe', 'prefinish', 'finish', 'unpipe', 'close' |
| 51 |
] |
| 52 |
|
| 53 |
streamEvents.forEach(function (ev) { |
| 54 |
parser.on(ev, function () { |
| 55 |
var args = [ev] |
| 56 |
args.push.apply(args, arguments) |
| 57 |
self.emit.apply(self, args) |
| 58 |
}) |
| 59 |
}) |
| 60 |
} |
| 61 |
|
| 62 |
Formatter.prototype.write = function (c, e, cb) { |
| 63 |
return this.parser.write(c, e, cb) |
| 64 |
} |
| 65 |
|
| 66 |
Formatter.prototype.end = function (c, e, cb) { |
| 67 |
return this.parser.end(c, e, cb) |
| 68 |
} |
| 69 |
|
| 70 |
Formatter.prototype._read = function () {} |
| 71 |
|
| 72 |
|
| 73 |
Formatter.prototype.push = function (c) { |
| 74 |
if (this.parent) |
| 75 |
return this.parent.push(c) |
| 76 |
|
| 77 |
Duplex.prototype.push.call(this, c) |
| 78 |
} |