diff --git a/lib/board.js b/lib/board.js index 5957b2735..cb8bd9865 100644 --- a/lib/board.js +++ b/lib/board.js @@ -104,6 +104,10 @@ var Serial = { // If no ports are detected... if (!length) { + if (IS_TEST_MODE && this.abort) { + return; + } + // Create an attempt counter if (!Serial.attempts[Serial.used.length]) { Serial.attempts[Serial.used.length] = 0; @@ -484,7 +488,6 @@ util.inherits(Board, Emitter); }; }); - Board.prototype.serialize = function(filter) { var blacklist = this.serialize.blacklist; var special = this.serialize.special; @@ -1137,6 +1140,9 @@ if (IS_TEST_MODE) { Board.purge = function() { Board.Pins.normalize.clear(); + Repl.isActive = false; + Repl.isBlocked = true; + Repl.ref = null; boards.length = 0; }; } diff --git a/test/board.js b/test/board.js index bcc163a7a..b0c943762 100644 --- a/test/board.js +++ b/test/board.js @@ -2,7 +2,7 @@ require("es6-shim"); var MockFirmata = require("./util/mock-firmata"), - // SerialPort = require("./util/mock-serial").SerialPort, + SerialPort = require("./util/mock-serial").SerialPort, five = require("../lib/johnny-five.js"), sinon = require("sinon"), __ = require("../lib/fn.js"), @@ -44,24 +44,28 @@ exports["Board"] = { done(); }, - // explicit: function(test) { - // test.expect(1); + explicit: function(test) { + test.expect(1); - // var sp = new SerialPort("/dev/foo", { - // baudrate: 57600, - // buffersize: 128 - // }); + var sp = new SerialPort("/dev/foo", { + baudrate: 57600, + buffersize: 128 + }); - // var board = new Board({ - // port: sp, - // debug: false, - // repl: false - // }); + var board = new Board({ + port: sp, + debug: false, + repl: false + }); - // test.equal(board.io.sp, sp); + test.equal(board.io.sp, sp); - // test.done(); - // }, + board.abort = true; + + setImmediate(function() { + test.done(); + }); + }, ioIsReady: function(test) { test.expect(2); @@ -322,6 +326,7 @@ exports["Boards"] = { tearDown: function(done) { Board.purge(); + if (this.replInit) { this.replInit.restore(); } @@ -419,7 +424,7 @@ exports["Boards"] = { }]); boards.on("ready", function() { - test.equal(this.replInit.called, true); + test.equal(this.replInit.callCount, 1); test.done(); }.bind(this)); diff --git a/test/esc.js b/test/esc.js index f0cc79217..60ed95a69 100644 --- a/test/esc.js +++ b/test/esc.js @@ -5,25 +5,26 @@ var MockFirmata = require("./util/mock-firmata"), Board = five.Board, ESC = five.ESC; - -var io = new MockFirmata(); -var board = new Board({ - io: io, - debug: false, - repl: false -}); - -io.emit("ready"); - exports["ESC"] = { setUp: function(done) { this.clock = sinon.useFakeTimers(); - this.servoWrite = sinon.spy(board.io, "servoWrite"); + this.servoWrite = sinon.spy(MockFirmata.prototype, "servoWrite"); + + var io = new MockFirmata(); + this.board = new Board({ + io: io, + debug: false, + repl: false + }); + + io.emit("ready"); + this.esc = new ESC({ pin: 12, - board: board + board: this.board }); + this.proto = [{ name: "speed" }, { @@ -52,6 +53,7 @@ exports["ESC"] = { }, tearDown: function(done) { + Board.purge(); this.clock.restore(); this.servoWrite.restore(); @@ -91,7 +93,7 @@ exports["ESC"] = { this.esc = new ESC({ pin: 12, - board: board, + board: this.board, startAt: 1 }); @@ -240,11 +242,21 @@ exports["ESC"] = { exports["ESC - PCA9685"] = { setUp: function(done) { this.clock = sinon.useFakeTimers(); - this.writeSpy = sinon.spy(board.io, "i2cWrite"); - this.readSpy = sinon.spy(board.io, "i2cRead"); + this.writeSpy = sinon.spy(MockFirmata.prototype, "i2cWrite"); + this.readSpy = sinon.spy(MockFirmata.prototype, "i2cRead"); + + var io = new MockFirmata(); + this.board = new Board({ + io: io, + debug: false, + repl: false + }); + + io.emit("ready"); + this.esc = new ESC({ pin: 0, - board: board, + board: this.board, controller: "PCA9685", address: 0x40 }); @@ -253,6 +265,7 @@ exports["ESC - PCA9685"] = { }, tearDown: function(done) { + Board.purge(); this.writeSpy.restore(); this.readSpy.restore(); this.clock.restore(); @@ -264,7 +277,7 @@ exports["ESC - PCA9685"] = { var esc = new ESC({ pin: 0, - board: board, + board: this.board, controller: "PCA9685", address: 0x40 }); @@ -278,7 +291,7 @@ exports["ESC - PCA9685"] = { var esc = new ESC({ pin: 0, - board: board, + board: this.board, controller: "PCA9685" }); @@ -307,10 +320,20 @@ exports["ESC - PCA9685"] = { exports["ESC - FORWARD_REVERSE"] = { setUp: function(done) { this.clock = sinon.useFakeTimers(); + var io = new MockFirmata(); + this.board = new Board({ + io: io, + debug: false, + repl: false + }); + + io.emit("ready"); + done(); }, tearDown: function(done) { + Board.purge(); this.clock.restore(); done(); }, @@ -321,8 +344,9 @@ exports["ESC - FORWARD_REVERSE"] = { new ESC({ device: "FORWARD_REVERSE", pin: 11, - }); - }); + board: this.board + }.bind(this)); + }.bind(this)); test.done(); }, @@ -334,7 +358,7 @@ exports["ESC - FORWARD_REVERSE"] = { device: "FORWARD_REVERSE", neutral: 50, pin: 11, - board: board, + board: this.board, }); test.ok(spy.calledOnce); @@ -352,7 +376,7 @@ exports["ESC - FORWARD_REVERSE"] = { device: "FORWARD_REVERSE", neutral: 50, pin: 11, - board: board, + board: this.board, }); spy.reset(); @@ -378,7 +402,7 @@ exports["ESC - FORWARD_REVERSE"] = { device: "FORWARD_REVERSE", neutral: 50, pin: 11, - board: board, + board: this.board, }); spy.reset(); @@ -403,7 +427,7 @@ exports["ESC - FORWARD_REVERSE"] = { device: "FORWARD_REVERSE", neutral: 50, pin: 11, - board: board, + board: this.board, }); var spy = sinon.spy(esc, "write"); @@ -423,27 +447,31 @@ exports["ESC - FORWARD_REVERSE"] = { exports["ESC.Array"] = { setUp: function(done) { - var board = new Board({ - io: new MockFirmata(), + + var io = new MockFirmata(); + this.board = new Board({ + io: io, debug: false, repl: false }); + io.emit("ready"); + ESC.purge(); this.a = new ESC({ pin: 3, - board: board + board: this.board }); this.b = new ESC({ pin: 6, - board: board + board: this.board }); this.c = new ESC({ pin: 9, - board: board + board: this.board }); this.spies = [ @@ -459,6 +487,7 @@ exports["ESC.Array"] = { }, tearDown: function(done) { + Board.purge(); this.spies.forEach(function(value) { this[value].restore(); }.bind(this)); diff --git a/test/repl.js b/test/repl.js index 79908a5ea..1848cc552 100644 --- a/test/repl.js +++ b/test/repl.js @@ -4,6 +4,13 @@ var MockFirmata = require("./util/mock-firmata"), Board = five.Board; exports["Repl"] = { + setUp: function(done) { + done(); + }, + tearDown: function(done) { + Board.purge(); + done(); + }, repl: function(test) { var io = new MockFirmata(); var board = new Board({ @@ -17,8 +24,6 @@ exports["Repl"] = { test.ok(this.repl === board.repl); test.ok(this.repl instanceof Repl); test.ok(this.repl.context); - Repl.isBlocked = true; - Board.purge(); test.done(); });