8000 FilterPropertyValue should not send objects not matching accept criteria by rybesh · Pull Request #20 · noflo/noflo · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/components/FilterPropertyValue.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ class FilterPropertyValue extends noflo.Component
return

mapParts = map.split "="
@accepts[mapParts[0]] = mapParts[1]
try
@accepts[mapParts[0]] = eval mapParts[1]
catch e
if e instanceof ReferenceError
@accepts[mapParts[0]] = mapParts[1]
else throw e

prepareRegExp: (map) ->
mapParts = map.split "="
Expand All @@ -42,16 +47,16 @@ class FilterPropertyValue extends noflo.Component
newData = {}
match = false
for property, value of object
if @accepts[property] and @accepts[property] isnt value
continue
if @accepts[property]
continue unless @accepts[property] is value
match = true

if @regexps[property]
regexp = new RegExp @regexps[property]
unless regexp.exec value
continue
continue unless regexp.exec value
match = true

newData[property] = value
match = true
continue

return unless match
Expand Down
61 changes: 61 additions & 0 deletions test/FilterPropertyValue.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
filterpv = require "../src/components/FilterPropertyValue"
socket = require "../src/lib/InternalSocket"

setupComponent = ->
c = filterpv.getComponent()
ins = socket.createSocket()
out = socket.createSocket()
c.inPorts.in.attach ins
c.outPorts.out.attach out
return [c, ins, out]

exports["test accept via map"] = (test) ->
[c, ins, out] = setupComponent()
acc = socket.createSocket()
c.inPorts.accept.attach acc
acc.send { good: true }
out.on "data", (data) ->
test.equal data.good, true
test.equal data.bar, 3
test.equal (k for k of data).length, 2
test.done()
ins.send { good: false, foo: 1 } # reject
ins.send { baz: 2 } # reject
ins.send { good: true, bar: 3 } # accept

exports["test accept via pairs"] = (test) ->
[c, ins, out] = setupComponent()
acc = socket.createSocket()
c.inPorts.accept.attach acc
acc.send "food=true"
acc.send "good=yes"
acc.send "hood=1"
expect = [["good","yes"],["hood",1],["food",true]]
out.on "data", (data) ->
[k,v] = expect.shift()
test.equal data[k], v
test.done() if expect.length is 0
ins.send { good: "yes" } # accept
ins.send { hood: 1 } # accept
ins.send { good: false, foo: 1 } # reject
ins.send { baz: 2 } # reject
ins.send { food: true, bar: 3 } # accept

exports["test accept via regexp"] = (test) ->
[c, ins, out] = setupComponent()
reg = socket.createSocket()
c.inPorts.regexp.attach reg
reg.send "good=[tg]rue"
expect = ["grue",true]
out.on "data", (data) ->
test.equal data.good, expect.shift()
test.equal data.bar, 3
test.equal (k for k of data).length, 2
test.done() if expect.length is 0
ins.send { good: "grue", bar: 3 } # accept
ins.send { good: false, foo: 1 } # reject
ins.send { baz: 2 } # reject
ins.send { good: true, bar: 3 } # accept



0