2019-04-18 21:32:38 +00:00
|
|
|
// Copyright IBM Corp. 2016,2019. All Rights Reserved.
|
2016-10-19 11:14:55 +00:00
|
|
|
// Node module: loopback-connector
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2019-08-02 13:50:51 +00:00
|
|
|
const BinaryPacker = require('../lib/binary-packer');
|
|
|
|
const expect = require('chai').expect;
|
2016-10-19 11:14:55 +00:00
|
|
|
|
|
|
|
describe('BinaryPacker', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
let packer;
|
2016-10-19 11:14:55 +00:00
|
|
|
|
|
|
|
beforeEach(function createPacker() {
|
|
|
|
packer = new BinaryPacker();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('encode()', function() {
|
|
|
|
it('supports invocation with a callback', function(done) {
|
|
|
|
packer.encode('a-value', done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('decode()', function() {
|
|
|
|
it('supports invocation with a callback', function(done) {
|
|
|
|
packer.encode('a-value', function(err, binary) {
|
|
|
|
if (err) return done(err);
|
|
|
|
packer.decode(binary, function(err, result) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(result).to.eql('a-value');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('roundtrip', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const TEST_CASES = {
|
2016-10-19 11:14:55 +00:00
|
|
|
String: 'a-value',
|
2017-03-06 23:40:47 +00:00
|
|
|
Object: {a: 1, b: 2},
|
2016-10-19 11:14:55 +00:00
|
|
|
Buffer: new Buffer([1, 2, 3]),
|
|
|
|
Date: new Date('2016-08-03T11:53:03.470Z'),
|
|
|
|
Integer: 12345,
|
|
|
|
Float: 12.345,
|
|
|
|
Boolean: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.keys(TEST_CASES).forEach(function(tc) {
|
|
|
|
it('works for ' + tc + ' values', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const value = TEST_CASES[tc];
|
2016-10-19 11:14:55 +00:00
|
|
|
return encodeAndDecode(value)
|
|
|
|
.then(function(result) {
|
|
|
|
expect(result).to.eql(value);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works for nested properties', function() {
|
|
|
|
return encodeAndDecode(TEST_CASES)
|
|
|
|
.then(function(result) {
|
|
|
|
expect(result).to.eql(TEST_CASES);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function encodeAndDecode(value) {
|
|
|
|
return packer.encode(value)
|
|
|
|
.then(function(binary) {
|
|
|
|
return packer.decode(binary);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|