Skip to content

Commit

Permalink
Complete Telegram documentation and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwippermann committed Feb 14, 2014
1 parent 1f3e1a0 commit 45fa115
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,23 @@ var optionKeys = [



var Telegram = Header.extend({
var Telegram = Header.extend(/** @lends Telegram# */ {

/**
* The VBus command of this Telegram instance.
* @type {number}
*/
command: 0,

frameData: null,

/**
* Creates a new Telegram instance.
*
* @constructs
* @augments Header
* @param {object} options Initialization options.
*/
constructor: function(options) {
Header.call(this, options);

Expand All @@ -48,11 +59,9 @@ var Telegram = Header.extend({
var buffer;
if (origBuffer === undefined) {
buffer = new Buffer(length);
} else if (start + length <= end) {
buffer = origBuffer.slice(start, start + length);
} else {
buffer = origBuffer.slice(start, end);
}

if (buffer.length < length) {
throw new Error('Buffer too small');
}

Expand Down Expand Up @@ -94,7 +103,7 @@ var Telegram = Header.extend({
return Telegram.getFrameCountForCommand(this.command);
},

}, {
}, /** @lends Telegram */ {

fromLiveBuffer: function(buffer, start, end) {
var frameCount = this.getFrameCountForCommand(buffer [start + 6]);
Expand Down
151 changes: 151 additions & 0 deletions test/specs/telegram.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,159 @@



var vbus = require('./resol-vbus');



var Telegram = vbus.Telegram;



describe('Telegram', function() {

describe('constructor', function() {

it('should be a constructor function', function() {
expect(Telegram).to.be.a('function');
expect(Telegram).to.have.a.property('extend').that.is.a('function');
});

it('should have reasonable defaults', function() {
var telegram = new Telegram();

expect(telegram).to.have.a.property('destinationAddress').that.is.equal(0);
expect(telegram).to.have.a.property('sourceAddress').that.is.equal(0);
expect(telegram).to.have.a.property('command').that.is.equal(0);
expect(telegram).to.have.a.property('frameData').that.is.an.instanceOf(Buffer).that.has.a.lengthOf(21);
});

it('should copy selected options', function() {
var options = {
destinationAddress: 0x1111,
sourceAddress: 0x2222,
command: 0x3333,
frameData: new Buffer('123456712345671234567123456712345671234567', 'hex'),
};

var telegram = new Telegram(options);

expect(telegram).to.have.a.property('destinationAddress').that.is.equal(options.destinationAddress);
expect(telegram).to.have.a.property('sourceAddress').that.is.equal(options.sourceAddress);
expect(telegram).to.have.a.property('command').that.is.equal(options.command);
expect(telegram).to.have.a.property('frameData').that.is.eql(options.frameData).that.is.not.equal(options.frameData);
});

it('should not copy frameData', function() {
var options = {
destinationAddress: 0x1111,
sourceAddress: 0x2222,
command: 0x3333,
frameData: new Buffer('123456712345671234567123456712345671234567', 'hex'),
dontCopyFrameData: true,
};

var telegram = new Telegram(options);

expect(telegram).to.have.a.property('destinationAddress').that.is.equal(options.destinationAddress);
expect(telegram).to.have.a.property('sourceAddress').that.is.equal(options.sourceAddress);
expect(telegram).to.have.a.property('command').that.is.equal(options.command);
expect(telegram).to.have.a.property('frameData').that.is.equal(options.frameData);
});

});

describe('#toLiveBuffer', function() {

it('should be a method', function() {
expect(Telegram.prototype).to.have.a.property('toLiveBuffer').that.is.a('function');
});

it('should work correctly without a buffer', function() {
var frameData = new Buffer(21);
for (var i = 0; i < frameData.length; i++) {
frameData [i] = i * 12;
}

var telegram = new Telegram({
destinationAddress: 0x1122,
sourceAddress: 0x3344,
command: 0x77,
frameData: frameData,
});

var buffer = telegram.toLiveBuffer();

expect(buffer.toString('hex')).to.equal('aa2211443330772e000c1824303c48000354606c7804101c70472834404c5864707f6c');
});

it('should work correctly with a buffer', function() {
var frameData = new Buffer(21);
for (var i = 0; i < frameData.length; i++) {
frameData [i] = i * 12;
}

var telegram = new Telegram({
destinationAddress: 0x1122,
sourceAddress: 0x3344,
command: 0x77,
frameData: frameData,
});

var bigBuffer = new Buffer(800);

var buffer = telegram.toLiveBuffer(bigBuffer, 100, 200);

expect(buffer.toString('hex')).to.equal('aa2211443330772e000c1824303c48000354606c7804101c70472834404c5864707f6c');
});

it('should throw if buffer is too small', function() {
var frameData = new Buffer(21);
for (var i = 0; i < frameData.length; i++) {
frameData [i] = i * 12;
}

var telegram = new Telegram({
destinationAddress: 0x1122,
sourceAddress: 0x3344,
command: 0x77,
frameData: frameData,
});

var bigBuffer = new Buffer(800);

expect(function() {
telegram.toLiveBuffer(bigBuffer, 100, 10);
}).to.throw();
});

});

describe('#getId', function() {

it('should be a method', function() {
expect(Telegram.prototype).to.have.a.property('getId').that.is.a('function');
});

it('should work correctly', function() {
var frameData = new Buffer(21);
for (var i = 0; i < frameData.length; i++) {
frameData [i] = i * 12;
}

var telegram = new Telegram({
channel: 0x13,
destinationAddress: 0x1122,
sourceAddress: 0x3344,
command: 0x77,
frameData: frameData,
});

expect(telegram).to.be.an('object');
expect(telegram.getId()).to.equal('13_1122_3344_30_77');
});

});

xit('should have tests', function() {
// TODO need raw data snippets of v3 communication
});
Expand Down

0 comments on commit 45fa115

Please sign in to comment.