test.js 2.0 KB

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