Skip to content

Commit

Permalink
Tests: updates for serialport 6; updates for sinon ^4.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Dec 18, 2017
1 parent 896f5d4 commit d1939f0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 52 deletions.
25 changes: 12 additions & 13 deletions lib/com.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var Emitter = require("events").EventEmitter;
const Emitter = require("events");

function Mock(path) {
this.isClosed = false;
function Mock(path, options, openCallback) {
this.isOpen = true;
this.baudRate = 0;
this.path = path;
}

Mock.prototype = Object.create(Emitter.prototype, {
Expand All @@ -15,23 +17,20 @@ Mock.prototype.write = function (buffer) {
// this shouldn"t impact the data, just the container
// This also should be changed in future test rewrites
if (Buffer.isBuffer(buffer)) {
buffer = Array.prototype.slice.call(buffer, 0);
buffer = Array.from(buffer);
}

this.lastWrite = buffer;
this.emit("write", buffer);
};

/* istanbul ignore next */
Mock.prototype.close = function () {
this.isClosed = true;
};

var com;
var sp;
var stub = {
let com;
let sp;
let stub = {
SerialPort: Mock,
list: function() {},
list() {
return Promise.resolve([]);
},
};

try {
Expand Down
26 changes: 17 additions & 9 deletions lib/firmata.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,15 @@ function Board(port, options, callback) {
samplingInterval: 19,
serialport: {
baudRate: 57600,
bufferSize: 256,
// https://github.com/node-serialport/node-serialport/blob/5.0.0/UPGRADE_GUIDE.md#open-options
highWaterMark: 256,
},
};

if (options.bufferSize) {
options.highWaterMark = options.bufferSize;
}

var settings = Object.assign({}, defaults, options);

this.isReady = false;
Expand Down Expand Up @@ -557,18 +562,21 @@ function Board(port, options, callback) {
// For backward compat
this.sp = this.transport;

this.transport.on("close", function() {
this.emit("close");
}.bind(this));
this.transport.on("close", function(event) {

this.transport.on("disconnect", function() {
this.emit("disconnect");
// https://github.com/node-serialport/node-serialport/blob/5.0.0/UPGRADE_GUIDE.md#opening-and-closing
if (event && event.disconnect && event.disconnected) {
this.emit("disconnect");
return;
}

this.emit("close");
}.bind(this));

this.transport.on("open", function() {
this.emit("open");
this.transport.on("open", function(event) {
this.emit("open", event);
// Legacy
this.emit("connect");
this.emit("connect", event);
}.bind(this));

this.transport.on("error", function(error) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"mocha": "^2.3.x",
"nyc": "^10.0.0",
"should": "^7.1.x",
"sinon": "~1.17.1",
"sinon": "^4.1.3",
"webpack": "^1.12.14"
},
"dependencies": {
Expand Down
1 change: 0 additions & 1 deletion test/unit/com.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ describe("com.*", function() {

it("com.list", function(done) {
assert.equal(typeof com.list, "function");
assert.equal(com.list(), undefined);
done();
});
});
59 changes: 33 additions & 26 deletions test/unit/firmata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ describe("Board.requestPort", function() {
};

beforeEach(function() {
sandbox.stub(com, "list", function(callback) {
process.nextTick(function() {
sandbox.stub(com, "list").callsFake(callback => {
process.nextTick(() => {
callback(response.error, [response.port]);
});
});
Expand Down Expand Up @@ -702,7 +702,7 @@ describe("Board: lifecycle", function() {

var SerialPort = sandbox.spy(com, "SerialPort");
var transportWrite = sandbox.spy(SerialPort.prototype, "write");
var initCallback = sandbox.spy(function(error) {
var initCallback = sandbox.spy(error => {
assert.equal(typeof error, "undefined");
});
var initNoop = sandbox.spy();
Expand Down Expand Up @@ -757,10 +757,10 @@ describe("Board: lifecycle", function() {
var b = new Board("/path/to/fake/usb2", initNoop);

assert.equal(transport.spy.getCall(0).args[0], "/path/to/fake/usb1");
assert.deepEqual(transport.spy.getCall(0).args[1], { baudRate: 57600, bufferSize: 256 });
assert.deepEqual(transport.spy.getCall(0).args[1], { baudRate: 57600, highWaterMark: 256 });

assert.equal(transport.spy.getCall(1).args[0], "/path/to/fake/usb2");
assert.deepEqual(transport.spy.getCall(1).args[1], { baudRate: 57600, bufferSize: 256 });
assert.deepEqual(transport.spy.getCall(1).args[1], { baudRate: 57600, highWaterMark: 256 });

done();
});
Expand All @@ -770,7 +770,7 @@ describe("Board: lifecycle", function() {
var board = new Board(port, function(err) {});

assert.deepEqual(
transport.spy.args, [ [ "fake port", { baudRate: 57600, bufferSize: 256 } ] ]
transport.spy.args, [ [ "fake port", { baudRate: 57600, highWaterMark: 256 } ] ]
);

done();
Expand All @@ -782,13 +782,13 @@ describe("Board: lifecycle", function() {
reportVersionTimeout: 1,
serialport: {
baudRate: 5,
bufferSize: 10
highWaterMark: 10
}
};
var board = new Board(port, opt, function(err) {});

assert.deepEqual(
transport.spy.args, [ [ "fake port", { baudRate: 5, bufferSize: 10 } ] ]
transport.spy.args, [ [ "fake port", { baudRate: 5, highWaterMark: 10 } ] ]
);

done();
Expand Down Expand Up @@ -904,7 +904,11 @@ describe("Board: lifecycle", function() {

board.on("disconnect", done);

transport.emit("disconnect");
// https://github.com/node-serialport/node-serialport/blob/5.0.0/UPGRADE_GUIDE.md#opening-and-closing
transport.emit("close", {
disconnect: true,
disconnected: true,
});
});

it("forwards 'error' event from transport", function(done) {
Expand Down Expand Up @@ -997,19 +1001,20 @@ describe("Board: lifecycle", function() {
});

it("Optionally call setSamplingInterval after queryfirmware", function(done) {
var spy = sandbox.spy(Board.prototype, "setSamplingInterval");
sandbox.spy(Board.prototype, "setSamplingInterval");
sandbox.spy(SerialPort.prototype, "write");

var transport = new SerialPort("/path/to/fake/usb");
var options = {
skipCapabilities: true,
samplingInterval: 100
};

var board = new Board(transport, options, function(err) {
assert.deepEqual(transport.lastWrite, [ 240, 122, 100, 0, 247 ]);
assert.equal(spy.callCount, 1);
assert.ok(spy.calledWith(100));

spy.restore();
var board = new Board(transport, options, error => {
assert.deepEqual(Array.from(transport.write.lastCall.args[0]), [
0xf0, 0x7a, 0x64, 0x00, 0xf7
]);
assert.equal(board.setSamplingInterval.callCount, 1);
assert.ok(board.setSamplingInterval.calledWith(100));
done();
});

Expand All @@ -1025,15 +1030,17 @@ describe("Board: lifecycle", function() {
});

it("Does not call setSamplingInterval after queryfirmware by default", function(done) {
var spy = sandbox.spy(Board.prototype, "setSamplingInterval");
sandbox.spy(Board.prototype, "setSamplingInterval");
sandbox.spy(SerialPort.prototype, "write");

var transport = new SerialPort("/path/to/fake/usb");
var options = {
skipCapabilities: true,
};

var board = new Board(transport, options, function() {
assert.equal(spy.callCount, 0);
spy.restore();
var board = new Board(transport, options, () => {
assert.equal(board.setSamplingInterval.callCount, 0);
assert.equal(transport.write.callCount, 0);
done();
});

Expand Down Expand Up @@ -1099,23 +1106,23 @@ describe("Board: lifecycle", function() {
board.pins.forEach(function(pin, index) {
if (index >= 2 && index <= 19) {

pin.supportedModes.indexOf(0).should.not.equal(-1);
pin.supportedModes.indexOf(1).should.not.equal(-1);
assert.notEqual(pin.supportedModes.indexOf(0), -1);
assert.notEqual(pin.supportedModes.indexOf(1), -1);
} else {
assert.equal(pin.supportedModes.length, 0);
}
if (index >= 14 && index <= 19) {
pin.supportedModes.indexOf(0x02).should.not.equal(-1);
assert.notEqual(pin.supportedModes.indexOf(0x02), -1);
} else {
assert.equal(pin.supportedModes.indexOf(0x02), -1);
}
if ([3, 5, 6, 10, 11].indexOf(index) > -1) {
pin.supportedModes.indexOf(0x03).should.not.equal(-1);
assert.notEqual(pin.supportedModes.indexOf(0x03), -1);
} else {
assert.equal(pin.supportedModes.indexOf(0x03), -1);
}
if (index >= 2) {
pin.supportedModes.indexOf(0x04).should.not.equal(-1);
assert.notEqual(pin.supportedModes.indexOf(0x04), -1);
}
});
done();
Expand Down
4 changes: 2 additions & 2 deletions test/unit/onewireutils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ describe("OneWire.crc8/OneWire.readDevices", function () {

it("detects and logs invalid ROM", function (done) {

sandbox.stub(console, "error", () => {});
sandbox.stub(OneWire, "crc8", () => null);
sandbox.stub(console, "error").callsFake(() => {});
sandbox.stub(OneWire, "crc8").callsFake(() => null);

var input = Encoder7Bit.to7BitArray([0x28, 0xDB, 0xEF, 0x21, 0x05, 0x00, 0x00, 0x5D, 0x28, 0xDB, 0xEF, 0x21, 0x05, 0x00, 0x00, 0x5D, 0x00, 0x01, 0x02]);
var devices = OneWire.readDevices(input);
Expand Down

0 comments on commit d1939f0

Please sign in to comment.