Something went wrong. Try again.
forked from sarma.tngl.sh/Testing-phase-vidhi
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496import assert from 'assert';import {VersionedStruct, String as StringT, Pointer, uint8, DecodeStream, EncodeStream} from 'restructure';
describe('VersionedStruct', function() { describe('decode', function() { it('should get version from number type', function() { const struct = new VersionedStruct(uint8, { 0: { name: new StringT(uint8, 'ascii'), age: uint8 }, 1: { name: new StringT(uint8, 'utf8'), age: uint8, gender: uint8 } } );
let stream = new DecodeStream(Buffer.from('\x00\x05devon\x15')); assert.deepEqual(struct.decode(stream), { version: 0, name: 'devon', age: 21 });
stream = new DecodeStream(Buffer.from('\x01\x0adevon 👍\x15\x00', 'utf8')); assert.deepEqual(struct.decode(stream), { version: 1, name: 'devon 👍', age: 21, gender: 0 }); });
it('should throw for unknown version', function() { const struct = new VersionedStruct(uint8, { 0: { name: new StringT(uint8, 'ascii'), age: uint8 }, 1: { name: new StringT(uint8, 'utf8'), age: uint8, gender: uint8 } } );
const stream = new DecodeStream(Buffer.from('\x05\x05devon\x15')); return assert.throws(() => struct.decode(stream)); });
it('should support common header block', function() { const struct = new VersionedStruct(uint8, { header: { age: uint8, alive: uint8 }, 0: { name: new StringT(uint8, 'ascii') }, 1: { name: new StringT(uint8, 'utf8'), gender: uint8 } } );
let stream = new DecodeStream(Buffer.from('\x00\x15\x01\x05devon')); assert.deepEqual(struct.decode(stream), { version: 0, age: 21, alive: 1, name: 'devon' });
stream = new DecodeStream(Buffer.from('\x01\x15\x01\x0adevon 👍\x00', 'utf8')); assert.deepEqual(struct.decode(stream), { version: 1, age: 21, alive: 1, name: 'devon 👍', gender: 0 }); });
it('should support parent version key', function() { const struct = new VersionedStruct('version', { 0: { name: new StringT(uint8, 'ascii'), age: uint8 }, 1: { name: new StringT(uint8, 'utf8'), age: uint8, gender: uint8 } } );
let stream = new DecodeStream(Buffer.from('\x05devon\x15')); assert.deepEqual(struct.decode(stream, {version: 0}), { version: 0, name: 'devon', age: 21 });
stream = new DecodeStream(Buffer.from('\x0adevon 👍\x15\x00', 'utf8')); assert.deepEqual(struct.decode(stream, {version: 1}), { version: 1, name: 'devon 👍', age: 21, gender: 0 }); });
it('should support parent version nested key', function() { const struct = new VersionedStruct('obj.version', { 0: { name: new StringT(uint8, 'ascii'), age: uint8 }, 1: { name: new StringT(uint8, 'utf8'), age: uint8, gender: uint8 } } );
let stream = new DecodeStream(Buffer.from('\x05devon\x15')); assert.deepEqual(struct.decode(stream, {obj: {version: 0}}), { version: 0, name: 'devon', age: 21 });
stream = new DecodeStream(Buffer.from('\x0adevon 👍\x15\x00', 'utf8')); assert.deepEqual(struct.decode(stream, {obj: {version: 1}}), { version: 1, name: 'devon 👍', age: 21, gender: 0 }); });
it('should support sub versioned structs', function() { const struct = new VersionedStruct(uint8, { 0: { name: new StringT(uint8, 'ascii'), age: uint8 }, 1: new VersionedStruct(uint8, { 0: { name: new StringT(uint8) }, 1: { name: new StringT(uint8), isDesert: uint8 } } ) } );
let stream = new DecodeStream(Buffer.from('\x00\x05devon\x15')); assert.deepEqual(struct.decode(stream, {version: 0}), { version: 0, name: 'devon', age: 21 });
stream = new DecodeStream(Buffer.from('\x01\x00\x05pasta')); assert.deepEqual(struct.decode(stream, {version: 0}), { version: 0, name: 'pasta' });
stream = new DecodeStream(Buffer.from('\x01\x01\x09ice cream\x01')); assert.deepEqual(struct.decode(stream, {version: 0}), { version: 1, name: 'ice cream', isDesert: 1 }); });
it('should support process hook', function() { const struct = new VersionedStruct(uint8, { 0: { name: new StringT(uint8, 'ascii'), age: uint8 }, 1: { name: new StringT(uint8, 'utf8'), age: uint8, gender: uint8 } } );
struct.process = function() { return this.processed = true; };
const stream = new DecodeStream(Buffer.from('\x00\x05devon\x15')); assert.deepEqual(struct.decode(stream), { version: 0, name: 'devon', age: 21, processed: true }); }); });
describe('size', function() { it('should compute the correct size', function() { const struct = new VersionedStruct(uint8, { 0: { name: new StringT(uint8, 'ascii'), age: uint8 }, 1: { name: new StringT(uint8, 'utf8'), age: uint8, gender: uint8 } } );
let size = struct.size({ version: 0, name: 'devon', age: 21 });
assert.equal(size, 8);
size = struct.size({ version: 1, name: 'devon 👍', age: 21, gender: 0 });
assert.equal(size, 14); });
it('should throw for unknown version', function() { const struct = new VersionedStruct(uint8, { 0: { name: new StringT(uint8, 'ascii'), age: uint8 }, 1: { name: new StringT(uint8, 'utf8'), age: uint8, gender: uint8 } } );
assert.throws(() => struct.size({ version: 5, name: 'devon', age: 21 }) ); });
it('should support common header block', function() { const struct = new VersionedStruct(uint8, { header: { age: uint8, alive: uint8 }, 0: { name: new StringT(uint8, 'ascii') }, 1: { name: new StringT(uint8, 'utf8'), gender: uint8 } } );
let size = struct.size({ version: 0, age: 21, alive: 1, name: 'devon' });
assert.equal(size, 9);
size = struct.size({ version: 1, age: 21, alive: 1, name: 'devon 👍', gender: 0 });
assert.equal(size, 15); });
it('should compute the correct size with pointers', function() { const struct = new VersionedStruct(uint8, { 0: { name: new StringT(uint8, 'ascii'), age: uint8 }, 1: { name: new StringT(uint8, 'utf8'), age: uint8, ptr: new Pointer(uint8, new StringT(uint8)) } } );
const size = struct.size({ version: 1, name: 'devon', age: 21, ptr: 'hello' });
assert.equal(size, 15); });
it('should throw if no value is given', function() { const struct = new VersionedStruct(uint8, { 0: { name: new StringT(4, 'ascii'), age: uint8 }, 1: { name: new StringT(4, 'utf8'), age: uint8, gender: uint8 } } );
assert.throws(() => struct.size(), /not a fixed size/i); }); });
describe('encode', function() { it('should encode objects to buffers', function() { const struct = new VersionedStruct(uint8, { 0: { name: new StringT(uint8, 'ascii'), age: uint8 }, 1: { name: new StringT(uint8, 'utf8'), age: uint8, gender: uint8 } });
const buf1 = struct.toBuffer({ version: 0, name: 'devon', age: 21 });
assert.deepEqual(buf1, Buffer.from('\x00\x05devon\x15', 'utf8'));
const buf2 = struct.toBuffer({ version: 1, name: 'devon 👍', age: 21, gender: 0 });
assert.deepEqual(buf2, Buffer.from('\x01\x0adevon 👍\x15\x00', 'utf8')); });
it('should throw for unknown version', function() { const struct = new VersionedStruct(uint8, { 0: { name: new StringT(uint8, 'ascii'), age: uint8 }, 1: { name: new StringT(uint8, 'utf8'), age: uint8, gender: uint8 } });
assert.throws(() => struct.toBuffer({ version: 5, name: 'devon', age: 21 }) ); });
it('should support common header block', function() { const struct = new VersionedStruct(uint8, { header: { age: uint8, alive: uint8 }, 0: { name: new StringT(uint8, 'ascii') }, 1: { name: new StringT(uint8, 'utf8'), gender: uint8 } });
const buf1 = struct.toBuffer({ version: 0, age: 21, alive: 1, name: 'devon' });
assert.deepEqual(buf1, Buffer.from('\x00\x15\x01\x05devon', 'utf8'));
const buf2 = struct.toBuffer({ version: 1, age: 21, alive: 1, name: 'devon 👍', gender: 0 });
assert.deepEqual(buf2, Buffer.from('\x01\x15\x01\x0adevon 👍\x00', 'utf8')); });
it('should encode pointer data after structure', function() { const struct = new VersionedStruct(uint8, { 0: { name: new StringT(uint8, 'ascii'), age: uint8 }, 1: { name: new StringT(uint8, 'utf8'), age: uint8, ptr: new Pointer(uint8, new StringT(uint8)) } });
const buf = struct.toBuffer({ version: 1, name: 'devon', age: 21, ptr: 'hello' });
assert.deepEqual(buf, Buffer.from('\x01\x05devon\x15\x09\x05hello', 'utf8')); });
it('should support preEncode hook', function() { const struct = new VersionedStruct(uint8, { 0: { name: new StringT(uint8, 'ascii'), age: uint8 }, 1: { name: new StringT(uint8, 'utf8'), age: uint8, gender: uint8 } });
struct.preEncode = function() { return this.version = (this.gender != null) ? 1 : 0; };
const buf1 = struct.toBuffer({ name: 'devon', age: 21 });
assert.deepEqual(buf1, Buffer.from('\x00\x05devon\x15', 'utf8'));
const buf2 = struct.toBuffer({ name: 'devon 👍', age: 21, gender: 0 });
assert.deepEqual(buf2, Buffer.from('\x01\x0adevon 👍\x15\x00', 'utf8')); }); });});