sphero-pwn
Version:
Driver for Sphero robots
92 lines (82 loc) • 4.14 kB
text/coffeescript
Tokenizer = SpheroPwn.Tokenizer
describe 'Tokenizer', ->
beforeEach ->
= new Tokenizer()
= []
.onError = (error) =>
.push error: error.message
.onResponse = (response) =>
.push response: response
.onAsync = (async) =>
.push async: async
.onText = (char) =>
.push text: char
it 'parses an echo response', ->
.consume new Buffer([0xFF, 0xFF, 0x52, 0x01, 0x01, 0xAB])
expect(.length).to.equal 1
expect([0]).to.have.property 'response'
expect([0].response).to.have.property 'code', 0x52
expect([0].response).to.have.property 'sequence', 0x01
expect([0].response).to.have.property 'data'
expect([0].response.data).to.be.instanceOf Uint8Array
expect([0].response.data.length).to.equal 0
it 'parses a get version response', ->
.consume new Buffer([0xFF, 0xFF, 0x52, 0x01, 0x09, 0x01, 0x03,
0x01, 0x00, 0x00, 0x33, 0x00, 0x00, 0x6B])
expect(.length).to.equal 1
expect([0]).to.have.property 'response'
expect([0].response).to.have.property 'code', 0x52
expect([0].response).to.have.property 'sequence', 0x01
expect([0].response).to.have.property 'data'
expect([0].response.data).to.be.instanceOf Uint8Array
expect([0].response.data.length).to.equal 8
expect(Array.from([0].response.data)).to.deep.equal(
[0x01, 0x03, 0x01, 0x00, 0x00, 0x33, 0x00, 0x00])
it 'bounces an echo response with a bad checksum', ->
.consume new Buffer([0xFF, 0xFF, 0x52, 0x01, 0x01, 0xAC])
expect().to.deep.equal([{
error: 'Invalid response checksum 172, expected 171' }])
it 'bounces a get version response with a bad checksum', ->
.consume new Buffer([0xFF, 0xFF, 0x52, 0x01, 0x09, 0x01, 0x03,
0x01, 0x00, 0x00, 0x33, 0x00, 0x00, 0xCC])
expect().to.deep.equal([
{ error: 'Invalid response checksum 204, expected 107' }])
it 'parses an empty async message', ->
.consume new Buffer([0xFF, 0xFE, 0x02, 0x00, 0x01, 0xFC])
expect(.length).to.equal 1
expect([0]).to.have.property 'async'
expect([0].async).to.have.property 'idCode', 0x02
expect([0].async.data).to.be.instanceOf Uint8Array
expect([0].async.data.length).to.equal 0
it 'parses a large async message', ->
data = (i % 256 for i in [1..632])
.consume new Buffer([].concat([0xFF, 0xFE, 0x04, 0x02, 0x79],
data, [0x24]))
expect(.length).to.equal 1
expect([0]).to.have.property 'async'
expect([0].async).to.have.property 'idCode', 0x04
expect([0].async.data).to.be.instanceOf Uint8Array
expect([0].async.data.length).to.equal 632
expect(Array.from([0].async.data)).to.deep.equal(data)
it 'parses an echo response surrounded by text', ->
.consume new Buffer([0x41, 0xFF, 0xFF, 0x52, 0x01, 0x01, 0xAB,
0x5A])
expect(.length).to.equal 3
expect([0]).to.deep.equal text: 'A'
expect([1]).to.have.property 'response'
expect([1].response).to.have.property 'code', 0x52
expect([1].response).to.have.property 'sequence', 0x01
expect([1].response).to.have.property 'data'
expect([1].response.data).to.be.instanceOf Uint8Array
expect([1].response.data.length).to.equal 0
expect([2]).to.deep.equal text: 'Z'
it 'parses an empty async surrounded by text', ->
.consume new Buffer([0x41, 0xFF, 0xFE, 0x02, 0x00, 0x01, 0xFC,
0x5A])
expect(.length).to.equal 3
expect([0]).to.deep.equal text: 'A'
expect([1]).to.have.property 'async'
expect([1].async).to.have.property 'idCode', 0x02
expect([1].async.data).to.be.instanceOf Uint8Array
expect([1].async.data.length).to.equal 0
expect([2]).to.deep.equal text: 'Z'