Skip to content

Commit

Permalink
Merge branch 'skip-normalization' into testing-controller-stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
dtex committed Oct 29, 2015
2 parents 4b1ffb2 + 6f4eedd commit 94eb633
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 29 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ language: node_js
node_js:
- "0.10"
- "0.12"
- "3.0"
- "4.0"
sudo: false
compiler: clang
Expand Down
12 changes: 12 additions & 0 deletions lib/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var __ = require("../lib/fn.js");
var Repl = require("../lib/repl.js");
var Options = require("../lib/board.options.js");
var Pins = require("../lib/board.pins.js");
var Expander;
//var temporal = require("temporal");
//var IO;

Expand Down Expand Up @@ -818,6 +819,17 @@ Board.Component = function(opts, componentOpts) {
}
}

if (!Expander) {
Expander = require("../lib/expander");
}

if (opts.controller && Expander.hasController(opts.controller)) {
componentOpts = {
normalizePin: false,
requestPin: false,
};
}

componentOpts = Board.Component.initialization(componentOpts);

if (componentOpts.normalizePin) {
Expand Down
4 changes: 4 additions & 0 deletions lib/expander.js
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,10 @@ Expander.byController = function(name) {
return controller;
};

Expander.hasController = function(key) {
return Controllers[key] !== undefined;
};

if (IS_TEST_MODE) {
Expander.purge = function() {
priv.clear();
Expand Down
63 changes: 35 additions & 28 deletions test/board.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ require("es6-shim");
var MockFirmata = require("./util/mock-firmata"),
five = require("../lib/johnny-five.js"),
sinon = require("sinon"),
Board = five.Board;
Board = five.Board,
Expander = five.Expander;

function newBoard() {
var io = new MockFirmata();
Expand All @@ -20,40 +21,25 @@ function newBoard() {
return board;
}

function restore(target) {
for (var prop in target) {

if (Array.isArray(target[prop])) {
continue;
}

if (target[prop] != null && typeof target[prop].restore === "function") {
target[prop].restore();
}

if (typeof target[prop] === "object") {
restore(target[prop]);
}
}
}

exports["Board.Component"] = {
setUp: function(done) {
this.board = newBoard();
this.sandbox = sinon.sandbox.create();
done();
},

tearDown: function(done) {
Board.purge();
restore(this);
Expander.purge();
this.sandbox.restore();
done();
},

callThroughs: function(test) {
test.expect(5);

var a = sinon.spy(Board, "mount");
var b = sinon.spy(Board.Pins, "normalize");
var a = this.sandbox.spy(Board, "mount");
var b = this.sandbox.spy(Board.Pins, "normalize");
var opts = {};

Board.purge();
Expand Down Expand Up @@ -152,6 +138,27 @@ exports["Board.Component"] = {
test.done();
},

noPinNormalization: function(test) {
test.expect(3);

var hasController = this.sandbox.stub(Expander, "hasController", function() {
return true;
});

var normalize = this.sandbox.spy(Board.Pins, "normalize");

var component = new Board.Component({
pin: 2,
controller: "FOO"
});

test.equal(component.pin, 2);
test.equal(hasController.callCount, 1);
test.equal(normalize.callCount, 0);

test.done();
},

explicitPinNormalized: function(test) {
test.expect(1);

Expand Down Expand Up @@ -203,7 +210,7 @@ exports["Board.Component"] = {

Board.Component.call(component, { pin: 1 }, { requestPin: false });

var spy = sinon.spy(component.board, "warn");
var spy = this.sandbox.spy(component.board, "warn");

Board.Component.call(component, { pin: 1 });

Expand All @@ -222,7 +229,7 @@ exports["Board.Component"] = {
pin: 1
});

var spy = sinon.spy(component.board, "warn");
var spy = this.sandbox.spy(component.board, "warn");

test.equal(component.board.occupied.length, 1);
test.deepEqual(component.board.occupied[0], {
Expand Down Expand Up @@ -349,7 +356,7 @@ exports["Board.Component"] = {
address: 0x00
});

var spy = sinon.spy(component.board, "warn");
var spy = this.sandbox.spy(component.board, "warn");

test.equal(component.board.occupied.length, 1);
test.deepEqual(component.board.occupied[0], {
Expand Down Expand Up @@ -389,7 +396,7 @@ exports["Board.Component"] = {
controller: "FOO"
});

var spy = sinon.spy(component.board, "warn");
var spy = this.sandbox.spy(component.board, "warn");

test.equal(component.board.occupied.length, 1);
test.deepEqual(component.board.occupied[0], {
Expand Down Expand Up @@ -430,7 +437,7 @@ exports["Board.Component"] = {
address: 0x01
});

var spy = sinon.spy(component.board, "warn");
var spy = this.sandbox.spy(component.board, "warn");

test.equal(component.board.occupied.length, 1);
test.deepEqual(component.board.occupied[0], {
Expand Down Expand Up @@ -471,7 +478,7 @@ exports["Board.Component"] = {
address: 0x01
});

var spy = sinon.spy(component.board, "warn");
var spy = this.sandbox.spy(component.board, "warn");

// No pins to occupy
test.equal(component.board.occupied.length, 0);
Expand All @@ -495,7 +502,7 @@ exports["Board.Component"] = {
pins: { a: 1, b: 2, c: 3 }
});

var spy = sinon.spy(component.board, "warn");
var spy = this.sandbox.spy(component.board, "warn");

test.equal(component.board.occupied.length, 3);
test.deepEqual(component.board.occupied[0], {
Expand Down
8 changes: 8 additions & 0 deletions test/esc.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ exports["ESC"] = {
exports["ESC - PCA9685"] = {
setUp: function(done) {
this.clock = sinon.useFakeTimers();
this.normalize = sinon.spy(Board.Pins, "normalize");
this.i2cWrite = sinon.spy(MockFirmata.prototype, "i2cWrite");
this.i2cConfig = sinon.spy(MockFirmata.prototype, "i2cConfig");
this.board = newBoard();
Expand Down Expand Up @@ -336,6 +337,13 @@ exports["ESC - PCA9685"] = {

test.done();
},

noNormalization: function(test) {
test.expect(1);
test.equal(this.normalize.callCount, 0);
test.done();
},

speed: function(test) {
test.expect(6);
this.i2cWrite.reset();
Expand Down
17 changes: 17 additions & 0 deletions test/expander.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ exports["Expander"] = {
test.done();
},

hasController: function(test) {
test.expect(6);

[
"MCP23017",
"MCP23008",
"PCF8574",
"PCF8574A",
"PCF8575",
"PCA9685",
].forEach(function(controller) {
test.equal(Expander.hasController(controller), true);
});

test.done();
},

skipsPinNormalizationCall: function(test) {
test.expect(6);

Expand Down
7 changes: 7 additions & 0 deletions test/led.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ exports["Led - PCA9685 (I2C)"] = {
setUp: function(done) {
this.clock = sinon.useFakeTimers();
this.board = newBoard();
this.normalize = sinon.spy(Board.Pins, "normalize");
this.i2cWrite = sinon.spy(this.board.io, "i2cWrite");
this.pinMode = sinon.spy(this.board.io, "pinMode");

Expand All @@ -397,6 +398,12 @@ exports["Led - PCA9685 (I2C)"] = {

shape: testLedShape,

noNormalization: function(test) {
test.expect(1);
test.equal(this.normalize.callCount, 0);
test.done();
},

defaultMode: function(test) {
test.expect(2);

Expand Down
7 changes: 7 additions & 0 deletions test/motor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,7 @@ exports["Motor: Inverse Speed With Brake"] = {
exports["Motor: I2C - PCA9685"] = {
setUp: function(done) {
this.board = newBoard();
this.normalize = sinon.spy(Board.Pins, "normalize");
this.i2cConfig = sinon.spy(MockFirmata.prototype, "i2cConfig");
this.i2cWrite = sinon.spy(MockFirmata.prototype, "i2cWrite");
this.motor = new Motor({
Expand Down Expand Up @@ -1252,6 +1253,12 @@ exports["Motor: I2C - PCA9685"] = {
test.done();
},

noNormalization: function(test) {
test.expect(1);
test.equal(this.normalize.callCount, 0);
test.done();
},

shape: function(test) {
test.expect(this.proto.length + this.instance.length);

Expand Down
7 changes: 7 additions & 0 deletions test/servo.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ exports["Servo - Allowed Pin Names"] = {
exports["Servo - PCA9685"] = {
setUp: function(done) {
this.board = newBoard();
this.normalize = sinon.spy(Board.Pins, "normalize");
this.i2cWrite = sinon.spy(MockFirmata.prototype, "i2cWrite");
this.i2cRead = sinon.spy(MockFirmata.prototype, "i2cRead");
this.i2cConfig = sinon.spy(MockFirmata.prototype, "i2cConfig");
Expand Down Expand Up @@ -655,6 +656,12 @@ exports["Servo - PCA9685"] = {
test.done();
},

noNormalization: function(test) {
test.expect(1);
test.equal(this.normalize.callCount, 0);
test.done();
},

to: function(test) {
test.expect(6);
this.i2cWrite.reset();
Expand Down

0 comments on commit 94eb633

Please sign in to comment.