Projekt

Obecné

Profil

Stáhnout (2.45 KB) Statistiky
| Větev: | Revize:
1
var tape = require('tape')
2
var through = require('through2')
3
var ndjson = require('ndjson')
4
var each = require('./')
5

    
6
tape('each', function (t) {
7
  var s = through.obj()
8
  s.write('a')
9
  s.write('b')
10
  s.write('c')
11
  s.end()
12

    
13
  s.on('end', function () {
14
    t.end()
15
  })
16

    
17
  var expected = ['a', 'b', 'c']
18
  each(s, function (data, next) {
19
    t.same(data, expected.shift())
20
    next()
21
  })
22
})
23

    
24
tape('each and callback', function (t) {
25
  var s = through.obj()
26
  s.write('a')
27
  s.write('b')
28
  s.write('c')
29
  s.end()
30

    
31
  var expected = ['a', 'b', 'c']
32
  each(s, function (data, next) {
33
    t.same(data, expected.shift())
34
    next()
35
  }, function () {
36
    t.end()
37
  })
38
})
39

    
40
tape('each (write after)', function (t) {
41
  var s = through.obj()
42
  s.on('end', function () {
43
    t.end()
44
  })
45

    
46
  var expected = ['a', 'b', 'c']
47
  each(s, function (data, next) {
48
    t.same(data, expected.shift())
49
    next()
50
  })
51

    
52
  setTimeout(function () {
53
    s.write('a')
54
    s.write('b')
55
    s.write('c')
56
    s.end()
57
  }, 100)
58
})
59

    
60
tape('each error', function (t) {
61
  var s = through.obj()
62
  s.write('hello')
63
  s.on('error', function (err) {
64
    t.same(err.message, 'stop')
65
    t.end()
66
  })
67

    
68
  each(s, function (data, next) {
69
    next(new Error('stop'))
70
  })
71
})
72

    
73
tape('each error and callback', function (t) {
74
  var s = through.obj()
75
  s.write('hello')
76

    
77
  each(s, function (data, next) {
78
    next(new Error('stop'))
79
  }, function (err) {
80
    t.same(err.message, 'stop')
81
    t.end()
82
  })
83
})
84

    
85
tape('each with falsey values', function (t) {
86
  var s = through.obj()
87
  s.write(0)
88
  s.write(false)
89
  s.write(undefined)
90
  s.end()
91

    
92
  s.on('end', function () {
93
    t.end()
94
  })
95

    
96
  var expected = [0, false]
97
  var count = 0
98
  each(s, function (data, next) {
99
    count++
100
    t.same(data, expected.shift())
101
    next()
102
  }, function () {
103
    t.same(count, 2)
104
  })
105
})
106

    
107
tape('huge stack', function (t) {
108
  var s = through.obj()
109

    
110
  for (var i = 0; i < 5000; i++) {
111
    s.write('foo')
112
  }
113

    
114
  s.end()
115

    
116
  each(s, function (data, cb) {
117
    if (data !== 'foo') t.fail('bad data')
118
    cb()
119
  }, function (err) {
120
    t.error(err, 'no error')
121
    t.end()
122
  })
123
})
124

    
125
tape('cb only once', function (t) {
126
  var p = ndjson.parse()
127
  var once = true
128
  var data = '{"foo":"' + Array(1000).join('x') + '"}\n'
129

    
130
  each(p, ondata, function (err) {
131
    t.ok(once, 'only once')
132
    t.ok(err, 'had error')
133
    once = false
134
    t.end()
135
  })
136

    
137
  for (var i = 0; i < 1000; i++) p.write(data)
138
  p.write('{...}\n')
139

    
140
  function ondata (data, cb) {
141
    process.nextTick(cb)
142
  }
143
})
(7-7/7)