Skip to content

Commit 235e6cb

Browse files
committed
More complete tests against gennodejs
1 parent 596ef7c commit 235e6cb

1 file changed

Lines changed: 310 additions & 1 deletion

File tree

test/genjsTest.js

Lines changed: 310 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const msgUtils = require('../utils/message_utils.js');
77
describe('genjsTests', () => {
88
msgUtils.findMessageFiles();
99
msgUtils.loadMessagePackage('std_msgs');
10-
msgUtils.loadMessagePackage('baxter_core_msgs');
10+
msgUtils.loadMessagePackage('test_msgs');
1111

1212
it('basic', (done) => {
1313
let stringMsg;
@@ -17,10 +17,15 @@ describe('genjsTests', () => {
1717
expect(loadStrFunc).to.not.throw(/good function/);
1818
expect(stringMsg).to.be.a('function');
1919
expect(stringMsg).to.have.property('serialize');
20+
expect(stringMsg.serialize).to.be.a('function');
2021
expect(stringMsg).to.have.property('deserialize');
22+
expect(stringMsg.deserialize).to.be.a('function');
2123
expect(stringMsg).to.have.property('datatype');
24+
expect(stringMsg.datatype).to.be.a('function');
2225
expect(stringMsg).to.have.property('md5sum');
26+
expect(stringMsg.md5sum).to.be.a('function');
2327
expect(stringMsg).to.have.property('messageDefinition');
28+
expect(stringMsg.messageDefinition).to.be.a('function');
2429

2530
done();
2631
});
@@ -281,4 +286,308 @@ describe('genjsTests', () => {
281286
done();
282287
});
283288
});
289+
290+
describe('complex_msgs', () => {
291+
it('messages and constants', (done) => {
292+
const BaseType = msgUtils.getHandlerForMsgType('test_msgs/BaseType');
293+
294+
expect(BaseType).to.be.a('function');
295+
expect(BaseType).to.have.property('Constants');
296+
297+
expect(BaseType.Constants.NUMERIC_CONSTANT_A).to.be.a('number');
298+
expect(BaseType.Constants.NUMERIC_CONSTANT_B).to.be.a('number');
299+
expect(BaseType.Constants.STRING_CONSTANT).to.be.a('string');
300+
301+
expect(BaseType.Constants.NUMERIC_CONSTANT_A).to.equal(1);
302+
expect(BaseType.Constants.NUMERIC_CONSTANT_B).to.equal(2);
303+
expect(BaseType.Constants.STRING_CONSTANT).to.equal('hello');
304+
305+
const baseType = new BaseType();
306+
baseType.string_field = BaseType.Constants.STRING_CONSTANT;
307+
baseType.num_field = BaseType.Constants.NUMERIC_CONSTANT_A;
308+
309+
let bufferInfo = {buffer: [], length: 0};
310+
BaseType.serialize(baseType, bufferInfo);
311+
const msgBuffer = Buffer.concat(bufferInfo.buffer);
312+
313+
const deserializedMsg = BaseType.deserialize(msgBuffer).data;
314+
315+
expect(deserializedMsg.string_field).to.equal(baseType.string_field);
316+
expect(deserializedMsg.num_field).to.equal(baseType.num_field);
317+
318+
done();
319+
});
320+
321+
it('constant length arrays', (done) => {
322+
const CLA = msgUtils.getHandlerForMsgType('test_msgs/ConstantLengthArray');
323+
324+
const cla = new CLA();
325+
const claArrLen = 10;
326+
expect(cla.array_field).to.be.a('Array');
327+
expect(cla.array_field.length).to.equal(claArrLen);
328+
329+
cla.array_field.forEach((item) => {
330+
expect(item).to.be.a('number');
331+
expect(item).to.equal(0);
332+
});
333+
334+
let bufferInfo = {buffer: [], length: 0};
335+
CLA.serialize(cla, bufferInfo);
336+
const msgBuffer = Buffer.concat(bufferInfo.buffer);
337+
expect(msgBuffer.length).to.equal(claArrLen);
338+
339+
const deserializedMsg = CLA.deserialize(msgBuffer).data;
340+
expect(deserializedMsg.array_field.length).to.equal(claArrLen);
341+
342+
const BTCLA = msgUtils.getHandlerForMsgType('test_msgs/BaseTypeConstantLengthArray');
343+
const BaseType = msgUtils.getHandlerForMsgType('test_msgs/BaseType');
344+
345+
const btcla = new BTCLA();
346+
const btclaArrLen = 5;
347+
expect(btcla.array_field).to.be.a('Array');
348+
expect(btcla.array_field.length).to.equal(btclaArrLen);
349+
350+
btcla.array_field.forEach((item) => {
351+
expect(item).to.be.an.instanceof(BaseType);
352+
});
353+
354+
bufferInfo = {buffer: [], length: 0};
355+
BTCLA.serialize(btcla, bufferInfo);
356+
const msgBuffer2 = Buffer.concat(bufferInfo.buffer);
357+
expect(msgBuffer2.length).to.equal(25);
358+
359+
const deserializedMsg2 = BTCLA.deserialize(msgBuffer2).data;
360+
expect(deserializedMsg2.array_field.length).to.equal(btclaArrLen);
361+
deserializedMsg2.array_field.forEach((item) => {
362+
expect(item).to.be.an.instanceof(BaseType);
363+
});
364+
365+
done();
366+
});
367+
368+
it('variable length arrays', (done) => {
369+
const VLA = msgUtils.getHandlerForMsgType('test_msgs/VariableLengthArray');
370+
371+
const vla = new VLA();
372+
expect(vla.array_field).to.be.a('Array');
373+
expect(vla.array_field.length).to.equal(0);
374+
375+
let bufferInfo = {buffer: [], length: 0};
376+
VLA.serialize(vla, bufferInfo);
377+
const msgBuffer = Buffer.concat(bufferInfo.buffer);
378+
expect(msgBuffer.length).to.equal(4);
379+
380+
const val = 12;
381+
const arrLen = 7;
382+
vla.array_field = new Array(arrLen).fill(val);
383+
vla.array_field.forEach((item) => {
384+
expect(item).to.be.a('number');
385+
expect(item).to.equal(val);
386+
});
387+
388+
bufferInfo = {buffer: [], length: 0};
389+
VLA.serialize(vla, bufferInfo);
390+
const msgBuffer2 = Buffer.concat(bufferInfo.buffer);
391+
expect(msgBuffer2.length).to.equal(arrLen + 4);
392+
393+
const deserializedMsg = VLA.deserialize(msgBuffer2).data;
394+
expect(deserializedMsg.array_field.length).to.equal(arrLen);
395+
deserializedMsg.array_field.forEach((item) => {
396+
expect(item).to.be.a('number');
397+
expect(item).to.equal(val);
398+
});
399+
400+
const BTVLA = msgUtils.getHandlerForMsgType('test_msgs/BaseTypeVariableLengthArray');
401+
const BaseType = msgUtils.getHandlerForMsgType('test_msgs/BaseType');
402+
403+
const btvla = new BTVLA();
404+
expect(btvla.array_field).to.be.a('Array');
405+
expect(btvla.array_field.length).to.equal(0);
406+
407+
bufferInfo = {buffer: [], length: 0};
408+
VLA.serialize(btvla, bufferInfo);
409+
const msgBuffer3 = Buffer.concat(bufferInfo.buffer);
410+
expect(msgBuffer3.length).to.equal(4);
411+
412+
const arrLen2 = 4;
413+
btvla.array_field = new Array(arrLen2).fill(new BaseType());
414+
415+
bufferInfo = {buffer: [], length: 0};
416+
BTVLA.serialize(btvla, bufferInfo);
417+
const msgBuffer4 = Buffer.concat(bufferInfo.buffer);
418+
expect(msgBuffer4.length).to.equal(24);
419+
420+
const deserializedMsg2 = BTVLA.deserialize(msgBuffer4).data;
421+
expect(deserializedMsg2.array_field.length).to.equal(arrLen2);
422+
deserializedMsg2.array_field.forEach((item) => {
423+
expect(item).to.be.an.instanceof(BaseType);
424+
});
425+
426+
done();
427+
});
428+
429+
it('services and constants', (done) => {
430+
const BasicService = msgUtils.getHandlerForSrvType('test_msgs/BasicService');
431+
432+
expect(BasicService).to.have.property('Request');
433+
expect(BasicService).to.have.property('Response');
434+
435+
const BSRequest = BasicService.Request;
436+
expect(BSRequest.Constants.OP_REVERSE).to.equal('reverse');
437+
expect(BSRequest.Constants.OP_LEFT_PAD).to.equal('left_pad');
438+
const bsRequest = new BSRequest();
439+
expect(bsRequest.data).to.be.a('string');
440+
expect(bsRequest.op).to.be.a('string');
441+
442+
const dataField = 'JUNK';
443+
bsRequest.data = dataField;
444+
bsRequest.op = BSRequest.Constants.OP_LEFT_PAD;
445+
let bufferInfo = {buffer: [], length: 0};
446+
BSRequest.serialize(bsRequest, bufferInfo);
447+
const msgBuffer = Buffer.concat(bufferInfo.buffer);
448+
expect(msgBuffer.length).to.equal(20);
449+
450+
const deserializedRequest = BSRequest.deserialize(msgBuffer).data;
451+
expect(deserializedRequest).to.be.an.instanceof(BSRequest);
452+
expect(deserializedRequest.data).to.equal(dataField);
453+
expect(deserializedRequest.op).to.equal(BSRequest.Constants.OP_LEFT_PAD);
454+
455+
const BSResponse = BasicService.Response;
456+
expect(BSResponse.Constants.RES_NULL).to.equal('null');
457+
const bsResponse = new BSResponse();
458+
expect(bsResponse.result).to.be.a('string');
459+
460+
bsResponse.result = BSResponse.Constants.RES_NULL;
461+
bufferInfo = {buffer: [], length: 0};
462+
BSResponse.serialize(bsResponse, bufferInfo);
463+
const msgBuffer2 = Buffer.concat(bufferInfo.buffer);
464+
expect(msgBuffer2.length).to.equal(8);
465+
466+
const deserializedResponse = BSResponse.deserialize(msgBuffer2).data;
467+
expect(deserializedResponse).to.be.an.instanceof(BSResponse);
468+
expect(deserializedResponse.result).to.equal(BSResponse.Constants.RES_NULL);
469+
470+
done();
471+
});
472+
473+
it('service depending on this package', (done) => {
474+
const TestService = msgUtils.getHandlerForSrvType('test_msgs/TestService');
475+
const BaseType = msgUtils.getHandlerForMsgType('test_msgs/BaseType');
476+
477+
const TSRequest = TestService.Request;
478+
const TSResponse = TestService.Response;
479+
expect(TSRequest).to.be.a('function');
480+
expect(TSResponse).to.be.a('function');
481+
482+
const tsRequest = new TSRequest();
483+
expect(tsRequest.input).to.be.an.instanceof(BaseType);
484+
tsRequest.input.string_field = BaseType.Constants.STRING_CONSTANT;
485+
486+
let bufferInfo = {buffer: [], length: 0};
487+
TSRequest.serialize(tsRequest, bufferInfo);
488+
const msgBuffer = Buffer.concat(bufferInfo.buffer);
489+
expect(msgBuffer.length).to.equal(10);
490+
491+
const deserializedRequest = TSRequest.deserialize(msgBuffer).data;
492+
expect(deserializedRequest).to.be.an.instanceof(TSRequest);
493+
expect(deserializedRequest.input).to.be.an.instanceof(BaseType);
494+
495+
496+
const tsResponse = new TSResponse();
497+
expect(tsResponse).to.be.empty;
498+
499+
bufferInfo = {buffer: [], length: 0};
500+
TSResponse.serialize(tsResponse, bufferInfo);
501+
const msgBuffer2 = Buffer.concat(bufferInfo.buffer);
502+
expect(msgBuffer2.length).to.equal(0);
503+
504+
const deserializedRequest2 = TSResponse.deserialize(msgBuffer2).data;
505+
expect(deserializedRequest2).to.be.an.instanceof(TSResponse);
506+
expect(deserializedRequest2).to.be.empty;
507+
508+
done();
509+
});
510+
511+
it('message depending on another package', (done) => {
512+
const StdMsg = msgUtils.getHandlerForMsgType('test_msgs/StdMsg');
513+
const Header = msgUtils.getHandlerForMsgType('std_msgs/Header');
514+
515+
const frameId = 'base';
516+
const time = {secs: 100, nsecs: 1000};
517+
const seq = 123;
518+
519+
const header = new Header();
520+
expect(header.frame_id).to.be.a('string');
521+
header.seq = seq;
522+
header.stamp = time;
523+
header.frame_id = frameId;
524+
525+
const stdMsg = new StdMsg();
526+
expect(stdMsg.header).to.be.an.instanceof(Header);
527+
stdMsg.header = header;
528+
stdMsg.time_field = time;
529+
530+
let bufferInfo = {buffer: [], length: 0};
531+
StdMsg.serialize(stdMsg, bufferInfo);
532+
const msgBuffer = Buffer.concat(bufferInfo.buffer);
533+
534+
const deserializedMsg = StdMsg.deserialize(msgBuffer).data;
535+
expect(deserializedMsg.header.seq).to.equal(seq);
536+
expect(deserializedMsg.header.stamp.secs).to.equal(time.secs);
537+
expect(deserializedMsg.header.stamp.nsecs).to.equal(time.nsecs);
538+
expect(deserializedMsg.header.frame_id).to.equal(frameId);
539+
expect(deserializedMsg.time_field.secs).to.equal(time.secs);
540+
expect(deserializedMsg.time_field.nsecs).to.equal(time.nsecs);
541+
542+
done();
543+
});
544+
545+
it('service depending on another package', (done) => {
546+
const HeaderService = msgUtils.getHandlerForSrvType('test_msgs/HeaderService');
547+
const Header = msgUtils.getHandlerForMsgType('std_msgs/Header');
548+
549+
const HRequest = HeaderService.Request;
550+
const HResponse = HeaderService.Response;
551+
expect(HRequest).to.be.a('function');
552+
expect(HResponse).to.be.a('function');
553+
554+
const hRequest = new HRequest();
555+
expect(hRequest).to.be.empty;
556+
557+
let bufferInfo = {buffer: [], length: 0};
558+
HRequest.serialize(hRequest, bufferInfo);
559+
const msgBuffer = Buffer.concat(bufferInfo.buffer);
560+
expect(msgBuffer.length).to.equal(0);
561+
562+
const deserializedRequest = HRequest.deserialize(msgBuffer).data;
563+
expect(deserializedRequest).to.be.an.instanceof(HRequest);
564+
expect(hRequest).to.be.empty;
565+
566+
const hResponse = new HResponse();
567+
expect(hResponse.header_response).to.be.an.instanceof(Header);
568+
const seq = 123;
569+
const frameId = 'base';
570+
hResponse.header_response.seq = seq;
571+
hResponse.header_response.frame_id = frameId;
572+
573+
bufferInfo = {buffer: [], length: 0};
574+
HResponse.serialize(hResponse, bufferInfo);
575+
const msgBuffer2 = Buffer.concat(bufferInfo.buffer);
576+
expect(msgBuffer2.length).to.equal(20);
577+
578+
const deserializedRequest2 = HResponse.deserialize(msgBuffer2).data;
579+
expect(deserializedRequest2).to.be.an.instanceof(HResponse);
580+
expect(deserializedRequest2.header_response).to.be.an.instanceof(Header);
581+
expect(deserializedRequest2.header_response.seq).to.equal(seq);
582+
expect(deserializedRequest2.header_response.frame_id).to.equal(frameId);
583+
expect(deserializedRequest2.header_response.stamp.secs).to.equal(0);
584+
expect(deserializedRequest2.header_response.stamp.nsecs).to.equal(0);
585+
586+
done();
587+
});
588+
});
589+
590+
describe('actions', () => {
591+
// TODO: TEST actions
592+
});
284593
});

0 commit comments

Comments
 (0)