8000 Add test and fix some corner cases. by rybesh · Pull Request #27 · 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
10 changes: 8 additions & 2 deletions src/components/MapProperty.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class MapProperty extends noflo.Component
mapData: (data) ->
newData = {}
for property, value of data
if @map[property]
if property of @map
property = @map[property]

for expression, replacement of @regexps
Expand All @@ -51,7 +51,13 @@ class MapProperty extends noflo.Component

property = property.replace regexp, replacement

newData[property] = value
if property of newData
if Array.isArray newData[property]
newData[property].push value
else
newData[property] = [newData[property], value]
else
newData[property] = value
@outPorts.out.send newData

exports.getComponent = -> new MapProperty
105 changes: 105 additions & 0 deletions test/MapProperty.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
mapper = require "../src/components/MapProperty"
socket = require "../src/lib/InternalSocket"

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

o = { a:1, b:2, c:3 }

exports["test no map"] = (test) ->
[c, ins, map, out] = setupComponent()
output = []
out.on "data", (data) ->
8000 output.push data
out.once "disconnect", ->
test.same output, [{ a:1, b:2, c:3 }]
test.done()
ins.send o
ins.disconnect()

exports["test map to letter key"] = (test) ->
[c, ins, map, out] = setupComponent()
output = []
out.on "data", (data) ->
output.push data
out.once "disconnect", ->
test.same output, [{ d:1, b:2, c:3 }]
test.done()
map.send {a:"d"}
map.disconnect
ins.send o
ins.disconnect()

exports["test map to colliding key"] = (test) ->
[c, ins, map, out] = setupComponent()
output = []
out.on "data", (data) ->
output.push data
out.once "disconnect", ->
test.same output, [{ b:[1,2], c:3 }]
test.done()
map.send {a:"b"}
map.disconnect
ins.send o
ins.disconnect()

exports["test map to 0 key"] = (test) ->
[c, ins, map, out] = setupComponent()
output = []
out.on "data", (data) ->
output.push data
out.once "disconnect", ->
test.same output, [{ 0:1, b:2, c:3 }]
test.done()
map.send {a:0}
map.disconnect
ins.send o
ins.disconnect()

exports["test map to null key"] = (test) ->
[c, ins, map, out] = setupComponent()
output = []
out.on "data", (data) ->
output.push data
out.once "disconnect", ->
test.same output, [{ null:1, b:2, c:3 }]
test.done()
map.send {a:null}
map.disconnect
ins.send o
ins.disconnect()

exports["test map to undefined key"] = (test) ->
[c, ins, map, out] = setupComponent()
output = []
out.on "data", (data) ->
output.push data
out.once "disconnect", ->
test.same output, [{ undefined:1, b:2, c:3 }]
test.done()
map.send {a:undefined}
map.disconnect
ins.send o
ins.disconnect()

exports["test map to false key"] = (test) ->
[c, ins, map, out] = setupComponent()
output = []
out.on "data", (data) ->
output.push data
out.once "disconnect", ->
test.same output, [{ false:1, b:2, c:3 }]
test.done()
map.send {a:false}
map.disconnect
ins.send o
ins.disconnect()


0