Skip to content

Commit

Permalink
Always produce an instance
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Mar 14, 2016
1 parent f40bea6 commit 8e5c1f1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/firmata.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,14 +416,19 @@ SYSEX_RESPONSE[SERIAL_MESSAGE] = function(board) {
* @property currentBuffer An array holding the current bytes received from the arduino.
* @property {SerialPort} sp The serial port object used to communicate with the arduino.
*/
function Board(port, options, callback) {
Emitter.call(this);

function Board(port, options, callback) {
if (typeof options === "function" || typeof options === "undefined") {
callback = options;
options = {};
}

if (!(this instanceof Board)) {
return new Board(port, options, callback);
}

Emitter.call(this);

var board = this;
var defaults = {
reportVersionTimeout: 5000,
Expand Down
28 changes: 28 additions & 0 deletions test/firmata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,34 @@ describe("Board: data handling", function() {
});
});

describe("Board: initialization", function() {
it("Always returns a Board instance", function(done) {
var boards = [
new Board("/path/to/fake1"),
Board("/path/to/fake2"),
];

boards.forEach(function(board) {
assert.equal(board instanceof Board, true);
});

done();
});

it("Is a subclass of EventEmitter", function(done) {

var boards = [
new Board("/path/to/fake1"),
Board("/path/to/fake2"),
];

boards.forEach(function(board) {
assert.equal(board instanceof Emitter, true);
});
done();
});
});

describe("Board: lifecycle", function() {

var SerialPort = sandbox.spy(com, "SerialPort");
Expand Down

0 comments on commit 8e5c1f1

Please sign in to comment.