@@ -22,6 +22,21 @@ var sys = require('sys'),
22
22
eyes = require ( 'eyes' ) . inspector ( { writer : null } ) ,
23
23
vows = exports ;
24
24
25
+ // Options
26
+ vows . options = {
27
+ Emitter : events . EventEmitter ,
28
+ brief : false ,
29
+ json : false ,
30
+ matcher : / .* / ,
31
+ printer : require ( 'vows/printers/console' )
32
+ } ;
33
+
34
+ vows . __defineGetter__ ( 'printer' , function ( ) {
35
+ return vows . options . printer ;
36
+ } ) ;
37
+
38
+ var stylize = require ( 'vows/printers/console' ) . stylize ;
39
+
25
40
// Keeps track of the outcome of vows.
26
41
var total = 0 , honored = 0 ,
27
42
broken = 0 , errored = 0 ,
@@ -36,6 +51,32 @@ var lastContext;
36
51
// Output buffer
37
52
var buffer ;
38
53
54
+ var argv = [ ] ;
55
+ //
56
+ // Parse command-line parameters
57
+ //
58
+ for ( var i = 0 , arg ; i < process . argv . length ; i ++ ) {
59
+ arg = process . argv [ i ] ;
60
+
61
+ if ( arg === __filename ) { continue }
62
+
63
+ if ( arg [ 0 ] !== '-' ) {
64
+ argv . push ( arg ) ;
65
+ } else {
66
+ arg = arg . match ( / ^ - - ? ( .* ) / ) [ 1 ] ;
67
+
68
+ if ( arg [ 0 ] === 'R' ) {
69
+ vows . options . matcher = new ( RegExp ) ( arg . slice ( 1 ) ) ;
70
+ } else if ( arg in vows . options ) {
71
+ vows . options [ arg ] = true ;
72
+ }
73
+ }
74
+ }
75
+
76
+ // Get rid of process runner
77
+ // ('node' in most cases)
78
+ argv = argv . slice ( 1 ) ;
79
+
39
80
//
40
81
// Assertion Macros
41
82
//
@@ -96,54 +137,55 @@ function addVow(/* description & callback */) {
96
137
args . unshift ( null ) ;
97
138
}
98
139
runTest ( args ) ;
140
+ tryFinish ( vows . remaining , vow . promise ) ;
99
141
100
142
} ) . addListener ( "error" , function ( err ) {
101
143
var exception ;
102
144
103
145
if ( vow . callback . length >= 2 ) {
104
146
runTest ( [ err ] ) ;
105
147
} else {
106
- exception = " * " + stylize ( 'The promise returned an error: ' +
107
- stylize ( err , 'bold' ) , 'red' ) ;
148
+ exception = { type : 'promise' , error : err } ;
108
149
errored ++ ;
109
- output ( '- ' + stylize ( vow . description , 'red' ) , exception + "\n" ) ;
150
+ output ( 'errored' , exception ) ;
110
151
}
152
+ tryFinish ( vows . remaining , vow . promise ) ;
111
153
} ) ;
112
154
113
155
function runTest ( args ) {
114
- var title = ' - ' , exception , topic , msg ;
156
+ var exception , topic , status ;
115
157
116
158
// Run the test, and try to catch `AssertionError`s and other exceptions;
117
159
// increment counters accordingly.
118
160
try {
119
161
vow . callback . apply ( vow . binding || null , args ) ;
120
- title += stylize ( vow . description , 'green' ) ;
162
+ output ( 'honored' , exception ) ;
121
163
honored ++ ;
122
164
} catch ( e ) {
123
165
if ( e . name && e . name . match ( / A s s e r t i o n E r r o r / ) ) {
124
- title += stylize ( vow . description , 'yellow' ) ;
125
- exception = ' ~ ' + e . toString ( ) ;
166
+ exception = e . toString ( ) ;
167
+ output ( 'broken' , exception ) ;
126
168
broken ++ ;
127
169
} else {
128
- title += stylize ( vow . description , 'red' ) ;
129
- msg = e . stack || e . message || e . toString ( ) || e ;
130
- exception = ' ! ' + stylize ( msg , 'red' ) ;
170
+ exception = e . stack || e . message || e . toString ( ) || e ;
131
171
errored ++ ;
172
+ output ( 'errored' , exception ) ;
132
173
}
133
174
}
134
- output ( title , exception ) ;
135
175
}
136
176
137
- function output ( title , exception ) {
177
+ function output ( status , exception ) {
138
178
if ( exception || ! vows . options . brief ) {
139
179
if ( vow . context && lastContext !== vow . context ) {
140
180
lastContext = vow . context ;
141
- puts ( vow . context ) ;
181
+ vows . printer . print ( [ 'context' , vow . context ] ) ;
142
182
}
143
- puts ( title ) ;
144
- if ( exception ) puts ( exception ) ;
183
+ vows . printer . print ( [ 'vow' , {
184
+ title : vow . description ,
185
+ status : status ,
186
+ exception : exception || null
187
+ } ] ) ;
145
188
}
146
- tryFinish ( vows . remaining , vow . promise ) ;
147
189
}
148
190
} ;
149
191
@@ -287,49 +329,40 @@ function addVows(tests) {
287
329
return promise ;
288
330
}
289
331
290
- function puts ( ) {
291
- var args = Array . prototype . slice . call ( arguments ) ;
292
- if ( vows . promises . length && vows . promises [ suites - 1 ] . listeners ( 'finish' ) . length > 0 ) {
293
- buffer . push ( args . join ( '\n' ) ) ;
294
- } else {
295
- sys . puts . apply ( null , args ) ;
296
- }
297
- }
298
-
299
332
//
300
333
// Checks if all the required tests have been run,
301
334
// and either triggers the next test suite, if any,
302
335
// or exits the process.
303
336
//
304
337
function tryFinish ( remaining , promise ) {
305
- var result , style ;
338
+ var result , style , time ;
306
339
307
340
// Output results once all the vows have been checked
308
341
if ( honored + broken + errored === total && remaining === 0 ) {
309
- result = honored + " honored, " +
310
- broken + " broken, " +
311
- errored + " errored" ,
312
- style = honored === total ? ( 'green' )
313
- : ( errored === 0 ? 'yellow' : 'red' ) ;
314
-
315
342
// If this isn't the last test suite in the chain,
316
343
// emit 'end', to trigger the next test suite.
317
344
if ( promise && promise . listeners ( 'end' ) . length > 0 ) {
318
- sys . print ( '\n' ) ;
345
+ if ( vows . options . json ) {
346
+ puts ( [ 'end' ] ) ;
347
+ } else {
348
+ sys . print ( '\n' ) ;
349
+ }
319
350
promise . emit ( 'end' , honored , broken , errored ) ;
320
351
} else {
321
- if ( ! vows . options . brief ) {
322
- puts ( "\nVerified " + total + " vows in " +
323
- ( ( ( new ( Date ) ) - start ) / 1000 ) + " seconds." ) ;
324
- puts ( "\n" + stylize ( result , style ) ) ;
325
- }
326
-
352
+ time = ( new ( Date ) - start ) / 1000 ;
327
353
// The 'finish' event is triggered once all the tests have been run.
328
354
// It's used by bin/vows
329
355
vows . promises [ suites - 1 ] . emit ( "finish" , honored , broken , errored ) ;
330
356
331
- if ( ( broken || errored ) && buffer . length ) { sys . puts ( buffer . join ( '\n' ) + '\n' ) }
357
+ vows . printer . print ( [ 'finish' , {
358
+ honored : honored ,
359
+ broken : broken ,
360
+ errored : errored ,
361
+ total : total ,
362
+ time : time
363
+ } ] ) ;
332
364
365
+ if ( ( broken || errored ) && buffer . length ) { sys . puts ( buffer . join ( '\n' ) + '\n' ) }
333
366
// Don't exit until stdout is empty
334
367
process . stdout . addListener ( 'drain' , function ( ) {
335
368
process . exit ( broken || errored ? 1 : 0 ) ;
@@ -344,7 +377,7 @@ function tryFinish(remaining, promise) {
344
377
//
345
378
process . addListener ( 'exit' , function ( ) {
346
379
if ( honored + broken + errored < total ) {
347
- sys . puts ( '\n* ' + stylize ( "An EventEmitter has failed to fire." , 'red' ) ) ;
380
+ vows . printer . print ( [ 'error' , { error : "An EventEmitter has failed to fire." , type : 'promise' } ] ) ;
348
381
}
349
382
} ) ;
350
383
@@ -375,13 +408,6 @@ vows.prepare = function (obj, targets) {
375
408
return obj ;
376
409
} ;
377
410
378
- // Options
379
- vows . options = {
380
- Emitter : events . EventEmitter ,
381
- brief : false ,
382
- matcher : / .* /
383
- } ;
384
-
385
411
// Run all vows/tests.
386
412
// It can take either a function as `tests`,
387
413
// or an object literal.
@@ -397,7 +423,7 @@ vows.describe = function (subject) {
397
423
buffer = [ ] , suites = 0 ;
398
424
399
425
if ( ! vows . options . brief ) {
400
- puts ( '\n' + stylize ( subject , 'underline' ) + '\n' ) ;
426
+ this . printer . print ( [ ' subject' , subject ] ) ;
401
427
}
402
428
403
429
return new ( events . EventEmitter ) ( ) . addListener ( 'newListener' , function ( e , listener ) {
@@ -412,8 +438,6 @@ vows.describe = function (subject) {
412
438
} ) ;
413
439
} ;
414
440
415
- vows . tell = vows . describe ;
416
-
417
441
// Return the `vows` object after setting some options
418
442
vows . config = function ( opts ) {
419
443
for ( var k in opts ) { this . options [ k ] = opts [ k ] }
@@ -428,17 +452,3 @@ function inspect(val) {
428
452
return '\033[1m' + eyes ( val ) + '\033[22m' ;
429
453
}
430
454
431
- // Stylize a string
432
- function stylize ( str , style ) {
433
- var styles = {
434
- 'bold' : [ 1 , 22 ] ,
435
- 'underline' : [ 4 , 24 ] ,
436
- 'yellow' : [ 33 , 39 ] ,
437
- 'green' : [ 32 , 39 ] ,
438
- 'red' : [ 31 , 39 ] ,
439
- 'grey' : [ 90 , 39 ]
440
- } ;
441
- return '\033[' + styles [ style ] [ 0 ] + 'm' + str +
442
- '\033[' + styles [ style ] [ 1 ] + 'm' ;
443
- }
444
-
0 commit comments