Skip to content

Commit

Permalink
add tests for serialport-comms
Browse files Browse the repository at this point in the history
  • Loading branch information
noopkat committed Aug 8, 2015
1 parent 10554d5 commit 59d6e9f
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/serialport-comms.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ serialCom.prototype.setUpInterface = function(callback) {
var self = this;
this.device.open(function() {
self.device.on('data', function(data) {
self.debug('data', data)
self.debug('data', data);
self.responses.push(data);
});
self.sync(function(error) {
Expand All @@ -35,7 +35,7 @@ serialCom.prototype.close = function() {
serialCom.prototype.write = function(data, callback) {
var self = this;
// this will need to swap between 200 and 1000 depending on the command
var drainDelay = 200;
var drainDelay = 800;

this.device.write(data, function(error, results) {
if (error) { return callback(new Error(error)); }
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"proxyquire": "^1.6.0",
"sinon": "^1.15.4",
"tap-spec": "^4.0.2",
"tape": "^4.0.1"
"tape": "^4.0.1",
"virtual-serialport": "^0.1.1"
}
}
1 change: 1 addition & 0 deletions tests/avrgirl-stk500v2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,5 @@ test('[ AVRGIRL-STK500V2 ] ::getChipSignature', function (t) {
});

require('./libusb-comms.spec');
require('./serialport-comms.spec');

112 changes: 112 additions & 0 deletions tests/serialport-comms.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// harness
var test = require('tape');
// test helpers
var sinon = require('sinon');
var bufferEqual = require('buffer-equal');
var vserial = require('virtual-serialport');
//var vserialport = vserial.SerialPort;

// module to test
var serialcom = require('../lib/serialport-comms');

var device = new vserial('/dev/cu.usbmodem1411', {
baudrate: 115200
}, false);
// stubby stub
device.open = function(callback) {
if (callback) {
return callback(null);
}
return;
};
device.close = function(callback) {
if (callback) {
return callback(null);
}
return;
};
device.drain = function(callback) {
if (callback) {
return callback(null);
}
return;
};

test('[ SERIALPORT-COMMS ] method presence', function (t) {
var b = new serialcom(device);
function isFn(name) {
return typeof b[name] === 'function';
};
var methods = [
'open',
'close',
'setUpInterface',
'read',
'write'
];
for (var i = 0; i < methods.length; i += 1) {
t.ok(isFn(methods[i]), methods[i]);
if (i === (methods.length - 1)) {
t.end();
}
}
});

test('[ SERIALPORT-COMMS ] ::open', function (t) {
var b = new serialcom(device);

var spy = sinon.spy(device, 'open');

t.plan(1);
b.open();
t.ok(spy.calledOnce, 'called device.open');
});

test('[ SERIALPORT-COMMS ] ::close', function (t) {
var b = new serialcom(device);
var spy = sinon.spy(device, 'close');

t.plan(1);
b.close();
t.ok(spy.calledOnce, 'called device.close');
});

test('[ SERIALPORT-COMMS ] ::write', function (t) {
var b = new serialcom(device);
var buf = new Buffer([0x01]);

t.plan(1);

var spy = sinon.spy(device, 'write');
b.write(buf);
var cond = (spy.calledOnce && spy.args[0][0] && bufferEqual(spy.args[0][0], buf));
t.ok(cond, 'called write method on correct endpoint with correct buffer arg');
});

test('[ SERIALPORT-COMMS ] ::read', function (t) {
var b = new serialcom(device);
b.responses.push(new Buffer(8));
var spy = sinon.spy(b, 'read');
t.plan(3);

b.read(8, function(error, data){
t.error(error, 'no error');
t.ok(spy.calledWith(8), 'called read method on correct endpoint with arg 8');
t.equals(data.length, 8, 'got data');
});
});

test('[ SERIALPORT-COMMS ] ::setUpInterface', function (t) {
var b = new serialcom(device);
var stub = sinon.stub(b, 'sync', function(callback) {
return callback(null);
});

t.plan(3);

b.setUpInterface(function (error) {
t.pass('interface set up and called back');
t.error(error, 'no error');
t.ok(stub.calledOnce, 'sync method called');
});
});

0 comments on commit 59d6e9f

Please sign in to comment.