encoder-test.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. jest.dontMock('../index');
  2. const erlpack = require('../index.js');
  3. describe('packs', () => {
  4. it('string with null byte', () => {
  5. const packed = erlpack.pack('hello\x00 world');
  6. const expected = new Buffer('\x83m\x00\x00\x00\x0chello\x00 world', 'binary');
  7. expect(packed.equals(expected)).toBeTruthy();
  8. });
  9. it('string without null byte', () => {
  10. const packed = erlpack.pack('hello world');
  11. const expected = new Buffer('\x83m\x00\x00\x00\x0bhello world', 'binary');
  12. expect(packed.equals(expected)).toBeTruthy();
  13. });
  14. it('dictionary', () => {
  15. const expected = new Buffer(
  16. '\x83t\x00\x00\x00\x03a\x02a\x02a\x03l\x00\x00\x00\x03a\x01a\x02a\x03jm\x00\x00\x00\x01aa\x01',
  17. 'binary'
  18. );
  19. const packed = erlpack.pack({'a': 1, 2: 2, 3: [1, 2, 3]});
  20. expect(packed.equals(expected)).toBeTruthy();
  21. });
  22. it('false', () => {
  23. const expected = new Buffer('\x83s\x05false', 'binary');
  24. const packed = erlpack.pack(false);
  25. expect(packed.equals(expected)).toBeTruthy();
  26. });
  27. it('true', () => {
  28. const expected = new Buffer('\x83s\x04true', 'binary');
  29. const packed = erlpack.pack(true);
  30. expect(packed.equals(expected)).toBeTruthy();
  31. });
  32. it('null is nil', () => {
  33. const expected = new Buffer('\x83s\x03nil', 'binary');
  34. const packed = erlpack.pack(null);
  35. expect(packed.equals(expected)).toBeTruthy();
  36. });
  37. it('undefined is nil', () => {
  38. const expected = new Buffer('\x83s\x03nil', 'binary');
  39. const packed = erlpack.pack(undefined);
  40. expect(packed.equals(expected)).toBeTruthy();
  41. });
  42. it('floats as new floats', () => {
  43. expect(erlpack.pack(2.5).equals(new Buffer('\x83F\x40\x04\x00\x00\x00\x00\x00\x00', 'binary'))).toBeTruthy();
  44. expect(erlpack.pack(51512123841234.31423412341435123412341342).equals(new Buffer('\x83F\x42\xc7\x6c\xcc\xeb\xed\x69\x28', 'binary'))).toBeTruthy();
  45. });
  46. it('small int', () => {
  47. function check(small_int) {
  48. expected = new Buffer(3);
  49. expected.write('\x83a', 0, 2, 'binary');
  50. expected.writeUInt8(small_int, 2);
  51. const packed = erlpack.pack(small_int);
  52. expect(expected.equals(packed)).toBeTruthy();
  53. }
  54. for(var i = 0; i < 256; ++i) {
  55. check(i);
  56. }
  57. });
  58. it('int32', () => {
  59. expect(erlpack.pack(1024).equals(new Buffer('\x83b\x00\x00\x04\x00', 'binary'))).toBeTruthy();
  60. expect(erlpack.pack(-2147483648).equals(new Buffer('\x83b\x80\x00\x00\x00', 'binary'))).toBeTruthy();
  61. expect(erlpack.pack(2147483647).equals(new Buffer('\x83b\x7f\xff\xff\xff', 'binary'))).toBeTruthy();
  62. });
  63. it('list', () => {
  64. const expected = new Buffer('\x83l\x00\x00\x00\x05a\x01m\x00\x00\x00\x03twoF\x40\x08\xcc\xcc\xcc\xcc\xcc\xcdm\x00\x00\x00\x04fourl\x00\x00\x00\x01m\x00\x00\x00\x04fivejj', 'binary');
  65. const packed = erlpack.pack([1, "two", 3.1, "four", ['five']]);
  66. expect(packed.equals(expected)).toBeTruthy();
  67. });
  68. it('empty list', () => {
  69. expect(erlpack.pack([]).equals(new Buffer('\x83j', 'binary'))).toBeTruthy();
  70. });
  71. });