mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
Fix random node to handle to or from being 0
and add tests to close #955
This commit is contained in:
parent
5aa99eca73
commit
584590b9a6
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name" : "node-red-node-random",
|
"name" : "node-red-node-random",
|
||||||
"version" : "0.4.0",
|
"version" : "0.4.1",
|
||||||
"description" : "A Node-RED node that when triggered generates a random number between two values.",
|
"description" : "A Node-RED node that when triggered generates a random number between two values.",
|
||||||
"dependencies" : {
|
"dependencies" : {
|
||||||
},
|
},
|
||||||
|
@ -18,9 +18,8 @@ module.exports = function(RED) {
|
|||||||
if (node.low) { // if the the node has a value use it
|
if (node.low) { // if the the node has a value use it
|
||||||
tmp.low = Number(node.low);
|
tmp.low = Number(node.low);
|
||||||
} else if ('from' in msg) { // else see if a 'from' is in the msg
|
} else if ('from' in msg) { // else see if a 'from' is in the msg
|
||||||
if (Number(msg.from)) { // if it is, and is a number, use it
|
|
||||||
tmp.low = Number(msg.from);
|
tmp.low = Number(msg.from);
|
||||||
} else { // otherwise setup NaN error
|
if (isNaN(msg.from)) { // if it isn't a number setup NaN error
|
||||||
tmp.low = NaN;
|
tmp.low = NaN;
|
||||||
tmp.low_e = " From: " + msg.from; // setup to show bad incoming msg.from
|
tmp.low_e = " From: " + msg.from; // setup to show bad incoming msg.from
|
||||||
}
|
}
|
||||||
@ -31,9 +30,8 @@ module.exports = function(RED) {
|
|||||||
if (node.high) { // if the the node has a value use it
|
if (node.high) { // if the the node has a value use it
|
||||||
tmp.high = Number(node.high);
|
tmp.high = Number(node.high);
|
||||||
} else if ('to' in msg) { // else see if a 'to' is in the msg
|
} else if ('to' in msg) { // else see if a 'to' is in the msg
|
||||||
if (Number(msg.to)) { // if it is, and is a number, use it
|
|
||||||
tmp.high = Number(msg.to);
|
tmp.high = Number(msg.to);
|
||||||
} else { // otherwise setup NaN error
|
if (isNaN(msg.to)) { // if it isn't a number setup NaN error
|
||||||
tmp.high = NaN
|
tmp.high = NaN
|
||||||
tmp.high_e = " To: " + msg.to // setup to show bad incoming msg.to
|
tmp.high_e = " To: " + msg.to // setup to show bad incoming msg.to
|
||||||
}
|
}
|
||||||
|
@ -204,6 +204,46 @@ describe('random node', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it ("Test i5a (integer) - msg From = '0' To = '2' node no entries", function(done) {
|
||||||
|
var flow = [{id:"n1", type:"random", low: "", high: "", inte:true, wires:[["n2"]] },
|
||||||
|
{id:"n2", type:"helper"} ];
|
||||||
|
helper.load(testNode, flow, function() {
|
||||||
|
var n1 = helper.getNode("n1");
|
||||||
|
var n2 = helper.getNode("n2");
|
||||||
|
n2.on("input", function(msg) {
|
||||||
|
try {
|
||||||
|
//console.log(msg);
|
||||||
|
msg.should.have.a.property("payload");
|
||||||
|
msg.payload.should.be.within(0,2);
|
||||||
|
msg.payload.toString().indexOf(".").should.equal(-1); // slightly dumb test to see if it really is an integer and not a float...
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
catch(err) { done(err); }
|
||||||
|
});
|
||||||
|
n1.emit("input", {"test":"Test i5", "from": '0', "to": '2'});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it ("Test i5b (integer) - msg From = '-3' To = '0' node no entries", function(done) {
|
||||||
|
var flow = [{id:"n1", type:"random", low: "", high: "", inte:true, wires:[["n2"]] },
|
||||||
|
{id:"n2", type:"helper"} ];
|
||||||
|
helper.load(testNode, flow, function() {
|
||||||
|
var n1 = helper.getNode("n1");
|
||||||
|
var n2 = helper.getNode("n2");
|
||||||
|
n2.on("input", function(msg) {
|
||||||
|
try {
|
||||||
|
//console.log(msg);
|
||||||
|
msg.should.have.a.property("payload");
|
||||||
|
msg.payload.should.be.within(-3,0);
|
||||||
|
msg.payload.toString().indexOf(".").should.equal(-1); // slightly dumb test to see if it really is an integer and not a float...
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
catch(err) { done(err); }
|
||||||
|
});
|
||||||
|
n1.emit("input", {"test":"Test i5", "from": '-3', "to": '0'});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it ("Test f5 (float) - msg From = '6' To = '9' node no entries", function(done) {
|
it ("Test f5 (float) - msg From = '6' To = '9' node no entries", function(done) {
|
||||||
var flow = [{id:"n1", type:"random", low: "", high: "", inte:false, wires:[["n2"]] },
|
var flow = [{id:"n1", type:"random", low: "", high: "", inte:false, wires:[["n2"]] },
|
||||||
{id:"n2", type:"helper"} ];
|
{id:"n2", type:"helper"} ];
|
||||||
|
Loading…
Reference in New Issue
Block a user