writable.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. var Stream = require('stream')
  2. var spec = require('..')
  3. var tests = 0
  4. function pass(message) {
  5. console.log('ok ' + ++tests, message ? ' -- ' + message : '')
  6. }
  7. function fail(message) {
  8. console.log('not ok ' + ++tests, message ? ' -- ' + message : '')
  9. }
  10. function checkValid(contract, expectFail) {
  11. return function (create, test) {
  12. var stream = create()
  13. try {
  14. var validate = contract(stream, spec).validate
  15. test(stream)
  16. validate()
  17. } catch (err) {
  18. if(!expectFail) {
  19. fail(err.message); throw err
  20. }
  21. return pass(err.message)
  22. }
  23. if(expectFail) {
  24. throw new Error('expected error')
  25. }
  26. }
  27. }
  28. var valid = checkValid(function (stream, spec) {
  29. return spec(stream).writable()
  30. }, false)
  31. var invalid = checkValid(function (stream, spec) {
  32. return spec(stream).writable()
  33. }, true)
  34. var wrong1 = function () {
  35. var s = new Stream()
  36. s.write = function () {}
  37. s.destroy = function () {}
  38. //s.end = function () {}
  39. //s.writable = true
  40. return s
  41. }
  42. var wrong2 = //fails because end isn't defined
  43. invalid(function () {
  44. var s = new Stream()
  45. s.write = function (){ }
  46. s.destroy = function () {}
  47. //s.end = function () {}
  48. s.writable = true
  49. return s
  50. }, function (s) {})
  51. //fails because end isn't defined
  52. invalid(function () {
  53. var s = new Stream()
  54. s.write = function (){ }
  55. s.destroy = function () {}
  56. s.end = function () {}
  57. //s.writable = true
  58. return s
  59. }, function (s) {})
  60. //fails because end isn't called
  61. invalid(function () {
  62. var s = new Stream()
  63. s.write = function (){ }
  64. s.destroy = function () {}
  65. s.end = function () {}
  66. s.writable = true
  67. return s
  68. }
  69. , function (s) {
  70. s.write('hello')
  71. })
  72. //fails because end doesn't set writable = false
  73. invalid(function () {
  74. var s = new Stream()
  75. s.write = function (){ }
  76. s.destroy = function () {}
  77. s.end = function () {}
  78. s.writable = true
  79. return s
  80. }, function (s) {
  81. s.write('hello')
  82. s.end()
  83. })
  84. //fails because does not emit 'close'
  85. invalid(function () {
  86. var s = new Stream()
  87. s.write = function (){ }
  88. s.destroy = function () {}
  89. s.end = function () {this.writable = false}
  90. s.writable = true
  91. return s
  92. }, function (s) {
  93. s.write('hello')
  94. s.end()
  95. })
  96. //passes
  97. valid(function () {
  98. var s = new Stream()
  99. s.write = function (){ }
  100. s.destroy = function () {}
  101. s.end = function () {
  102. this.writable = false
  103. this.emit('close')
  104. }
  105. s.writable = true
  106. return s
  107. }, function (s) {
  108. s.write('hello')
  109. s.end()
  110. })