1
1
var Stream = require ( 'stream' ) ;
2
+ var EventEmitter = require ( 'events' ) . EventEmitter ;
3
+ var inherits = require ( 'inherits' ) ;
2
4
var json = typeof JSON === 'object' ? JSON : require ( 'jsonify' ) ;
3
5
var through = require ( 'through' ) ;
6
+ var resumer = require ( 'resumer' ) ;
4
7
var nextTick = typeof setImmediate !== 'undefined'
5
8
? setImmediate
6
9
: process . nextTick
7
10
;
8
11
9
- module . exports = function ( ) {
10
- var output = through ( ) ;
11
- output . pause ( ) ;
12
- output . queue ( 'TAP version 13\n' ) ;
13
-
14
- var results = new Results ( output ) ;
15
- output . push = function ( t ) { results . push ( t ) } ;
16
-
17
- output . only = function ( name ) {
18
- results . only = name ;
19
- } ;
20
-
21
- nextTick ( function next ( ) {
22
- var t = getNextTest ( results ) ;
23
- if ( t ) t . run ( ) ;
24
- else results . close ( ) ;
25
- } ) ;
26
-
27
- return output ;
28
- } ;
12
+ module . exports = Results ;
13
+ inherits ( Results , EventEmitter ) ;
29
14
30
- function Results ( stream ) {
15
+ function Results ( ) {
16
+ if ( ! ( this instanceof Results ) ) return new Results ;
31
17
this . count = 0 ;
32
18
this . fail = 0 ;
33
19
this . pass = 0 ;
34
- this . stream = stream ;
20
+ this . _stream = through ( ) ;
35
21
this . tests = [ ] ;
36
22
}
37
23
24
+ Results . prototype . createStream = function ( ) {
25
+ var self = this ;
26
+ var output = resumer ( ) ;
27
+ output . queue ( 'TAP version 13\n' ) ;
28
+
29
+ nextTick ( function ( ) {
30
+ var t = getNextTest ( self ) ;
31
+ if ( t ) t . run ( )
32
+ else self . emit ( 'done' )
33
+ } ) ;
34
+ self . _stream . pipe ( output ) ;
35
+
36
+ return output ;
37
+ } ;
38
+
38
39
Results . prototype . push = function ( t ) {
39
40
var self = this ;
40
41
self . tests . push ( t ) ;
41
42
self . _watch ( t ) ;
42
43
t . once ( 'end' , function ( ) {
43
44
var nt = getNextTest ( self ) ;
44
- if ( nt ) nt . run ( ) ;
45
- else self . close ( ) ;
45
+ if ( nt ) nt . run ( )
46
+ else self . emit ( 'done' )
46
47
} ) ;
47
48
} ;
48
49
50
+ Results . prototype . only = function ( name ) {
51
+ if ( this . _only ) {
52
+ self . count ++ ;
53
+ self . fail ++ ;
54
+ write ( 'not ok ' + self . count + ' already called .only()\n' ) ;
55
+ }
56
+ this . _only = name ;
57
+ } ;
58
+
49
59
Results . prototype . _watch = function ( t ) {
50
60
var self = this ;
51
- var write = function ( s ) { self . stream . queue ( s ) } ;
61
+ var write = function ( s ) { self . _stream . queue ( s ) } ;
52
62
t . once ( 'prerun' , function ( ) {
53
63
write ( '# ' + t . name + '\n' ) ;
54
64
} ) ;
@@ -70,17 +80,17 @@ Results.prototype._watch = function (t) {
70
80
71
81
Results . prototype . close = function ( ) {
72
82
var self = this ;
73
- if ( self . closed ) self . stream . emit ( 'error' , new Error ( 'ALREADY CLOSED' ) ) ;
83
+ if ( self . closed ) self . _stream . emit ( 'error' , new Error ( 'ALREADY CLOSED' ) ) ;
74
84
self . closed = true ;
75
- var write = function ( s ) { self . stream . queue ( s ) } ;
85
+ var write = function ( s ) { self . _stream . queue ( s ) } ;
76
86
77
87
write ( '\n1..' + self . count + '\n' ) ;
78
88
write ( '# tests ' + self . count + '\n' ) ;
79
89
write ( '# pass ' + self . pass + '\n' ) ;
80
90
if ( self . fail ) write ( '# fail ' + self . fail + '\n' )
81
91
else write ( '\n# ok\n' )
82
92
83
- self . stream . queue ( null ) ;
93
+ self . _stream . queue ( null ) ;
84
94
} ;
85
95
86
96
function encodeResult ( res , count ) {
@@ -148,7 +158,7 @@ function getSerialize () {
148
158
}
149
159
150
160
function getNextTest ( results ) {
151
- if ( ! results . only ) {
161
+ if ( ! results . _only ) {
152
162
return results . tests . shift ( ) ;
153
163
}
154
164
@@ -157,7 +167,7 @@ function getNextTest(results) {
157
167
if ( ! t ) {
158
168
return null ;
159
169
}
160
- if ( results . only === t . name ) {
170
+ if ( results . _only === t . name ) {
161
171
return t ;
162
172
}
163
173
} while ( results . tests . length !== 0 )
0 commit comments