basic.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. var Stream = require('stream')
  2. var tap = require('tap')
  3. var MS = require('../mute.js')
  4. // some marker objects
  5. var END = {}
  6. var PAUSE = {}
  7. var RESUME = {}
  8. function PassThrough () {
  9. Stream.call(this)
  10. this.readable = this.writable = true
  11. }
  12. PassThrough.prototype = Object.create(Stream.prototype, {
  13. constructor: {
  14. value: PassThrough
  15. },
  16. write: {
  17. value: function (c) {
  18. this.emit('data', c)
  19. return true
  20. }
  21. },
  22. end: {
  23. value: function (c) {
  24. if (c) this.write(c)
  25. this.emit('end')
  26. }
  27. },
  28. pause: {
  29. value: function () {
  30. this.emit('pause')
  31. }
  32. },
  33. resume: {
  34. value: function () {
  35. this.emit('resume')
  36. }
  37. }
  38. })
  39. tap.test('incoming', function (t) {
  40. var ms = new MS
  41. var str = new PassThrough
  42. str.pipe(ms)
  43. var expect = ['foo', 'boo', END]
  44. ms.on('data', function (c) {
  45. t.equal(c, expect.shift())
  46. })
  47. ms.on('end', function () {
  48. t.equal(END, expect.shift())
  49. t.end()
  50. })
  51. str.write('foo')
  52. ms.mute()
  53. str.write('bar')
  54. ms.unmute()
  55. str.write('boo')
  56. ms.mute()
  57. str.write('blaz')
  58. str.end('grelb')
  59. })
  60. tap.test('outgoing', function (t) {
  61. var ms = new MS
  62. var str = new PassThrough
  63. ms.pipe(str)
  64. var expect = ['foo', 'boo', END]
  65. str.on('data', function (c) {
  66. t.equal(c, expect.shift())
  67. })
  68. str.on('end', function () {
  69. t.equal(END, expect.shift())
  70. t.end()
  71. })
  72. ms.write('foo')
  73. ms.mute()
  74. ms.write('bar')
  75. ms.unmute()
  76. ms.write('boo')
  77. ms.mute()
  78. ms.write('blaz')
  79. ms.end('grelb')
  80. })
  81. tap.test('isTTY', function (t) {
  82. var str = new PassThrough
  83. str.isTTY = true
  84. str.columns=80
  85. str.rows=24
  86. var ms = new MS
  87. t.equal(ms.isTTY, false)
  88. t.equal(ms.columns, undefined)
  89. t.equal(ms.rows, undefined)
  90. ms.pipe(str)
  91. t.equal(ms.isTTY, true)
  92. t.equal(ms.columns, 80)
  93. t.equal(ms.rows, 24)
  94. str.isTTY = false
  95. t.equal(ms.isTTY, false)
  96. t.equal(ms.columns, 80)
  97. t.equal(ms.rows, 24)
  98. str.isTTY = true
  99. t.equal(ms.isTTY, true)
  100. t.equal(ms.columns, 80)
  101. t.equal(ms.rows, 24)
  102. ms.isTTY = false
  103. t.equal(ms.isTTY, false)
  104. t.equal(ms.columns, 80)
  105. t.equal(ms.rows, 24)
  106. ms = new MS
  107. t.equal(ms.isTTY, false)
  108. str.pipe(ms)
  109. t.equal(ms.isTTY, true)
  110. str.isTTY = false
  111. t.equal(ms.isTTY, false)
  112. str.isTTY = true
  113. t.equal(ms.isTTY, true)
  114. ms.isTTY = false
  115. t.equal(ms.isTTY, false)
  116. t.end()
  117. })
  118. tap.test('pause/resume incoming', function (t) {
  119. var str = new PassThrough
  120. var ms = new MS
  121. str.on('pause', function () {
  122. t.equal(PAUSE, expect.shift())
  123. })
  124. str.on('resume', function () {
  125. t.equal(RESUME, expect.shift())
  126. })
  127. var expect = [PAUSE, RESUME, PAUSE, RESUME]
  128. str.pipe(ms)
  129. ms.pause()
  130. ms.resume()
  131. ms.pause()
  132. ms.resume()
  133. t.equal(expect.length, 0, 'saw all events')
  134. t.end()
  135. })
  136. tap.test('replace with *', function (t) {
  137. var str = new PassThrough
  138. var ms = new MS({replace: '*'})
  139. str.pipe(ms)
  140. var expect = ['foo', '*****', 'bar', '***', 'baz', 'boo', '**', '****']
  141. ms.on('data', function (c) {
  142. t.equal(c, expect.shift())
  143. })
  144. str.write('foo')
  145. ms.mute()
  146. str.write('12345')
  147. ms.unmute()
  148. str.write('bar')
  149. ms.mute()
  150. str.write('baz')
  151. ms.unmute()
  152. str.write('baz')
  153. str.write('boo')
  154. ms.mute()
  155. str.write('xy')
  156. str.write('xyzΩ')
  157. t.equal(expect.length, 0)
  158. t.end()
  159. })
  160. tap.test('replace with ~YARG~', function (t) {
  161. var str = new PassThrough
  162. var ms = new MS({replace: '~YARG~'})
  163. str.pipe(ms)
  164. var expect = ['foo', '~YARG~~YARG~~YARG~~YARG~~YARG~', 'bar',
  165. '~YARG~~YARG~~YARG~', 'baz', 'boo', '~YARG~~YARG~',
  166. '~YARG~~YARG~~YARG~~YARG~']
  167. ms.on('data', function (c) {
  168. t.equal(c, expect.shift())
  169. })
  170. // also throw some unicode in there, just for good measure.
  171. str.write('foo')
  172. ms.mute()
  173. str.write('ΩΩ')
  174. ms.unmute()
  175. str.write('bar')
  176. ms.mute()
  177. str.write('Ω')
  178. ms.unmute()
  179. str.write('baz')
  180. str.write('boo')
  181. ms.mute()
  182. str.write('Ω')
  183. str.write('ΩΩ')
  184. t.equal(expect.length, 0)
  185. t.end()
  186. })