選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

test.js 2.4KB

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