|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var notepack = { |
| 4 | + encode: require('../browser/encode'), |
| 5 | + decode: require('../browser/decode') |
| 6 | +}; |
| 7 | + |
| 8 | +function array(length) { |
| 9 | + var arr = new Array(length); |
| 10 | + for (var i = 0; i < arr.length; i++) { |
| 11 | + arr[i] = 0; |
| 12 | + } |
| 13 | + return arr; |
| 14 | +} |
| 15 | + |
| 16 | +function map(length) { |
| 17 | + var result = {}; |
| 18 | + for (var i = 0; i < length; i++) { |
| 19 | + result[i + ''] = 0; |
| 20 | + } |
| 21 | + return result; |
| 22 | +} |
| 23 | + |
| 24 | +describe('notepack (browser build)', function() { |
| 25 | + it('ArrayBuffer view', function() { |
| 26 | + expect(notepack.decode(Uint8Array.from([ 0x93, 1, 2, 3 ]))).to.deep.equal([ 1, 2, 3 ]); |
| 27 | + }); |
| 28 | + |
| 29 | + it('offset ArrayBuffer view', function() { |
| 30 | + var buffer = new ArrayBuffer(14); |
| 31 | + var view = new Uint8Array(buffer); |
| 32 | + |
| 33 | + // Fill with junk before setting the encoded data |
| 34 | + view.fill(0xFF); |
| 35 | + |
| 36 | + // Put the encoded data somewhere in the middle of the buffer |
| 37 | + view.set([ 0x93, 1, 2, 3 ], 4); |
| 38 | + |
| 39 | + expect(notepack.decode(new Uint8Array(buffer, 4, 4))).to.deep.equal([ 1, 2, 3 ]); |
| 40 | + expect(notepack.decode(new Uint16Array(buffer, 4, 2))).to.deep.equal([ 1, 2, 3 ]); |
| 41 | + }); |
| 42 | + |
| 43 | + it('toJSON', function () { |
| 44 | + var obj = { |
| 45 | + a: 'b', |
| 46 | + toJSON: function () { |
| 47 | + return 'c'; |
| 48 | + } |
| 49 | + }; |
| 50 | + expect(notepack.encode(obj)).to.deep.equal(notepack.encode('c')); |
| 51 | + }); |
| 52 | + |
| 53 | + it('all formats', function () { |
| 54 | + this.timeout(20000); |
| 55 | + var expected = { |
| 56 | + unsigned: [1, 2, 3, 4, { b: { c: [128, 256, 65536, 4294967296] } }], |
| 57 | + signed: [-1, -2, -3, -4, { b: { c: [-33, -129, -32769, -2147483649] } }], |
| 58 | + bin: [Uint8Array.of('1', '2', '3').buffer, Uint8Array.from('1'.repeat(256)).buffer, Uint8Array.from('2'.repeat(65536)).buffer], |
| 59 | + str: ['abc', 'g'.repeat(32), 'h'.repeat(256), 'i'.repeat(65536)], |
| 60 | + array: [[], array(16), array(65536)], |
| 61 | + map: {}, |
| 62 | + nil: null, |
| 63 | + bool: { 'true': true, 'false': false, both: [true, false, false, false, true] }, |
| 64 | + fixext: [undefined, new Date('2140-01-01T13:14:15.678Z'), undefined, new Date('2005-12-31T23:59:59.999Z')], |
| 65 | + utf8: ['α', '亜', '\uD83D\uDC26'], |
| 66 | + float: [1.1, 1234567891234567.5, Infinity, -Infinity, NaN] |
| 67 | + }; |
| 68 | + expected.map['a'.repeat(32)] = { a: 'a', b: 'b', c: 'c' }; |
| 69 | + expected.map['b'.repeat(256)] = { a: { b: 1, c: 1, d: 1, e: { f: { g: 2, h: 2 } } } }; |
| 70 | + expected.map['c'.repeat(65536)] = [{ a: { b: 1, c: 1, d: 1, e: { f: [{ g: 2, h: 2 }] } } }]; |
| 71 | + expected.map16 = map(65535); |
| 72 | + expected.map32 = map(65536); |
| 73 | + |
| 74 | + expect(notepack.decode(notepack.encode(expected))).to.deep.equal(expected); |
| 75 | + }); |
| 76 | +}); |
0 commit comments