Permalink
Browse files

Fix component API tests

  • Loading branch information...
1 parent 0881525 commit a13b2ba301d9bae743ab486d8be6fe0f70cfdb80 @bergie bergie committed Mar 2, 2014
Showing with 55 additions and 84 deletions.
  1. +32 −48 spec/Component.coffee
  2. +13 −32 spec/OutPort.coffee
  3. +6 −4 spec/runner.html
  4. +4 −0 src/lib/InPort.coffee
View
@@ -1,76 +1,60 @@
if typeof process isnt 'undefined' and process.execPath and process.execPath.indexOf('node') isnt -1
chai = require 'chai' unless chai
component = require '../src/lib/Component.coffee'
- port = require '../src/lib/Port.coffee'
socket = require '../src/lib/InternalSocket.coffee'
else
component = require 'noflo/src/lib/Component.js'
- port = require 'noflo/src/lib/Port.js'
socket = require 'noflo/src/lib/InternalSocket.js'
describe 'Component', ->
describe 'with required ports', ->
- it 'should throw an error upon receiving packet with an unattached required port', ->
- s1 = new socket
- s2 = new socket
- c = new component
- inPorts:
- requiredPort: new port
+ it 'should throw an error upon sending packet to an unattached required port', ->
+ s2 = new socket.InternalSocket
+ c = new component.Component
+ outPorts:
+ requiredPort:
required: true
- optionalPort: new port
- c.inPorts.optionalPort.attach s2
- run = ->
- s2.send 'some-data'
- chai.expect(run).to.throw()
+ optionalPort: {}
+ c.outPorts.optionalPort.attach s2
+ chai.expect(-> c.outPorts.requiredPorts.send('foo')).to.throw()
it 'should be cool with an attached port', ->
- s1 = new socket
- s2 = new socket
- c = new component
+ s1 = new socket.InternalSocket
+ s2 = new socket.InternalSocket
+ c = new component.Component
inPorts:
- requiredPort: new port
+ requiredPort:
required: true
- optionalPort: new port
+ optionalPort: {}
c.inPorts.requiredPort.attach s1
c.inPorts.optionalPort.attach s2
f = ->
s2.send 'some-data'
chai.expect(f).to.not.throw()
- it 'should simply forward error if error port is attached', (done) ->
- s1 = new socket
- s2 = new socket
- s3 = new socket
- c = new component
- inPorts:
- requiredPort: new port
- required: true
- optionalPort: new port
- outPorts:
- error: new port
- c.inPorts.optionalPort.attach s2
- c.outPorts.error.attach s3
- s3.on 'connect', ->
- chai.assert true
- done()
- f = ->
- s2.send 'some-data'
- chai.expect(f).to.not.throw()
-
describe 'with component creation shorthand', ->
- it 'should make component creation easy', ->
- c = new component
+ it 'should make component creation easy', (done) ->
+ c = new component.Component
inPorts:
in:
datatype: 'string'
required: true
- processor: (packet, component) ->
- chai.expect(packet).toEqual 'some-data'
- chai.expect(component).toBe c
+ process: (event, packet, component) ->
+ return unless event is 'data'
+ chai.expect(packet).to.equal 'some-data'
+ chai.expect(component).to.equal c
- justProcessor: (packet, component) ->
- chai.expect(packet).toEqual 'some-data'
- chai.expect(component).toBe c
+ justProcessor: (event, packet, component) ->
+ return unless event is 'data'
+ chai.expect(packet).to.equal 'some-data'
+ chai.expect(component).to.equal c
+ done()
- c.inPorts.in.send 'some-data'
- c.inPorts.justProcessor.send 'some-data'
+ s1 = new socket.InternalSocket
+ c.inPorts.in.attach s1
+ c.inPorts.in.nodeInstance = c
+ s2 = new socket.InternalSocket
+ c.inPorts.justProcessor.attach s1
+ c.inPorts.justProcessor.nodeInstance = c
+ s1.send 'some-data'
+ s2.send 'some-data'
View
@@ -10,49 +10,30 @@ describe 'Outport Port', ->
describe 'with addressable ports', ->
s1 = s2 = s3 = null
beforeEach ->
- s1 = new socket
- s2 = new socket
- s3 = new socket
+ s1 = new socket.InternalSocket
+ s2 = new socket.InternalSocket
+ s3 = new socket.InternalSocket
it 'should be able to send to a specific port', ->
p = new outport
addressable: true
p.attach s1
p.attach s2
p.attach s3
- cb1 = jasmine.createSpy()
- cb2 = jasmine.createSpy()
- cb3 = jasmine.createSpy()
- s1.on 'data', cb1
- s2.on 'data', cb2
- s3.on 'data', cb3
- p.send 'some-data', 2
- cb1.not.toHaveBeenCalled()
- cb2.toHaveBeenCalled()
- cb3.not.toHaveBeenCalled()
+ s1.on 'data', ->
+ chai.expect(true).to.equal false
+ s2.on 'data', (data) ->
+ chai.expect(data).to.equal 'some-data'
+ s3.on 'data', ->
+ chai.expect(true).to.equal false
+ p.send 'some-data', 1
- it 'should send to all with no specific port', ->
- p = new outport
- addressable: true
- p.attach s1
- p.attach s2
- p.attach s3
- cb1 = jasmine.createSpy()
- cb2 = jasmine.createSpy()
- cb3 = jasmine.createSpy()
- s1.on 'data', cb1
- s2.on 'data', cb2
- s3.on 'data', cb3
- p.send 'some-data'
- cb1.toHaveBeenCalled()
- cb2.toHaveBeenCalled()
- cb3.toHaveBeenCalled()
+ it 'should throw an error when sent data without address', ->
+ chai.expect(-> p.send('some-data')).to.throw
it 'should throw an error when a specific port is requested with non-addressable port', ->
p = new outport
p.attach s1
p.attach s2
p.attach s3
- f ->
- p.send 'some-data'
- expect(f).toHaveThrown()
+ chai.expect(-> p.send('some-data', 1)).to.throw
View
@@ -17,15 +17,17 @@
<script src="http://chaijs.com/chai.js"></script>
<script src="../node_modules/mocha/mocha.js"></script>
<script>mocha.setup('bdd');</script>
- <script src="./Graph.js"></script>
- <script src="./Journal.js"></script>
- <script src="./InPort.js"></script>
- <script src="./Port.js"></script>
<script src="./ArrayPort.js"></script>
<script src="./AsyncComponent.js"></script>
+ <script src="./Component.js"></script>
<script src="./ComponentLoader.js"></script>
+ <script src="./Graph.js"></script>
+ <script src="./InPort.js"></script>
+ <script src="./Journal.js"></script>
<script src="./Network.js"></script>
<script src="./NoFlo.js"></script>
+ <script src="./OutPort.js"></script>
+ <script src="./Port.js"></script>
<script src="./Subgraph.js"></script>
<script>
if (window.mochaPhantomJS) {
View
@@ -16,6 +16,10 @@ class InPort extends BasePort
if options and options.buffered is undefined
options.buffered = false
+ if not process and options and options.process
+ process = options.process
+ delete options.process
+
if process
unless typeof process is 'function'
throw new Error 'process must be a function'

0 comments on commit a13b2ba

Please sign in to comment.