summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDavid Cai <davidcai1993@yahoo.com>2017-04-28 11:07:55 +0800
committerDavid Cai <davidcai1993@yahoo.com>2017-05-03 10:35:56 +0800
commit04796ee97f594186c6aed9ecfeae4cf61dca544d (patch)
tree48deb1fba390c917b6658938d986257cc5e303eb /test
parent133fb0c3b784cc06f5847c4662d4d9c91f66146d (diff)
downloadandroid-node-v8-04796ee97f594186c6aed9ecfeae4cf61dca544d.tar.gz
android-node-v8-04796ee97f594186c6aed9ecfeae4cf61dca544d.tar.bz2
android-node-v8-04796ee97f594186c6aed9ecfeae4cf61dca544d.zip
test: increase coverage of buffer
Increase coverage of lib/buffer.js. PR-URL: https://github.com/nodejs/node/pull/12714 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Diffstat (limited to 'test')
-rw-r--r--test/parallel/test-buffer-bytelength.js21
-rw-r--r--test/parallel/test-buffer-tostring.js25
-rw-r--r--test/parallel/test-buffer-write.js55
3 files changed, 95 insertions, 6 deletions
diff --git a/test/parallel/test-buffer-bytelength.js b/test/parallel/test-buffer-bytelength.js
index c30e06cc73..b140478933 100644
--- a/test/parallel/test-buffer-bytelength.js
+++ b/test/parallel/test-buffer-bytelength.js
@@ -16,6 +16,8 @@ assert.throws(() => { Buffer.byteLength({}, 'latin1'); },
assert.throws(() => { Buffer.byteLength(); },
/"string" must be a string, Buffer, or ArrayBuffer/);
+assert.strictEqual(Buffer.byteLength('', undefined, true), -1);
+
assert(ArrayBuffer.isView(new Buffer(10)));
assert(ArrayBuffer.isView(new SlowBuffer(10)));
assert(ArrayBuffer.isView(Buffer.alloc(10)));
@@ -76,6 +78,7 @@ assert.strictEqual(Buffer.byteLength('ßœ∑≈', 'unkn0wn enc0ding'), 10);
// base64
assert.strictEqual(Buffer.byteLength('aGVsbG8gd29ybGQ=', 'base64'), 11);
+assert.strictEqual(Buffer.byteLength('aGVsbG8gd29ybGQ=', 'BASE64'), 11);
assert.strictEqual(Buffer.byteLength('bm9kZS5qcyByb2NrcyE=', 'base64'), 14);
assert.strictEqual(Buffer.byteLength('aGkk', 'base64'), 3);
assert.strictEqual(
@@ -87,12 +90,18 @@ assert.strictEqual(Buffer.byteLength('aaaa==', 'base64'), 3);
assert.strictEqual(Buffer.byteLength('Il était tué'), 14);
assert.strictEqual(Buffer.byteLength('Il était tué', 'utf8'), 14);
-assert.strictEqual(Buffer.byteLength('Il était tué', 'ascii'), 12);
-assert.strictEqual(Buffer.byteLength('Il était tué', 'latin1'), 12);
-assert.strictEqual(Buffer.byteLength('Il était tué', 'binary'), 12);
-['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
- assert.strictEqual(24, Buffer.byteLength('Il était tué', encoding));
-});
+
+['ascii', 'latin1', 'binary']
+ .reduce((es, e) => es.concat(e, e.toUpperCase()), [])
+ .forEach((encoding) => {
+ assert.strictEqual(Buffer.byteLength('Il était tué', encoding), 12);
+ });
+
+['ucs2', 'ucs-2', 'utf16le', 'utf-16le']
+ .reduce((es, e) => es.concat(e, e.toUpperCase()), [])
+ .forEach((encoding) => {
+ assert.strictEqual(Buffer.byteLength('Il était tué', encoding), 24);
+ });
// Test that ArrayBuffer from a different context is detected correctly
const arrayBuf = vm.runInNewContext('new ArrayBuffer()');
diff --git a/test/parallel/test-buffer-tostring.js b/test/parallel/test-buffer-tostring.js
new file mode 100644
index 0000000000..776b7c0892
--- /dev/null
+++ b/test/parallel/test-buffer-tostring.js
@@ -0,0 +1,25 @@
+'use strict';
+
+require('../common');
+const assert = require('assert');
+
+// utf8, ucs2, ascii, latin1, utf16le
+const encodings = ['utf8', 'ucs2', 'ucs-2', 'ascii', 'latin1', 'binary',
+ 'utf16le', 'utf-16le'];
+
+encodings
+ .reduce((es, e) => es.concat(e, e.toUpperCase()), [])
+ .forEach((encoding) => {
+ assert.strictEqual(Buffer.from('foo', encoding).toString(encoding), 'foo');
+ });
+
+// base64
+['base64', 'BASE64'].forEach((encoding) => {
+ assert.strictEqual(Buffer.from('Zm9v', encoding).toString(encoding), 'Zm9v');
+});
+
+// hex
+['hex', 'HEX'].forEach((encoding) => {
+ assert.strictEqual(Buffer.from('666f6f', encoding).toString(encoding),
+ '666f6f');
+});
diff --git a/test/parallel/test-buffer-write.js b/test/parallel/test-buffer-write.js
new file mode 100644
index 0000000000..80c95dda44
--- /dev/null
+++ b/test/parallel/test-buffer-write.js
@@ -0,0 +1,55 @@
+'use strict';
+
+require('../common');
+const assert = require('assert');
+
+const outsideBounds = /^RangeError: Attempt to write outside buffer bounds$/;
+
+assert.throws(() => Buffer.alloc(9).write('foo', -1), outsideBounds);
+assert.throws(() => Buffer.alloc(9).write('foo', 10), outsideBounds);
+
+const resultMap = new Map([
+ ['utf8', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
+ ['ucs2', Buffer.from([102, 0, 111, 0, 111, 0, 0, 0, 0])],
+ ['ascii', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
+ ['latin1', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
+ ['binary', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
+ ['utf16le', Buffer.from([102, 0, 111, 0, 111, 0, 0, 0, 0])],
+ ['base64', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
+ ['hex', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])]
+]);
+
+// utf8, ucs2, ascii, latin1, utf16le
+const encodings = ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1',
+ 'binary', 'utf16le', 'utf-16le'];
+
+encodings
+ .reduce((es, e) => es.concat(e, e.toUpperCase()), [])
+ .forEach((encoding) => {
+ const buf = Buffer.alloc(9);
+ const len = Buffer.byteLength('foo', encoding);
+ assert.strictEqual(buf.write('foo', 0, len, encoding), len);
+
+ if (encoding.indexOf('-') !== -1)
+ encoding = encoding.replace('-', '');
+
+ assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase()));
+ });
+
+// base64
+['base64', 'BASE64'].forEach((encoding) => {
+ const buf = Buffer.alloc(9);
+ const len = Buffer.byteLength('Zm9v', encoding);
+
+ assert.strictEqual(buf.write('Zm9v', 0, len, encoding), len);
+ assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase()));
+});
+
+// hex
+['hex', 'HEX'].forEach((encoding) => {
+ const buf = Buffer.alloc(9);
+ const len = Buffer.byteLength('666f6f', encoding);
+
+ assert.strictEqual(buf.write('666f6f', 0, len, encoding), len);
+ assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase()));
+});