1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Updates to sentiment, exec and range to handle missing payload properties

This commit is contained in:
dceejay 2015-03-31 08:35:56 +01:00
parent 9c22a770ef
commit 78d1da5fbc
5 changed files with 74 additions and 33 deletions

View File

@ -23,10 +23,13 @@ module.exports = function(RED) {
var node = this; var node = this;
this.on("input", function(msg) { this.on("input", function(msg) {
if (msg.hasOwnProperty("payload")) {
sentiment(msg.payload, msg.overrides || null, function (err, result) { sentiment(msg.payload, msg.overrides || null, function (err, result) {
msg.sentiment = result; msg.sentiment = result;
node.send(msg); node.send(msg);
}); });
}
else { node.send(msg); } // If no payload - just pass it on.
}); });
} }
RED.nodes.registerType("sentiment",SentimentNode); RED.nodes.registerType("sentiment",SentimentNode);

View File

@ -66,7 +66,7 @@ module.exports = function(RED) {
} }
else { else {
var cl = node.cmd; var cl = node.cmd;
if ((node.addpay === true) && (msg.payload.trim() !== "")) { cl += " "+msg.payload; } if ((node.addpay === true) && ((msg.payload || "").trim() !== "")) { cl += " "+msg.payload; }
if (node.append.trim() !== "") { cl += " "+node.append; } if (node.append.trim() !== "") { cl += " "+node.append; }
if (RED.settings.verbose) { node.log(cl); } if (RED.settings.verbose) { node.log(cl); }
var child = exec(cl, {encoding: 'binary', maxBuffer:10000000}, function (error, stdout, stderr) { var child = exec(cl, {encoding: 'binary', maxBuffer:10000000}, function (error, stdout, stderr) {

View File

@ -27,6 +27,7 @@ module.exports = function(RED) {
var node = this; var node = this;
this.on('input', function (msg) { this.on('input', function (msg) {
if (msg.hasOwnProperty("payload")) {
var n = Number(msg.payload); var n = Number(msg.payload);
if (!isNaN(n)) { if (!isNaN(n)) {
if (node.action == "clamp") { if (node.action == "clamp") {
@ -42,6 +43,8 @@ module.exports = function(RED) {
node.send(msg); node.send(msg);
} }
else { node.log("Not a number: "+msg.payload); } else { node.log("Not a number: "+msg.payload); }
}
else { node.send(msg); } // If no payload - just pass it on.
}); });
} }
RED.nodes.registerType("range", RangeNode); RED.nodes.registerType("range", RangeNode);

View File

@ -37,6 +37,22 @@ describe('sentiment Node', function() {
}); });
}); });
it('should pass on msg if no payload', function(done) {
var flow = [{id:"jn1",type:"sentiment",wires:[["jn2"]]},
{id:"jn2", type:"helper"}];
helper.load(sentimentNode, flow, function() {
var jn1 = helper.getNode("jn1");
var jn2 = helper.getNode("jn2");
jn2.on("input", function(msg) {
msg.should.not.have.property('sentiment');
msg.topic.should.equal("pass on");
done();
});
var testString = 'good, great, best, brilliant';
jn1.receive({topic:"pass on"});
});
});
it('should add a positive score for good words', function(done) { it('should add a positive score for good words', function(done) {
var flow = [{id:"jn1",type:"sentiment",wires:[["jn2"]]}, var flow = [{id:"jn1",type:"sentiment",wires:[["jn2"]]},
{id:"jn2", type:"helper"}]; {id:"jn2", type:"helper"}];

View File

@ -106,6 +106,25 @@ describe('range Node', function() {
genericRangeTest("clamp", 0, 10, 0, 1000, false, -1, 0, done); genericRangeTest("clamp", 0, 10, 0, 1000, false, -1, 0, done);
}); });
it('just passes on msg if payload not present', function(done) {
var flow = [{"id":"rangeNode1","type":"range","minin":0,"maxin":100,"minout":0,"maxout":100,"action":"scale","round":true,"name":"rangeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(rangeNode, flow, function() {
var rangeNode1 = helper.getNode("rangeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.should.not.have.property('payload');
msg.topic.should.equal("pass on");
done();
} catch(err) {
done(err);
}
});
rangeNode1.receive({topic:"pass on"});
});
});
it('reports if input is not a number', function(done) { it('reports if input is not a number', function(done) {
var flow = [{"id":"rangeNode1","type":"range","minin":0,"maxin":0,"minout":0,"maxout":0,"action":"scale","round":true,"name":"rangeNode","wires":[["helperNode1"]]}, var flow = [{"id":"rangeNode1","type":"range","minin":0,"maxin":0,"minout":0,"maxout":0,"action":"scale","round":true,"name":"rangeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}]; {id:"helperNode1", type:"helper", wires:[]}];