50496fa3b54d7b7570ff2758ad9fdabfcb4d9b19.svn-base
1.86 KB
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
// A formatter is a Duplex stream that TAP data is written into,
// and then something else (presumably not-TAP) is read from.
//
// See tap-classic.js for an example of a formatter in use.
var Duplex = require('stream').Duplex
var util = require('util')
var Parser = require('tap-parser')
util.inherits(Formatter, Duplex)
module.exports = Formatter
function Formatter(options, parser, parent) {
if (!(this instanceof Formatter))
return new Formatter(options, parser, parent)
if (!parser)
parser = new Parser()
Duplex.call(this, options)
this.child = null
this.parent = parent || null
this.level = parser.level
this.parser = parser
attachEvents(this, parser, options)
if (options.init)
options.init.call(this)
}
function attachEvents (self, parser, options) {
var events = [
'version', 'plan', 'assert', 'comment',
'complete', 'extra', 'bailout'
]
parser.on('child', function (childparser) {
self.child = new Formatter(options, childparser, self)
if (options.child)
options.child.call(self, self.child)
})
events.forEach(function (ev) {
if (typeof options[ev] === 'function')
parser.on(ev, options[ev].bind(self))
})
// proxy all stream events directly
var streamEvents = [
'pipe', 'prefinish', 'finish', 'unpipe', 'close'
]
streamEvents.forEach(function (ev) {
parser.on(ev, function () {
var args = [ev]
args.push.apply(args, arguments)
self.emit.apply(self, args)
})
})
}
Formatter.prototype.write = function (c, e, cb) {
return this.parser.write(c, e, cb)
}
Formatter.prototype.end = function (c, e, cb) {
return this.parser.end(c, e, cb)
}
Formatter.prototype._read = function () {}
// child formatters always push data to the root obj
Formatter.prototype.push = function (c) {
if (this.parent)
return this.parent.push(c)
Duplex.prototype.push.call(this, c)
}