From 971ab3c2e618c067966dfe7edebba520b6698e4d Mon Sep 17 00:00:00 2001 From: Jesse Tane Date: Mon, 15 Dec 2014 18:53:47 -0500 Subject: [PATCH] update from-string tests and rename from utf16 --- test/from-string.js | 126 ++++++++++++++++++++++++++++++++++++++++++++ test/utf16.js | 44 ---------------- 2 files changed, 126 insertions(+), 44 deletions(-) create mode 100644 test/from-string.js delete mode 100644 test/utf16.js diff --git a/test/from-string.js b/test/from-string.js new file mode 100644 index 0000000..eebd395 --- /dev/null +++ b/test/from-string.js @@ -0,0 +1,126 @@ +var B = require('../').Buffer +var test = require('tape') +if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false + + +test('detect utf16 surrogate pairs', function(t) { + var text = '\uD83D\uDE38' + '\uD83D\uDCAD' + '\uD83D\uDC4D' + var buf = new B(text) + t.equal(text, buf.toString()) + t.end() +}) + +test('replace orphaned utf16 surrogate lead code point', function(t) { + var text = '\uD83D\uDE38' + '\uD83D' + '\uD83D\uDC4D' + var buf = new B(text) + t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0xef, 0xbf, 0xbd, 0xf0, 0x9f, 0x91, 0x8d ])) + t.end() +}) + +test('replace orphaned utf16 surrogate trail code point', function(t) { + var text = '\uD83D\uDE38' + '\uDCAD' + '\uD83D\uDC4D' + var buf = new B(text) + t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0xef, 0xbf, 0xbd, 0xf0, 0x9f, 0x91, 0x8d ])) + t.end() +}) + +test('do not write partial utf16 code units', function(t) { + var f = new B([0, 0, 0, 0, 0]) + t.equal(f.length, 5) + var size = f.write('あいうえお', 'utf16le') + t.equal(size, 4) + t.deepEqual(f, new B([0x42, 0x30, 0x44, 0x30, 0x00])) + t.end() +}) + +test('handle partial utf16 code points when encoding to utf8 the way node does', function(t) { + var text = '\uD83D\uDE38' + '\uD83D\uDC4D' + + var buf = new B(8) + buf.fill(0) + buf.write(text) + t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0xf0, 0x9f, 0x91, 0x8d ])) + + buf = new B(7) + buf.fill(0) + buf.write(text) + t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00, 0x00, 0x00 ])) + + buf = new B(6) + buf.fill(0) + buf.write(text) + t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00, 0x00 ])) + + var buf = new B(5); + buf.fill(0) + buf.write(text) + t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00 ])) + + var buf = new B(4); + buf.fill(0) + buf.write(text) + t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8 ])) + + var buf = new B(3); + buf.fill(0) + buf.write(text) + t.deepEqual(buf, new B([ 0x00, 0x00, 0x00 ])) + + var buf = new B(2); + buf.fill(0) + buf.write(text) + t.deepEqual(buf, new B([ 0x00, 0x00 ])) + + var buf = new B(1); + buf.fill(0) + buf.write(text) + t.deepEqual(buf, new B([ 0x00 ])) + + t.end() +}) + +test('handle invalid utf16 code points when encoding to utf8 the way node does', function(t) { + var text = 'a' + '\uDE38\uD83D' + 'b' + + var buf = new B(8) + buf.fill(0) + buf.write(text) + t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd, 0x62 ])) + + buf = new B(7) + buf.fill(0) + buf.write(text) + t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd ])) + + buf = new B(6) + buf.fill(0) + buf.write(text) + t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0x00, 0x00 ])) + + buf = new B(5) + buf.fill(0) + buf.write(text) + t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd, 0x00 ])) + + buf = new B(4) + buf.fill(0) + buf.write(text) + t.deepEqual(buf, new B([ 0x61, 0xef, 0xbf, 0xbd ])) + + buf = new B(3) + buf.fill(0) + buf.write(text) + t.deepEqual(buf, new B([ 0x61, 0x00, 0x00 ])) + + buf = new B(2) + buf.fill(0) + buf.write(text) + t.deepEqual(buf, new B([ 0x61, 0x00 ])) + + buf = new B(1) + buf.fill(0) + buf.write(text) + t.deepEqual(buf, new B([ 0x61 ])) + + t.end() +}) diff --git a/test/utf16.js b/test/utf16.js deleted file mode 100644 index 73349a0..0000000 --- a/test/utf16.js +++ /dev/null @@ -1,44 +0,0 @@ -var B = require('../').Buffer -var test = require('tape') -if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false - - -test('detect utf16 surrogate pairs', function(t) { - var text = '\uD83D\uDE38' + '\uD83D\uDCAD' + '\uD83D\uDC4D' - var buf = new B(text) - t.equal(text, buf.toString()) - t.end() -}) - -test('throw on orphaned utf16 surrogate lead code point', function(t) { - var text = '\uD83D\uDE38' + '\uD83D' + '\uD83D\uDC4D' - var err - try { - var buf = new B(text) - } catch (e) { - err = e - } - t.equal(err instanceof URIError, true) - t.end() -}) - -test('throw on orphaned utf16 surrogate trail code point', function(t) { - var text = '\uD83D\uDE38' + '\uDCAD' + '\uD83D\uDC4D' - var err - try { - var buf = new B(text) - } catch (e) { - err = e - } - t.equal(err instanceof URIError, true) - t.end() -}) - -test('do not write partial utf16 code units', function(t) { - var f = new B([0, 0, 0, 0, 0]) - t.equal(f.length, 5) - var size = f.write('あいうえお', 'utf16le') - t.equal(size, 4) - t.deepEqual(f, new B([0x42, 0x30, 0x44, 0x30, 0x00])) - t.end() -}) -- 2.34.1