boundary.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. var test = require('tape');
  2. var Parser = require('../');
  3. test('2 byte utf8 \'De\' character: д', function (t) {
  4. t.plan(1);
  5. var p = new Parser();
  6. p.onValue = function (value) {
  7. t.equal(value, 'д');
  8. };
  9. var de_buffer = new Buffer([0xd0, 0xb4]);
  10. p.write('"');
  11. p.write(de_buffer);
  12. p.write('"');
  13. });
  14. test('3 byte utf8 \'Han\' character: 我', function (t) {
  15. t.plan(1);
  16. var p = new Parser();
  17. p.onValue = function (value) {
  18. t.equal(value, '我');
  19. };
  20. var han_buffer = new Buffer([0xe6, 0x88, 0x91]);
  21. p.write('"');
  22. p.write(han_buffer);
  23. p.write('"');
  24. });
  25. test('4 byte utf8 character (unicode scalar U+2070E): 𠜎', function (t) {
  26. t.plan(1);
  27. var p = new Parser();
  28. p.onValue = function (value) {
  29. t.equal(value, '𠜎');
  30. };
  31. var Ux2070E_buffer = new Buffer([0xf0, 0xa0, 0x9c, 0x8e]);
  32. p.write('"');
  33. p.write(Ux2070E_buffer);
  34. p.write('"');
  35. });
  36. test('3 byte utf8 \'Han\' character chunked inbetween 2nd and 3rd byte: 我', function (t) {
  37. t.plan(1);
  38. var p = new Parser();
  39. p.onValue = function (value) {
  40. t.equal(value, '我');
  41. };
  42. var han_buffer_first = new Buffer([0xe6, 0x88]);
  43. var han_buffer_second = new Buffer([0x91]);
  44. p.write('"');
  45. p.write(han_buffer_first);
  46. p.write(han_buffer_second);
  47. p.write('"');
  48. });
  49. test('4 byte utf8 character (unicode scalar U+2070E) chunked inbetween 2nd and 3rd byte: 𠜎', function (t) {
  50. t.plan(1);
  51. var p = new Parser();
  52. p.onValue = function (value) {
  53. t.equal(value, '𠜎');
  54. };
  55. var Ux2070E_buffer_first = new Buffer([0xf0, 0xa0]);
  56. var Ux2070E_buffer_second = new Buffer([0x9c, 0x8e]);
  57. p.write('"');
  58. p.write(Ux2070E_buffer_first);
  59. p.write(Ux2070E_buffer_second);
  60. p.write('"');
  61. });
  62. test('1-4 byte utf8 character string chunked inbetween random bytes: Aж文𠜱B', function (t) {
  63. t.plan(1);
  64. var p = new Parser();
  65. p.onValue = function (value) {
  66. t.equal(value, 'Aж文𠜱B');
  67. };
  68. var eclectic_buffer = new Buffer([0x41, // A
  69. 0xd0, 0xb6, // ж
  70. 0xe6, 0x96, 0x87, // 文
  71. 0xf0, 0xa0, 0x9c, 0xb1, // 𠜱
  72. 0x42]); // B
  73. var rand_chunk = Math.floor(Math.random() * (eclectic_buffer.length));
  74. var first_buffer = eclectic_buffer.slice(0, rand_chunk);
  75. var second_buffer = eclectic_buffer.slice(rand_chunk);
  76. //console.log('eclectic_buffer: ' + eclectic_buffer)
  77. //console.log('sliced from 0 to ' + rand_chunk);
  78. //console.log(first_buffer);
  79. //console.log('then sliced from ' + rand_chunk + ' to the end');
  80. //console.log(second_buffer);
  81. console.log('chunked after offset ' + rand_chunk);
  82. p.write('"');
  83. p.write(first_buffer);
  84. p.write(second_buffer);
  85. p.write('"');
  86. });