Permalink
Browse files

write tests for writable and readable

  • Loading branch information...
1 parent a18b5fa commit cc959b5082bd0b62ff0ed1a58679a67aef26ea17 @dominictarr committed Jul 11, 2012
Showing with 198 additions and 0 deletions.
  1. +83 −0 test/readable.js
  2. +115 −0 test/writable.js
View
@@ -0,0 +1,83 @@
+
+var Stream = require('stream')
+var spec = require('..')
+
+var tests = 0
+function pass(message) {
+ console.log('ok ' + ++tests, message ? ' -- ' + message : '')
+}
+function fail(message) {
+ console.log('not ok ' + ++tests, message ? ' -- ' + message : '')
+}
+
+function checkValid(contract, expectFail) {
+ return function (create, test) {
+ var stream = create()
+ try {
+ var validate = contract(stream, spec).validate
+ test(stream)
+ validate()
+ } catch (err) {
+ if(!expectFail) {
+ fail(err.message); throw err
+ }
+ return pass(err.message)
+ }
+ if(expectFail) {
+ throw new Error('expected error')
+ }
+ pass('valid')
+ }
+}
+
+var valid = checkValid(function (stream, spec) {
+ return spec(stream).readable()
+}, false)
+
+var invalid = checkValid(function (stream, spec) {
+ return spec(stream).readable()
+}, true)
+
+//does not emit 'end'
+invalid(function () {
+ var s = new Stream()
+ return s
+}, function (s) {
+})
+
+//does not set readable = false, on 'end'
+invalid(function () {
+ var s = new Stream()
+ s._end = function () {
+ this.emit('end')
+ }
+ return s
+}, function (s) {
+ s._end()
+})
+
+//does not set emit 'close'
+invalid(function () {
+ var s = new Stream()
+ s._end = function () {
+ this.readable = false
+ this.emit('end')
+ }
+ return s
+}, function (s) {
+ s._end()
+})
+
+valid(function () {
+ var s = new Stream()
+ s._end = function () {
+ this.readable = false
+ this.emit('end')
+ this.emit('close')
+ }
+ return s
+}, function (s) {
+ s._end()
+})
+
+
View
@@ -0,0 +1,115 @@
+
+var Stream = require('stream')
+var spec = require('..')
+
+var tests = 0
+function pass(message) {
+ console.log('ok ' + ++tests, message ? ' -- ' + message : '')
+}
+function fail(message) {
+ console.log('not ok ' + ++tests, message ? ' -- ' + message : '')
+}
+
+function checkValid(contract, expectFail) {
+ return function (create, test) {
+ var stream = create()
+ try {
+ var validate = contract(stream, spec).validate
+ test(stream)
+ validate()
+ } catch (err) {
+ if(!expectFail) {
+ fail(err.message); throw err
+ }
+ return pass(err.message)
+ }
+ if(expectFail) {
+ throw new Error('expected error')
+ }
+ }
+}
+
+var valid = checkValid(function (stream, spec) {
+ return spec(stream).writable()
+}, false)
+
+var invalid = checkValid(function (stream, spec) {
+ return spec(stream).writable()
+}, true)
+
+var wrong1 = function () {
+ var s = new Stream()
+ s.write = function (){ }
+ //s.end = function () {}
+ //s.writable = true
+ return s
+}
+var wrong2 = //fails because end isn't defined
+invalid(function () {
+ var s = new Stream()
+ s.write = function (){ }
+ //s.end = function () {}
+ s.writable = true
+ return s
+}, function (s) {})
+
+//fails because end isn't defined
+invalid(function () {
+ var s = new Stream()
+ s.write = function (){ }
+ s.end = function () {}
+ //s.writable = true
+ return s
+}, function (s) {})
+
+
+//fails because end isn't called
+invalid(function () {
+ var s = new Stream()
+ s.write = function (){ }
+ s.end = function () {}
+ s.writable = true
+ return s
+}
+, function (s) {
+ s.write('hello')
+})
+
+//fails because end doesn't set writable = false
+invalid(function () {
+ var s = new Stream()
+ s.write = function (){ }
+ s.end = function () {}
+ s.writable = true
+ return s
+}, function (s) {
+ s.write('hello')
+ s.end()
+})
+//fails because does not emit 'close'
+invalid(function () {
+ var s = new Stream()
+ s.write = function (){ }
+ s.end = function () {this.writable = false}
+ s.writable = true
+ return s
+}, function (s) {
+ s.write('hello')
+ s.end()
+})
+
+//passes
+valid(function () {
+ var s = new Stream()
+ s.write = function (){ }
+ s.end = function () {
+ this.writable = false
+ this.emit('close')
+ }
+ s.writable = true
+ return s
+}, function (s) {
+ s.write('hello')
+ s.end()
+})
+

0 comments on commit cc959b5

Please sign in to comment.