node-serialport-bindings-cpp/test/parser-byte-length.js
Francis Gulotta d2bb973382
Transform parsers into streams
Parsers are now transform streams! I'm not 100% convinced this is easier to use but it is more clear about the mechanics. I think that might make it worthwhile. It also opens up a clear example to how to make your own parser.
2016-09-08 19:18:36 -04:00

53 lines
1.5 KiB
JavaScript

'use strict';
var assert = require('chai').assert;
var sinon = require('sinon');
var ByteLengthParser = require('../lib/parser-byte-length');
describe('ByteLengthParser', function() {
it('emits data events every 8 bytes', function() {
var data = new Buffer('Robots are so freaking cool!');
var spy = sinon.spy();
var parser = new ByteLengthParser({length: 8});
parser.on('data', spy);
parser.write(data);
assert.equal(spy.callCount, 3);
assert.deepEqual(spy.getCall(0).args[0], new Buffer('Robots a'));
assert.deepEqual(spy.getCall(1).args[0], new Buffer('re so fr'));
assert.deepEqual(spy.getCall(2).args[0], new Buffer('eaking c'));
});
it('throws when not provided with a length', function() {
assert.throws(function() {
new ByteLengthParser({});
});
});
it('throws when length is zero', function() {
assert.throws(function() {
new ByteLengthParser({
length: 0
});
});
});
it('throws when called with a non numeric length', function() {
assert.throws(function() {
new ByteLengthParser({
length: 'foop'
});
});
});
it('continues looking for bytes in additional writes', function() {
var parser = new ByteLengthParser({length: 4});
var spy = sinon.spy();
parser.on('data', spy);
parser.write(new Buffer('ab'));
parser.write(new Buffer('cd'));
assert.equal(spy.callCount, 1);
assert.deepEqual(spy.getCall(0).args[0], new Buffer('abcd'));
});
});