-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-util.js
38 lines (35 loc) · 1.2 KB
/
test-util.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
// @param count
// - if count is an array then its length is the expected count and each value
// is what the data should be. If the array value is a buffer then it will
// be checked against the data chunk directly, otherwise the data chunk
// will be toString()'d before being checked against the array value
//
// - if count is a number then data content is ignored and only the correct
// number of data events is checked
exports.shouldStreamDataTimes = function shouldStreamDataTimes(stream, count, cb) {
var expectedValues = null
if (Array.isArray(count)) {
expectedValues = count
count = count.length
}
var outputCount = 0
stream.on('data', function(data) {
if (expectedValues) {
var expectedValue = expectedValues[outputCount]
if (!Buffer.isBuffer(expectedValue)) data = data.toString()
data.should.equal(expectedValue)
}
outputCount++
})
stream.on('end', function() {
outputCount.should.equal(count)
if (typeof cb === 'function') cb()
})
}
exports.execEachOnOwnTick = function execEachOnOwnTick(fn, fnArgs, thisBinding) {
fnArgs.forEach(function(fnArg) {
process.nextTick(function(){
fn.apply(thisBinding, fnArg)
})
})
}