test.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. var tape = require('tape')
  2. , EE = require('events').EventEmitter
  3. , util = require('util')
  4. , isStream = require('./')
  5. , isReadable = require('./').isReadable
  6. , isWritable = require('./').isWritable
  7. , isDuplex = require('./').isDuplex
  8. , CoreStreams = require('stream')
  9. , ReadableStream10 = require('./readable-stream-1.0/')
  10. , ReadableStream11 = require('./readable-stream-1.1/')
  11. function test (pass, type, stream) {
  12. tape('isStream(' + type + ')', function (t) {
  13. t.plan(1)
  14. t.ok(pass === isStream(stream), type)
  15. })
  16. }
  17. function testReadable (pass, type, stream) {
  18. tape('isReadable(' + type + ')', function (t) {
  19. t.plan(1)
  20. t.ok(pass === isReadable(stream), type)
  21. })
  22. }
  23. function testWritable (pass, type, stream) {
  24. tape('isWritable(' + type + ')', function (t) {
  25. t.plan(1)
  26. t.ok(pass === isWritable(stream), type)
  27. })
  28. }
  29. function testDuplex (pass, type, stream) {
  30. tape('isDuplex(' + type + ')', function (t) {
  31. t.plan(1)
  32. t.ok(pass === isDuplex(stream), type)
  33. })
  34. }
  35. [ undefined, null, '', true, false, 0, 1, 1.0, 'string', {}, function foo () {} ].forEach(function (o) {
  36. test(false, 'non-stream / primitive: ' + (JSON.stringify(o) || (o && o.toString()) || o), o)
  37. })
  38. test(false, 'fake stream obj', { pipe: function () {} })
  39. ;(function () {
  40. // looks like a stream!
  41. function Stream () {
  42. EE.call(this)
  43. }
  44. util.inherits(Stream, EE)
  45. Stream.prototype.pipe = function () {}
  46. Stream.Stream = Stream
  47. test(false, 'fake stream "new Stream()"', new Stream())
  48. }())
  49. test(true, 'CoreStreams.Stream', new (CoreStreams.Stream)())
  50. test(true, 'CoreStreams.Readable', new (CoreStreams.Readable)())
  51. test(true, 'CoreStreams.Writable', new (CoreStreams.Writable)())
  52. test(true, 'CoreStreams.Duplex', new (CoreStreams.Duplex)())
  53. test(true, 'CoreStreams.Transform', new (CoreStreams.Transform)())
  54. test(true, 'CoreStreams.PassThrough', new (CoreStreams.PassThrough)())
  55. test(true, 'ReadableStream10.Readable', new (ReadableStream10.Readable)())
  56. test(true, 'ReadableStream10.Writable', new (ReadableStream10.Writable)())
  57. test(true, 'ReadableStream10.Duplex', new (ReadableStream10.Duplex)())
  58. test(true, 'ReadableStream10.Transform', new (ReadableStream10.Transform)())
  59. test(true, 'ReadableStream10.PassThrough', new (ReadableStream10.PassThrough)())
  60. test(true, 'ReadableStream11.Readable', new (ReadableStream11.Readable)())
  61. test(true, 'ReadableStream11.Writable', new (ReadableStream11.Writable)())
  62. test(true, 'ReadableStream11.Duplex', new (ReadableStream11.Duplex)())
  63. test(true, 'ReadableStream11.Transform', new (ReadableStream11.Transform)())
  64. test(true, 'ReadableStream11.PassThrough', new (ReadableStream11.PassThrough)())
  65. testReadable(false, 'CoreStreams.Stream', new (CoreStreams.Stream)())
  66. testReadable(true, 'CoreStreams.Readable', new (CoreStreams.Readable)())
  67. testReadable(false, 'CoreStreams.Writable', new (CoreStreams.Writable)())
  68. testReadable(true, 'CoreStreams.Duplex', new (CoreStreams.Duplex)())
  69. testReadable(true, 'CoreStreams.Transform', new (CoreStreams.Transform)())
  70. testReadable(true, 'CoreStreams.PassThrough', new (CoreStreams.PassThrough)())
  71. testReadable(true, 'ReadableStream10.Readable', new (ReadableStream10.Readable)())
  72. testReadable(false, 'ReadableStream10.Writable', new (ReadableStream10.Writable)())
  73. testReadable(true, 'ReadableStream10.Duplex', new (ReadableStream10.Duplex)())
  74. testReadable(true, 'ReadableStream10.Transform', new (ReadableStream10.Transform)())
  75. testReadable(true, 'ReadableStream10.PassThrough', new (ReadableStream10.PassThrough)())
  76. testReadable(true, 'ReadableStream11.Readable', new (ReadableStream11.Readable)())
  77. testReadable(false, 'ReadableStream11.Writable', new (ReadableStream11.Writable)())
  78. testReadable(true, 'ReadableStream11.Duplex', new (ReadableStream11.Duplex)())
  79. testReadable(true, 'ReadableStream11.Transform', new (ReadableStream11.Transform)())
  80. testReadable(true, 'ReadableStream11.PassThrough', new (ReadableStream11.PassThrough)())
  81. testWritable(false, 'CoreStreams.Stream', new (CoreStreams.Stream)())
  82. testWritable(false, 'CoreStreams.Readable', new (CoreStreams.Readable)())
  83. testWritable(true, 'CoreStreams.Writable', new (CoreStreams.Writable)())
  84. testWritable(true, 'CoreStreams.Duplex', new (CoreStreams.Duplex)())
  85. testWritable(true, 'CoreStreams.Transform', new (CoreStreams.Transform)())
  86. testWritable(true, 'CoreStreams.PassThrough', new (CoreStreams.PassThrough)())
  87. testWritable(false, 'ReadableStream10.Readable', new (ReadableStream10.Readable)())
  88. testWritable(true, 'ReadableStream10.Writable', new (ReadableStream10.Writable)())
  89. testWritable(true, 'ReadableStream10.Duplex', new (ReadableStream10.Duplex)())
  90. testWritable(true, 'ReadableStream10.Transform', new (ReadableStream10.Transform)())
  91. testWritable(true, 'ReadableStream10.PassThrough', new (ReadableStream10.PassThrough)())
  92. testWritable(false, 'ReadableStream11.Readable', new (ReadableStream11.Readable)())
  93. testWritable(true, 'ReadableStream11.Writable', new (ReadableStream11.Writable)())
  94. testWritable(true, 'ReadableStream11.Duplex', new (ReadableStream11.Duplex)())
  95. testWritable(true, 'ReadableStream11.Transform', new (ReadableStream11.Transform)())
  96. testWritable(true, 'ReadableStream11.PassThrough', new (ReadableStream11.PassThrough)())
  97. testDuplex(false, 'CoreStreams.Stream', new (CoreStreams.Stream)())
  98. testDuplex(false, 'CoreStreams.Readable', new (CoreStreams.Readable)())
  99. testDuplex(false, 'CoreStreams.Writable', new (CoreStreams.Writable)())
  100. testDuplex(true, 'CoreStreams.Duplex', new (CoreStreams.Duplex)())
  101. testDuplex(true, 'CoreStreams.Transform', new (CoreStreams.Transform)())
  102. testDuplex(true, 'CoreStreams.PassThrough', new (CoreStreams.PassThrough)())
  103. testDuplex(false, 'ReadableStream10.Readable', new (ReadableStream10.Readable)())
  104. testDuplex(false, 'ReadableStream10.Writable', new (ReadableStream10.Writable)())
  105. testDuplex(true, 'ReadableStream10.Duplex', new (ReadableStream10.Duplex)())
  106. testDuplex(true, 'ReadableStream10.Transform', new (ReadableStream10.Transform)())
  107. testDuplex(true, 'ReadableStream10.PassThrough', new (ReadableStream10.PassThrough)())
  108. testDuplex(false, 'ReadableStream11.Readable', new (ReadableStream11.Readable)())
  109. testDuplex(false, 'ReadableStream11.Writable', new (ReadableStream11.Writable)())
  110. testDuplex(true, 'ReadableStream11.Duplex', new (ReadableStream11.Duplex)())
  111. testDuplex(true, 'ReadableStream11.Transform', new (ReadableStream11.Transform)())
  112. testDuplex(true, 'ReadableStream11.PassThrough', new (ReadableStream11.PassThrough)())
  113. ;[ CoreStreams, ReadableStream10, ReadableStream11 ].forEach(function (p) {
  114. [ 'Stream', 'Readable', 'Writable', 'Duplex', 'Transform', 'PassThrough' ].forEach(function (k) {
  115. if (!p[k])
  116. return
  117. function SubStream () {
  118. p[k].call(this)
  119. }
  120. util.inherits(SubStream, p[k])
  121. test(true, 'Stream subclass: ' + p.name + '.' + k, new SubStream())
  122. })
  123. })