Skip to content

Commit

Permalink
Add test and fix some corner cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
rybesh committed May 23, 2012
1 parent aad7c76 commit 2a684e0
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/components/MapProperty.coffee
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
@@ -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) ->
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 comments on commit 2a684e0

Please sign in to comment.