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