test.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. var peek = require('./')
  2. var tape = require('tape')
  3. var concat = require('concat-stream')
  4. var through = require('through2')
  5. var uppercase = function(data, enc, cb) {
  6. cb(null, data.toString().toUpperCase())
  7. }
  8. tape('swap to uppercase', function(t) {
  9. var p = peek(function(data, swap) {
  10. swap(null, through(uppercase))
  11. })
  12. p.pipe(concat(function(data) {
  13. t.same(data.toString(), 'HELLO\nWORLD\n')
  14. t.end()
  15. }))
  16. p.write('hello\n')
  17. p.write('world\n')
  18. p.end()
  19. })
  20. tape('swap to uppercase no newline', function(t) {
  21. var p = peek(function(data, swap) {
  22. swap(null, through(uppercase))
  23. })
  24. p.pipe(concat(function(data) {
  25. t.same(data.toString(), 'HELLOWORLD')
  26. t.end()
  27. }))
  28. p.write('hello')
  29. p.write('world')
  30. p.end()
  31. })
  32. tape('swap to uppercase async', function(t) {
  33. var p = peek(function(data, swap) {
  34. setTimeout(function() {
  35. swap(null, through(uppercase))
  36. }, 100)
  37. })
  38. p.pipe(concat(function(data) {
  39. t.same(data.toString(), 'HELLO\nWORLD\n')
  40. t.end()
  41. }))
  42. p.write('hello\n')
  43. p.write('world\n')
  44. p.end()
  45. })
  46. tape('swap to error', function(t) {
  47. var p = peek(function(data, swap) {
  48. swap(new Error('nogo'))
  49. })
  50. p.on('error', function(err) {
  51. t.ok(err)
  52. t.same(err.message, 'nogo')
  53. t.end()
  54. })
  55. p.write('hello\n')
  56. p.write('world\n')
  57. p.end()
  58. })
  59. tape('swap to error async', function(t) {
  60. var p = peek(function(data, swap) {
  61. setTimeout(function() {
  62. swap(new Error('nogo'))
  63. }, 100)
  64. })
  65. p.on('error', function(err) {
  66. t.ok(err)
  67. t.same(err.message, 'nogo')
  68. t.end()
  69. })
  70. p.write('hello\n')
  71. p.write('world\n')
  72. p.end()
  73. })