1
1
var fs = require ( 'fs' ) ;
2
2
var test = require ( 'tape' ) ;
3
+ var Transform = require ( 'stream' ) . Transform ;
3
4
var jsonmlify = require ( '../' ) ;
4
5
5
6
function verify ( markup , expected , t ) {
7
+ var result ;
6
8
jsonmlify ( )
7
9
. on ( 'data' , function ( data ) {
8
- t . deepEqual ( data , expected ) ;
10
+ result = data ;
11
+ } )
12
+ . on ( 'end' , function ( ) {
13
+ t . deepEqual ( result , expected ) ;
9
14
t . end ( ) ;
10
15
} )
11
16
. end ( markup ) ;
12
17
}
13
18
14
- test ( 'should return an empty array for empty input markup' , function ( t ) {
19
+ test ( 'should return a transform stream' , function ( t ) {
20
+ t . ok ( jsonmlify ( ) instanceof Transform ) ;
21
+ t . ok ( new jsonmlify ( ) instanceof Transform ) ;
22
+ t . end ( ) ;
23
+ } ) ;
24
+
25
+ test ( 'should not return anything in \'callback mode\'' , function ( t ) {
26
+ t . equal ( jsonmlify ( 'foo' , function ( ) { } ) , undefined ) ;
27
+ t . end ( ) ;
28
+ } ) ;
29
+
30
+ test ( 'should emit an empty array for empty input markup' , function ( t ) {
15
31
var markup = fs . readFileSync ( __dirname + '/01-empty/markup.html' , 'utf8' ) ;
16
32
var expected = require ( './01-empty/expected.json' ) ;
17
33
verify ( markup , expected , t ) ;
@@ -41,7 +57,7 @@ test('should parse comments', function(t) {
41
57
verify ( markup , expected , t ) ;
42
58
} ) ;
43
59
44
- test ( 'should also expose a node-style callback API' , function ( t ) {
60
+ test ( 'should also offer a node-style callback API' , function ( t ) {
45
61
jsonmlify ( '<div></div>' , function ( err , result ) {
46
62
t . notOk ( err ) ;
47
63
t . deepEqual ( result , [ [ 'div' ] ] ) ;
@@ -50,11 +66,18 @@ test('should also expose a node-style callback API', function(t) {
50
66
} ) ;
51
67
52
68
test ( 'should report errors' , function ( t ) {
69
+ var errorCount = 0 ;
53
70
var stream = jsonmlify ( ) ;
54
71
stream . on ( 'error' , function ( err ) {
72
+ errorCount ++ ;
55
73
t . ok ( err ) ;
56
- t . end ( ) ;
74
+ if ( errorCount === 2 ) {
75
+ t . end ( ) ;
76
+ }
57
77
} ) ;
58
78
stream . end ( ) ;
59
- stream . write ( '<div></div>' ) ; // Write after close to provoke an error
60
- } ) ;
79
+ // Write after end to provoke an error in the writable stream
80
+ stream . write ( '<div></div>' ) ;
81
+ // Write to HTML parser after end to provoke an internal error
82
+ stream . source . write ( '<div></div>' ) ;
83
+ } ) ;
0 commit comments