• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var tape = require('tape')
2var writer = require('./')
3
4tape('is a write stream', function (t) {
5  var expected = ['hello', 'world', 'verden']
6  var ws = writer.obj(write)
7
8  ws.write('hello')
9  ws.write('world')
10  ws.write('verden')
11  ws.end(function () {
12    t.same(expected.length, 0)
13    t.end()
14  })
15
16  function write (data, enc, cb) {
17    t.same(data, expected.shift())
18    cb()
19  }
20})
21
22tape('is flushable', function (t) {
23  var expected = ['hello', 'world', 'verden']
24  var flushed = false
25
26  var ws = writer.obj(write, flush)
27
28  ws.write('hello')
29  ws.write('world')
30  ws.write('verden')
31  ws.end(function () {
32    t.same(expected.length, 0)
33    t.ok(flushed, 'was flushed')
34    t.end()
35  })
36
37  function write (data, enc, cb) {
38    t.same(data, expected.shift())
39    cb()
40  }
41
42  function flush (cb) {
43    flushed = true
44    process.nextTick(cb)
45  }
46})
47
48tape('can pass options', function (t) {
49  var expected = ['hello', 'world', 'verden']
50  var flushed = false
51
52  var ws = writer({objectMode: true}, write, flush)
53
54  ws.write('hello')
55  ws.write('world')
56  ws.write('verden')
57  ws.end(function () {
58    t.same(expected.length, 0)
59    t.ok(flushed, 'was flushed')
60    t.end()
61  })
62
63  function write (data, enc, cb) {
64    t.same(data, expected.shift())
65    cb()
66  }
67
68  function flush (cb) {
69    flushed = true
70    process.nextTick(cb)
71  }
72})
73
74tape('emits error on destroy', function (t) {
75  var expected = new Error()
76
77  var ws = writer({objectMode: true}, function () {})
78
79  ws.on('error', function (err) {
80    t.equal(err, expected)
81  })
82  ws.on('close', t.end)
83
84  ws.destroy(expected)
85})
86