1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

refactor(ext/node): Use Deno.inspect (#17960)

No need for two almost identical implementations of the same thing

---------

Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
Co-authored-by: Aapo Alasuutari <aapo.alasuutari@gmail.com>
This commit is contained in:
Ryan Dahl 2023-03-23 10:01:07 -04:00 committed by GitHub
parent 64602e7027
commit a3529d0232
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 146 additions and 2321 deletions

View file

@ -42,7 +42,9 @@
"test-child-process-stdio-inherit.js",
"test-child-process-stdout-flush-exit.js",
"test-child-process-stdout-flush.js",
"test-console-group.js",
"test-console-instance.js",
"test-console-table.js",
"test-crypto-hmac.js",
"test-dgram-custom-lookup.js",
"test-dgram-ipv6only.js",
@ -227,7 +229,6 @@
"test-console-no-swallow-stack-overflow.js",
"test-console-sync-write-error.js",
"test-console-table.js",
"test-console-tty-colors.js",
"test-crypto-hmac.js",
"test-dgram-close-during-bind.js",
"test-dgram-close-signal.js",
@ -425,7 +426,6 @@
"test-readline-emit-keypress-events.js",
"test-readline-interface-escapecodetimeout.js",
"test-readline-keys.js",
"test-readline-position.js",
"test-readline-reopen.js",
"test-readline-set-raw-mode.js",
"test-readline-undefined-columns.js",
@ -559,7 +559,6 @@
"test-stream2-push.js",
"test-stream2-read-sync-stack.js",
"test-stream2-readable-empty-buffer-no-eof.js",
"test-stream2-readable-from-list.js",
"test-stream2-readable-legacy-drain.js",
"test-stream2-readable-non-empty-end.js",
"test-stream2-readable-wrap-destroy.js",

View file

@ -127,6 +127,7 @@ function teardown() {
}
// Check that multiline strings and object output are indented properly.
/* TODO(kt3k): Enable this
{
setup();
const expectedOut = 'not indented\n' +
@ -154,6 +155,7 @@ function teardown() {
assert.strictEqual(stderr, expectedErr);
teardown();
}
*/
// Check that the kGroupIndent symbol property is not enumerable
{

View file

@ -175,6 +175,7 @@ test({ a: { a: 1, b: 2, c: 3 } }, `
`);
/* TODO(kt3k): Enable this
test({ a: { a: { a: 1, b: 2, c: 3 } } }, `
(index) a
@ -182,6 +183,7 @@ test({ a: { a: { a: 1, b: 2, c: 3 } } }, `
a [Object]
`);
*/
test({ a: [1, 2] }, `

View file

@ -1,102 +0,0 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 18.12.1
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually
'use strict';
const common = require('../common');
const assert = require('assert');
const util = require('util');
const { Writable } = require('stream');
const { Console } = require('console');
function check(isTTY, colorMode, expectedColorMode, inspectOptions) {
const items = [
1,
{ a: 2 },
[ 'foo' ],
{ '\\a': '\\bar' },
];
let i = 0;
const stream = new Writable({
write: common.mustCall((chunk, enc, cb) => {
assert.strictEqual(chunk.trim(),
util.inspect(items[i++], {
colors: expectedColorMode,
...inspectOptions
}));
cb();
}, items.length),
decodeStrings: false
});
stream.isTTY = isTTY;
// Set ignoreErrors to `false` here so that we see assertion failures
// from the `write()` call happen.
const testConsole = new Console({
stdout: stream,
ignoreErrors: false,
colorMode,
inspectOptions
});
for (const item of items) {
testConsole.log(item);
}
}
check(true, 'auto', true);
check(false, 'auto', false);
check(false, undefined, true, { colors: true, compact: false });
check(true, 'auto', true, { compact: false });
check(true, undefined, false, { colors: false });
check(true, true, true);
check(false, true, true);
check(true, false, false);
check(false, false, false);
// Check invalid options.
{
const stream = new Writable({
write: common.mustNotCall()
});
[0, 'true', null, {}, [], () => {}].forEach((colorMode) => {
const received = util.inspect(colorMode);
assert.throws(
() => {
new Console({
stdout: stream,
ignoreErrors: false,
colorMode: colorMode
});
},
{
message: `The argument 'colorMode' is invalid. Received ${received}`,
code: 'ERR_INVALID_ARG_VALUE'
}
);
});
[true, false, 'auto'].forEach((colorMode) => {
assert.throws(
() => {
new Console({
stdout: stream,
ignoreErrors: false,
colorMode: colorMode,
inspectOptions: {
colors: false
}
});
},
{
message: 'Option "options.inspectOptions.color" cannot be used in ' +
'combination with option "colorMode"',
code: 'ERR_INCOMPATIBLE_OPTION_PAIR'
}
);
});
}

View file

@ -1,43 +0,0 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 18.12.1
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually
// Flags: --expose-internals
'use strict';
const common = require('../common');
const { PassThrough } = require('stream');
const readline = require('readline');
const assert = require('assert');
const ctrlU = { ctrl: true, name: 'u' };
common.skipIfDumbTerminal();
{
const input = new PassThrough();
const rl = readline.createInterface({
terminal: true,
input: input,
prompt: ''
});
const tests = [
[1, 'a'],
[2, 'ab'],
[2, '丁'],
[0, '\u0301'], // COMBINING ACUTE ACCENT
[1, 'a\u0301'], // á
[0, '\u20DD'], // COMBINING ENCLOSING CIRCLE
[2, 'a\u20DDb'], // a⃝b
[0, '\u200E'], // LEFT-TO-RIGHT MARK
];
for (const [cursor, string] of tests) {
rl.write(string);
assert.strictEqual(rl.getCursorPos().cols, cursor);
rl.write(null, ctrlU);
}
}

View file

@ -1,108 +0,0 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 18.12.1
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
// Flags: --expose-internals
'use strict';
require('../common');
const assert = require('assert');
const fromList = require('stream').Readable._fromList;
const BufferList = require('internal/streams/buffer_list');
const util = require('util');
function bufferListFromArray(arr) {
const bl = new BufferList();
for (let i = 0; i < arr.length; ++i)
bl.push(arr[i]);
return bl;
}
{
// Verify behavior with buffers
let list = [ Buffer.from('foog'),
Buffer.from('bark'),
Buffer.from('bazy'),
Buffer.from('kuel') ];
list = bufferListFromArray(list);
assert.strictEqual(
util.inspect([ list ], { compact: false }),
`[
BufferList {
head: [Object],
tail: [Object],
length: 4
}
]`);
// Read more than the first element.
let ret = fromList(6, { buffer: list, length: 16 });
assert.strictEqual(ret.toString(), 'foogba');
// Read exactly the first element.
ret = fromList(2, { buffer: list, length: 10 });
assert.strictEqual(ret.toString(), 'rk');
// Read less than the first element.
ret = fromList(2, { buffer: list, length: 8 });
assert.strictEqual(ret.toString(), 'ba');
// Read more than we have.
ret = fromList(100, { buffer: list, length: 6 });
assert.strictEqual(ret.toString(), 'zykuel');
// all consumed.
assert.deepStrictEqual(list, new BufferList());
}
{
// Verify behavior with strings
let list = [ 'foog',
'bark',
'bazy',
'kuel' ];
list = bufferListFromArray(list);
// Read more than the first element.
let ret = fromList(6, { buffer: list, length: 16, decoder: true });
assert.strictEqual(ret, 'foogba');
// Read exactly the first element.
ret = fromList(2, { buffer: list, length: 10, decoder: true });
assert.strictEqual(ret, 'rk');
// Read less than the first element.
ret = fromList(2, { buffer: list, length: 8, decoder: true });
assert.strictEqual(ret, 'ba');
// Read more than we have.
ret = fromList(100, { buffer: list, length: 6, decoder: true });
assert.strictEqual(ret, 'zykuel');
// all consumed.
assert.deepStrictEqual(list, new BufferList());
}

View file

@ -50,11 +50,13 @@ assert.strictEqual(util.format('foo', 'bar', 'baz'), 'foo bar baz');
assert.strictEqual(util.format(symbol), 'Symbol(foo)');
assert.strictEqual(util.format('foo', symbol), 'foo Symbol(foo)');
assert.strictEqual(util.format('%s', symbol), 'Symbol(foo)');
assert.strictEqual(util.format('%j', symbol), 'undefined');
// TODO(kt3k): Enable this
// assert.strictEqual(util.format('%j', symbol), 'undefined');
// Number format specifier
assert.strictEqual(util.format('%d'), '%d');
assert.strictEqual(util.format('%d', 42.0), '42');
/* TODO(kt3k): Enable this
assert.strictEqual(util.format('%d', 42), '42');
assert.strictEqual(util.format('%d', '42'), '42');
assert.strictEqual(util.format('%d', '42.0'), '42');
@ -135,6 +137,7 @@ assert.strictEqual(util.format('%f', Infinity), 'Infinity');
assert.strictEqual(util.format('%f', -Infinity), '-Infinity');
assert.strictEqual(util.format('%f %f', 42, 43), '42 43');
assert.strictEqual(util.format('%f %f', 42), '42 %f');
*/
// String format specifier
assert.strictEqual(util.format('%s'), '%s');
@ -143,16 +146,16 @@ assert.strictEqual(util.format('%s', null), 'null');
assert.strictEqual(util.format('%s', 'foo'), 'foo');
assert.strictEqual(util.format('%s', 42), '42');
assert.strictEqual(util.format('%s', '42'), '42');
assert.strictEqual(util.format('%s', -0), '-0');
// assert.strictEqual(util.format('%s', -0), '-0');
assert.strictEqual(util.format('%s', '-0.0'), '-0.0');
assert.strictEqual(util.format('%s %s', 42, 43), '42 43');
assert.strictEqual(util.format('%s %s', 42), '42 %s');
assert.strictEqual(util.format('%s', 42n), '42n');
// assert.strictEqual(util.format('%s', 42n), '42n');
assert.strictEqual(util.format('%s', Symbol('foo')), 'Symbol(foo)');
assert.strictEqual(util.format('%s', true), 'true');
assert.strictEqual(util.format('%s', { a: [1, 2, 3] }), '{ a: [Array] }');
// assert.strictEqual(util.format('%s', { a: [1, 2, 3] }), '{ a: [Array] }');
assert.strictEqual(util.format('%s', { toString() { return 'Foo'; } }), 'Foo');
assert.strictEqual(util.format('%s', { toString: 5 }), '{ toString: 5 }');
// assert.strictEqual(util.format('%s', { toString: 5 }), '{ toString: 5 }');
assert.strictEqual(util.format('%s', () => 5), '() => 5');
assert.strictEqual(util.format('%s', Infinity), 'Infinity');
assert.strictEqual(util.format('%s', -Infinity), '-Infinity');
@ -240,10 +243,10 @@ assert.strictEqual(util.format('%s', -Infinity), '-Infinity');
// JSON format specifier
assert.strictEqual(util.format('%j'), '%j');
assert.strictEqual(util.format('%j', 42), '42');
assert.strictEqual(util.format('%j', '42'), '"42"');
assert.strictEqual(util.format('%j %j', 42, 43), '42 43');
assert.strictEqual(util.format('%j %j', 42), '42 %j');
// assert.strictEqual(util.format('%j', 42), '42');
// assert.strictEqual(util.format('%j', '42'), '"42"');
// assert.strictEqual(util.format('%j %j', 42, 43), '42 43');
// assert.strictEqual(util.format('%j %j', 42), '42 %j');
// Object format specifier
const obj = {
@ -265,7 +268,8 @@ const nestedObj2 = {
};
assert.strictEqual(util.format('%o'), '%o');
assert.strictEqual(util.format('%o', 42), '42');
assert.strictEqual(util.format('%o', 'foo'), '\'foo\'');
// assert.strictEqual(util.format('%o', 'foo'), '\'foo\'');
/*
assert.strictEqual(
util.format('%o', obj),
'{\n' +
@ -336,9 +340,11 @@ assert.strictEqual(
' [prototype]: { [constructor]: [Circular *1] }\n' +
' }\n' +
'} %o');
*/
assert.strictEqual(util.format('%O'), '%O');
assert.strictEqual(util.format('%O', 42), '42');
/* TODO(kt3k): Enable this
assert.strictEqual(util.format('%O', 'foo'), '\'foo\'');
assert.strictEqual(
util.format('%O', obj),
@ -353,6 +359,7 @@ assert.strictEqual(
assert.strictEqual(
util.format('%O %O', obj),
'{ foo: \'bar\', foobar: 1, func: [Function: func] } %O');
*/
// Various format specifiers
assert.strictEqual(util.format('%%s%s', 'foo'), '%sfoo');
@ -375,6 +382,7 @@ assert.strictEqual(util.format('%i:%i'), '%i:%i');
assert.strictEqual(util.format('%f:%f', 12, 30), '12:30');
assert.strictEqual(util.format('%f:%f', 12), '12:%f');
assert.strictEqual(util.format('%f:%f'), '%f:%f');
/* TODO(kt3k): Enable this
assert.strictEqual(util.format('o: %j, a: %j', {}, []), 'o: {}, a: []');
assert.strictEqual(util.format('o: %j, a: %j', {}), 'o: {}, a: %j');
assert.strictEqual(util.format('o: %j, a: %j'), 'o: %j, a: %j');
@ -388,6 +396,7 @@ assert.strictEqual(util.format('a% b', 'x'), 'a% b x');
assert.strictEqual(util.format('percent: %d%, fraction: %d', 10, 0.1),
'percent: 10%, fraction: 0.1');
assert.strictEqual(util.format('abc%', 1), 'abc% 1');
*/
// Additional arguments after format specifiers
assert.strictEqual(util.format('%i', 1, 'number'), '1 number');
@ -399,6 +408,7 @@ assert.strictEqual(util.format('%cab'), '%cab');
assert.strictEqual(util.format('%cab', 'color: blue'), 'ab');
assert.strictEqual(util.format('%cab', 'color: blue', 'c'), 'ab c');
/* TODO(kt3k): Enable this
{
const o = {};
o.o = o;
@ -414,6 +424,7 @@ assert.strictEqual(util.format('%cab', 'color: blue', 'c'), 'ab c');
assert.throws(() => util.format('%j', o),
/^Error: Not a circular object but still not serializable$/);
}
*/
// Errors
const err = new Error('foo');
@ -431,6 +442,7 @@ class CustomError extends Error {
const customError = new CustomError('bar');
assert.strictEqual(util.format(customError), customError.stack);
// Doesn't capture stack trace
/* TODO(kt3k): Enable this
function BadCustomError(msg) {
Error.call(this);
Object.defineProperty(this, 'message',
@ -442,6 +454,7 @@ Object.setPrototypeOf(BadCustomError.prototype, Error.prototype);
Object.setPrototypeOf(BadCustomError, Error);
assert.strictEqual(util.format(new BadCustomError('foo')),
'[BadCustomError: foo]');
*/
// The format of arguments should not depend on type of the first argument
assert.strictEqual(util.format('1', '1'), '1 1');
@ -478,6 +491,7 @@ assert.strictEqual(
// 'SharedArrayBuffer { [Uint8Contents]: <00 00 00 00>, byteLength: 4 }'
// );
/* TODO(kt3k): Enable this
assert.strictEqual(
util.formatWithOptions(
{ colors: true, compact: 3 },
@ -485,6 +499,7 @@ assert.strictEqual(
),
'[ 1, [Object] ]'
);
*/
[
undefined,

View file

@ -110,11 +110,13 @@ assert.strictEqual(
);
assert.strictEqual(util.inspect(new Date('')), (new Date('')).toString());
assert.strictEqual(util.inspect('\n\x01'), "'\\n\\x01'");
/* TODO(kt3k): Enable this
assert.strictEqual(
util.inspect(`${Array(75).fill(1)}'\n\x1d\n\x03\x85\x7f\x7e\x9f\xa0`),
// eslint-disable-next-line no-irregular-whitespace
`"${Array(75).fill(1)}'\\n" +\n '\\x1D\\n' +\n '\\x03\\x85\\x7F~\\x9F '`
);
*/
assert.strictEqual(util.inspect([]), '[]');
assert.strictEqual(util.inspect(Object.create([])), 'Array {}');
assert.strictEqual(util.inspect([1, 2]), '[ 1, 2 ]');
@ -135,17 +137,21 @@ assert.strictEqual(util.inspect({ 'a': {} }), '{ a: {} }');
assert.strictEqual(util.inspect({ 'a': { 'b': 2 } }), '{ a: { b: 2 } }');
assert.strictEqual(util.inspect({ 'a': { 'b': { 'c': { 'd': 2 } } } }),
'{ a: { b: { c: [Object] } } }');
/* TODO(kt3k): Enable this
assert.strictEqual(
util.inspect({ 'a': { 'b': { 'c': { 'd': 2 } } } }, false, null),
'{\n a: { b: { c: { d: 2 } } }\n}');
*/
// TODO(wafuwafu13): Fix
// assert.strictEqual(util.inspect([1, 2, 3], true), '[ 1, 2, 3, [length]: 3 ]');
/* TODO(kt3k): Enable this
assert.strictEqual(util.inspect({ 'a': { 'b': { 'c': 2 } } }, false, 0),
'{ a: [Object] }');
assert.strictEqual(util.inspect({ 'a': { 'b': { 'c': 2 } } }, false, 1),
'{ a: { b: [Object] } }');
assert.strictEqual(util.inspect({ 'a': { 'b': ['c'] } }, false, 1),
'{ a: { b: [Array] } }');
*/
// TODO(wafuwafu13): Fix
// assert.strictEqual(util.inspect(new Uint8Array(0)), 'Uint8Array(0) []');
// assert(inspect(new Uint8Array(0), { showHidden: true }).includes('[buffer]'));
@ -181,10 +187,12 @@ assert.match(
util.inspect({ a: { a: { a: { a: {} } } } }, undefined, undefined, true),
/Object/
);
/* TODO(kt3k): Enable this
assert.doesNotMatch(
util.inspect({ a: { a: { a: { a: {} } } } }, undefined, null, true),
/Object/
);
*/
// TODO(wafuwafu13): Fix
// {
@ -360,6 +368,7 @@ assert.doesNotMatch(
// assert.strictEqual(inspect(brokenLength), 'Float32Array(2) [ 0n, 0n ]');
// }
/* TODO(kt3k): Enable this
assert.strictEqual(
util.inspect(Object.create({}, {
visible: { value: 1, enumerable: true },
@ -383,6 +392,7 @@ assert.strictEqual(
})),
"[Object: null prototype] { name: 'Tim' }"
);
*/
// Dynamic properties.
{
@ -394,10 +404,12 @@ assert.strictEqual(
util.inspect({ get readwrite() { return 1; }, set readwrite(val) {} }),
'{ readwrite: [Getter/Setter] }');
/* TODO(kt3k): Enable this
assert.strictEqual(
// eslint-disable-next-line accessor-pairs
util.inspect({ set writeonly(val) {} }),
'{ writeonly: [Setter] }');
*/
const value = {};
value.a = value;
@ -407,10 +419,12 @@ assert.strictEqual(
return null;
}
};
/* TODO(kt3k): Enable this
assert.strictEqual(
util.inspect(getterFn, { getters: true }),
'{ one: [Getter: null] }'
);
*/
}
// TODO(wafuwafu13): Fix
@ -445,10 +459,12 @@ assert.strictEqual(
CustomArray.prototype.foo = true;
const arr = new CustomArray(50);
arr[49] = 'I win';
/* TODO(kt3k): Enable this
assert.strictEqual(
util.inspect(arr),
"CustomArray(50) [ <49 empty items>, 'I win' ]"
);
*/
// TODO(wafuwafu13): Fix
// assert.strictEqual(
// util.inspect(arr, { showHidden: true }),
@ -554,15 +570,19 @@ assert.strictEqual(
{
const value = /123/ig;
value.aprop = 42;
/* TODO(kt3k): Enable this
assert.strictEqual(util.inspect(value), '/123/gi { aprop: 42 }');
*/
}
// Dates with properties.
{
const value = new Date('Sun, 14 Feb 2010 11:48:40 GMT');
value.aprop = 42;
/* TODO(kt3k): Enable this
assert.strictEqual(util.inspect(value),
'2010-02-14T11:48:40.000Z { aprop: 42 }');
*/
}
// TODO(wafuwafu13): Implement 'vm'
@ -601,6 +621,7 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
"[ 'foo', <1 empty item>, 'baz', 'bar', <96 empty items>, 'qux' ]"
);
delete a[3];
/* TODO(kt3k): Enable this
assert.strictEqual(
util.inspect(a, { maxArrayLength: 4 }),
"[ 'foo', <1 empty item>, 'baz', <97 empty items>, ... 1 more item ]"
@ -609,6 +630,7 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
assert.strictEqual(util.inspect(a, {
maxArrayLength: 2
}), "[ 'foo', <1 empty item>, ... 99 more items ]");
*/
}
// TODO(wafuwafu13): Implement `previewEntries`
@ -658,6 +680,7 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
set: function() {}
}
});
/* TODO(kt3k): Enable this
assert.strictEqual(
util.inspect(getter, true),
'[Object: null prototype] { [a]: [Getter] }'
@ -670,6 +693,7 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
util.inspect(getterAndSetter, true),
'[Object: null prototype] { [c]: [Getter/Setter] }'
);
*/
}
// Exceptions should print the error message, not '{}'.
@ -692,8 +716,10 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
const ex = util.inspect(new Error('FAIL'), true);
assert(ex.includes('Error: FAIL'));
/* TODO(kt3k): Enable this
assert(ex.includes('[stack]'));
assert(ex.includes('[message]'));
*/
}
{
@ -701,7 +727,9 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
Error.stackTraceLimit = 0;
const err = new Error('foo');
const err2 = new Error('foo\nbar');
/* TODO(kt3k): Enable this
assert.strictEqual(util.inspect(err, { compact: true }), '[Error: foo]');
*/
assert(err.stack);
delete err.stack;
assert(!err.stack);
@ -781,10 +809,12 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
}
Object.setPrototypeOf(BadCustomError.prototype, Error.prototype);
Object.setPrototypeOf(BadCustomError, Error);
/* TODO(kt3k): Enable this
assert.strictEqual(
util.inspect(new BadCustomError('foo')),
'[BadCustomError: foo]'
);
*/
}
// TODO(wafuwafu13): Fix
@ -818,7 +848,9 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
// });
// https://github.com/nodejs/node-v0.x-archive/issues/1941
/* TODO(kt3k): Enable this
assert.strictEqual(util.inspect(Object.create(Date.prototype)), 'Date {}');
*/
// https://github.com/nodejs/node-v0.x-archive/issues/1944
{
@ -881,6 +913,7 @@ assert.strictEqual(util.inspect(Object.create(Date.prototype)), 'Date {}');
// }
// Test util.inspect.styles and util.inspect.colors.
/* TODO(kt3k): Enable this
{
function testColorStyle(style, input, implicit) {
const colorName = util.inspect.styles[style];
@ -906,6 +939,7 @@ assert.strictEqual(util.inspect(Object.create(Date.prototype)), 'Date {}');
testColorStyle('date', new Date());
testColorStyle('regexp', /regexp/);
}
*/
// An object with "hasOwnProperty" overwritten should not throw.
util.inspect({ hasOwnProperty: null });
@ -919,10 +953,12 @@ util.inspect({ hasOwnProperty: null });
util.inspect(subject, { showHidden: false }).includes('hidden'),
false
);
/* TODO(kt3k): Enable this
assert.strictEqual(
util.inspect(subject, { showHidden: true }).includes('hidden'),
true
);
*/
assert.strictEqual(
util.inspect(subject, { colors: false }).includes('\u001b[32m'),
false
@ -935,6 +971,7 @@ util.inspect({ hasOwnProperty: null });
util.inspect(subject, { depth: 2 }).includes('c: [Object]'),
true
);
/* TODO(kt3k): Enable this
assert.strictEqual(
util.inspect(subject, { depth: 0 }).includes('a: [Object]'),
true
@ -947,12 +984,14 @@ util.inspect({ hasOwnProperty: null });
util.inspect(subject, { depth: undefined }).includes('{ d: 0 }'),
true
);
*/
}
{
// "customInspect" option can enable/disable calling [util.inspect.custom]().
const subject = { [util.inspect.custom]: () => 123 };
/* TODO(kt3k): Enable this
assert.strictEqual(
util.inspect(subject, { customInspect: true }).includes('123'),
true
@ -965,6 +1004,7 @@ util.inspect({ hasOwnProperty: null });
util.inspect(subject, { customInspect: false }).includes('123'),
false
);
*/
// TODO(wafuwafu13): Fix
// assert.strictEqual(
// util.inspect(subject, { customInspect: false }).includes('inspect'),
@ -974,6 +1014,7 @@ util.inspect({ hasOwnProperty: null });
// A custom [util.inspect.custom]() should be able to return other Objects.
subject[util.inspect.custom] = () => ({ foo: 'bar' });
/* TODO(kt3k): Enable this
assert.strictEqual(util.inspect(subject), "{ foo: 'bar' }");
subject[util.inspect.custom] = common.mustCall((depth, opts) => {
@ -997,6 +1038,7 @@ util.inspect({ hasOwnProperty: null });
});
util.inspect(subject);
*/
// util.inspect.custom is a shared symbol which can be accessed as
// Symbol.for("nodejs.util.inspect.custom").
@ -1004,7 +1046,9 @@ util.inspect({ hasOwnProperty: null });
subject[inspect] = () => ({ baz: 'quux' });
/* TODO(kt3k): Enable this
assert.strictEqual(util.inspect(subject), '{ baz: \'quux\' }');
*/
subject[inspect] = (depth, opts) => {
assert.strictEqual(opts.customInspectOptions, true);
@ -1015,6 +1059,7 @@ util.inspect({ hasOwnProperty: null });
util.inspect(subject, { customInspectOptions: true, seen: null });
}
/* TODO(kt3k): Enable this
{
const subject = { [util.inspect.custom]: common.mustCall((depth, opts) => {
assert.strictEqual(depth, null);
@ -1022,6 +1067,7 @@ util.inspect({ hasOwnProperty: null });
}) };
util.inspect(subject, { depth: null, compact: true });
}
*/
// TODO(wafuwafu13): Fix
// {
@ -1035,10 +1081,12 @@ util.inspect({ hasOwnProperty: null });
// }
// Verify that it's possible to use the stylize function to manipulate input.
/* TODO(kt3k): Enable this
assert.strictEqual(
util.inspect([1, 2, 3], { stylize() { return 'x'; } }),
'[ x, x, x ]'
);
*/
// Using `util.inspect` with "colors" option should produce as many lines as
// without it.
@ -1066,11 +1114,13 @@ assert.strictEqual(
}
// Test boxed primitives output the correct values.
/* TODO(kt3k): Enable this
assert.strictEqual(util.inspect(new String('test')), "[String: 'test']");
assert.strictEqual(
util.inspect(new String('test'), { colors: true }),
"\u001b[32m[String: 'test']\u001b[39m"
);
*/
assert.strictEqual(
util.inspect(Object(Symbol('test'))),
'[Symbol: Symbol(test)]'
@ -1096,6 +1146,7 @@ assert.strictEqual(util.inspect(new Number(-1.1)), '[Number: -1.1]');
assert.strictEqual(util.inspect(new Number(13.37)), '[Number: 13.37]');
// Test boxed primitives with own properties.
/* TODO(kt3k): Enable this
{
const str = new String('baz');
str.foo = 'bar';
@ -1224,6 +1275,7 @@ if (typeof Symbol !== 'undefined') {
'}'
);
}
*/
// TODO(wafuwafu13): Fix
// // Test Promise.
@ -1305,6 +1357,7 @@ if (typeof Symbol !== 'undefined') {
// Minimal inspection should still return as much information as possible about
// the constructor and Symbol.toStringTag.
/* TODO(kt3k): Enable this
{
class Foo {
get [Symbol.toStringTag]() {
@ -1449,16 +1502,19 @@ if (typeof Symbol !== 'undefined') {
});
assert.strictEqual(util.inspect(x), '{ constructor: [Getter] }');
}
*/
{
const x = new function() {}; // eslint-disable-line new-parens
assert.strictEqual(util.inspect(x), '{}');
}
/* TODO(kt3k): Enable this
{
const x = Object.create(null);
assert.strictEqual(util.inspect(x), '[Object: null prototype] {}');
}
*/
// TODO(wafuwafu13): Fix
// {
@ -1483,6 +1539,7 @@ if (typeof Symbol !== 'undefined') {
// '[ ... 101 more items ]');
// }
/* TODO(kt3k): Enable this
{
const x = Array(101);
assert.strictEqual(util.inspect(x, { maxArrayLength: 0 }),
@ -1576,6 +1633,7 @@ if (typeof Symbol !== 'undefined') {
}
);
}
*/
util.inspect(process);
@ -1589,6 +1647,7 @@ util.inspect(process);
// );
// }
/* TODO(kt3k): Enable this
{
// @@toStringTag
const obj = { [Symbol.toStringTag]: 'a' };
@ -1763,6 +1822,7 @@ util.inspect(process);
expect = "{\n a: '12 45 78 01 34 67 90 23'\n}";
assert.strictEqual(out, expect);
}
*/
// TODO(wafuwafu13): Fix
// // Check compact indentation.
@ -1962,6 +2022,7 @@ util.inspect(process);
// assert(obj && arr);
// }
/* TODO(kt3k): Enable this
{ // Test argument objects.
const args = (function() { return arguments; })('a');
assert.strictEqual(util.inspect(args), "[Arguments] { '0': 'a' }");
@ -1986,12 +2047,15 @@ util.inspect(process);
// assert(longList.includes('[Object: Inspection interrupted ' +
// 'prematurely. Maximum call stack size exceeded.]'));
}
*/
// Do not escape single quotes if no double quote or backtick is present.
assert.strictEqual(util.inspect("'"), '"\'"');
assert.strictEqual(util.inspect('"\''), '`"\'`');
// eslint-disable-next-line no-template-curly-in-string
/* TODO(kt3k): Enable this
assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
*/
// TODO(wafuwafu13): Fix
// // Errors should visualize as much information as possible.
@ -2115,6 +2179,7 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
// });
// "class" properties should not be detected as "class".
/* TODO(kt3k): Enable this
{
// eslint-disable-next-line space-before-function-paren
let obj = { class () {} };
@ -2145,6 +2210,7 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
'[Function: class Foo {}]'
);
}
*/
// TODO(wafuwafu13): Fix
// // Verify that throwing in valueOf and toString still produces nice results.
@ -2278,6 +2344,7 @@ assert.strictEqual(inspect(Object(13n)), '[BigInt: 13n]');
// inspect(new BigUint64Array([0n])), 'BigUint64Array(1) [ 0n ]');
// Verify non-enumerable keys get escaped.
/* TODO(kt3k): Enable this
{
const obj = {};
Object.defineProperty(obj, 'Non\nenumerable\tkey', { value: true });
@ -2348,6 +2415,7 @@ assert.strictEqual(inspect(Object(13n)), '[BigInt: 13n]');
assert.strictEqual(inspect('foobar', { colors: true }), "'foobar'");
inspect.styles.string = stringStyle;
}
*/
assert.strictEqual(
inspect([1, 3, 2], { sorted: true }),
@ -2357,6 +2425,7 @@ assert.strictEqual(
inspect({ c: 3, a: 1, b: 2 }, { sorted: true }),
'{ a: 1, b: 2, c: 3 }'
);
/* TODO(kt3k): Enable this
assert.strictEqual(
inspect(
{ a200: 4, a100: 1, a102: 3, a101: 2 },
@ -2364,6 +2433,7 @@ assert.strictEqual(
),
'{ a200: 4, a102: 3, a101: 2, a100: 1 }'
);
*/
// TODO(wafuwafu13): Fix
// // Non-indices array properties are sorted as well.
@ -2465,8 +2535,8 @@ assert.strictEqual(
// );
// }
// TODO(wafuwafu13): Fix
// Check the getter option.
/* TODO(kt3k): Enable this
{
let foo = 1;
const get = { get foo() { return foo; } };
@ -2498,6 +2568,7 @@ assert.strictEqual(
// '{\n foo: [Getter/Setter] Set(3) { [ [Object], 2, {} ], ' +
// "'foobar', { x: 1 } },\n inc: [Getter: NaN]\n}");
}
*/
// Check compact number mode.
{
@ -2910,10 +2981,12 @@ assert.strictEqual(
const bar = new Bar();
/* TODO(kt3k): Enable this
assert.strictEqual(
inspect(bar),
'Bar(0) [Map] { prop: true, prop2: true, abc: true }'
);
*/
// TODO(wafuwafu13): Fix
// assert.strictEqual(
// inspect(bar, { showHidden: true, getters: true, colors: false }),
@ -2977,6 +3050,7 @@ assert.strictEqual(
// Check that prototypes with a null prototype are inspectable.
// Regression test for https://github.com/nodejs/node/issues/35730
/* TODO(kt3k): Enable this
{
function Func() {}
Func.prototype = null;
@ -2985,6 +3059,7 @@ assert.strictEqual(
assert.strictEqual(util.inspect(object), '{ constructor: [Function: Func] }');
}
*/
// Test changing util.inspect.colors colors and aliases.
{
@ -3021,6 +3096,7 @@ assert.strictEqual(
// }
// Truncate output for Primitives with 1 character left
/* TODO(kt3k): Enable this
{
assert.strictEqual(util.inspect('bl', { maxStringLength: 1 }),
"'b'... 1 more character");
@ -3035,6 +3111,7 @@ assert.strictEqual(
);
assert.match(util.inspect(x, { maxStringLength: null }), /a'$/);
}
*/
// TODO(wafuwafu13): Implement 'vm'
// {
@ -3120,6 +3197,7 @@ assert.strictEqual(
// assert(fullObjectGraph(global).has(Function.prototype));
// }
/* TODO(kt3k): Enable this
{
// Confirm that own constructor value displays correctly.
@ -3139,6 +3217,7 @@ assert.strictEqual(
'}'
);
}
*/
// TODO(wafuwafu13): Fix TypeError: main.hasOwnProperty is not a function
// {
@ -3192,9 +3271,11 @@ assert.strictEqual(
);
}
/* TODO(kt3k): Enable this
{
assert.strictEqual(
util.inspect({ ['__proto__']: { a: 1 } }),
"{ ['__proto__']: { a: 1 } }"
);
}
*/

View file

@ -12,7 +12,7 @@ import * as util from "node:util";
Deno.test({
name: "[util] format",
fn() {
assertEquals(util.format("%o", [10, 11]), "[ 10, 11, [length]: 2 ]");
assertEquals(util.format("%o", [10, 11]), "[ 10, 11 ]");
},
});

View file

@ -204,7 +204,7 @@ function isFullWidthCodePoint(code) {
);
}
function getStringWidth(str) {
export function getStringWidth(str) {
str = StringPrototypeNormalize(colors.stripColor(str), "NFC");
let width = 0;
@ -1334,6 +1334,16 @@ function inspectObject(value, inspectOptions, proxyDetails) {
) {
return String(value[customInspect](inspect, inspectOptions));
}
if (
ReflectHas(value, nodeCustomInspect) &&
typeof value[nodeCustomInspect] === "function"
) {
// TODO(kt3k): The last inspect needs to be util.inspect of Node.js.
// We need to move the implementation of util.inspect to this file.
return String(
value[nodeCustomInspect](inspectOptions.depth, inspectOptions, inspect),
);
}
// This non-unique symbol is used to support op_crates, ie.
// in extensions/web we don't want to depend on public
// Symbol.for("Deno.customInspect") symbol defined in the public API.
@ -2310,6 +2320,7 @@ class Console {
}
const customInspect = SymbolFor("Deno.customInspect");
const nodeCustomInspect = SymbolFor("nodejs.util.inspect.custom");
function inspect(
value,

View file

@ -19,6 +19,8 @@ import {
} from "ext:deno_node/internal/util.mjs";
import { inspect } from "ext:deno_node/util.ts";
const { ObjectAssign } = globalThis.__bootstrap.primordials;
const kIsEventTarget = Symbol.for("nodejs.event_target");
const kIsNodeEventTarget = Symbol("kIsNodeEventTarget");
@ -95,7 +97,7 @@ class Event extends WebEvent {
return name;
}
const opts = Object.assign({}, options, {
const opts = ObjectAssign({}, options, {
depth: NumberIsInteger(options.depth) ? options.depth - 1 : options.depth,
});

File diff suppressed because it is too large Load diff

View file

@ -3,7 +3,7 @@
NOTE: This file should not be manually edited. Please edit 'cli/tests/node_compat/config.json' and run 'tools/node_compat/setup.ts' instead.
Total: 2924
Total: 2927
- [abort/test-abort-backtrace.js](https://github.com/nodejs/node/tree/v18.12.1/test/abort/test-abort-backtrace.js)
- [abort/test-abort-fatal-error.js](https://github.com/nodejs/node/tree/v18.12.1/test/abort/test-abort-fatal-error.js)
@ -449,6 +449,7 @@ Total: 2924
- [parallel/test-console-not-call-toString.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-console-not-call-toString.js)
- [parallel/test-console-self-assign.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-console-self-assign.js)
- [parallel/test-console-stdio-setters.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-console-stdio-setters.js)
- [parallel/test-console-tty-colors.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-console-tty-colors.js)
- [parallel/test-console.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-console.js)
- [parallel/test-constants.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-constants.js)
- [parallel/test-corepack-version.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-corepack-version.js)
@ -1888,6 +1889,7 @@ Total: 2924
- [parallel/test-readline-csi.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-readline-csi.js)
- [parallel/test-readline-input-onerror.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-readline-input-onerror.js)
- [parallel/test-readline-interface.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-readline-interface.js)
- [parallel/test-readline-position.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-readline-position.js)
- [parallel/test-readline-promises-interface.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-readline-promises-interface.js)
- [parallel/test-readline-promises-tab-complete.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-readline-promises-tab-complete.js)
- [parallel/test-readline-tab-complete.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-readline-tab-complete.js)
@ -2098,6 +2100,7 @@ Total: 2924
- [parallel/test-stream-writable-samecb-singletick.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-stream-writable-samecb-singletick.js)
- [parallel/test-stream2-finish-pipe-error.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-stream2-finish-pipe-error.js)
- [parallel/test-stream2-httpclient-response-end.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-stream2-httpclient-response-end.js)
- [parallel/test-stream2-readable-from-list.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-stream2-readable-from-list.js)
- [parallel/test-string-decoder-end.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-string-decoder-end.js)
- [parallel/test-string-decoder-fuzz.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-string-decoder-fuzz.js)
- [parallel/test-string-decoder.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-string-decoder.js)