aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorEvan Lucas <evanlucas@me.com>2015-12-01 08:11:18 -0600
committerEvan Lucas <evanlucas@me.com>2015-12-02 06:11:45 -0600
commit24012a879d2fe17287a857ee20f696a78590db15 (patch)
tree1a7f0649a2e7453cac6e1516a61582668381be84 /test
parent451254ed25a3b9860d7b45dade6ed05b629d6bbd (diff)
downloadandroid-node-v8-24012a879d2fe17287a857ee20f696a78590db15.tar.gz
android-node-v8-24012a879d2fe17287a857ee20f696a78590db15.tar.bz2
android-node-v8-24012a879d2fe17287a857ee20f696a78590db15.zip
util: make inspect more reliable
34a35919e165cba6d5972e004e6b2cbdf2f4c65a added pretty printing for TypedArray, ArrayBuffer, and DataView. This change allows inspecting those across different contexts. Since instanceof does not work across contexts, we can use v8::Value::IsTypedArray, v8::Value::IsArrayBuffer, and v8::Value::IsDataView PR-URL: https://github.com/nodejs/node/pull/4098 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'test')
-rw-r--r--test/parallel/test-util-inspect.js62
1 files changed, 62 insertions, 0 deletions
diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js
index 93ac7157fd..3a033eb3aa 100644
--- a/test/parallel/test-util-inspect.js
+++ b/test/parallel/test-util-inspect.js
@@ -2,6 +2,7 @@
var common = require('../common');
var assert = require('assert');
var util = require('util');
+const vm = require('vm');
assert.equal(util.inspect(1), '1');
assert.equal(util.inspect(false), 'false');
@@ -68,6 +69,35 @@ for (const showHidden of [true, false]) {
' y: 1337 }');
}
+// Now do the same checks but from a different context
+for (const showHidden of [true, false]) {
+ const ab = vm.runInNewContext('new ArrayBuffer(4)');
+ const dv = vm.runInNewContext('new DataView(ab, 1, 2)', { ab: ab });
+ assert.equal(util.inspect(ab, showHidden), 'ArrayBuffer { byteLength: 4 }');
+ assert.equal(util.inspect(new DataView(ab, 1, 2), showHidden),
+ 'DataView {\n' +
+ ' byteLength: 2,\n' +
+ ' byteOffset: 1,\n' +
+ ' buffer: ArrayBuffer { byteLength: 4 } }');
+ assert.equal(util.inspect(ab, showHidden), 'ArrayBuffer { byteLength: 4 }');
+ assert.equal(util.inspect(dv, showHidden),
+ 'DataView {\n' +
+ ' byteLength: 2,\n' +
+ ' byteOffset: 1,\n' +
+ ' buffer: ArrayBuffer { byteLength: 4 } }');
+ ab.x = 42;
+ dv.y = 1337;
+ assert.equal(util.inspect(ab, showHidden),
+ 'ArrayBuffer { byteLength: 4, x: 42 }');
+ assert.equal(util.inspect(dv, showHidden),
+ 'DataView {\n' +
+ ' byteLength: 2,\n' +
+ ' byteOffset: 1,\n' +
+ ' buffer: ArrayBuffer { byteLength: 4, x: 42 },\n' +
+ ' y: 1337 }');
+}
+
+
[ Float32Array,
Float64Array,
Int16Array,
@@ -94,6 +124,38 @@ for (const showHidden of [true, false]) {
assert.equal(util.inspect(array, false), `${constructor.name} [ 65, 97 ]`);
});
+// Now check that declaring a TypedArray in a different context works the same
+[ Float32Array,
+ Float64Array,
+ Int16Array,
+ Int32Array,
+ Int8Array,
+ Uint16Array,
+ Uint32Array,
+ Uint8Array,
+ Uint8ClampedArray ].forEach(constructor => {
+ const length = 2;
+ const byteLength = length * constructor.BYTES_PER_ELEMENT;
+ const array = vm.runInNewContext('new constructor(new ArrayBuffer(' +
+ 'byteLength), 0, length)',
+ { constructor: constructor,
+ byteLength: byteLength,
+ length: length
+ });
+ array[0] = 65;
+ array[1] = 97;
+ assert.equal(util.inspect(array, true),
+ `${constructor.name} [\n` +
+ ` 65,\n` +
+ ` 97,\n` +
+ ` [BYTES_PER_ELEMENT]: ${constructor.BYTES_PER_ELEMENT},\n` +
+ ` [length]: ${length},\n` +
+ ` [byteLength]: ${byteLength},\n` +
+ ` [byteOffset]: 0,\n` +
+ ` [buffer]: ArrayBuffer { byteLength: ${byteLength} } ]`);
+ assert.equal(util.inspect(array, false), `${constructor.name} [ 65, 97 ]`);
+ });
+
// Due to the hash seed randomization it's not deterministic the order that
// the following ways this hash is displayed.
// See http://codereview.chromium.org/9124004/