heyujie
2021-05-24 4885600ecc369aa2e30a65de8dd7a410f13c34df
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
'use strict'
 
var test = require('tap').test
var callback = require('./')
var fs = require('fs')
 
test('call the callback after end with object mode', function (t) {
  var opts = { objectMode: true }
  var stream = callback(opts, function (err, results) {
    t.deepEqual(results, ['hello'], 'should return the ending value')
    t.end()
  })
 
  stream.end('hello')
})
 
test('support multiple writes with object mode', function (t) {
  var opts = { objectMode: true }
  var stream = callback(opts, function (err, results) {
    t.deepEqual(results, ['hello', 'world'], 'should return the ending value')
    t.end()
  })
 
  stream.write('hello')
  stream.end('world')
})
 
test('works without object mode', function (t) {
  var stream = callback(function (err, results) {
    t.equal(results.length, 1, 'should contain only one value')
    t.deepEqual(results[0].toString(), 'world', 'should return the ending value')
    t.end()
  })
 
  stream.end('world')
})
 
test('is pipeable', function (t) {
  var write = callback(function (err, results) {
    var actual = Buffer.concat(results).toString()
    var expected = fs.readFileSync('README.md').toString()
    t.equal(actual, expected, 'should have the same content of the file')
    t.end()
  })
  var read = fs.createReadStream('README.md')
 
  read.pipe(write)
})
 
test('callback.obj shortcut for objectMode', function (t) {
  var stream = callback.obj(function (err, results) {
    t.deepEqual(results, ['hello'], 'should return the ending value')
    t.end()
  })
 
  stream.end('hello')
})