11var createDefaultStream = require ( './lib/default_stream' ) ;
22var Render = require ( './lib/render' ) ;
33var Test = require ( './lib/test' ) ;
4+ var through = require ( 'through' ) ;
45
56var canEmitExit = typeof process !== 'undefined' && process
67 && typeof process . on === 'function'
78;
89var canExit = typeof process !== 'undefined' && process
910 && typeof process . exit === 'function'
1011;
11- var onexit = ( function ( ) {
12- var stack = [ ] ;
13- if ( canEmitExit ) process . on ( 'exit' , function ( code ) {
14- for ( var i = 0 ; i < stack . length ; i ++ ) stack [ i ] ( code ) ;
15- } ) ;
16- return function ( cb ) { stack . push ( cb ) } ;
17- } ) ( ) ;
1812
1913var nextTick = typeof setImmediate !== 'undefined'
2014 ? setImmediate
@@ -28,138 +22,32 @@ exports.Test = Test;
2822var exitInterval ;
2923
3024function createHarness ( conf_ ) {
31- var pending = [ ] ;
32- var running = false ;
3325 var count = 0 ;
34-
35- var began = false ;
36- var only = false ;
37- var closed = false ;
38- var out = new Render ( ) ;
39- if ( ! conf_ ) conf_ = { } ;
40-
41- var tests = [ ] ;
42- if ( conf_ . exit === false && exitInterval ) clearInterval ( exitInterval ) ;
43-
44- exitInterval = ! exitInterval && conf_ . exit !== false && canEmitExit
45- && typeof process . _getActiveHandles === 'function'
46- && setInterval ( function ( ) {
47- if ( / ^ v 0 \. 8 \. / . test ( process . version )
48- && process . _getActiveHandles ( ) . length === 1 ) {
49- tests . forEach ( function ( t ) { t . _exit ( ) } ) ;
50- }
51- } , 200 ) ;
52-
5326 var exitCode = 0 ;
54- var exit = function ( c ) { exitCode = c } ;
55-
56- out . on ( 'end' , function ( ) {
57- nextTick ( function ( ) {
58- clearInterval ( exitInterval ) ;
59- if ( canExit && conf_ . exit !== false ) process . exit ( exitCode ) ;
60- } ) ;
27+ var output = through ( null , function ( ) {
28+ if ( -- count === 0 && ! closed ) {
29+ closed = true
30+ out . close ( ) ;
31+ }
6132 } ) ;
33+ output . pause ( ) ;
34+ nextTick ( function ( ) { output . resume ( ) } ) ;
6235
6336 var test = function ( name , conf , cb ) {
6437 count ++ ;
6538 var t = new Test ( name , conf , cb ) ;
66- tests . push ( t ) ;
67- if ( ! conf || typeof conf !== 'object' ) conf = conf_ ;
68-
69- if ( conf . exit !== false ) {
70- onexit ( function ( code ) {
71- t . _exit ( ) ;
72- if ( ! closed ) {
73- closed = true
74- out . close ( ) ;
75- }
76- if ( ! code && ! t . _ok && ( ! only || name === only ) ) {
77- exit ( 1 ) ;
78- }
79- } ) ;
80- }
81-
82- nextTick ( function ( ) {
83- if ( ! out . piped ) out . pipe ( createDefaultStream ( ) ) ;
84- if ( ! began ) out . begin ( ) ;
85- began = true ;
86-
87- var run = function ( ) {
88- running = true ;
89- out . push ( t ) ;
90- t . run ( ) ;
91- } ;
92-
93- if ( only && name !== only ) {
94- count -- ;
95- return ;
96- }
97-
98- if ( running || pending . length ) {
99- pending . push ( run ) ;
100- }
101- else run ( ) ;
102- } ) ;
103-
10439 t . on ( 'test' , function sub ( st ) {
105- count ++ ;
106- st . on ( 'test' , sub ) ;
107- st . on ( 'end' , onend ) ;
40+ console . log ( 'SUBTEST' ) ;
41+ } ) ;
42+ t . on ( 'result' , function ( r ) {
43+ console . dir ( r ) ;
44+ if ( ! r . ok ) exitCode = 1
45+ } ) ;
46+ nextTick ( function ( ) {
47+ t . run ( ) ;
10848 } ) ;
109- t . on ( 'result' , function ( r ) { if ( ! r . ok ) exitCode = 1 } ) ;
110-
111- t . on ( 'end' , onend ) ;
112-
11349 return t ;
114-
115- function onend ( ) {
116- count -- ;
117- if ( this . _progeny . length ) {
118- var unshifts = map ( this . _progeny , function ( st ) {
119- return function ( ) {
120- running = true ;
121- out . push ( st ) ;
122- st . run ( ) ;
123- } ;
124- } ) ;
125- pending . unshift . apply ( pending , unshifts ) ;
126- }
127-
128- process . nextTick ( function ( ) {
129- running = false ;
130- if ( pending . length ) return pending . shift ( ) ( ) ;
131- if ( count === 0 && ! closed ) {
132- closed = true
133- out . close ( ) ;
134- }
135- if ( conf . exit !== false && canExit && ! t . _ok ) {
136- exit ( 1 ) ;
137- }
138- } ) ;
139- }
14050 } ;
14151
142- test . only = function ( name ) {
143- if ( only ) {
144- throw new Error ( "there can only be one only test" ) ;
145- }
146-
147- only = name ;
148-
149- return test . apply ( null , arguments ) ;
150- } ;
151-
152- test . stream = out ;
15352 return test ;
15453}
155-
156- function map ( xs , f ) {
157- if ( xs . map ) return xs . map ( f ) ;
158- var res = [ ] ;
159- for ( var i = 0 ; i < xs . length ; i ++ ) {
160- res . push ( f ( xs [ i ] ) ) ;
161- }
162- return res ;
163- }
164-
165- // vim: set softtabstop=4 shiftwidth=4:
0 commit comments