test.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. var test = require('tape')
  2. var path = require('path')
  3. var from = require('./')
  4. var fs = require('fs')
  5. var tmp = path.resolve(
  6. __dirname, 'tmp.txt'
  7. )
  8. function fromString(string) {
  9. return from(function(size, next) {
  10. if (string.length <= 0) return next(null, null)
  11. var chunk = string.slice(0, size)
  12. string = string.slice(size)
  13. next(null, chunk)
  14. })
  15. }
  16. test('from2', function(t) {
  17. var contents = fs.readFileSync(__filename, 'utf8')
  18. var stream = fromString(contents)
  19. stream
  20. .pipe(fs.createWriteStream(tmp))
  21. .on('close', function() {
  22. t.equal(fs.readFileSync(tmp, 'utf8'), contents)
  23. fs.unlinkSync(tmp)
  24. t.end()
  25. })
  26. })
  27. test('old mode', function(t) {
  28. var contents = fs.readFileSync(__filename, 'utf8')
  29. var stream = fromString(contents)
  30. var buffer = ''
  31. stream.on('data', function(data) {
  32. buffer += data
  33. }).on('end', function() {
  34. t.equal(buffer, contents)
  35. t.end()
  36. })
  37. })
  38. test('destroy', function(t) {
  39. var stream = from(function(size, next) {
  40. process.nextTick(function() {
  41. next(null, 'no')
  42. })
  43. })
  44. stream.on('data', function(data) {
  45. t.ok(false)
  46. }).on('close', function() {
  47. t.ok(true)
  48. t.end()
  49. })
  50. stream.destroy()
  51. })
  52. test('arrays', function (t) {
  53. var input = ['a', 'b', 'c']
  54. var stream = from(input)
  55. var output = []
  56. stream.on('data', function (letter) {
  57. output.push(letter.toString())
  58. })
  59. stream.on('end', function () {
  60. t.deepEqual(input, output)
  61. t.end()
  62. })
  63. })
  64. test('obj arrays', function (t) {
  65. var input = [{foo:'a'}, {foo:'b'}, {foo:'c'}]
  66. var stream = from.obj(input)
  67. var output = []
  68. stream.on('data', function (letter) {
  69. output.push(letter)
  70. })
  71. stream.on('end', function () {
  72. t.deepEqual(input, output)
  73. t.end()
  74. })
  75. })
  76. test('arrays can emit errors', function (t) {
  77. var input = ['a', 'b', new Error('ooops'), 'c']
  78. var stream = from(input)
  79. var output = []
  80. stream.on('data', function (letter) {
  81. output.push(letter.toString())
  82. })
  83. stream.on('error', function(e){
  84. t.deepEqual(['a', 'b'], output)
  85. t.equal('ooops', e.message)
  86. t.end()
  87. })
  88. stream.on('end', function () {
  89. t.fail('the stream should have errored')
  90. })
  91. })
  92. test('obj arrays can emit errors', function (t) {
  93. var input = [{foo:'a'}, {foo:'b'}, new Error('ooops'), {foo:'c'}]
  94. var stream = from.obj(input)
  95. var output = []
  96. stream.on('data', function (letter) {
  97. output.push(letter)
  98. })
  99. stream.on('error', function(e){
  100. t.deepEqual([{foo:'a'}, {foo:'b'}], output)
  101. t.equal('ooops', e.message)
  102. t.end()
  103. })
  104. stream.on('end', function () {
  105. t.fail('the stream should have errored')
  106. })
  107. })