• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var tape = require('tape')
2var through = require('through2')
3var each = require('./')
4
5tape('each', function (t) {
6  var s = through.obj()
7  s.write('a')
8  s.write('b')
9  s.write('c')
10  s.end()
11
12  s.on('end', function () {
13    t.end()
14  })
15
16  var expected = ['a', 'b', 'c']
17  each(s, function (data, next) {
18    t.same(data, expected.shift())
19    next()
20  })
21})
22
23tape('each and callback', function (t) {
24  var s = through.obj()
25  s.write('a')
26  s.write('b')
27  s.write('c')
28  s.end()
29
30  var expected = ['a', 'b', 'c']
31  each(s, function (data, next) {
32    t.same(data, expected.shift())
33    next()
34  }, function () {
35    t.end()
36  })
37})
38
39tape('each (write after)', function (t) {
40  var s = through.obj()
41  s.on('end', function () {
42    t.end()
43  })
44
45  var expected = ['a', 'b', 'c']
46  each(s, function (data, next) {
47    t.same(data, expected.shift())
48    next()
49  })
50
51  setTimeout(function () {
52    s.write('a')
53    s.write('b')
54    s.write('c')
55    s.end()
56  }, 100)
57})
58
59tape('each error', function (t) {
60  var s = through.obj()
61  s.write('hello')
62  s.on('error', function (err) {
63    t.same(err.message, 'stop')
64    t.end()
65  })
66
67  each(s, function (data, next) {
68    next(new Error('stop'))
69  })
70})
71
72tape('each error and callback', function (t) {
73  var s = through.obj()
74  s.write('hello')
75
76  each(s, function (data, next) {
77    next(new Error('stop'))
78  }, function (err) {
79    t.same(err.message, 'stop')
80    t.end()
81  })
82})
83
84tape('each with falsey values', function (t) {
85  var s = through.obj()
86  s.write(0)
87  s.write(false)
88  s.write(undefined)
89  s.end()
90
91  s.on('end', function () {
92    t.end()
93  })
94
95  var expected = [0, false]
96  var count = 0
97  each(s, function (data, next) {
98    count++
99    t.same(data, expected.shift())
100    next()
101  }, function () {
102    t.same(count, 2)
103  })
104})
105
106tape('huge stack', function (t) {
107  var s = through.obj()
108
109  for (var i = 0; i < 5000; i++) {
110    s.write('foo')
111  }
112
113  s.end()
114
115  each(s, function (data, cb) {
116    if (data !== 'foo') t.fail('bad data')
117    cb()
118  }, function (err) {
119    t.error(err, 'no error')
120    t.end()
121  })
122})
123