Skip to content

Commit

Permalink
Fix the MakeFunction component
Browse files Browse the repository at this point in the history
  • Loading branch information
bergie committed Oct 23, 2017
1 parent 2b9b354 commit 8c46eab
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
49 changes: 30 additions & 19 deletions components/MakeFunction.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,42 @@ exports.getComponent = ->
c.outPorts.add 'error',
datatype: 'object'

prepareFunction = (func, callback) ->
if typeof func is 'function'
callback null, func
return
try
newFunc = Function 'x', func
catch e
callback e
return
callback null, newFunc

c.process (input, output) ->
if input.has 'function'
func = input.getData 'function'
unless typeof func is 'function'
return if input.attached('in').length and not input.hasData 'in'
if input.hasData 'function', 'in'
# Both function and input data
prepareFunction input.getData('function'), (err, func) ->
if err
output.done e
return
data = input.getData 'in'
try
func = Function 'x', func
result = func data
catch e
output.sendDone e
output.done e
return
unless input.has 'in'
output.sendDone
function: func
out: result
return

unless func
output.sendDone new Error 'No function defined'
return

data = input.getData 'in'
try
result = func data
catch e
output.sendDone e
return unless input.hasData 'function'
prepareFunction input.getData('function'), (err, func) ->
if err
output.done e
return
output.sendDone
function: func
return

output.sendDone
function: func
out: result
return
19 changes: 8 additions & 11 deletions spec/MakeFunction.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ describe 'MakeFunction component', ->
loader.load 'core/MakeFunction', (err, instance) ->
return done err if err
c = instance
ins = noflo.internalSocket.createSocket()
func = noflo.internalSocket.createSocket()
c.inPorts.in.attach ins
c.inPorts.function.attach func
done()
beforeEach ->
Expand All @@ -47,15 +45,7 @@ describe 'MakeFunction component', ->
chai.expect(c.outPorts.out).to.be.an 'object'
chai.expect(c.outPorts.error).to.be.an 'object'

describe 'test function', ->
it 'without function', (done) ->
err.on 'data', (data) ->
chai.expect(data).to.be.an 'error'
done()

ins.send 'Foo bar'
ins.disconnect()

describe 'with only function', ->
it 'wrong function', (done) ->
err.on 'data', (data) ->
chai.expect(data).to.be.ok
Expand All @@ -72,6 +62,13 @@ describe 'MakeFunction component', ->
done data
func.send 'return x*x;'

describe 'with function and input data', ->
before ->
ins = noflo.internalSocket.createSocket()
c.inPorts.in.attach ins
after ->
c.inPorts.in.detach ins
ins = null
it 'square function', (done) ->
out.on 'data', (data) ->
chai.expect(data).to.equal 81
Expand Down

0 comments on commit 8c46eab

Please sign in to comment.