Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

index.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. var test = require('tape')
  2. var spec = require('stream-spec')
  3. var through = require('../')
  4. /*
  5. I'm using these two functions, and not streams and pipe
  6. so there is less to break. if this test fails it must be
  7. the implementation of _through_
  8. */
  9. function write(array, stream) {
  10. array = array.slice()
  11. function next() {
  12. while(array.length)
  13. if(stream.write(array.shift()) === false)
  14. return stream.once('drain', next)
  15. stream.end()
  16. }
  17. next()
  18. }
  19. function read(stream, callback) {
  20. var actual = []
  21. stream.on('data', function (data) {
  22. actual.push(data)
  23. })
  24. stream.once('end', function () {
  25. callback(null, actual)
  26. })
  27. stream.once('error', function (err) {
  28. callback(err)
  29. })
  30. }
  31. test('simple defaults', function(assert) {
  32. var l = 1000
  33. , expected = []
  34. while(l--) expected.push(l * Math.random())
  35. var t = through()
  36. var s = spec(t).through().pausable()
  37. read(t, function (err, actual) {
  38. assert.ifError(err)
  39. assert.deepEqual(actual, expected)
  40. assert.end()
  41. })
  42. t.on('close', s.validate)
  43. write(expected, t)
  44. });
  45. test('simple functions', function(assert) {
  46. var l = 1000
  47. , expected = []
  48. while(l--) expected.push(l * Math.random())
  49. var t = through(function (data) {
  50. this.emit('data', data*2)
  51. })
  52. var s = spec(t).through().pausable()
  53. read(t, function (err, actual) {
  54. assert.ifError(err)
  55. assert.deepEqual(actual, expected.map(function (data) {
  56. return data*2
  57. }))
  58. assert.end()
  59. })
  60. t.on('close', s.validate)
  61. write(expected, t)
  62. })
  63. test('pauses', function(assert) {
  64. var l = 1000
  65. , expected = []
  66. while(l--) expected.push(l) //Math.random())
  67. var t = through()
  68. var s = spec(t)
  69. .through()
  70. .pausable()
  71. t.on('data', function () {
  72. if(Math.random() > 0.1) return
  73. t.pause()
  74. process.nextTick(function () {
  75. t.resume()
  76. })
  77. })
  78. read(t, function (err, actual) {
  79. assert.ifError(err)
  80. assert.deepEqual(actual, expected)
  81. })
  82. t.on('close', function () {
  83. s.validate()
  84. assert.end()
  85. })
  86. write(expected, t)
  87. })
  88. test('does not soft-end on `undefined`', function(assert) {
  89. var stream = through()
  90. , count = 0
  91. stream.on('data', function (data) {
  92. count++
  93. })
  94. stream.write(undefined)
  95. stream.write(undefined)
  96. assert.equal(count, 2)
  97. assert.end()
  98. })