preserves/implementations/javascript/test/test-decode.js

107 lines
3.7 KiB
JavaScript
Raw Normal View History

"use strict";
const chai = require('chai');
const expect = chai.expect;
chai.use(require('chai-immutable'));
const Immutable = require('immutable');
const { List, Set, Map, fromJS } = Immutable;
const Preserves = require('../src/index.js');
const { Decoder, Encoder, Record, Single, Double } = Preserves;
const fs = require('fs');
const util = require('util');
const shortForms = {
0: Symbol.for('discard'),
1: Symbol.for('capture'),
2: Symbol.for('observe'),
};
const Discard = Record.makeConstructor('discard', []);
const Capture = Record.makeConstructor('capture', ['pattern']);
const Observe = Record.makeConstructor('observe', ['pattern']);
describe('hex samples', () => {
const samples = fs.readFileSync(__dirname + '/samples.txt').toString().split(/\n/)
.filter((h) => h)
.map((h) => Buffer.from(h, 'hex'));
// As new samples are added to samples.txt, we will need to update this list:
const samplesExpected = [
{ expected: new Single(1), },
{ expected: new Double(1), },
{ expected: new Double(-1.202e+300), },
{ expected: 0, },
{ expected: 1, },
{ expected: 12, },
{ expected: -3, },
{ expected: -2, },
{ expected: -1, },
{ expected: Buffer.from("hello"), encodesTo: '6568656c6c6f', },
{ expected: Buffer.from("hello"), encodesTo: '6568656c6c6f', },
{ expected: Buffer.from("hello"), encodesTo: '6568656c6c6f', },
{ expected: Buffer.from("hello"), encodesTo: '6568656c6c6f', },
{ expected: List([1, 2, 3, 4]), encodesTo: 'c411121314', },
{ expected: fromJS([["a", 1], ["b", 2], ["c", 3]]), encodesTo: 'c3c2516111c2516212c2516313', },
{ expected: 13, },
{ expected: 127, },
{ expected: -128, },
{ expected: -127, },
{ expected: -4, },
{ expected: 128, },
{ expected: 255, },
{ expected: 256, },
{ expected: 32767, },
{ expected: -257, },
{ expected: -256, },
{ expected: -255, },
{ expected: -254, },
{ expected: -129, },
{ expected: 32768, },
{ expected: 65535, },
{ expected: 65536, },
{ expected: 131072, },
{ expected: "hello", },
{ expected: Buffer.from("hello"), },
{ expected: Symbol.for("hello"), },
{ expected: Capture(Discard()), },
{ expected: Observe(new Record(Symbol.for('speak'), [Discard(), Capture(Discard())])), },
{ expected:
new Record([Symbol.for('titled'), Symbol.for('person'), 2, Symbol.for('thing'), 1],
[101, "Blackwell", new Record(Symbol.for('date'), [1821, 2, 3]), "Dr"]), },
{ expected: List([1, 2, 3, 4]), },
{ expected: List([-2, -1, 0, 1]), },
{ expected:
fromJS(["hello", Symbol.for('there'), Buffer.from('world'), [], Set(), true, false]), },
{ expected:
Map()
.set(Symbol.for('a'), 1)
.set('b', true)
.set(fromJS([1, 2, 3]), Buffer.from('c'))
.set(Map().set(Symbol.for('first-name'), 'Elizabeth'),
Map().set(Symbol.for('surname'), 'Blackwell')), },
];
samples.forEach((s, sampleIndex) => {
it('[' + sampleIndex + '] ' + s.toString('hex') + ' should decode OK', () => {
const actual = new Decoder(s, { shortForms }).next();
const expected = samplesExpected[sampleIndex].expected;
expect(Immutable.is(actual, expected),
'[' + sampleIndex + '] actual ' + util.inspect(actual) +
', expected ' + util.inspect(expected))
.to.be.true;
});
});
samples.forEach((s, sampleIndex) => {
it('[' + sampleIndex + '] ' + s.toString('hex') + ' should encode OK', () => {
const entry = samplesExpected[sampleIndex];
const actual = entry.encodesTo || s;
const expected = new Encoder({ shortForms }).push(entry.expected).contents();
expect(actual.toString('hex')).to.equal(expected.toString('hex'));
});
});
});