Skip to content

Commit

Permalink
Tests: refactor all, phase 1
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Jul 16, 2015
1 parent 838c313 commit 077784b
Show file tree
Hide file tree
Showing 29 changed files with 1,650 additions and 1,031 deletions.
47 changes: 25 additions & 22 deletions test/accelerometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,26 @@ function newBoard() {

io.emit("connect");
io.emit("ready");

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["Accelerometer -- Analog"] = {

Expand Down Expand Up @@ -61,8 +78,7 @@ exports["Accelerometer -- Analog"] = {

tearDown: function(done) {
Board.purge();
this.analogRead.restore();
this.clock.restore();
restore(this);
done();
},

Expand Down Expand Up @@ -221,8 +237,7 @@ exports["Accelerometer -- distinctZeroV"] = {

tearDown: function(done) {
Board.purge();
this.analogRead.restore();
this.clock.restore();
restore(this);
done();
},

Expand Down Expand Up @@ -262,8 +277,7 @@ exports["Accelerometer -- autoCalibrate"] = {

tearDown: function(done) {
Board.purge();
this.analogRead.restore();
this.clock.restore();
restore(this);
done();
},

Expand Down Expand Up @@ -309,8 +323,7 @@ exports["Accelerometer -- ADXL335"] = {

tearDown: function(done) {
Board.purge();
this.analogRead.restore();
this.clock.restore();
restore(this);
done();
},

Expand Down Expand Up @@ -358,10 +371,7 @@ exports["Accelerometer -- MPU-6050"] = {

tearDown: function(done) {
Board.purge();
this.i2cConfig.restore();
this.i2cWrite.restore();
this.i2cRead.restore();
this.clock.restore();
restore(this);
done();
},

Expand Down Expand Up @@ -444,10 +454,7 @@ exports["Accelerometer -- ADXL345"] = {

tearDown: function(done) {
Board.purge();
this.i2cConfig.restore();
this.i2cWrite.restore();
this.i2cRead.restore();
this.clock.restore();
restore(this);
done();
},

Expand Down Expand Up @@ -514,10 +521,7 @@ exports["Accelerometer -- MMA7361"] = {

tearDown: function(done) {
Board.purge();
this.analogRead.restore();
this.pinMode.restore();
this.digitalWrite.restore();
this.clock.restore();
restore(this);
done();
},

Expand Down Expand Up @@ -584,8 +588,7 @@ exports["Accelerometer -- ESPLORA"] = {

tearDown: function(done) {
Board.purge();
this.analogRead.restore();
this.clock.restore();
restore(this);
done();
},

Expand Down
53 changes: 42 additions & 11 deletions test/barometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@ var MockFirmata = require("./util/mock-firmata"),
five = require("../lib/johnny-five.js"),
sinon = require("sinon"),
Board = five.Board,
Barometer = five.Barometer,
board = new Board({
io: new MockFirmata(),
Barometer = five.Barometer;

function newBoard() {
var io = new MockFirmata();
var board = new Board({
io: io,
debug: false,
repl: false
});

io.emit("connect");
io.emit("ready");
return board;
}

exports["Barometer -- MPL115A2"] = {

setUp: function(done) {

this.i2cConfig = sinon.spy(board.io, "i2cConfig");
this.i2cWrite = sinon.spy(board.io, "i2cWrite");
this.i2cRead = sinon.spy(board.io, "i2cRead");
this.i2cReadOnce = sinon.spy(board.io, "i2cReadOnce");
this.board = newBoard();
this.i2cConfig = sinon.spy(MockFirmata.prototype, "i2cConfig");
this.i2cWrite = sinon.spy(MockFirmata.prototype, "i2cWrite");
this.i2cRead = sinon.spy(MockFirmata.prototype, "i2cRead");
this.i2cReadOnce = sinon.spy(MockFirmata.prototype, "i2cReadOnce");

this.barometer = new Barometer({
controller: "MPL115A2",
board: board,
board: this.board,
freq: 10
});

Expand All @@ -32,13 +40,35 @@ exports["Barometer -- MPL115A2"] = {
},

tearDown: function(done) {
Board.purge();
this.i2cConfig.restore();
this.i2cWrite.restore();
this.i2cRead.restore();
this.i2cReadOnce.restore();
done();
},

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

this.i2cConfig.reset();

new Barometer({
controller: "MPL115A2",
address: 0xff,
bus: "i2c-1",
board: this.board
});

var forwarded = this.i2cConfig.lastCall.args[0];

test.equal(this.i2cConfig.callCount, 1);
test.equal(forwarded.address, 0xff);
test.equal(forwarded.bus, "i2c-1");

test.done();
},

data: function(test) {
test.expect(8);

Expand Down Expand Up @@ -90,14 +120,14 @@ exports["Barometer -- MPL115A2"] = {
exports["Barometer -- BMP180"] = {

setUp: function(done) {

this.board = newBoard();
this.i2cConfig = sinon.spy(MockFirmata.prototype, "i2cConfig");
this.i2cWriteReg = sinon.spy(MockFirmata.prototype, "i2cWriteReg");
this.i2cReadOnce = sinon.spy(MockFirmata.prototype, "i2cReadOnce");

this.barometer = new Barometer({
controller: "BMP180",
board: board,
board: this.board,
freq: 10
});

Expand All @@ -109,6 +139,7 @@ exports["Barometer -- BMP180"] = {
},

tearDown: function(done) {
Board.purge();
this.i2cConfig.restore();
this.i2cWriteReg.restore();
this.i2cReadOnce.restore();
Expand Down
49 changes: 35 additions & 14 deletions test/board.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,46 @@ var MockFirmata = require("./util/mock-firmata"),
sinon = require("sinon"),
Board = five.Board;

function newBoard() {
var io = new MockFirmata();
var board = new Board({
io: io,
debug: false,
repl: false
});

io.emit("connect");
io.emit("ready");

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.io = new MockFirmata();
this.board = new Board({
io: this.io,
debug: false,
repl: false
});
this.board = newBoard();
done();
},

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

Expand All @@ -33,11 +58,7 @@ exports["Board.Component"] = {

Board.purge();

var board = new Board({
io: this.io,
debug: false,
repl: false
});
var board = newBoard();

Board.Component.call({}, opts);

Expand All @@ -62,7 +83,7 @@ exports["Board.Component"] = {

test.equal(typeof component.id, "string");
test.equal(component.board, this.board);
test.equal(component.io, this.io);
test.equal(component.io, this.board.io);

test.done();
},
Expand All @@ -76,7 +97,7 @@ exports["Board.Component"] = {

test.equal(typeof component.id, "string");
test.equal(component.board, this.board);
test.equal(component.io, this.io);
test.equal(component.io, this.board.io);

test.done();
},
Expand Down
Loading

0 comments on commit 077784b

Please sign in to comment.