/
readable.js
86 lines (75 loc) · 1.58 KB
/
readable.js
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
79
80
81
82
83
84
var Stream = require('stream')
var spec = require('..')
var tests = 0
function pass(message) {
console.log('ok ' + ++tests, message ? ' -- ' + message : '')
}
function fail(message) {
console.log('not ok ' + ++tests, message ? ' -- ' + message : '')
}
function checkValid(contract, expectFail) {
return function (create, test) {
var stream = create()
try {
var validate = contract(stream, spec).validate
test(stream)
validate()
} catch (err) {
if(!expectFail) {
fail(err.message); throw err
}
return pass(err.message)
}
if(expectFail) {
throw new Error('expected error')
}
pass('valid')
}
}
var valid = checkValid(function (stream, spec) {
return spec(stream).readable()
}, false)
var invalid = checkValid(function (stream, spec) {
return spec(stream).readable()
}, true)
//does not emit 'end'
invalid(function () {
var s = new Stream()
return s
}, function (s) {
})
//does not set readable = false, on 'end'
invalid(function () {
var s = new Stream()
s.destroy = function () {}
s._end = function () {
this.emit('end')
}
return s
}, function (s) {
s._end()
})
//does not set emit 'close'
invalid(function () {
var s = new Stream()
s.destroy = function () {}
s._end = function () {
this.readable = false
this.emit('end')
}
return s
}, function (s) {
s._end()
})
valid(function () {
var s = new Stream()
s.destroy = function () {}
s._end = function () {
this.readable = false
this.emit('end')
this.emit('close')
}
return s
}, function (s) {
s._end()
})