2 * Copyright 2014 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 var should = require("should");
19 var rangeNode = require("../../../../nodes/core/logic/16-range.js");
20 var helper = require("../../helper.js");
22 describe('RangeNode', function() {
24 beforeEach(function(done) {
25 helper.startServer(done);
28 afterEach(function(done) {
30 helper.stopServer(done);
33 it('should be loaded', function(done) {
34 var flow = [{"id":"rangeNode1","type":"range","minin":"0","maxin":"99","minout":"100","maxout":"199","action":"range","round":true,"name":"rangeNode","wires":[[]]}];
35 helper.load(rangeNode, flow, function() {
36 var rangeNode1 = helper.getNode("rangeNode1");
37 rangeNode1.should.have.property('name', 'rangeNode');
43 * Run a generic range test
44 * @param action - scale/clamp (range limit)/roll (modulo): what action to choose
45 * @param minin - map from minimum value
46 * @param maxin - map from maximum value
47 * @param minout - map to minimum value
48 * @param maxout - map to maximum value
49 * @param round - whether to round the result to the nearest integer
50 * @param aPayload - what payload to send to the range node
51 * @param expectedResult - what result we're expecting
52 * @param done - the callback to call when test done
54 function genericRangeTest(action, minin, maxin, minout, maxout, round, aPayload, expectedResult, done) {
55 var flow = [{"id":"rangeNode1","type":"range","minin":minin,"maxin":maxin,"minout":minout,"maxout":maxout,"action":action,"round":round,"name":"rangeNode","wires":[["helperNode1"]]},
56 {id:"helperNode1", type:"helper", wires:[]}];
57 helper.load(rangeNode, flow, function() {
58 var rangeNode1 = helper.getNode("rangeNode1");
59 var helperNode1 = helper.getNode("helperNode1");
60 helperNode1.on("input", function(msg) {
62 msg.payload.should.equal(expectedResult);
68 rangeNode1.receive({payload:aPayload});
72 it('ranges numbers up tenfold', function(done) {
73 genericRangeTest("scale", 0, 100, 0, 1000, false, 50, 500, done);
76 it('ranges numbers down such as centimetres to metres', function(done) {
77 genericRangeTest("scale", 0, 100, 0, 1, false, 55, 0.55, done);
80 it('wraps numbers down say for degree/rotation reading 1/2', function(done) {
81 genericRangeTest("roll", 0, 10, 0, 360, true, 15, 180, done); // 1/2 around wrap => "one and a half turns"
84 it('wraps numbers around say for degree/rotation reading 1/3', function(done) {
85 genericRangeTest("roll", 0, 10, 0, 360, true, 13.3333, 120, done); // 1/3 around wrap => "one and a third turns"
88 it('wraps numbers around say for degree/rotation reading 1/4', function(done) {
89 genericRangeTest("roll", 0, 10, 0, 360, true, 12.5, 90, done); // 1/4 around wrap => "one and a quarter turns"
92 it('wraps numbers around say for degree/rotation reading 1/4', function(done) {
93 genericRangeTest("roll", 0, 10, 0, 360, true, 12.5, 90, done); // 1/4 around wrap => "one and a quarter turns"
96 it('wraps numbers down say for degree/rotation reading 1/4', function(done) {
97 genericRangeTest("roll", 0, 10, 0, 360, true, -12.5, 270, done); // 1/4 backwards wrap => "one and a quarter turns backwards"
100 it('clamps numbers within a range - over max', function(done) {
101 genericRangeTest("clamp", 0, 10, 0, 1000, false, 111, 1000, done);
104 it('clamps numbers within a range - below min', function(done) {
105 genericRangeTest("clamp", 0, 10, 0, 1000, false, -1, 0, done);
108 it('reports if input is not a number', function(done) {
109 var flow = [{"id":"rangeNode1","type":"range","minin":0,"maxin":0,"minout":0,"maxout":0,"action":"scale","round":true,"name":"rangeNode","wires":[["helperNode1"]]},
110 {id:"helperNode1", type:"helper", wires:[]}];
111 helper.load(rangeNode, flow, function() {
112 var rangeNode1 = helper.getNode("rangeNode1");
113 var helperNode1 = helper.getNode("helperNode1");
115 var sinon = require('sinon');
116 sinon.stub(rangeNode1, 'log', function(log) {
117 if(log.indexOf("Not a number") > -1) {
121 should.fail(null, null, "Non-number inputs should be reported!");
128 rangeNode1.receive({payload:"NOT A NUMBER"});