You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
3.9 KiB

4 years ago
  1. 'use strict'
  2. var test = require('tape')
  3. var txtStr = require('./')()
  4. var txtBin = require('./')({ binary: true })
  5. var obj = {
  6. String: 'foo',
  7. number: 42,
  8. empty: '',
  9. null: null,
  10. bool: true,
  11. buffer: new Buffer('bar')
  12. }
  13. test('encodingLength', function (t) {
  14. var len = txtBin.encodingLength(obj)
  15. t.equal(len, 54)
  16. t.end()
  17. })
  18. test('encode', function (t) {
  19. var buf = txtBin.encode(obj)
  20. var expected = new Buffer('0a' + '537472696e67' + '3d' + '666f6f' +
  21. '09' + '6e756d626572' + '3d' + '3432' +
  22. '06' + '656d707479' + '3d' +
  23. '09' + '6e756c6c' + '3d' + '6e756c6c' +
  24. '04' + '626f6f6c' +
  25. '0a' + '627566666572' + '3d' + '626172', 'hex')
  26. t.deepEqual(buf, expected)
  27. t.equal(txtBin.encode.bytes, expected.length)
  28. t.end()
  29. })
  30. test('encode - empty', function (t) {
  31. var buf = txtBin.encode({})
  32. var expected = new Buffer('00', 'hex')
  33. t.deepEqual(buf, expected)
  34. t.equal(txtBin.encode.bytes, expected.length)
  35. t.end()
  36. })
  37. test('encode - undefined', function (t) {
  38. var buf = txtBin.encode()
  39. var expected = new Buffer('00', 'hex')
  40. t.deepEqual(buf, expected)
  41. t.equal(txtBin.encode.bytes, expected.length)
  42. t.end()
  43. })
  44. test('encode - with buffer', function (t) {
  45. var buf = new Buffer(3)
  46. buf.fill(255)
  47. txtBin.encode({}, buf)
  48. var expected = new Buffer('00ffff', 'hex')
  49. t.deepEqual(buf, expected)
  50. t.equal(txtBin.encode.bytes, 1)
  51. t.end()
  52. })
  53. test('encode - with buffer and offset', function (t) {
  54. var buf = new Buffer(3)
  55. buf.fill(255)
  56. txtBin.encode({}, buf, 1)
  57. var expected = new Buffer('ff00ff', 'hex')
  58. t.deepEqual(buf, expected)
  59. t.equal(txtBin.encode.bytes, 1)
  60. t.end()
  61. })
  62. test('decode', function (t) {
  63. var encoded = txtBin.encode(obj)
  64. var result = txtBin.decode(encoded)
  65. var expected = {
  66. string: new Buffer('foo'),
  67. number: new Buffer('42'),
  68. empty: new Buffer(0),
  69. null: new Buffer('null'),
  70. bool: true,
  71. buffer: new Buffer('bar')
  72. }
  73. t.deepEqual(result, expected)
  74. t.equal(txtBin.decode.bytes, encoded.length)
  75. t.end()
  76. })
  77. test('decode - strings', function (t) {
  78. var encoded = txtStr.encode(obj)
  79. var result = txtStr.decode(encoded)
  80. var expected = {
  81. string: 'foo',
  82. number: '42',
  83. empty: '',
  84. null: 'null',
  85. bool: true,
  86. buffer: 'bar'
  87. }
  88. t.deepEqual(result, expected)
  89. t.equal(txtStr.decode.bytes, encoded.length)
  90. t.end()
  91. })
  92. test('decode - duplicate', function (t) {
  93. var orig = {
  94. Foo: 'bar',
  95. foo: 'ignore this'
  96. }
  97. var expected = {
  98. foo: new Buffer('bar')
  99. }
  100. var encoded = txtBin.encode(orig)
  101. var result = txtBin.decode(encoded)
  102. t.deepEqual(result, expected)
  103. t.equal(txtBin.decode.bytes, encoded.length)
  104. t.end()
  105. })
  106. test('decode - single zero bype', function (t) {
  107. var encoded = new Buffer('00', 'hex')
  108. var result = txtBin.decode(encoded)
  109. t.deepEqual(result, {})
  110. t.equal(txtBin.decode.bytes, encoded.length)
  111. t.end()
  112. })
  113. test('decode - with offset', function (t) {
  114. var encoded = new Buffer('012300', 'hex')
  115. var result = txtBin.decode(encoded, 2)
  116. t.deepEqual(result, {})
  117. t.equal(txtBin.decode.bytes, 1)
  118. t.end()
  119. })
  120. test('decode - exactly 256 bytes', function (t) {
  121. var expected = { foo: '' }
  122. var maxLength = Object.keys(expected).reduce(function (total, key) {
  123. return total - key.length - 1 // - 1 for the equal sign used to separate the key and the value
  124. }, 255)
  125. for (var n = 0; n < maxLength; n++) {
  126. expected.foo += 'x'
  127. }
  128. // the max case:
  129. var encoded = txtStr.encode(expected)
  130. t.equal(txtStr.encode.bytes, 256)
  131. var result = txtStr.decode(encoded)
  132. t.deepEqual(result, expected)
  133. t.equal(txtStr.decode.bytes, encoded.length)
  134. // go beound the max:
  135. expected.foo += 'x'
  136. encoded = txtStr.encode(expected)
  137. t.equal(txtStr.encode.bytes, 257)
  138. result = txtStr.decode(encoded)
  139. t.notDeepEqual(result, expected)
  140. t.ok(txtStr.decode.bytes > encoded.length)
  141. t.end()
  142. })