binary-data
Version:
Declarative binary data encoder / decoder.
1 lines • 11.3 kB
Source Map (JSON)
{"version":3,"sources":["string.js"],"names":["string","require","describe","test","str","wstream","writeBuffer","jest","fn","writeInt8","type","encode","expect","bytes","toEqual","length","toBeCalledWith","Buffer","from","expectedValue","expectedLength","values","rstream","readBuffer","mockImplementation","indexOf","i","decode","value","encodingLength","lengthBytes","buf","lengthType","toHaveBeenCalledTimes","toBe","typeLength","callback"],"mappings":"AAAA;;AAEA,MAAMA,SAASC,QAAQ,cAAR,CAAf;;AAEAC,SAAS,QAAT,EAAmB,MAAM;AACvBA,WAAS,aAAT,EAAwB,MAAM;AAC5BC,SAAK,QAAL,EAAe,MAAM;AACnB,YAAMC,MAAM,QAAZ;AACA,YAAMC,UAAU;AACdC,qBAAaC,KAAKC,EAAL,EADC;AAEdC,mBAAWF,KAAKC,EAAL;AAFG,OAAhB;;AAKA,YAAME,OAAOV,OAAO,IAAP,CAAb;;AAEAU,WAAKC,MAAL,CAAYP,GAAZ,EAAiBC,OAAjB;;AAEAO,aAAOF,KAAKC,MAAL,CAAYE,KAAnB,EAA0BC,OAA1B,CAAkCV,IAAIW,MAAJ,GAAa,CAA/C;AACAH,aAAOP,QAAQC,WAAf,EAA4BU,cAA5B,CAA2CC,OAAOC,IAAP,CAAYd,GAAZ,EAAiB,OAAjB,CAA3C;AACAQ,aAAOP,QAAQI,SAAf,EAA0BO,cAA1B,CAAyC,CAAzC;AACD,KAdD;;AAgBAb,SAAK,QAAL,EAAe,MAAM;AACnB,YAAMgB,gBAAgB,QAAtB;AACA,YAAMC,iBAAiBD,cAAcJ,MAAd,GAAuB,CAA9C;;AAEA,YAAMM,SAAS,CAAC,CAAD,EAAI,CAAJ,EAAO,CAAP,EAAU,CAAV,EAAa,CAAb,EAAgB,CAAhB,EAAmB,CAAnB,CAAf;AACA,YAAMC,UAAU;AACdC,oBAAYhB,KACTC,EADS,GAETgB,kBAFS,CAEU,MAAMP,OAAOC,IAAP,CAAa,GAAEC,aAA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B,aAAOF,KAAKiB,MAAL,CAAYd,KAAnB,EAA0BqB,IAA1B,CAA+B9B,IAAIW,MAAnC;AACD,KAjBD;;AAmBAZ,SAAK,gBAAL,EAAuB,MAAM;AAC3B,YAAMC,MAAM,KAAZ;;AAEA,YAAMM,OAAOV,OAAO,MAAMI,IAAIW,MAAJ,GAAa,CAA1B,CAAb;AACAH,aAAOF,KAAKmB,cAAL,CAAoBzB,GAApB,CAAP,EAAiC8B,IAAjC,CAAsC9B,IAAIW,MAA1C;AACD,KALD;AAMD,GA/CD;AAgDD,CApMD","file":"string.js","sourcesContent":["'use strict';\n\nconst string = require('types/string');\n\ndescribe('string', () => {\n describe('null string', () => {\n test('encode', () => {\n const str = 'qwerty';\n const wstream = {\n writeBuffer: jest.fn(),\n writeInt8: jest.fn(),\n };\n\n const type = string(null);\n\n type.encode(str, wstream);\n\n expect(type.encode.bytes).toEqual(str.length + 1);\n expect(wstream.writeBuffer).toBeCalledWith(Buffer.from(str, 'ascii'));\n expect(wstream.writeInt8).toBeCalledWith(0);\n });\n\n test('decode', () => {\n const expectedValue = 'qwerty';\n const expectedLength = expectedValue.length + 1;\n\n const values = [1, 2, 3, 4, 5, 6, 0];\n const rstream = {\n readBuffer: jest\n .fn()\n .mockImplementation(() => Buffer.from(`${expectedValue}\\0`)),\n length: expectedLength,\n indexOf: jest.fn().mockImplementation(i => values.indexOf(i)),\n };\n\n const type = string(null);\n\n expect(type.decode(rstream)).toEqual(expectedValue);\n expect(type.decode.bytes).toEqual(expectedLength);\n expect(rstream.readBuffer).toBeCalledWith(expectedLength);\n });\n\n test('encodingLength', () => {\n const value = 'qwerty';\n const type = string(null);\n\n expect(type.encodingLength(value)).toEqual(value.length + 1);\n });\n });\n\n describe('fixed length', () => {\n test('encode', () => {\n const length = 3;\n const wstream = {\n writeBuffer: jest.fn(),\n };\n const str = 'qwe';\n\n const type = string(length);\n type.encode(str, wstream);\n\n expect(type.encode.bytes).toEqual(length);\n expect(wstream.writeBuffer).toBeCalledWith(Buffer.from(str, 'ascii'));\n });\n\n test('decode', () => {\n const str = 'qwe';\n const length = 3;\n const rstream = {\n readBuffer: jest.fn().mockImplementation(() => Buffer.from(str)),\n };\n const type = string(length);\n\n expect(type.decode(rstream)).toEqual(str);\n expect(type.decode.bytes).toEqual(length);\n });\n\n test('encodingLength', () => {\n const length = 3;\n const value = 'qw';\n const type = string(length);\n\n expect(type.encodingLength(value)).toEqual(length);\n });\n });\n\n describe('length is type', () => {\n test('encode', () => {\n const writeBuffer = jest.fn();\n const wstream = {\n writeBuffer,\n };\n\n const str = 'qwe';\n const lengthBytes = 3;\n const buf = Buffer.from(str, 'ascii');\n\n const lengthType = {\n decode() {},\n encode: jest.fn().mockImplementation(() => {\n lengthType.encode.bytes = lengthBytes;\n }),\n };\n\n const type = string(lengthType);\n type.encode(str, wstream);\n\n expect(lengthType.encode).toHaveBeenCalledTimes(1);\n expect(lengthType.encode).toBeCalledWith(str.length, wstream);\n expect(writeBuffer).toHaveBeenCalledTimes(1);\n expect(writeBuffer).toBeCalledWith(buf);\n expect(type.encode.bytes).toBe(str.length + lengthBytes);\n });\n\n test('decode', () => {\n const str = 'qwe';\n const length = 3;\n const lengthBytes = 3;\n const rstream = {\n readBuffer: jest.fn().mockImplementation(() => Buffer.from(str)),\n };\n\n const lengthType = {\n encode() {},\n decode: () => length,\n };\n\n lengthType.decode.bytes = lengthBytes;\n const type = string(lengthType);\n\n expect(type.decode(rstream)).toEqual(str);\n expect(type.decode.bytes).toEqual(length + lengthBytes);\n });\n\n test('encodingLength', () => {\n const typeLength = 2;\n const str = 'qwe';\n\n const lengthType = {\n encode() {},\n decode() {},\n encodingLength() {\n return typeLength;\n },\n };\n\n const type = string(lengthType);\n\n expect(type.encodingLength(str)).toBe(typeLength + str.length);\n });\n });\n\n describe('length is callback', () => {\n test('encode', () => {\n const writeBuffer = jest.fn();\n const wstream = {\n writeBuffer,\n };\n\n const str = 'qwe';\n const lengthBytes = 3;\n const buf = Buffer.from(str, 'ascii');\n\n const callback = jest.fn().mockImplementation(() => lengthBytes);\n\n const type = string(callback);\n type.encode(str, wstream);\n\n expect(callback).toHaveBeenCalledTimes(1);\n expect(writeBuffer).toHaveBeenCalledTimes(1);\n expect(writeBuffer).toBeCalledWith(buf);\n expect(type.encode.bytes).toBe(str.length);\n });\n\n test('decode', () => {\n const str = 'qwe';\n const buf = Buffer.from(str);\n const readBuffer = jest.fn().mockImplementation(() => buf);\n\n const rstream = {\n readBuffer,\n };\n\n const callback = jest.fn().mockImplementation(() => str.length);\n const type = string(callback);\n\n expect(type.decode(rstream)).toBe(str);\n expect(readBuffer).toHaveBeenCalledTimes(1);\n expect(readBuffer).toBeCalledWith(str.length);\n expect(callback).toHaveBeenCalledTimes(1);\n expect(type.decode.bytes).toBe(str.length);\n });\n\n test('encodingLength', () => {\n const str = 'qwe';\n\n const type = string(() => str.length * 2);\n expect(type.encodingLength(str)).toBe(str.length);\n });\n });\n});\n"]}