11cb0ef41Sopenharmony_ci// Flags: --expose-internals 21cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 31cb0ef41Sopenharmony_ci// 41cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 51cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 61cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 71cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 81cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 91cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 101cb0ef41Sopenharmony_ci// following conditions: 111cb0ef41Sopenharmony_ci// 121cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 131cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 141cb0ef41Sopenharmony_ci// 151cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 161cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 171cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 181cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 191cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 201cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 211cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ciconst assert = require('assert'); 251cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding'); 261cb0ef41Sopenharmony_ciconst JSStream = internalBinding('js_stream').JSStream; 271cb0ef41Sopenharmony_ciconst util = require('util'); 281cb0ef41Sopenharmony_ciconst vm = require('vm'); 291cb0ef41Sopenharmony_ciconst v8 = require('v8'); 301cb0ef41Sopenharmony_ciconst { previewEntries } = internalBinding('util'); 311cb0ef41Sopenharmony_ciconst { inspect } = util; 321cb0ef41Sopenharmony_ciconst { MessageChannel } = require('worker_threads'); 331cb0ef41Sopenharmony_ciconst url = require('url'); 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(1), '1'); 361cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(false), 'false'); 371cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(''), "''"); 381cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect('hello'), "'hello'"); 391cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(function abc() {}), '[Function: abc]'); 401cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(() => {}), '[Function (anonymous)]'); 411cb0ef41Sopenharmony_ciassert.strictEqual( 421cb0ef41Sopenharmony_ci util.inspect(async function() {}), 431cb0ef41Sopenharmony_ci '[AsyncFunction (anonymous)]' 441cb0ef41Sopenharmony_ci); 451cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(async () => {}), '[AsyncFunction (anonymous)]'); 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci// Special function inspection. 481cb0ef41Sopenharmony_ci{ 491cb0ef41Sopenharmony_ci const fn = (() => function*() {})(); 501cb0ef41Sopenharmony_ci assert.strictEqual( 511cb0ef41Sopenharmony_ci util.inspect(fn), 521cb0ef41Sopenharmony_ci '[GeneratorFunction (anonymous)]' 531cb0ef41Sopenharmony_ci ); 541cb0ef41Sopenharmony_ci assert.strictEqual( 551cb0ef41Sopenharmony_ci util.inspect(async function* abc() {}), 561cb0ef41Sopenharmony_ci '[AsyncGeneratorFunction: abc]' 571cb0ef41Sopenharmony_ci ); 581cb0ef41Sopenharmony_ci Object.setPrototypeOf(fn, Object.getPrototypeOf(async () => {})); 591cb0ef41Sopenharmony_ci assert.strictEqual( 601cb0ef41Sopenharmony_ci util.inspect(fn), 611cb0ef41Sopenharmony_ci '[GeneratorFunction (anonymous)] AsyncFunction' 621cb0ef41Sopenharmony_ci ); 631cb0ef41Sopenharmony_ci Object.defineProperty(fn, 'name', { value: 5, configurable: true }); 641cb0ef41Sopenharmony_ci assert.strictEqual( 651cb0ef41Sopenharmony_ci util.inspect(fn), 661cb0ef41Sopenharmony_ci '[GeneratorFunction: 5] AsyncFunction' 671cb0ef41Sopenharmony_ci ); 681cb0ef41Sopenharmony_ci Object.defineProperty(fn, Symbol.toStringTag, { 691cb0ef41Sopenharmony_ci value: 'Foobar', 701cb0ef41Sopenharmony_ci configurable: true 711cb0ef41Sopenharmony_ci }); 721cb0ef41Sopenharmony_ci assert.strictEqual( 731cb0ef41Sopenharmony_ci util.inspect({ ['5']: fn }), 741cb0ef41Sopenharmony_ci "{ '5': [GeneratorFunction: 5] AsyncFunction [Foobar] }" 751cb0ef41Sopenharmony_ci ); 761cb0ef41Sopenharmony_ci Object.defineProperty(fn, 'name', { value: '5', configurable: true }); 771cb0ef41Sopenharmony_ci Object.setPrototypeOf(fn, null); 781cb0ef41Sopenharmony_ci assert.strictEqual( 791cb0ef41Sopenharmony_ci util.inspect(fn), 801cb0ef41Sopenharmony_ci '[GeneratorFunction (null prototype): 5] [Foobar]' 811cb0ef41Sopenharmony_ci ); 821cb0ef41Sopenharmony_ci assert.strictEqual( 831cb0ef41Sopenharmony_ci util.inspect({ ['5']: fn }), 841cb0ef41Sopenharmony_ci "{ '5': [GeneratorFunction (null prototype): 5] [Foobar] }" 851cb0ef41Sopenharmony_ci ); 861cb0ef41Sopenharmony_ci} 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(undefined), 'undefined'); 891cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(null), 'null'); 901cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(/foo(bar\n)?/gi), '/foo(bar\\n)?/gi'); 911cb0ef41Sopenharmony_ciassert.strictEqual( 921cb0ef41Sopenharmony_ci util.inspect(new Date('Sun, 14 Feb 2010 11:48:40 GMT')), 931cb0ef41Sopenharmony_ci new Date('2010-02-14T12:48:40+01:00').toISOString() 941cb0ef41Sopenharmony_ci); 951cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(new Date('')), (new Date('')).toString()); 961cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect('\n\x01'), "'\\n\\x01'"); 971cb0ef41Sopenharmony_ciassert.strictEqual( 981cb0ef41Sopenharmony_ci util.inspect(`${Array(75).fill(1)}'\n\x1d\n\x03\x85\x7f\x7e\x9f\xa0`), 991cb0ef41Sopenharmony_ci // eslint-disable-next-line no-irregular-whitespace 1001cb0ef41Sopenharmony_ci `"${Array(75).fill(1)}'\\n" +\n '\\x1D\\n' +\n '\\x03\\x85\\x7F~\\x9F '` 1011cb0ef41Sopenharmony_ci); 1021cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect([]), '[]'); 1031cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(Object.create([])), 'Array {}'); 1041cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect([1, 2]), '[ 1, 2 ]'); 1051cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect([1, [2, 3]]), '[ 1, [ 2, 3 ] ]'); 1061cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect({}), '{}'); 1071cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect({ a: 1 }), '{ a: 1 }'); 1081cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect({ a: function() {} }), '{ a: [Function: a] }'); 1091cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect({ a: () => {} }), '{ a: [Function: a] }'); 1101cb0ef41Sopenharmony_ci// eslint-disable-next-line func-name-matching 1111cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect({ a: async function abc() {} }), 1121cb0ef41Sopenharmony_ci '{ a: [AsyncFunction: abc] }'); 1131cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect({ a: async () => {} }), 1141cb0ef41Sopenharmony_ci '{ a: [AsyncFunction: a] }'); 1151cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect({ a: function*() {} }), 1161cb0ef41Sopenharmony_ci '{ a: [GeneratorFunction: a] }'); 1171cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect({ a: 1, b: 2 }), '{ a: 1, b: 2 }'); 1181cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect({ 'a': {} }), '{ a: {} }'); 1191cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect({ 'a': { 'b': 2 } }), '{ a: { b: 2 } }'); 1201cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect({ 'a': { 'b': { 'c': { 'd': 2 } } } }), 1211cb0ef41Sopenharmony_ci '{ a: { b: { c: [Object] } } }'); 1221cb0ef41Sopenharmony_ciassert.strictEqual( 1231cb0ef41Sopenharmony_ci util.inspect({ 'a': { 'b': { 'c': { 'd': 2 } } } }, false, null), 1241cb0ef41Sopenharmony_ci '{\n a: { b: { c: { d: 2 } } }\n}'); 1251cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect([1, 2, 3], true), '[ 1, 2, 3, [length]: 3 ]'); 1261cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect({ 'a': { 'b': { 'c': 2 } } }, false, 0), 1271cb0ef41Sopenharmony_ci '{ a: [Object] }'); 1281cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect({ 'a': { 'b': { 'c': 2 } } }, false, 1), 1291cb0ef41Sopenharmony_ci '{ a: { b: [Object] } }'); 1301cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect({ 'a': { 'b': ['c'] } }, false, 1), 1311cb0ef41Sopenharmony_ci '{ a: { b: [Array] } }'); 1321cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(new Uint8Array(0)), 'Uint8Array(0) []'); 1331cb0ef41Sopenharmony_ciassert(inspect(new Uint8Array(0), { showHidden: true }).includes('[buffer]')); 1341cb0ef41Sopenharmony_ciassert.strictEqual( 1351cb0ef41Sopenharmony_ci util.inspect( 1361cb0ef41Sopenharmony_ci Object.create( 1371cb0ef41Sopenharmony_ci {}, 1381cb0ef41Sopenharmony_ci { visible: { value: 1, enumerable: true }, hidden: { value: 2 } } 1391cb0ef41Sopenharmony_ci ) 1401cb0ef41Sopenharmony_ci ), 1411cb0ef41Sopenharmony_ci '{ visible: 1 }' 1421cb0ef41Sopenharmony_ci); 1431cb0ef41Sopenharmony_ciassert.strictEqual( 1441cb0ef41Sopenharmony_ci util.inspect( 1451cb0ef41Sopenharmony_ci Object.assign(new String('hello'), { [Symbol('foo')]: 123 }), 1461cb0ef41Sopenharmony_ci { showHidden: true } 1471cb0ef41Sopenharmony_ci ), 1481cb0ef41Sopenharmony_ci "[String: 'hello'] { [length]: 5, [Symbol(foo)]: 123 }" 1491cb0ef41Sopenharmony_ci); 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ciassert.match(util.inspect((new JSStream())._externalStream), 1521cb0ef41Sopenharmony_ci /^\[External: [0-9a-f]+\]$/); 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci{ 1551cb0ef41Sopenharmony_ci const regexp = /regexp/; 1561cb0ef41Sopenharmony_ci regexp.aprop = 42; 1571cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect({ a: regexp }, false, 0), '{ a: /regexp/ }'); 1581cb0ef41Sopenharmony_ci} 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ciassert.match( 1611cb0ef41Sopenharmony_ci util.inspect({ a: { a: { a: { a: {} } } } }, undefined, undefined, true), 1621cb0ef41Sopenharmony_ci /Object/ 1631cb0ef41Sopenharmony_ci); 1641cb0ef41Sopenharmony_ciassert.doesNotMatch( 1651cb0ef41Sopenharmony_ci util.inspect({ a: { a: { a: { a: {} } } } }, undefined, null, true), 1661cb0ef41Sopenharmony_ci /Object/ 1671cb0ef41Sopenharmony_ci); 1681cb0ef41Sopenharmony_ci 1691cb0ef41Sopenharmony_ci{ 1701cb0ef41Sopenharmony_ci const showHidden = true; 1711cb0ef41Sopenharmony_ci const ab = new Uint8Array([1, 2, 3, 4]).buffer; 1721cb0ef41Sopenharmony_ci const dv = new DataView(ab, 1, 2); 1731cb0ef41Sopenharmony_ci assert.strictEqual( 1741cb0ef41Sopenharmony_ci util.inspect(ab, showHidden), 1751cb0ef41Sopenharmony_ci 'ArrayBuffer { [Uint8Contents]: <01 02 03 04>, byteLength: 4 }' 1761cb0ef41Sopenharmony_ci ); 1771cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(new DataView(ab, 1, 2), showHidden), 1781cb0ef41Sopenharmony_ci 'DataView {\n' + 1791cb0ef41Sopenharmony_ci ' byteLength: 2,\n' + 1801cb0ef41Sopenharmony_ci ' byteOffset: 1,\n' + 1811cb0ef41Sopenharmony_ci ' buffer: ArrayBuffer {' + 1821cb0ef41Sopenharmony_ci ' [Uint8Contents]: <01 02 03 04>, byteLength: 4 }\n}'); 1831cb0ef41Sopenharmony_ci assert.strictEqual( 1841cb0ef41Sopenharmony_ci util.inspect(ab, showHidden), 1851cb0ef41Sopenharmony_ci 'ArrayBuffer { [Uint8Contents]: <01 02 03 04>, byteLength: 4 }' 1861cb0ef41Sopenharmony_ci ); 1871cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(dv, showHidden), 1881cb0ef41Sopenharmony_ci 'DataView {\n' + 1891cb0ef41Sopenharmony_ci ' byteLength: 2,\n' + 1901cb0ef41Sopenharmony_ci ' byteOffset: 1,\n' + 1911cb0ef41Sopenharmony_ci ' buffer: ArrayBuffer { [Uint8Contents]: ' + 1921cb0ef41Sopenharmony_ci '<01 02 03 04>, byteLength: 4 }\n}'); 1931cb0ef41Sopenharmony_ci ab.x = 42; 1941cb0ef41Sopenharmony_ci dv.y = 1337; 1951cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(ab, showHidden), 1961cb0ef41Sopenharmony_ci 'ArrayBuffer { [Uint8Contents]: <01 02 03 04>, ' + 1971cb0ef41Sopenharmony_ci 'byteLength: 4, x: 42 }'); 1981cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(dv, showHidden), 1991cb0ef41Sopenharmony_ci 'DataView {\n' + 2001cb0ef41Sopenharmony_ci ' byteLength: 2,\n' + 2011cb0ef41Sopenharmony_ci ' byteOffset: 1,\n' + 2021cb0ef41Sopenharmony_ci ' buffer: ArrayBuffer { [Uint8Contents]: <01 02 03 04>,' + 2031cb0ef41Sopenharmony_ci ' byteLength: 4, x: 42 },\n' + 2041cb0ef41Sopenharmony_ci ' y: 1337\n}'); 2051cb0ef41Sopenharmony_ci} 2061cb0ef41Sopenharmony_ci 2071cb0ef41Sopenharmony_ci{ 2081cb0ef41Sopenharmony_ci const ab = new ArrayBuffer(42); 2091cb0ef41Sopenharmony_ci assert.strictEqual(ab.byteLength, 42); 2101cb0ef41Sopenharmony_ci new MessageChannel().port1.postMessage(ab, [ ab ]); 2111cb0ef41Sopenharmony_ci assert.strictEqual(ab.byteLength, 0); 2121cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(ab), 2131cb0ef41Sopenharmony_ci 'ArrayBuffer { (detached), byteLength: 0 }'); 2141cb0ef41Sopenharmony_ci} 2151cb0ef41Sopenharmony_ci 2161cb0ef41Sopenharmony_ci// Truncate output for ArrayBuffers using plural or singular bytes 2171cb0ef41Sopenharmony_ci{ 2181cb0ef41Sopenharmony_ci const ab = new ArrayBuffer(3); 2191cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(ab, { showHidden: true, maxArrayLength: 2 }), 2201cb0ef41Sopenharmony_ci 'ArrayBuffer { [Uint8Contents]' + 2211cb0ef41Sopenharmony_ci ': <00 00 ... 1 more byte>, byteLength: 3 }'); 2221cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(ab, { showHidden: true, maxArrayLength: 1 }), 2231cb0ef41Sopenharmony_ci 'ArrayBuffer { [Uint8Contents]' + 2241cb0ef41Sopenharmony_ci ': <00 ... 2 more bytes>, byteLength: 3 }'); 2251cb0ef41Sopenharmony_ci} 2261cb0ef41Sopenharmony_ci 2271cb0ef41Sopenharmony_ci// Now do the same checks but from a different context. 2281cb0ef41Sopenharmony_ci{ 2291cb0ef41Sopenharmony_ci const showHidden = false; 2301cb0ef41Sopenharmony_ci const ab = vm.runInNewContext('new ArrayBuffer(4)'); 2311cb0ef41Sopenharmony_ci const dv = vm.runInNewContext('new DataView(ab, 1, 2)', { ab }); 2321cb0ef41Sopenharmony_ci assert.strictEqual( 2331cb0ef41Sopenharmony_ci util.inspect(ab, showHidden), 2341cb0ef41Sopenharmony_ci 'ArrayBuffer { [Uint8Contents]: <00 00 00 00>, byteLength: 4 }' 2351cb0ef41Sopenharmony_ci ); 2361cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(new DataView(ab, 1, 2), showHidden), 2371cb0ef41Sopenharmony_ci 'DataView {\n' + 2381cb0ef41Sopenharmony_ci ' byteLength: 2,\n' + 2391cb0ef41Sopenharmony_ci ' byteOffset: 1,\n' + 2401cb0ef41Sopenharmony_ci ' buffer: ArrayBuffer { [Uint8Contents]: <00 00 00 00>,' + 2411cb0ef41Sopenharmony_ci ' byteLength: 4 }\n}'); 2421cb0ef41Sopenharmony_ci assert.strictEqual( 2431cb0ef41Sopenharmony_ci util.inspect(ab, showHidden), 2441cb0ef41Sopenharmony_ci 'ArrayBuffer { [Uint8Contents]: <00 00 00 00>, byteLength: 4 }' 2451cb0ef41Sopenharmony_ci ); 2461cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(dv, showHidden), 2471cb0ef41Sopenharmony_ci 'DataView {\n' + 2481cb0ef41Sopenharmony_ci ' byteLength: 2,\n' + 2491cb0ef41Sopenharmony_ci ' byteOffset: 1,\n' + 2501cb0ef41Sopenharmony_ci ' buffer: ArrayBuffer { [Uint8Contents]: <00 00 00 00>,' + 2511cb0ef41Sopenharmony_ci ' byteLength: 4 }\n}'); 2521cb0ef41Sopenharmony_ci ab.x = 42; 2531cb0ef41Sopenharmony_ci dv.y = 1337; 2541cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(ab, showHidden), 2551cb0ef41Sopenharmony_ci 'ArrayBuffer { [Uint8Contents]: <00 00 00 00>, ' + 2561cb0ef41Sopenharmony_ci 'byteLength: 4, x: 42 }'); 2571cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(dv, showHidden), 2581cb0ef41Sopenharmony_ci 'DataView {\n' + 2591cb0ef41Sopenharmony_ci ' byteLength: 2,\n' + 2601cb0ef41Sopenharmony_ci ' byteOffset: 1,\n' + 2611cb0ef41Sopenharmony_ci ' buffer: ArrayBuffer { [Uint8Contents]: <00 00 00 00>,' + 2621cb0ef41Sopenharmony_ci ' byteLength: 4, x: 42 },\n' + 2631cb0ef41Sopenharmony_ci ' y: 1337\n}'); 2641cb0ef41Sopenharmony_ci} 2651cb0ef41Sopenharmony_ci 2661cb0ef41Sopenharmony_ci[ Float32Array, 2671cb0ef41Sopenharmony_ci Float64Array, 2681cb0ef41Sopenharmony_ci Int16Array, 2691cb0ef41Sopenharmony_ci Int32Array, 2701cb0ef41Sopenharmony_ci Int8Array, 2711cb0ef41Sopenharmony_ci Uint16Array, 2721cb0ef41Sopenharmony_ci Uint32Array, 2731cb0ef41Sopenharmony_ci Uint8Array, 2741cb0ef41Sopenharmony_ci Uint8ClampedArray ].forEach((constructor) => { 2751cb0ef41Sopenharmony_ci const length = 2; 2761cb0ef41Sopenharmony_ci const byteLength = length * constructor.BYTES_PER_ELEMENT; 2771cb0ef41Sopenharmony_ci const array = new constructor(new ArrayBuffer(byteLength), 0, length); 2781cb0ef41Sopenharmony_ci array[0] = 65; 2791cb0ef41Sopenharmony_ci array[1] = 97; 2801cb0ef41Sopenharmony_ci assert.strictEqual( 2811cb0ef41Sopenharmony_ci util.inspect(array, { showHidden: true }), 2821cb0ef41Sopenharmony_ci `${constructor.name}(${length}) [\n` + 2831cb0ef41Sopenharmony_ci ' 65,\n' + 2841cb0ef41Sopenharmony_ci ' 97,\n' + 2851cb0ef41Sopenharmony_ci ` [BYTES_PER_ELEMENT]: ${constructor.BYTES_PER_ELEMENT},\n` + 2861cb0ef41Sopenharmony_ci ` [length]: ${length},\n` + 2871cb0ef41Sopenharmony_ci ` [byteLength]: ${byteLength},\n` + 2881cb0ef41Sopenharmony_ci ' [byteOffset]: 0,\n' + 2891cb0ef41Sopenharmony_ci ` [buffer]: ArrayBuffer { byteLength: ${byteLength} }\n]`); 2901cb0ef41Sopenharmony_ci assert.strictEqual( 2911cb0ef41Sopenharmony_ci util.inspect(array, false), 2921cb0ef41Sopenharmony_ci `${constructor.name}(${length}) [ 65, 97 ]` 2931cb0ef41Sopenharmony_ci ); 2941cb0ef41Sopenharmony_ci}); 2951cb0ef41Sopenharmony_ci 2961cb0ef41Sopenharmony_ci// Now check that declaring a TypedArray in a different context works the same. 2971cb0ef41Sopenharmony_ci[ Float32Array, 2981cb0ef41Sopenharmony_ci Float64Array, 2991cb0ef41Sopenharmony_ci Int16Array, 3001cb0ef41Sopenharmony_ci Int32Array, 3011cb0ef41Sopenharmony_ci Int8Array, 3021cb0ef41Sopenharmony_ci Uint16Array, 3031cb0ef41Sopenharmony_ci Uint32Array, 3041cb0ef41Sopenharmony_ci Uint8Array, 3051cb0ef41Sopenharmony_ci Uint8ClampedArray ].forEach((constructor) => { 3061cb0ef41Sopenharmony_ci const length = 2; 3071cb0ef41Sopenharmony_ci const byteLength = length * constructor.BYTES_PER_ELEMENT; 3081cb0ef41Sopenharmony_ci const array = vm.runInNewContext( 3091cb0ef41Sopenharmony_ci 'new constructor(new ArrayBuffer(byteLength), 0, length)', 3101cb0ef41Sopenharmony_ci { constructor, byteLength, length } 3111cb0ef41Sopenharmony_ci ); 3121cb0ef41Sopenharmony_ci array[0] = 65; 3131cb0ef41Sopenharmony_ci array[1] = 97; 3141cb0ef41Sopenharmony_ci assert.strictEqual( 3151cb0ef41Sopenharmony_ci util.inspect(array, true), 3161cb0ef41Sopenharmony_ci `${constructor.name}(${length}) [\n` + 3171cb0ef41Sopenharmony_ci ' 65,\n' + 3181cb0ef41Sopenharmony_ci ' 97,\n' + 3191cb0ef41Sopenharmony_ci ` [BYTES_PER_ELEMENT]: ${constructor.BYTES_PER_ELEMENT},\n` + 3201cb0ef41Sopenharmony_ci ` [length]: ${length},\n` + 3211cb0ef41Sopenharmony_ci ` [byteLength]: ${byteLength},\n` + 3221cb0ef41Sopenharmony_ci ' [byteOffset]: 0,\n' + 3231cb0ef41Sopenharmony_ci ` [buffer]: ArrayBuffer { byteLength: ${byteLength} }\n]`); 3241cb0ef41Sopenharmony_ci assert.strictEqual( 3251cb0ef41Sopenharmony_ci util.inspect(array, false), 3261cb0ef41Sopenharmony_ci `${constructor.name}(${length}) [ 65, 97 ]` 3271cb0ef41Sopenharmony_ci ); 3281cb0ef41Sopenharmony_ci}); 3291cb0ef41Sopenharmony_ci 3301cb0ef41Sopenharmony_ci{ 3311cb0ef41Sopenharmony_ci const brokenLength = new Float32Array(2); 3321cb0ef41Sopenharmony_ci Object.defineProperty(brokenLength, 'length', { value: -1 }); 3331cb0ef41Sopenharmony_ci assert.strictEqual(inspect(brokenLength), 'Float32Array(2) [ 0n, 0n ]'); 3341cb0ef41Sopenharmony_ci} 3351cb0ef41Sopenharmony_ci 3361cb0ef41Sopenharmony_ciassert.strictEqual( 3371cb0ef41Sopenharmony_ci util.inspect(Object.create({}, { 3381cb0ef41Sopenharmony_ci visible: { value: 1, enumerable: true }, 3391cb0ef41Sopenharmony_ci hidden: { value: 2 } 3401cb0ef41Sopenharmony_ci }), { showHidden: true }), 3411cb0ef41Sopenharmony_ci '{ visible: 1, [hidden]: 2 }' 3421cb0ef41Sopenharmony_ci); 3431cb0ef41Sopenharmony_ci// Objects without prototype. 3441cb0ef41Sopenharmony_ciassert.strictEqual( 3451cb0ef41Sopenharmony_ci util.inspect(Object.create(null, { 3461cb0ef41Sopenharmony_ci name: { value: 'Tim', enumerable: true }, 3471cb0ef41Sopenharmony_ci hidden: { value: 'secret' } 3481cb0ef41Sopenharmony_ci }), { showHidden: true }), 3491cb0ef41Sopenharmony_ci "[Object: null prototype] { name: 'Tim', [hidden]: 'secret' }" 3501cb0ef41Sopenharmony_ci); 3511cb0ef41Sopenharmony_ci 3521cb0ef41Sopenharmony_ciassert.strictEqual( 3531cb0ef41Sopenharmony_ci util.inspect(Object.create(null, { 3541cb0ef41Sopenharmony_ci name: { value: 'Tim', enumerable: true }, 3551cb0ef41Sopenharmony_ci hidden: { value: 'secret' } 3561cb0ef41Sopenharmony_ci })), 3571cb0ef41Sopenharmony_ci "[Object: null prototype] { name: 'Tim' }" 3581cb0ef41Sopenharmony_ci); 3591cb0ef41Sopenharmony_ci 3601cb0ef41Sopenharmony_ci// Dynamic properties. 3611cb0ef41Sopenharmony_ci{ 3621cb0ef41Sopenharmony_ci assert.strictEqual( 3631cb0ef41Sopenharmony_ci util.inspect({ get readonly() { return 1; } }), 3641cb0ef41Sopenharmony_ci '{ readonly: [Getter] }'); 3651cb0ef41Sopenharmony_ci 3661cb0ef41Sopenharmony_ci assert.strictEqual( 3671cb0ef41Sopenharmony_ci util.inspect({ get readwrite() { return 1; }, set readwrite(val) {} }), 3681cb0ef41Sopenharmony_ci '{ readwrite: [Getter/Setter] }'); 3691cb0ef41Sopenharmony_ci 3701cb0ef41Sopenharmony_ci assert.strictEqual( 3711cb0ef41Sopenharmony_ci // eslint-disable-next-line accessor-pairs 3721cb0ef41Sopenharmony_ci util.inspect({ set writeonly(val) {} }), 3731cb0ef41Sopenharmony_ci '{ writeonly: [Setter] }'); 3741cb0ef41Sopenharmony_ci 3751cb0ef41Sopenharmony_ci const value = {}; 3761cb0ef41Sopenharmony_ci value.a = value; 3771cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(value), '<ref *1> { a: [Circular *1] }'); 3781cb0ef41Sopenharmony_ci const getterFn = { 3791cb0ef41Sopenharmony_ci get one() { 3801cb0ef41Sopenharmony_ci return null; 3811cb0ef41Sopenharmony_ci } 3821cb0ef41Sopenharmony_ci }; 3831cb0ef41Sopenharmony_ci assert.strictEqual( 3841cb0ef41Sopenharmony_ci util.inspect(getterFn, { getters: true }), 3851cb0ef41Sopenharmony_ci '{ one: [Getter: null] }' 3861cb0ef41Sopenharmony_ci ); 3871cb0ef41Sopenharmony_ci} 3881cb0ef41Sopenharmony_ci 3891cb0ef41Sopenharmony_ci// Array with dynamic properties. 3901cb0ef41Sopenharmony_ci{ 3911cb0ef41Sopenharmony_ci const value = [1, 2, 3]; 3921cb0ef41Sopenharmony_ci Object.defineProperty( 3931cb0ef41Sopenharmony_ci value, 3941cb0ef41Sopenharmony_ci 'growingLength', 3951cb0ef41Sopenharmony_ci { 3961cb0ef41Sopenharmony_ci enumerable: true, 3971cb0ef41Sopenharmony_ci get: function() { this.push(true); return this.length; } 3981cb0ef41Sopenharmony_ci } 3991cb0ef41Sopenharmony_ci ); 4001cb0ef41Sopenharmony_ci Object.defineProperty( 4011cb0ef41Sopenharmony_ci value, 4021cb0ef41Sopenharmony_ci '-1', 4031cb0ef41Sopenharmony_ci { 4041cb0ef41Sopenharmony_ci enumerable: true, 4051cb0ef41Sopenharmony_ci value: -1 4061cb0ef41Sopenharmony_ci } 4071cb0ef41Sopenharmony_ci ); 4081cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(value), 4091cb0ef41Sopenharmony_ci "[ 1, 2, 3, growingLength: [Getter], '-1': -1 ]"); 4101cb0ef41Sopenharmony_ci} 4111cb0ef41Sopenharmony_ci 4121cb0ef41Sopenharmony_ci// Array with inherited number properties. 4131cb0ef41Sopenharmony_ci{ 4141cb0ef41Sopenharmony_ci class CustomArray extends Array {} 4151cb0ef41Sopenharmony_ci CustomArray.prototype[5] = 'foo'; 4161cb0ef41Sopenharmony_ci CustomArray.prototype[49] = 'bar'; 4171cb0ef41Sopenharmony_ci CustomArray.prototype.foo = true; 4181cb0ef41Sopenharmony_ci const arr = new CustomArray(50); 4191cb0ef41Sopenharmony_ci arr[49] = 'I win'; 4201cb0ef41Sopenharmony_ci assert.strictEqual( 4211cb0ef41Sopenharmony_ci util.inspect(arr), 4221cb0ef41Sopenharmony_ci "CustomArray(50) [ <49 empty items>, 'I win' ]" 4231cb0ef41Sopenharmony_ci ); 4241cb0ef41Sopenharmony_ci assert.strictEqual( 4251cb0ef41Sopenharmony_ci util.inspect(arr, { showHidden: true }), 4261cb0ef41Sopenharmony_ci 'CustomArray(50) [\n' + 4271cb0ef41Sopenharmony_ci ' <49 empty items>,\n' + 4281cb0ef41Sopenharmony_ci " 'I win',\n" + 4291cb0ef41Sopenharmony_ci ' [length]: 50,\n' + 4301cb0ef41Sopenharmony_ci " '5': 'foo',\n" + 4311cb0ef41Sopenharmony_ci ' foo: true\n' + 4321cb0ef41Sopenharmony_ci ']' 4331cb0ef41Sopenharmony_ci ); 4341cb0ef41Sopenharmony_ci} 4351cb0ef41Sopenharmony_ci 4361cb0ef41Sopenharmony_ci// Array with extra properties. 4371cb0ef41Sopenharmony_ci{ 4381cb0ef41Sopenharmony_ci const arr = [1, 2, 3, , ]; // eslint-disable-line no-sparse-arrays 4391cb0ef41Sopenharmony_ci arr.foo = 'bar'; 4401cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr), 4411cb0ef41Sopenharmony_ci "[ 1, 2, 3, <1 empty item>, foo: 'bar' ]"); 4421cb0ef41Sopenharmony_ci 4431cb0ef41Sopenharmony_ci const arr2 = []; 4441cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect([], { showHidden: true }), '[ [length]: 0 ]'); 4451cb0ef41Sopenharmony_ci arr2['00'] = 1; 4461cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr2), "[ '00': 1 ]"); 4471cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr2, { showHidden: true }), 4481cb0ef41Sopenharmony_ci "[ [length]: 0, '00': 1 ]"); 4491cb0ef41Sopenharmony_ci arr2[1] = 0; 4501cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr2), "[ <1 empty item>, 0, '00': 1 ]"); 4511cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr2, { showHidden: true }), 4521cb0ef41Sopenharmony_ci "[ <1 empty item>, 0, [length]: 2, '00': 1 ]"); 4531cb0ef41Sopenharmony_ci delete arr2[1]; 4541cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr2), "[ <2 empty items>, '00': 1 ]"); 4551cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr2, { showHidden: true }), 4561cb0ef41Sopenharmony_ci "[ <2 empty items>, [length]: 2, '00': 1 ]"); 4571cb0ef41Sopenharmony_ci arr2['01'] = 2; 4581cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr2), 4591cb0ef41Sopenharmony_ci "[ <2 empty items>, '00': 1, '01': 2 ]"); 4601cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr2, { showHidden: true }), 4611cb0ef41Sopenharmony_ci "[ <2 empty items>, [length]: 2, '00': 1, '01': 2 ]"); 4621cb0ef41Sopenharmony_ci delete arr2['00']; 4631cb0ef41Sopenharmony_ci arr2[0] = 0; 4641cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr2), 4651cb0ef41Sopenharmony_ci "[ 0, <1 empty item>, '01': 2 ]"); 4661cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr2, { showHidden: true }), 4671cb0ef41Sopenharmony_ci "[ 0, <1 empty item>, [length]: 2, '01': 2 ]"); 4681cb0ef41Sopenharmony_ci delete arr2['01']; 4691cb0ef41Sopenharmony_ci arr2[2 ** 32 - 2] = 'max'; 4701cb0ef41Sopenharmony_ci arr2[2 ** 32 - 1] = 'too far'; 4711cb0ef41Sopenharmony_ci assert.strictEqual( 4721cb0ef41Sopenharmony_ci util.inspect(arr2), 4731cb0ef41Sopenharmony_ci "[ 0, <4294967293 empty items>, 'max', '4294967295': 'too far' ]" 4741cb0ef41Sopenharmony_ci ); 4751cb0ef41Sopenharmony_ci 4761cb0ef41Sopenharmony_ci const arr3 = []; 4771cb0ef41Sopenharmony_ci arr3[-1] = -1; 4781cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr3), "[ '-1': -1 ]"); 4791cb0ef41Sopenharmony_ci} 4801cb0ef41Sopenharmony_ci 4811cb0ef41Sopenharmony_ci// Indices out of bounds. 4821cb0ef41Sopenharmony_ci{ 4831cb0ef41Sopenharmony_ci const arr = []; 4841cb0ef41Sopenharmony_ci arr[2 ** 32] = true; // Not a valid array index. 4851cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr), "[ '4294967296': true ]"); 4861cb0ef41Sopenharmony_ci arr[0] = true; 4871cb0ef41Sopenharmony_ci arr[10] = true; 4881cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr), 4891cb0ef41Sopenharmony_ci "[ true, <9 empty items>, true, '4294967296': true ]"); 4901cb0ef41Sopenharmony_ci arr[2 ** 32 - 2] = true; 4911cb0ef41Sopenharmony_ci arr[2 ** 32 - 1] = true; 4921cb0ef41Sopenharmony_ci arr[2 ** 32 + 1] = true; 4931cb0ef41Sopenharmony_ci delete arr[0]; 4941cb0ef41Sopenharmony_ci delete arr[10]; 4951cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr), 4961cb0ef41Sopenharmony_ci ['[', 4971cb0ef41Sopenharmony_ci '<4294967294 empty items>,', 4981cb0ef41Sopenharmony_ci 'true,', 4991cb0ef41Sopenharmony_ci "'4294967296': true,", 5001cb0ef41Sopenharmony_ci "'4294967295': true,", 5011cb0ef41Sopenharmony_ci "'4294967297': true\n]", 5021cb0ef41Sopenharmony_ci ].join('\n ')); 5031cb0ef41Sopenharmony_ci} 5041cb0ef41Sopenharmony_ci 5051cb0ef41Sopenharmony_ci// Function with properties. 5061cb0ef41Sopenharmony_ci{ 5071cb0ef41Sopenharmony_ci const value = () => {}; 5081cb0ef41Sopenharmony_ci value.aprop = 42; 5091cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(value), '[Function: value] { aprop: 42 }'); 5101cb0ef41Sopenharmony_ci} 5111cb0ef41Sopenharmony_ci 5121cb0ef41Sopenharmony_ci// Anonymous function with properties. 5131cb0ef41Sopenharmony_ci{ 5141cb0ef41Sopenharmony_ci const value = (() => function() {})(); 5151cb0ef41Sopenharmony_ci value.aprop = 42; 5161cb0ef41Sopenharmony_ci assert.strictEqual( 5171cb0ef41Sopenharmony_ci util.inspect(value), 5181cb0ef41Sopenharmony_ci '[Function (anonymous)] { aprop: 42 }' 5191cb0ef41Sopenharmony_ci ); 5201cb0ef41Sopenharmony_ci} 5211cb0ef41Sopenharmony_ci 5221cb0ef41Sopenharmony_ci// Regular expressions with properties. 5231cb0ef41Sopenharmony_ci{ 5241cb0ef41Sopenharmony_ci const value = /123/ig; 5251cb0ef41Sopenharmony_ci value.aprop = 42; 5261cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(value), '/123/gi { aprop: 42 }'); 5271cb0ef41Sopenharmony_ci} 5281cb0ef41Sopenharmony_ci 5291cb0ef41Sopenharmony_ci// Dates with properties. 5301cb0ef41Sopenharmony_ci{ 5311cb0ef41Sopenharmony_ci const value = new Date('Sun, 14 Feb 2010 11:48:40 GMT'); 5321cb0ef41Sopenharmony_ci value.aprop = 42; 5331cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(value), 5341cb0ef41Sopenharmony_ci '2010-02-14T11:48:40.000Z { aprop: 42 }'); 5351cb0ef41Sopenharmony_ci} 5361cb0ef41Sopenharmony_ci 5371cb0ef41Sopenharmony_ci// Test the internal isDate implementation. 5381cb0ef41Sopenharmony_ci{ 5391cb0ef41Sopenharmony_ci const Date2 = vm.runInNewContext('Date'); 5401cb0ef41Sopenharmony_ci const d = new Date2(); 5411cb0ef41Sopenharmony_ci const orig = util.inspect(d); 5421cb0ef41Sopenharmony_ci Date2.prototype.foo = 'bar'; 5431cb0ef41Sopenharmony_ci const after = util.inspect(d); 5441cb0ef41Sopenharmony_ci assert.strictEqual(orig, after); 5451cb0ef41Sopenharmony_ci} 5461cb0ef41Sopenharmony_ci 5471cb0ef41Sopenharmony_ci// Test positive/negative zero. 5481cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(0), '0'); 5491cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(-0), '-0'); 5501cb0ef41Sopenharmony_ci// Edge case from check. 5511cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(-5e-324), '-5e-324'); 5521cb0ef41Sopenharmony_ci 5531cb0ef41Sopenharmony_ci// Test for sparse array. 5541cb0ef41Sopenharmony_ci{ 5551cb0ef41Sopenharmony_ci const a = ['foo', 'bar', 'baz']; 5561cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(a), "[ 'foo', 'bar', 'baz' ]"); 5571cb0ef41Sopenharmony_ci delete a[1]; 5581cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(a), "[ 'foo', <1 empty item>, 'baz' ]"); 5591cb0ef41Sopenharmony_ci assert.strictEqual( 5601cb0ef41Sopenharmony_ci util.inspect(a, true), 5611cb0ef41Sopenharmony_ci "[ 'foo', <1 empty item>, 'baz', [length]: 3 ]" 5621cb0ef41Sopenharmony_ci ); 5631cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(new Array(5)), '[ <5 empty items> ]'); 5641cb0ef41Sopenharmony_ci a[3] = 'bar'; 5651cb0ef41Sopenharmony_ci a[100] = 'qux'; 5661cb0ef41Sopenharmony_ci assert.strictEqual( 5671cb0ef41Sopenharmony_ci util.inspect(a, { breakLength: Infinity }), 5681cb0ef41Sopenharmony_ci "[ 'foo', <1 empty item>, 'baz', 'bar', <96 empty items>, 'qux' ]" 5691cb0ef41Sopenharmony_ci ); 5701cb0ef41Sopenharmony_ci delete a[3]; 5711cb0ef41Sopenharmony_ci assert.strictEqual( 5721cb0ef41Sopenharmony_ci util.inspect(a, { maxArrayLength: 4 }), 5731cb0ef41Sopenharmony_ci "[ 'foo', <1 empty item>, 'baz', <97 empty items>, ... 1 more item ]" 5741cb0ef41Sopenharmony_ci ); 5751cb0ef41Sopenharmony_ci // test 4 special case 5761cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(a, { 5771cb0ef41Sopenharmony_ci maxArrayLength: 2 5781cb0ef41Sopenharmony_ci }), "[ 'foo', <1 empty item>, ... 99 more items ]"); 5791cb0ef41Sopenharmony_ci} 5801cb0ef41Sopenharmony_ci 5811cb0ef41Sopenharmony_ci// Test for Array constructor in different context. 5821cb0ef41Sopenharmony_ci{ 5831cb0ef41Sopenharmony_ci const map = new Map(); 5841cb0ef41Sopenharmony_ci map.set(1, 2); 5851cb0ef41Sopenharmony_ci // Passing only a single argument to indicate a set iterator. 5861cb0ef41Sopenharmony_ci const valsSetIterator = previewEntries(map.entries()); 5871cb0ef41Sopenharmony_ci // Passing through true to indicate a map iterator. 5881cb0ef41Sopenharmony_ci const valsMapIterEntries = previewEntries(map.entries(), true); 5891cb0ef41Sopenharmony_ci const valsMapIterKeys = previewEntries(map.keys(), true); 5901cb0ef41Sopenharmony_ci 5911cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(valsSetIterator), '[ 1, 2 ]'); 5921cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(valsMapIterEntries), '[ [ 1, 2 ], true ]'); 5931cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(valsMapIterKeys), '[ [ 1 ], false ]'); 5941cb0ef41Sopenharmony_ci} 5951cb0ef41Sopenharmony_ci 5961cb0ef41Sopenharmony_ci// Test for other constructors in different context. 5971cb0ef41Sopenharmony_ci{ 5981cb0ef41Sopenharmony_ci let obj = vm.runInNewContext('(function(){return {}})()', {}); 5991cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(obj), '{}'); 6001cb0ef41Sopenharmony_ci obj = vm.runInNewContext('const m=new Map();m.set(1,2);m', {}); 6011cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(obj), 'Map(1) { 1 => 2 }'); 6021cb0ef41Sopenharmony_ci obj = vm.runInNewContext('const s=new Set();s.add(1);s.add(2);s', {}); 6031cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(obj), 'Set(2) { 1, 2 }'); 6041cb0ef41Sopenharmony_ci obj = vm.runInNewContext('fn=function(){};new Promise(fn,fn)', {}); 6051cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(obj), 'Promise { <pending> }'); 6061cb0ef41Sopenharmony_ci} 6071cb0ef41Sopenharmony_ci 6081cb0ef41Sopenharmony_ci// Test for property descriptors. 6091cb0ef41Sopenharmony_ci{ 6101cb0ef41Sopenharmony_ci const getter = Object.create(null, { 6111cb0ef41Sopenharmony_ci a: { 6121cb0ef41Sopenharmony_ci get: function() { return 'aaa'; } 6131cb0ef41Sopenharmony_ci } 6141cb0ef41Sopenharmony_ci }); 6151cb0ef41Sopenharmony_ci const setter = Object.create(null, { 6161cb0ef41Sopenharmony_ci b: { // eslint-disable-line accessor-pairs 6171cb0ef41Sopenharmony_ci set: function() {} 6181cb0ef41Sopenharmony_ci } 6191cb0ef41Sopenharmony_ci }); 6201cb0ef41Sopenharmony_ci const getterAndSetter = Object.create(null, { 6211cb0ef41Sopenharmony_ci c: { 6221cb0ef41Sopenharmony_ci get: function() { return 'ccc'; }, 6231cb0ef41Sopenharmony_ci set: function() {} 6241cb0ef41Sopenharmony_ci } 6251cb0ef41Sopenharmony_ci }); 6261cb0ef41Sopenharmony_ci assert.strictEqual( 6271cb0ef41Sopenharmony_ci util.inspect(getter, true), 6281cb0ef41Sopenharmony_ci '[Object: null prototype] { [a]: [Getter] }' 6291cb0ef41Sopenharmony_ci ); 6301cb0ef41Sopenharmony_ci assert.strictEqual( 6311cb0ef41Sopenharmony_ci util.inspect(setter, true), 6321cb0ef41Sopenharmony_ci '[Object: null prototype] { [b]: [Setter] }' 6331cb0ef41Sopenharmony_ci ); 6341cb0ef41Sopenharmony_ci assert.strictEqual( 6351cb0ef41Sopenharmony_ci util.inspect(getterAndSetter, true), 6361cb0ef41Sopenharmony_ci '[Object: null prototype] { [c]: [Getter/Setter] }' 6371cb0ef41Sopenharmony_ci ); 6381cb0ef41Sopenharmony_ci} 6391cb0ef41Sopenharmony_ci 6401cb0ef41Sopenharmony_ci// Exceptions should print the error message, not '{}'. 6411cb0ef41Sopenharmony_ci{ 6421cb0ef41Sopenharmony_ci [ 6431cb0ef41Sopenharmony_ci new Error(), 6441cb0ef41Sopenharmony_ci new Error('FAIL'), 6451cb0ef41Sopenharmony_ci new TypeError('FAIL'), 6461cb0ef41Sopenharmony_ci new SyntaxError('FAIL'), 6471cb0ef41Sopenharmony_ci ].forEach((err) => { 6481cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(err), err.stack); 6491cb0ef41Sopenharmony_ci }); 6501cb0ef41Sopenharmony_ci assert.throws( 6511cb0ef41Sopenharmony_ci () => undef(), // eslint-disable-line no-undef 6521cb0ef41Sopenharmony_ci (e) => { 6531cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(e), e.stack); 6541cb0ef41Sopenharmony_ci return true; 6551cb0ef41Sopenharmony_ci } 6561cb0ef41Sopenharmony_ci ); 6571cb0ef41Sopenharmony_ci 6581cb0ef41Sopenharmony_ci const ex = util.inspect(new Error('FAIL'), true); 6591cb0ef41Sopenharmony_ci assert(ex.includes('Error: FAIL')); 6601cb0ef41Sopenharmony_ci assert(ex.includes('[stack]')); 6611cb0ef41Sopenharmony_ci assert(ex.includes('[message]')); 6621cb0ef41Sopenharmony_ci} 6631cb0ef41Sopenharmony_ci 6641cb0ef41Sopenharmony_ci{ 6651cb0ef41Sopenharmony_ci const falsyCause1 = new Error('', { cause: false }); 6661cb0ef41Sopenharmony_ci delete falsyCause1.stack; 6671cb0ef41Sopenharmony_ci const falsyCause2 = new Error(undefined, { cause: null }); 6681cb0ef41Sopenharmony_ci falsyCause2.stack = ''; 6691cb0ef41Sopenharmony_ci const undefinedCause = new Error('', { cause: undefined }); 6701cb0ef41Sopenharmony_ci undefinedCause.stack = ''; 6711cb0ef41Sopenharmony_ci 6721cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(falsyCause1), '[Error] { [cause]: false }'); 6731cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(falsyCause2), '[Error] { [cause]: null }'); 6741cb0ef41Sopenharmony_ci assert.strictEqual( 6751cb0ef41Sopenharmony_ci util.inspect(undefinedCause), 6761cb0ef41Sopenharmony_ci '[Error] { [cause]: undefined }' 6771cb0ef41Sopenharmony_ci ); 6781cb0ef41Sopenharmony_ci} 6791cb0ef41Sopenharmony_ci 6801cb0ef41Sopenharmony_ci{ 6811cb0ef41Sopenharmony_ci const tmp = Error.stackTraceLimit; 6821cb0ef41Sopenharmony_ci Error.stackTraceLimit = 0; 6831cb0ef41Sopenharmony_ci const err = new Error('foo'); 6841cb0ef41Sopenharmony_ci const err2 = new Error('foo\nbar'); 6851cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(err, { compact: true }), '[Error: foo]'); 6861cb0ef41Sopenharmony_ci assert(err.stack); 6871cb0ef41Sopenharmony_ci delete err.stack; 6881cb0ef41Sopenharmony_ci assert(!err.stack); 6891cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(err, { compact: true }), '[Error: foo]'); 6901cb0ef41Sopenharmony_ci assert.strictEqual( 6911cb0ef41Sopenharmony_ci util.inspect(err2, { compact: true }), 6921cb0ef41Sopenharmony_ci '[Error: foo\nbar]' 6931cb0ef41Sopenharmony_ci ); 6941cb0ef41Sopenharmony_ci 6951cb0ef41Sopenharmony_ci err.bar = true; 6961cb0ef41Sopenharmony_ci err2.bar = true; 6971cb0ef41Sopenharmony_ci 6981cb0ef41Sopenharmony_ci assert.strictEqual( 6991cb0ef41Sopenharmony_ci util.inspect(err, { compact: true }), 7001cb0ef41Sopenharmony_ci '{ [Error: foo] bar: true }' 7011cb0ef41Sopenharmony_ci ); 7021cb0ef41Sopenharmony_ci assert.strictEqual( 7031cb0ef41Sopenharmony_ci util.inspect(err2, { compact: true }), 7041cb0ef41Sopenharmony_ci '{ [Error: foo\nbar]\n bar: true }' 7051cb0ef41Sopenharmony_ci ); 7061cb0ef41Sopenharmony_ci assert.strictEqual( 7071cb0ef41Sopenharmony_ci util.inspect(err, { compact: true, breakLength: 5 }), 7081cb0ef41Sopenharmony_ci '{ [Error: foo]\n bar: true }' 7091cb0ef41Sopenharmony_ci ); 7101cb0ef41Sopenharmony_ci assert.strictEqual( 7111cb0ef41Sopenharmony_ci util.inspect(err, { compact: true, breakLength: 1 }), 7121cb0ef41Sopenharmony_ci '{ [Error: foo]\n bar:\n true }' 7131cb0ef41Sopenharmony_ci ); 7141cb0ef41Sopenharmony_ci assert.strictEqual( 7151cb0ef41Sopenharmony_ci util.inspect(err2, { compact: true, breakLength: 5 }), 7161cb0ef41Sopenharmony_ci '{ [Error: foo\nbar]\n bar: true }' 7171cb0ef41Sopenharmony_ci ); 7181cb0ef41Sopenharmony_ci assert.strictEqual( 7191cb0ef41Sopenharmony_ci util.inspect(err, { compact: false }), 7201cb0ef41Sopenharmony_ci '[Error: foo] {\n bar: true\n}' 7211cb0ef41Sopenharmony_ci ); 7221cb0ef41Sopenharmony_ci assert.strictEqual( 7231cb0ef41Sopenharmony_ci util.inspect(err2, { compact: false }), 7241cb0ef41Sopenharmony_ci '[Error: foo\nbar] {\n bar: true\n}' 7251cb0ef41Sopenharmony_ci ); 7261cb0ef41Sopenharmony_ci 7271cb0ef41Sopenharmony_ci Error.stackTraceLimit = tmp; 7281cb0ef41Sopenharmony_ci} 7291cb0ef41Sopenharmony_ci 7301cb0ef41Sopenharmony_ci// Prevent enumerable error properties from being printed. 7311cb0ef41Sopenharmony_ci{ 7321cb0ef41Sopenharmony_ci let err = new Error(); 7331cb0ef41Sopenharmony_ci err.message = 'foobar'; 7341cb0ef41Sopenharmony_ci let out = util.inspect(err).split('\n'); 7351cb0ef41Sopenharmony_ci assert.strictEqual(out[0], 'Error: foobar'); 7361cb0ef41Sopenharmony_ci assert(out[out.length - 1].startsWith(' at ')); 7371cb0ef41Sopenharmony_ci // Reset the error, the stack is otherwise not recreated. 7381cb0ef41Sopenharmony_ci err = new Error(); 7391cb0ef41Sopenharmony_ci err.message = 'foobar'; 7401cb0ef41Sopenharmony_ci err.name = 'Unique'; 7411cb0ef41Sopenharmony_ci Object.defineProperty(err, 'stack', { value: err.stack, enumerable: true }); 7421cb0ef41Sopenharmony_ci out = util.inspect(err).split('\n'); 7431cb0ef41Sopenharmony_ci assert.strictEqual(out[0], 'Unique: foobar'); 7441cb0ef41Sopenharmony_ci assert(out[out.length - 1].startsWith(' at ')); 7451cb0ef41Sopenharmony_ci err.name = 'Baz'; 7461cb0ef41Sopenharmony_ci out = util.inspect(err).split('\n'); 7471cb0ef41Sopenharmony_ci assert.strictEqual(out[0], 'Unique: foobar'); 7481cb0ef41Sopenharmony_ci assert.strictEqual(out[out.length - 2], " name: 'Baz'"); 7491cb0ef41Sopenharmony_ci assert.strictEqual(out[out.length - 1], '}'); 7501cb0ef41Sopenharmony_ci} 7511cb0ef41Sopenharmony_ci 7521cb0ef41Sopenharmony_ci// Doesn't capture stack trace. 7531cb0ef41Sopenharmony_ci{ 7541cb0ef41Sopenharmony_ci function BadCustomError(msg) { 7551cb0ef41Sopenharmony_ci Error.call(this); 7561cb0ef41Sopenharmony_ci Object.defineProperty(this, 'message', 7571cb0ef41Sopenharmony_ci { value: msg, enumerable: false }); 7581cb0ef41Sopenharmony_ci Object.defineProperty(this, 'name', 7591cb0ef41Sopenharmony_ci { value: 'BadCustomError', enumerable: false }); 7601cb0ef41Sopenharmony_ci } 7611cb0ef41Sopenharmony_ci Object.setPrototypeOf(BadCustomError.prototype, Error.prototype); 7621cb0ef41Sopenharmony_ci Object.setPrototypeOf(BadCustomError, Error); 7631cb0ef41Sopenharmony_ci assert.strictEqual( 7641cb0ef41Sopenharmony_ci util.inspect(new BadCustomError('foo')), 7651cb0ef41Sopenharmony_ci '[BadCustomError: foo]' 7661cb0ef41Sopenharmony_ci ); 7671cb0ef41Sopenharmony_ci} 7681cb0ef41Sopenharmony_ci 7691cb0ef41Sopenharmony_ci// Tampered error stack or name property (different type than string). 7701cb0ef41Sopenharmony_ci// Note: Symbols are not supported by `Error#toString()` which is called by 7711cb0ef41Sopenharmony_ci// accessing the `stack` property. 7721cb0ef41Sopenharmony_ci[ 7731cb0ef41Sopenharmony_ci [404, '404: foo', '[404]'], 7741cb0ef41Sopenharmony_ci [0, '0: foo', '[RangeError: foo]'], 7751cb0ef41Sopenharmony_ci [0n, '0: foo', '[RangeError: foo]'], 7761cb0ef41Sopenharmony_ci [null, 'null: foo', '[RangeError: foo]'], 7771cb0ef41Sopenharmony_ci [undefined, 'RangeError: foo', '[RangeError: foo]'], 7781cb0ef41Sopenharmony_ci [false, 'false: foo', '[RangeError: foo]'], 7791cb0ef41Sopenharmony_ci ['', 'foo', '[RangeError: foo]'], 7801cb0ef41Sopenharmony_ci [[1, 2, 3], '1,2,3: foo', '[1,2,3]'], 7811cb0ef41Sopenharmony_ci].forEach(([value, outputStart, stack]) => { 7821cb0ef41Sopenharmony_ci let err = new RangeError('foo'); 7831cb0ef41Sopenharmony_ci err.name = value; 7841cb0ef41Sopenharmony_ci assert( 7851cb0ef41Sopenharmony_ci util.inspect(err).startsWith(outputStart), 7861cb0ef41Sopenharmony_ci util.format( 7871cb0ef41Sopenharmony_ci 'The name set to %o did not result in the expected output "%s"', 7881cb0ef41Sopenharmony_ci value, 7891cb0ef41Sopenharmony_ci outputStart 7901cb0ef41Sopenharmony_ci ) 7911cb0ef41Sopenharmony_ci ); 7921cb0ef41Sopenharmony_ci 7931cb0ef41Sopenharmony_ci err = new RangeError('foo'); 7941cb0ef41Sopenharmony_ci err.stack = value; 7951cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(err), stack); 7961cb0ef41Sopenharmony_ci}); 7971cb0ef41Sopenharmony_ci 7981cb0ef41Sopenharmony_ci// https://github.com/nodejs/node-v0.x-archive/issues/1941 7991cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(Object.create(Date.prototype)), 'Date {}'); 8001cb0ef41Sopenharmony_ci 8011cb0ef41Sopenharmony_ci// https://github.com/nodejs/node-v0.x-archive/issues/1944 8021cb0ef41Sopenharmony_ci{ 8031cb0ef41Sopenharmony_ci const d = new Date(); 8041cb0ef41Sopenharmony_ci d.toUTCString = null; 8051cb0ef41Sopenharmony_ci util.inspect(d); 8061cb0ef41Sopenharmony_ci} 8071cb0ef41Sopenharmony_ci 8081cb0ef41Sopenharmony_ci// Should not throw. 8091cb0ef41Sopenharmony_ci{ 8101cb0ef41Sopenharmony_ci const d = new Date(); 8111cb0ef41Sopenharmony_ci d.toISOString = null; 8121cb0ef41Sopenharmony_ci util.inspect(d); 8131cb0ef41Sopenharmony_ci} 8141cb0ef41Sopenharmony_ci 8151cb0ef41Sopenharmony_ci// Should not throw. 8161cb0ef41Sopenharmony_ci{ 8171cb0ef41Sopenharmony_ci const r = /regexp/; 8181cb0ef41Sopenharmony_ci r.toString = null; 8191cb0ef41Sopenharmony_ci util.inspect(r); 8201cb0ef41Sopenharmony_ci} 8211cb0ef41Sopenharmony_ci 8221cb0ef41Sopenharmony_ci// See https://github.com/nodejs/node-v0.x-archive/issues/2225 8231cb0ef41Sopenharmony_ci{ 8241cb0ef41Sopenharmony_ci const x = { [util.inspect.custom]: util.inspect }; 8251cb0ef41Sopenharmony_ci assert(util.inspect(x).includes( 8261cb0ef41Sopenharmony_ci '[Symbol(nodejs.util.inspect.custom)]: [Function: inspect] {\n')); 8271cb0ef41Sopenharmony_ci} 8281cb0ef41Sopenharmony_ci 8291cb0ef41Sopenharmony_ci// `util.inspect` should display the escaped value of a key. 8301cb0ef41Sopenharmony_ci{ 8311cb0ef41Sopenharmony_ci const w = { 8321cb0ef41Sopenharmony_ci '\\': 1, 8331cb0ef41Sopenharmony_ci '\\\\': 2, 8341cb0ef41Sopenharmony_ci '\\\\\\': 3, 8351cb0ef41Sopenharmony_ci '\\\\\\\\': 4, 8361cb0ef41Sopenharmony_ci '\n': 5, 8371cb0ef41Sopenharmony_ci '\r': 6 8381cb0ef41Sopenharmony_ci }; 8391cb0ef41Sopenharmony_ci 8401cb0ef41Sopenharmony_ci const y = ['a', 'b', 'c']; 8411cb0ef41Sopenharmony_ci y['\\\\'] = 'd'; 8421cb0ef41Sopenharmony_ci y['\n'] = 'e'; 8431cb0ef41Sopenharmony_ci y['\r'] = 'f'; 8441cb0ef41Sopenharmony_ci 8451cb0ef41Sopenharmony_ci assert.strictEqual( 8461cb0ef41Sopenharmony_ci util.inspect(w), 8471cb0ef41Sopenharmony_ci "{ '\\\\': 1, '\\\\\\\\': 2, '\\\\\\\\\\\\': 3, " + 8481cb0ef41Sopenharmony_ci "'\\\\\\\\\\\\\\\\': 4, '\\n': 5, '\\r': 6 }" 8491cb0ef41Sopenharmony_ci ); 8501cb0ef41Sopenharmony_ci assert.strictEqual( 8511cb0ef41Sopenharmony_ci util.inspect(y), 8521cb0ef41Sopenharmony_ci "[ 'a', 'b', 'c', '\\\\\\\\': 'd', " + 8531cb0ef41Sopenharmony_ci "'\\n': 'e', '\\r': 'f' ]" 8541cb0ef41Sopenharmony_ci ); 8551cb0ef41Sopenharmony_ci} 8561cb0ef41Sopenharmony_ci 8571cb0ef41Sopenharmony_ci// Escape unpaired surrogate pairs. 8581cb0ef41Sopenharmony_ci{ 8591cb0ef41Sopenharmony_ci const edgeChar = String.fromCharCode(0xd799); 8601cb0ef41Sopenharmony_ci 8611cb0ef41Sopenharmony_ci for (let charCode = 0xD800; charCode < 0xDFFF; charCode++) { 8621cb0ef41Sopenharmony_ci const surrogate = String.fromCharCode(charCode); 8631cb0ef41Sopenharmony_ci 8641cb0ef41Sopenharmony_ci assert.strictEqual( 8651cb0ef41Sopenharmony_ci util.inspect(surrogate), 8661cb0ef41Sopenharmony_ci `'\\u${charCode.toString(16)}'` 8671cb0ef41Sopenharmony_ci ); 8681cb0ef41Sopenharmony_ci assert.strictEqual( 8691cb0ef41Sopenharmony_ci util.inspect(`${'a'.repeat(200)}${surrogate}`), 8701cb0ef41Sopenharmony_ci `'${'a'.repeat(200)}\\u${charCode.toString(16)}'` 8711cb0ef41Sopenharmony_ci ); 8721cb0ef41Sopenharmony_ci assert.strictEqual( 8731cb0ef41Sopenharmony_ci util.inspect(`${surrogate}${'a'.repeat(200)}`), 8741cb0ef41Sopenharmony_ci `'\\u${charCode.toString(16)}${'a'.repeat(200)}'` 8751cb0ef41Sopenharmony_ci ); 8761cb0ef41Sopenharmony_ci if (charCode < 0xdc00) { 8771cb0ef41Sopenharmony_ci const highSurrogate = surrogate; 8781cb0ef41Sopenharmony_ci const lowSurrogate = String.fromCharCode(charCode + 1024); 8791cb0ef41Sopenharmony_ci assert( 8801cb0ef41Sopenharmony_ci !util.inspect( 8811cb0ef41Sopenharmony_ci `${edgeChar}${highSurrogate}${lowSurrogate}${edgeChar}` 8821cb0ef41Sopenharmony_ci ).includes('\\u') 8831cb0ef41Sopenharmony_ci ); 8841cb0ef41Sopenharmony_ci assert.strictEqual( 8851cb0ef41Sopenharmony_ci (util.inspect( 8861cb0ef41Sopenharmony_ci `${highSurrogate}${highSurrogate}${lowSurrogate}` 8871cb0ef41Sopenharmony_ci ).match(/\\u/g) ?? []).length, 8881cb0ef41Sopenharmony_ci 1 8891cb0ef41Sopenharmony_ci ); 8901cb0ef41Sopenharmony_ci } else { 8911cb0ef41Sopenharmony_ci assert.strictEqual( 8921cb0ef41Sopenharmony_ci util.inspect(`${edgeChar}${surrogate}${edgeChar}`), 8931cb0ef41Sopenharmony_ci `'${edgeChar}\\u${charCode.toString(16)}${edgeChar}'` 8941cb0ef41Sopenharmony_ci ); 8951cb0ef41Sopenharmony_ci } 8961cb0ef41Sopenharmony_ci } 8971cb0ef41Sopenharmony_ci} 8981cb0ef41Sopenharmony_ci 8991cb0ef41Sopenharmony_ci// Test util.inspect.styles and util.inspect.colors. 9001cb0ef41Sopenharmony_ci{ 9011cb0ef41Sopenharmony_ci function testColorStyle(style, input) { 9021cb0ef41Sopenharmony_ci const colorName = util.inspect.styles[style]; 9031cb0ef41Sopenharmony_ci let color = ['', '']; 9041cb0ef41Sopenharmony_ci if (util.inspect.colors[colorName]) 9051cb0ef41Sopenharmony_ci color = util.inspect.colors[colorName]; 9061cb0ef41Sopenharmony_ci 9071cb0ef41Sopenharmony_ci const withoutColor = util.inspect(input, false, 0, false); 9081cb0ef41Sopenharmony_ci const withColor = util.inspect(input, false, 0, true); 9091cb0ef41Sopenharmony_ci const expect = `\u001b[${color[0]}m${withoutColor}\u001b[${color[1]}m`; 9101cb0ef41Sopenharmony_ci assert.strictEqual( 9111cb0ef41Sopenharmony_ci withColor, 9121cb0ef41Sopenharmony_ci expect, 9131cb0ef41Sopenharmony_ci `util.inspect color for style ${style}`); 9141cb0ef41Sopenharmony_ci } 9151cb0ef41Sopenharmony_ci 9161cb0ef41Sopenharmony_ci testColorStyle('special', function() {}); 9171cb0ef41Sopenharmony_ci testColorStyle('number', 123.456); 9181cb0ef41Sopenharmony_ci testColorStyle('boolean', true); 9191cb0ef41Sopenharmony_ci testColorStyle('undefined', undefined); 9201cb0ef41Sopenharmony_ci testColorStyle('null', null); 9211cb0ef41Sopenharmony_ci testColorStyle('string', 'test string'); 9221cb0ef41Sopenharmony_ci testColorStyle('date', new Date()); 9231cb0ef41Sopenharmony_ci testColorStyle('regexp', /regexp/); 9241cb0ef41Sopenharmony_ci} 9251cb0ef41Sopenharmony_ci 9261cb0ef41Sopenharmony_ci// An object with "hasOwnProperty" overwritten should not throw. 9271cb0ef41Sopenharmony_ciutil.inspect({ hasOwnProperty: null }); 9281cb0ef41Sopenharmony_ci 9291cb0ef41Sopenharmony_ci// New API, accepts an "options" object. 9301cb0ef41Sopenharmony_ci{ 9311cb0ef41Sopenharmony_ci const subject = { foo: 'bar', hello: 31, a: { b: { c: { d: 0 } } } }; 9321cb0ef41Sopenharmony_ci Object.defineProperty(subject, 'hidden', { enumerable: false, value: null }); 9331cb0ef41Sopenharmony_ci 9341cb0ef41Sopenharmony_ci assert.strictEqual( 9351cb0ef41Sopenharmony_ci util.inspect(subject, { showHidden: false }).includes('hidden'), 9361cb0ef41Sopenharmony_ci false 9371cb0ef41Sopenharmony_ci ); 9381cb0ef41Sopenharmony_ci assert.strictEqual( 9391cb0ef41Sopenharmony_ci util.inspect(subject, { showHidden: true }).includes('hidden'), 9401cb0ef41Sopenharmony_ci true 9411cb0ef41Sopenharmony_ci ); 9421cb0ef41Sopenharmony_ci assert.strictEqual( 9431cb0ef41Sopenharmony_ci util.inspect(subject, { colors: false }).includes('\u001b[32m'), 9441cb0ef41Sopenharmony_ci false 9451cb0ef41Sopenharmony_ci ); 9461cb0ef41Sopenharmony_ci assert.strictEqual( 9471cb0ef41Sopenharmony_ci util.inspect(subject, { colors: true }).includes('\u001b[32m'), 9481cb0ef41Sopenharmony_ci true 9491cb0ef41Sopenharmony_ci ); 9501cb0ef41Sopenharmony_ci assert.strictEqual( 9511cb0ef41Sopenharmony_ci util.inspect(subject, { depth: 2 }).includes('c: [Object]'), 9521cb0ef41Sopenharmony_ci true 9531cb0ef41Sopenharmony_ci ); 9541cb0ef41Sopenharmony_ci assert.strictEqual( 9551cb0ef41Sopenharmony_ci util.inspect(subject, { depth: 0 }).includes('a: [Object]'), 9561cb0ef41Sopenharmony_ci true 9571cb0ef41Sopenharmony_ci ); 9581cb0ef41Sopenharmony_ci assert.strictEqual( 9591cb0ef41Sopenharmony_ci util.inspect(subject, { depth: null }).includes('{ d: 0 }'), 9601cb0ef41Sopenharmony_ci true 9611cb0ef41Sopenharmony_ci ); 9621cb0ef41Sopenharmony_ci assert.strictEqual( 9631cb0ef41Sopenharmony_ci util.inspect(subject, { depth: undefined }).includes('{ d: 0 }'), 9641cb0ef41Sopenharmony_ci true 9651cb0ef41Sopenharmony_ci ); 9661cb0ef41Sopenharmony_ci} 9671cb0ef41Sopenharmony_ci 9681cb0ef41Sopenharmony_ci{ 9691cb0ef41Sopenharmony_ci // "customInspect" option can enable/disable calling [util.inspect.custom](). 9701cb0ef41Sopenharmony_ci const subject = { [util.inspect.custom]: () => 123 }; 9711cb0ef41Sopenharmony_ci 9721cb0ef41Sopenharmony_ci assert.strictEqual( 9731cb0ef41Sopenharmony_ci util.inspect(subject, { customInspect: true }).includes('123'), 9741cb0ef41Sopenharmony_ci true 9751cb0ef41Sopenharmony_ci ); 9761cb0ef41Sopenharmony_ci assert.strictEqual( 9771cb0ef41Sopenharmony_ci util.inspect(subject, { customInspect: true }).includes('inspect'), 9781cb0ef41Sopenharmony_ci false 9791cb0ef41Sopenharmony_ci ); 9801cb0ef41Sopenharmony_ci assert.strictEqual( 9811cb0ef41Sopenharmony_ci util.inspect(subject, { customInspect: false }).includes('123'), 9821cb0ef41Sopenharmony_ci false 9831cb0ef41Sopenharmony_ci ); 9841cb0ef41Sopenharmony_ci assert.strictEqual( 9851cb0ef41Sopenharmony_ci util.inspect(subject, { customInspect: false }).includes('inspect'), 9861cb0ef41Sopenharmony_ci true 9871cb0ef41Sopenharmony_ci ); 9881cb0ef41Sopenharmony_ci 9891cb0ef41Sopenharmony_ci // A custom [util.inspect.custom]() should be able to return other Objects. 9901cb0ef41Sopenharmony_ci subject[util.inspect.custom] = () => ({ foo: 'bar' }); 9911cb0ef41Sopenharmony_ci 9921cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(subject), "{ foo: 'bar' }"); 9931cb0ef41Sopenharmony_ci 9941cb0ef41Sopenharmony_ci subject[util.inspect.custom] = common.mustCall((depth, opts, inspect) => { 9951cb0ef41Sopenharmony_ci const clone = { ...opts }; 9961cb0ef41Sopenharmony_ci // This might change at some point but for now we keep the stylize function. 9971cb0ef41Sopenharmony_ci // The function should either be documented or an alternative should be 9981cb0ef41Sopenharmony_ci // implemented. 9991cb0ef41Sopenharmony_ci assert.strictEqual(typeof opts.stylize, 'function'); 10001cb0ef41Sopenharmony_ci assert.strictEqual(opts.seen, undefined); 10011cb0ef41Sopenharmony_ci assert.strictEqual(opts.budget, undefined); 10021cb0ef41Sopenharmony_ci assert.strictEqual(opts.indentationLvl, undefined); 10031cb0ef41Sopenharmony_ci assert.strictEqual(opts.showHidden, false); 10041cb0ef41Sopenharmony_ci assert.strictEqual(inspect, util.inspect); 10051cb0ef41Sopenharmony_ci assert.deepStrictEqual( 10061cb0ef41Sopenharmony_ci new Set(Object.keys(inspect.defaultOptions).concat(['stylize'])), 10071cb0ef41Sopenharmony_ci new Set(Object.keys(opts)) 10081cb0ef41Sopenharmony_ci ); 10091cb0ef41Sopenharmony_ci opts.showHidden = true; 10101cb0ef41Sopenharmony_ci return { [inspect.custom]: common.mustCall((depth, opts2) => { 10111cb0ef41Sopenharmony_ci assert.deepStrictEqual(clone, opts2); 10121cb0ef41Sopenharmony_ci }) }; 10131cb0ef41Sopenharmony_ci }); 10141cb0ef41Sopenharmony_ci 10151cb0ef41Sopenharmony_ci util.inspect(subject); 10161cb0ef41Sopenharmony_ci 10171cb0ef41Sopenharmony_ci // util.inspect.custom is a shared symbol which can be accessed as 10181cb0ef41Sopenharmony_ci // Symbol.for("nodejs.util.inspect.custom"). 10191cb0ef41Sopenharmony_ci const inspect = Symbol.for('nodejs.util.inspect.custom'); 10201cb0ef41Sopenharmony_ci 10211cb0ef41Sopenharmony_ci subject[inspect] = () => ({ baz: 'quux' }); 10221cb0ef41Sopenharmony_ci 10231cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(subject), '{ baz: \'quux\' }'); 10241cb0ef41Sopenharmony_ci 10251cb0ef41Sopenharmony_ci subject[inspect] = (depth, opts) => { 10261cb0ef41Sopenharmony_ci assert.strictEqual(opts.customInspectOptions, true); 10271cb0ef41Sopenharmony_ci assert.strictEqual(opts.seen, null); 10281cb0ef41Sopenharmony_ci return {}; 10291cb0ef41Sopenharmony_ci }; 10301cb0ef41Sopenharmony_ci 10311cb0ef41Sopenharmony_ci util.inspect(subject, { customInspectOptions: true, seen: null }); 10321cb0ef41Sopenharmony_ci} 10331cb0ef41Sopenharmony_ci 10341cb0ef41Sopenharmony_ci{ 10351cb0ef41Sopenharmony_ci const subject = { [util.inspect.custom]: common.mustCall((depth, opts) => { 10361cb0ef41Sopenharmony_ci assert.strictEqual(depth, null); 10371cb0ef41Sopenharmony_ci assert.strictEqual(opts.compact, true); 10381cb0ef41Sopenharmony_ci }) }; 10391cb0ef41Sopenharmony_ci util.inspect(subject, { depth: null, compact: true }); 10401cb0ef41Sopenharmony_ci} 10411cb0ef41Sopenharmony_ci 10421cb0ef41Sopenharmony_ci{ 10431cb0ef41Sopenharmony_ci // Returning `this` from a custom inspection function works. 10441cb0ef41Sopenharmony_ci const subject = { a: 123, [util.inspect.custom]() { return this; } }; 10451cb0ef41Sopenharmony_ci const UIC = 'nodejs.util.inspect.custom'; 10461cb0ef41Sopenharmony_ci assert.strictEqual( 10471cb0ef41Sopenharmony_ci util.inspect(subject), 10481cb0ef41Sopenharmony_ci `{\n a: 123,\n [Symbol(${UIC})]: [Function: [${UIC}]]\n}` 10491cb0ef41Sopenharmony_ci ); 10501cb0ef41Sopenharmony_ci} 10511cb0ef41Sopenharmony_ci 10521cb0ef41Sopenharmony_ci// Verify that it's possible to use the stylize function to manipulate input. 10531cb0ef41Sopenharmony_ciassert.strictEqual( 10541cb0ef41Sopenharmony_ci util.inspect([1, 2, 3], { stylize() { return 'x'; } }), 10551cb0ef41Sopenharmony_ci '[ x, x, x ]' 10561cb0ef41Sopenharmony_ci); 10571cb0ef41Sopenharmony_ci 10581cb0ef41Sopenharmony_ci// Using `util.inspect` with "colors" option should produce as many lines as 10591cb0ef41Sopenharmony_ci// without it. 10601cb0ef41Sopenharmony_ci{ 10611cb0ef41Sopenharmony_ci function testLines(input) { 10621cb0ef41Sopenharmony_ci const countLines = (str) => (str.match(/\n/g) || []).length; 10631cb0ef41Sopenharmony_ci const withoutColor = util.inspect(input); 10641cb0ef41Sopenharmony_ci const withColor = util.inspect(input, { colors: true }); 10651cb0ef41Sopenharmony_ci assert.strictEqual(countLines(withoutColor), countLines(withColor)); 10661cb0ef41Sopenharmony_ci } 10671cb0ef41Sopenharmony_ci 10681cb0ef41Sopenharmony_ci const bigArray = new Array(100).fill().map((value, index) => index); 10691cb0ef41Sopenharmony_ci 10701cb0ef41Sopenharmony_ci testLines([1, 2, 3, 4, 5, 6, 7]); 10711cb0ef41Sopenharmony_ci testLines(bigArray); 10721cb0ef41Sopenharmony_ci testLines({ foo: 'bar', baz: 35, b: { a: 35 } }); 10731cb0ef41Sopenharmony_ci testLines({ a: { a: 3, b: 1, c: 1, d: 1, e: 1, f: 1, g: 1, h: 1 }, b: 1 }); 10741cb0ef41Sopenharmony_ci testLines({ 10751cb0ef41Sopenharmony_ci foo: 'bar', 10761cb0ef41Sopenharmony_ci baz: 35, 10771cb0ef41Sopenharmony_ci b: { a: 35 }, 10781cb0ef41Sopenharmony_ci veryLongKey: 'very long value', 10791cb0ef41Sopenharmony_ci evenLongerKey: ['with even longer value in array'] 10801cb0ef41Sopenharmony_ci }); 10811cb0ef41Sopenharmony_ci} 10821cb0ef41Sopenharmony_ci 10831cb0ef41Sopenharmony_ci// Test boxed primitives output the correct values. 10841cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(new String('test')), "[String: 'test']"); 10851cb0ef41Sopenharmony_ciassert.strictEqual( 10861cb0ef41Sopenharmony_ci util.inspect(new String('test'), { colors: true }), 10871cb0ef41Sopenharmony_ci "\u001b[32m[String: 'test']\u001b[39m" 10881cb0ef41Sopenharmony_ci); 10891cb0ef41Sopenharmony_ciassert.strictEqual( 10901cb0ef41Sopenharmony_ci util.inspect(Object(Symbol('test'))), 10911cb0ef41Sopenharmony_ci '[Symbol: Symbol(test)]' 10921cb0ef41Sopenharmony_ci); 10931cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(new Boolean(false)), '[Boolean: false]'); 10941cb0ef41Sopenharmony_ciassert.strictEqual( 10951cb0ef41Sopenharmony_ci util.inspect(Object.setPrototypeOf(new Boolean(true), null)), 10961cb0ef41Sopenharmony_ci '[Boolean (null prototype): true]' 10971cb0ef41Sopenharmony_ci); 10981cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(new Number(0)), '[Number: 0]'); 10991cb0ef41Sopenharmony_ciassert.strictEqual( 11001cb0ef41Sopenharmony_ci util.inspect( 11011cb0ef41Sopenharmony_ci Object.defineProperty( 11021cb0ef41Sopenharmony_ci Object.setPrototypeOf(new Number(-0), Array.prototype), 11031cb0ef41Sopenharmony_ci Symbol.toStringTag, 11041cb0ef41Sopenharmony_ci { value: 'Foobar' } 11051cb0ef41Sopenharmony_ci ) 11061cb0ef41Sopenharmony_ci ), 11071cb0ef41Sopenharmony_ci '[Number (Array): -0] [Foobar]' 11081cb0ef41Sopenharmony_ci); 11091cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(new Number(-1.1)), '[Number: -1.1]'); 11101cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect(new Number(13.37)), '[Number: 13.37]'); 11111cb0ef41Sopenharmony_ci 11121cb0ef41Sopenharmony_ci// Test boxed primitives with own properties. 11131cb0ef41Sopenharmony_ci{ 11141cb0ef41Sopenharmony_ci const str = new String('baz'); 11151cb0ef41Sopenharmony_ci str.foo = 'bar'; 11161cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(str), "[String: 'baz'] { foo: 'bar' }"); 11171cb0ef41Sopenharmony_ci 11181cb0ef41Sopenharmony_ci const bool = new Boolean(true); 11191cb0ef41Sopenharmony_ci bool.foo = 'bar'; 11201cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(bool), "[Boolean: true] { foo: 'bar' }"); 11211cb0ef41Sopenharmony_ci 11221cb0ef41Sopenharmony_ci const num = new Number(13.37); 11231cb0ef41Sopenharmony_ci num.foo = 'bar'; 11241cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(num), "[Number: 13.37] { foo: 'bar' }"); 11251cb0ef41Sopenharmony_ci 11261cb0ef41Sopenharmony_ci const sym = Object(Symbol('foo')); 11271cb0ef41Sopenharmony_ci sym.foo = 'bar'; 11281cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(sym), "[Symbol: Symbol(foo)] { foo: 'bar' }"); 11291cb0ef41Sopenharmony_ci 11301cb0ef41Sopenharmony_ci const big = Object(BigInt(55)); 11311cb0ef41Sopenharmony_ci big.foo = 'bar'; 11321cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(big), "[BigInt: 55n] { foo: 'bar' }"); 11331cb0ef41Sopenharmony_ci} 11341cb0ef41Sopenharmony_ci 11351cb0ef41Sopenharmony_ci// Test es6 Symbol. 11361cb0ef41Sopenharmony_ciif (typeof Symbol !== 'undefined') { 11371cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(Symbol()), 'Symbol()'); 11381cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(Symbol(123)), 'Symbol(123)'); 11391cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(Symbol('hi')), 'Symbol(hi)'); 11401cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect([Symbol()]), '[ Symbol() ]'); 11411cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect({ foo: Symbol() }), '{ foo: Symbol() }'); 11421cb0ef41Sopenharmony_ci 11431cb0ef41Sopenharmony_ci const options = { showHidden: true }; 11441cb0ef41Sopenharmony_ci let subject = {}; 11451cb0ef41Sopenharmony_ci 11461cb0ef41Sopenharmony_ci subject[Symbol('sym\nbol')] = 42; 11471cb0ef41Sopenharmony_ci 11481cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(subject), '{ [Symbol(sym\\nbol)]: 42 }'); 11491cb0ef41Sopenharmony_ci assert.strictEqual( 11501cb0ef41Sopenharmony_ci util.inspect(subject, options), 11511cb0ef41Sopenharmony_ci '{ [Symbol(sym\\nbol)]: 42 }' 11521cb0ef41Sopenharmony_ci ); 11531cb0ef41Sopenharmony_ci 11541cb0ef41Sopenharmony_ci Object.defineProperty( 11551cb0ef41Sopenharmony_ci subject, 11561cb0ef41Sopenharmony_ci Symbol(), 11571cb0ef41Sopenharmony_ci { enumerable: false, value: 'non-enum' }); 11581cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(subject), '{ [Symbol(sym\\nbol)]: 42 }'); 11591cb0ef41Sopenharmony_ci assert.strictEqual( 11601cb0ef41Sopenharmony_ci util.inspect(subject, options), 11611cb0ef41Sopenharmony_ci "{ [Symbol(sym\\nbol)]: 42, [Symbol()]: 'non-enum' }" 11621cb0ef41Sopenharmony_ci ); 11631cb0ef41Sopenharmony_ci 11641cb0ef41Sopenharmony_ci subject = [1, 2, 3]; 11651cb0ef41Sopenharmony_ci subject[Symbol('symbol')] = 42; 11661cb0ef41Sopenharmony_ci 11671cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(subject), 11681cb0ef41Sopenharmony_ci '[ 1, 2, 3, [Symbol(symbol)]: 42 ]'); 11691cb0ef41Sopenharmony_ci} 11701cb0ef41Sopenharmony_ci 11711cb0ef41Sopenharmony_ci// Test Set. 11721cb0ef41Sopenharmony_ci{ 11731cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(new Set()), 'Set(0) {}'); 11741cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(new Set([1, 2, 3])), 'Set(3) { 1, 2, 3 }'); 11751cb0ef41Sopenharmony_ci const set = new Set(['foo']); 11761cb0ef41Sopenharmony_ci set.bar = 42; 11771cb0ef41Sopenharmony_ci assert.strictEqual( 11781cb0ef41Sopenharmony_ci util.inspect(set, { showHidden: true }), 11791cb0ef41Sopenharmony_ci "Set(1) { 'foo', bar: 42 }" 11801cb0ef41Sopenharmony_ci ); 11811cb0ef41Sopenharmony_ci} 11821cb0ef41Sopenharmony_ci 11831cb0ef41Sopenharmony_ci// Test circular Set. 11841cb0ef41Sopenharmony_ci{ 11851cb0ef41Sopenharmony_ci const set = new Set(); 11861cb0ef41Sopenharmony_ci set.add(set); 11871cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(set), '<ref *1> Set(1) { [Circular *1] }'); 11881cb0ef41Sopenharmony_ci} 11891cb0ef41Sopenharmony_ci 11901cb0ef41Sopenharmony_ci// Test Map. 11911cb0ef41Sopenharmony_ci{ 11921cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(new Map()), 'Map(0) {}'); 11931cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(new Map([[1, 'a'], [2, 'b'], [3, 'c']])), 11941cb0ef41Sopenharmony_ci "Map(3) { 1 => 'a', 2 => 'b', 3 => 'c' }"); 11951cb0ef41Sopenharmony_ci const map = new Map([['foo', null]]); 11961cb0ef41Sopenharmony_ci map.bar = 42; 11971cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(map, true), 11981cb0ef41Sopenharmony_ci "Map(1) { 'foo' => null, bar: 42 }"); 11991cb0ef41Sopenharmony_ci} 12001cb0ef41Sopenharmony_ci 12011cb0ef41Sopenharmony_ci// Test circular Map. 12021cb0ef41Sopenharmony_ci{ 12031cb0ef41Sopenharmony_ci const map = new Map(); 12041cb0ef41Sopenharmony_ci map.set(map, 'map'); 12051cb0ef41Sopenharmony_ci assert.strictEqual( 12061cb0ef41Sopenharmony_ci inspect(map), 12071cb0ef41Sopenharmony_ci "<ref *1> Map(1) { [Circular *1] => 'map' }" 12081cb0ef41Sopenharmony_ci ); 12091cb0ef41Sopenharmony_ci map.set(map, map); 12101cb0ef41Sopenharmony_ci assert.strictEqual( 12111cb0ef41Sopenharmony_ci inspect(map), 12121cb0ef41Sopenharmony_ci '<ref *1> Map(1) { [Circular *1] => [Circular *1] }' 12131cb0ef41Sopenharmony_ci ); 12141cb0ef41Sopenharmony_ci map.delete(map); 12151cb0ef41Sopenharmony_ci map.set('map', map); 12161cb0ef41Sopenharmony_ci assert.strictEqual( 12171cb0ef41Sopenharmony_ci inspect(map), 12181cb0ef41Sopenharmony_ci "<ref *1> Map(1) { 'map' => [Circular *1] }" 12191cb0ef41Sopenharmony_ci ); 12201cb0ef41Sopenharmony_ci} 12211cb0ef41Sopenharmony_ci 12221cb0ef41Sopenharmony_ci// Test multiple circular references. 12231cb0ef41Sopenharmony_ci{ 12241cb0ef41Sopenharmony_ci const obj = {}; 12251cb0ef41Sopenharmony_ci obj.a = [obj]; 12261cb0ef41Sopenharmony_ci obj.b = {}; 12271cb0ef41Sopenharmony_ci obj.b.inner = obj.b; 12281cb0ef41Sopenharmony_ci obj.b.obj = obj; 12291cb0ef41Sopenharmony_ci 12301cb0ef41Sopenharmony_ci assert.strictEqual( 12311cb0ef41Sopenharmony_ci inspect(obj), 12321cb0ef41Sopenharmony_ci '<ref *1> {\n' + 12331cb0ef41Sopenharmony_ci ' a: [ [Circular *1] ],\n' + 12341cb0ef41Sopenharmony_ci ' b: <ref *2> { inner: [Circular *2], obj: [Circular *1] }\n' + 12351cb0ef41Sopenharmony_ci '}' 12361cb0ef41Sopenharmony_ci ); 12371cb0ef41Sopenharmony_ci} 12381cb0ef41Sopenharmony_ci 12391cb0ef41Sopenharmony_ci// Test Promise. 12401cb0ef41Sopenharmony_ci{ 12411cb0ef41Sopenharmony_ci const resolved = Promise.resolve(3); 12421cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(resolved), 'Promise { 3 }'); 12431cb0ef41Sopenharmony_ci 12441cb0ef41Sopenharmony_ci const rejected = Promise.reject(3); 12451cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(rejected), 'Promise { <rejected> 3 }'); 12461cb0ef41Sopenharmony_ci // Squelch UnhandledPromiseRejection. 12471cb0ef41Sopenharmony_ci rejected.catch(() => {}); 12481cb0ef41Sopenharmony_ci 12491cb0ef41Sopenharmony_ci const pending = new Promise(() => {}); 12501cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(pending), 'Promise { <pending> }'); 12511cb0ef41Sopenharmony_ci 12521cb0ef41Sopenharmony_ci const promiseWithProperty = Promise.resolve('foo'); 12531cb0ef41Sopenharmony_ci promiseWithProperty.bar = 42; 12541cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(promiseWithProperty), 12551cb0ef41Sopenharmony_ci "Promise { 'foo', bar: 42 }"); 12561cb0ef41Sopenharmony_ci} 12571cb0ef41Sopenharmony_ci 12581cb0ef41Sopenharmony_ci// Make sure it doesn't choke on polyfills. Unlike Set/Map, there is no standard 12591cb0ef41Sopenharmony_ci// interface to synchronously inspect a Promise, so our techniques only work on 12601cb0ef41Sopenharmony_ci// a bonafide native Promise. 12611cb0ef41Sopenharmony_ci{ 12621cb0ef41Sopenharmony_ci const oldPromise = Promise; 12631cb0ef41Sopenharmony_ci global.Promise = function() { this.bar = 42; }; 12641cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(new Promise()), '{ bar: 42 }'); 12651cb0ef41Sopenharmony_ci global.Promise = oldPromise; 12661cb0ef41Sopenharmony_ci} 12671cb0ef41Sopenharmony_ci 12681cb0ef41Sopenharmony_ci// Test Map iterators. 12691cb0ef41Sopenharmony_ci{ 12701cb0ef41Sopenharmony_ci const map = new Map([['foo', 'bar']]); 12711cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(map.keys()), '[Map Iterator] { \'foo\' }'); 12721cb0ef41Sopenharmony_ci const mapValues = map.values(); 12731cb0ef41Sopenharmony_ci Object.defineProperty(mapValues, Symbol.toStringTag, { value: 'Foo' }); 12741cb0ef41Sopenharmony_ci assert.strictEqual( 12751cb0ef41Sopenharmony_ci util.inspect(mapValues), 12761cb0ef41Sopenharmony_ci '[Foo] [Map Iterator] { \'bar\' }' 12771cb0ef41Sopenharmony_ci ); 12781cb0ef41Sopenharmony_ci map.set('A', 'B!'); 12791cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(map.entries(), { maxArrayLength: 1 }), 12801cb0ef41Sopenharmony_ci "[Map Entries] { [ 'foo', 'bar' ], ... 1 more item }"); 12811cb0ef41Sopenharmony_ci // Make sure the iterator doesn't get consumed. 12821cb0ef41Sopenharmony_ci const keys = map.keys(); 12831cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(keys), "[Map Iterator] { 'foo', 'A' }"); 12841cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(keys), "[Map Iterator] { 'foo', 'A' }"); 12851cb0ef41Sopenharmony_ci keys.extra = true; 12861cb0ef41Sopenharmony_ci assert.strictEqual( 12871cb0ef41Sopenharmony_ci util.inspect(keys, { maxArrayLength: 0 }), 12881cb0ef41Sopenharmony_ci '[Map Iterator] { ... 2 more items, extra: true }'); 12891cb0ef41Sopenharmony_ci} 12901cb0ef41Sopenharmony_ci 12911cb0ef41Sopenharmony_ci// Test Set iterators. 12921cb0ef41Sopenharmony_ci{ 12931cb0ef41Sopenharmony_ci const aSet = new Set([1]); 12941cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(aSet.entries(), { compact: false }), 12951cb0ef41Sopenharmony_ci '[Set Entries] {\n [\n 1,\n 1\n ]\n}'); 12961cb0ef41Sopenharmony_ci aSet.add(3); 12971cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(aSet.keys()), '[Set Iterator] { 1, 3 }'); 12981cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(aSet.values()), '[Set Iterator] { 1, 3 }'); 12991cb0ef41Sopenharmony_ci const setEntries = aSet.entries(); 13001cb0ef41Sopenharmony_ci Object.defineProperty(setEntries, Symbol.toStringTag, { value: 'Foo' }); 13011cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(setEntries), 13021cb0ef41Sopenharmony_ci '[Foo] [Set Entries] { [ 1, 1 ], [ 3, 3 ] }'); 13031cb0ef41Sopenharmony_ci // Make sure the iterator doesn't get consumed. 13041cb0ef41Sopenharmony_ci const keys = aSet.keys(); 13051cb0ef41Sopenharmony_ci Object.defineProperty(keys, Symbol.toStringTag, { value: null }); 13061cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(keys), '[Set Iterator] { 1, 3 }'); 13071cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(keys), '[Set Iterator] { 1, 3 }'); 13081cb0ef41Sopenharmony_ci keys.extra = true; 13091cb0ef41Sopenharmony_ci assert.strictEqual( 13101cb0ef41Sopenharmony_ci util.inspect(keys, { maxArrayLength: 1 }), 13111cb0ef41Sopenharmony_ci '[Set Iterator] { 1, ... 1 more item, extra: true }'); 13121cb0ef41Sopenharmony_ci} 13131cb0ef41Sopenharmony_ci 13141cb0ef41Sopenharmony_ci// Minimal inspection should still return as much information as possible about 13151cb0ef41Sopenharmony_ci// the constructor and Symbol.toStringTag. 13161cb0ef41Sopenharmony_ci{ 13171cb0ef41Sopenharmony_ci class Foo { 13181cb0ef41Sopenharmony_ci get [Symbol.toStringTag]() { 13191cb0ef41Sopenharmony_ci return 'ABC'; 13201cb0ef41Sopenharmony_ci } 13211cb0ef41Sopenharmony_ci } 13221cb0ef41Sopenharmony_ci const a = new Foo(); 13231cb0ef41Sopenharmony_ci assert.strictEqual(inspect(a, { depth: -1 }), 'Foo [ABC] {}'); 13241cb0ef41Sopenharmony_ci a.foo = true; 13251cb0ef41Sopenharmony_ci assert.strictEqual(inspect(a, { depth: -1 }), '[Foo [ABC]]'); 13261cb0ef41Sopenharmony_ci Object.defineProperty(a, Symbol.toStringTag, { 13271cb0ef41Sopenharmony_ci value: 'Foo', 13281cb0ef41Sopenharmony_ci configurable: true, 13291cb0ef41Sopenharmony_ci writable: true 13301cb0ef41Sopenharmony_ci }); 13311cb0ef41Sopenharmony_ci assert.strictEqual(inspect(a, { depth: -1 }), '[Foo]'); 13321cb0ef41Sopenharmony_ci delete a[Symbol.toStringTag]; 13331cb0ef41Sopenharmony_ci Object.setPrototypeOf(a, null); 13341cb0ef41Sopenharmony_ci assert.strictEqual(inspect(a, { depth: -1 }), '[Foo: null prototype]'); 13351cb0ef41Sopenharmony_ci delete a.foo; 13361cb0ef41Sopenharmony_ci assert.strictEqual(inspect(a, { depth: -1 }), '[Foo: null prototype] {}'); 13371cb0ef41Sopenharmony_ci Object.defineProperty(a, Symbol.toStringTag, { 13381cb0ef41Sopenharmony_ci value: 'ABC', 13391cb0ef41Sopenharmony_ci configurable: true 13401cb0ef41Sopenharmony_ci }); 13411cb0ef41Sopenharmony_ci assert.strictEqual( 13421cb0ef41Sopenharmony_ci inspect(a, { depth: -1 }), 13431cb0ef41Sopenharmony_ci '[Foo: null prototype] [ABC] {}' 13441cb0ef41Sopenharmony_ci ); 13451cb0ef41Sopenharmony_ci Object.defineProperty(a, Symbol.toStringTag, { 13461cb0ef41Sopenharmony_ci value: 'Foo', 13471cb0ef41Sopenharmony_ci configurable: true 13481cb0ef41Sopenharmony_ci }); 13491cb0ef41Sopenharmony_ci assert.strictEqual( 13501cb0ef41Sopenharmony_ci inspect(a, { depth: -1 }), 13511cb0ef41Sopenharmony_ci '[Object: null prototype] [Foo] {}' 13521cb0ef41Sopenharmony_ci ); 13531cb0ef41Sopenharmony_ci} 13541cb0ef41Sopenharmony_ci 13551cb0ef41Sopenharmony_ci// Test alignment of items in container. 13561cb0ef41Sopenharmony_ci// Assumes that the first numeric character is the start of an item. 13571cb0ef41Sopenharmony_ci{ 13581cb0ef41Sopenharmony_ci function checkAlignment(container, start, lineX, end) { 13591cb0ef41Sopenharmony_ci const lines = util.inspect(container).split('\n'); 13601cb0ef41Sopenharmony_ci lines.forEach((line, i) => { 13611cb0ef41Sopenharmony_ci if (i === 0) { 13621cb0ef41Sopenharmony_ci assert.strictEqual(line, start); 13631cb0ef41Sopenharmony_ci } else if (i === lines.length - 1) { 13641cb0ef41Sopenharmony_ci assert.strictEqual(line, end); 13651cb0ef41Sopenharmony_ci } else { 13661cb0ef41Sopenharmony_ci let expected = lineX.replace('X', i - 1); 13671cb0ef41Sopenharmony_ci if (i !== lines.length - 2) 13681cb0ef41Sopenharmony_ci expected += ','; 13691cb0ef41Sopenharmony_ci assert.strictEqual(line, expected); 13701cb0ef41Sopenharmony_ci } 13711cb0ef41Sopenharmony_ci }); 13721cb0ef41Sopenharmony_ci } 13731cb0ef41Sopenharmony_ci 13741cb0ef41Sopenharmony_ci const bigArray = []; 13751cb0ef41Sopenharmony_ci for (let i = 0; i < 100; i++) { 13761cb0ef41Sopenharmony_ci bigArray.push(i); 13771cb0ef41Sopenharmony_ci } 13781cb0ef41Sopenharmony_ci 13791cb0ef41Sopenharmony_ci const obj = {}; 13801cb0ef41Sopenharmony_ci bigArray.forEach((prop) => { 13811cb0ef41Sopenharmony_ci obj[prop] = null; 13821cb0ef41Sopenharmony_ci }); 13831cb0ef41Sopenharmony_ci 13841cb0ef41Sopenharmony_ci checkAlignment(obj, '{', " 'X': null", '}'); 13851cb0ef41Sopenharmony_ci checkAlignment(new Set(bigArray), 'Set(100) {', ' X', '}'); 13861cb0ef41Sopenharmony_ci checkAlignment( 13871cb0ef41Sopenharmony_ci new Map(bigArray.map((number) => [number, null])), 13881cb0ef41Sopenharmony_ci 'Map(100) {', ' X => null', '}' 13891cb0ef41Sopenharmony_ci ); 13901cb0ef41Sopenharmony_ci} 13911cb0ef41Sopenharmony_ci 13921cb0ef41Sopenharmony_ci 13931cb0ef41Sopenharmony_ci// Test display of constructors. 13941cb0ef41Sopenharmony_ci{ 13951cb0ef41Sopenharmony_ci class ObjectSubclass {} 13961cb0ef41Sopenharmony_ci class ArraySubclass extends Array {} 13971cb0ef41Sopenharmony_ci class SetSubclass extends Set {} 13981cb0ef41Sopenharmony_ci class MapSubclass extends Map {} 13991cb0ef41Sopenharmony_ci class PromiseSubclass extends Promise {} 14001cb0ef41Sopenharmony_ci class SymbolNameClass { 14011cb0ef41Sopenharmony_ci static name = Symbol('name'); 14021cb0ef41Sopenharmony_ci } 14031cb0ef41Sopenharmony_ci 14041cb0ef41Sopenharmony_ci const x = new ObjectSubclass(); 14051cb0ef41Sopenharmony_ci x.foo = 42; 14061cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(x), 14071cb0ef41Sopenharmony_ci 'ObjectSubclass { foo: 42 }'); 14081cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(new ArraySubclass(1, 2, 3)), 14091cb0ef41Sopenharmony_ci 'ArraySubclass(3) [ 1, 2, 3 ]'); 14101cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(new SetSubclass([1, 2, 3])), 14111cb0ef41Sopenharmony_ci 'SetSubclass(3) [Set] { 1, 2, 3 }'); 14121cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(new MapSubclass([['foo', 42]])), 14131cb0ef41Sopenharmony_ci "MapSubclass(1) [Map] { 'foo' => 42 }"); 14141cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(new PromiseSubclass(() => {})), 14151cb0ef41Sopenharmony_ci 'PromiseSubclass [Promise] { <pending> }'); 14161cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(new SymbolNameClass()), 14171cb0ef41Sopenharmony_ci 'Symbol(name) {}'); 14181cb0ef41Sopenharmony_ci assert.strictEqual( 14191cb0ef41Sopenharmony_ci util.inspect({ a: { b: new ArraySubclass([1, [2], 3]) } }, { depth: 1 }), 14201cb0ef41Sopenharmony_ci '{ a: { b: [ArraySubclass] } }' 14211cb0ef41Sopenharmony_ci ); 14221cb0ef41Sopenharmony_ci assert.strictEqual( 14231cb0ef41Sopenharmony_ci util.inspect(Object.setPrototypeOf(x, null)), 14241cb0ef41Sopenharmony_ci '[ObjectSubclass: null prototype] { foo: 42 }' 14251cb0ef41Sopenharmony_ci ); 14261cb0ef41Sopenharmony_ci} 14271cb0ef41Sopenharmony_ci 14281cb0ef41Sopenharmony_ci// Empty and circular before depth. 14291cb0ef41Sopenharmony_ci{ 14301cb0ef41Sopenharmony_ci const arr = [[[[]]]]; 14311cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr), '[ [ [ [] ] ] ]'); 14321cb0ef41Sopenharmony_ci arr[0][0][0][0] = []; 14331cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr), '[ [ [ [Array] ] ] ]'); 14341cb0ef41Sopenharmony_ci arr[0][0][0] = {}; 14351cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr), '[ [ [ {} ] ] ]'); 14361cb0ef41Sopenharmony_ci arr[0][0][0] = { a: 2 }; 14371cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr), '[ [ [ [Object] ] ] ]'); 14381cb0ef41Sopenharmony_ci arr[0][0][0] = arr; 14391cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr), '<ref *1> [ [ [ [Circular *1] ] ] ]'); 14401cb0ef41Sopenharmony_ci arr[0][0][0] = arr[0][0]; 14411cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(arr), '[ [ <ref *1> [ [Circular *1] ] ] ]'); 14421cb0ef41Sopenharmony_ci} 14431cb0ef41Sopenharmony_ci 14441cb0ef41Sopenharmony_ci// Corner cases. 14451cb0ef41Sopenharmony_ci{ 14461cb0ef41Sopenharmony_ci const x = { constructor: 42 }; 14471cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(x), '{ constructor: 42 }'); 14481cb0ef41Sopenharmony_ci} 14491cb0ef41Sopenharmony_ci 14501cb0ef41Sopenharmony_ci{ 14511cb0ef41Sopenharmony_ci const x = {}; 14521cb0ef41Sopenharmony_ci Object.defineProperty(x, 'constructor', { 14531cb0ef41Sopenharmony_ci get: function() { 14541cb0ef41Sopenharmony_ci throw new Error('should not access constructor'); 14551cb0ef41Sopenharmony_ci }, 14561cb0ef41Sopenharmony_ci enumerable: true 14571cb0ef41Sopenharmony_ci }); 14581cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(x), '{ constructor: [Getter] }'); 14591cb0ef41Sopenharmony_ci} 14601cb0ef41Sopenharmony_ci 14611cb0ef41Sopenharmony_ci{ 14621cb0ef41Sopenharmony_ci const x = new function() {}; // eslint-disable-line new-parens 14631cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(x), '{}'); 14641cb0ef41Sopenharmony_ci} 14651cb0ef41Sopenharmony_ci 14661cb0ef41Sopenharmony_ci{ 14671cb0ef41Sopenharmony_ci const x = Object.create(null); 14681cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(x), '[Object: null prototype] {}'); 14691cb0ef41Sopenharmony_ci} 14701cb0ef41Sopenharmony_ci 14711cb0ef41Sopenharmony_ci{ 14721cb0ef41Sopenharmony_ci const x = []; 14731cb0ef41Sopenharmony_ci x[''] = 1; 14741cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(x), "[ '': 1 ]"); 14751cb0ef41Sopenharmony_ci} 14761cb0ef41Sopenharmony_ci 14771cb0ef41Sopenharmony_ci// The following maxArrayLength tests were introduced after v6.0.0 was released. 14781cb0ef41Sopenharmony_ci// Do not backport to v5/v4 unless all of 14791cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/pull/6334 is backported. 14801cb0ef41Sopenharmony_ci{ 14811cb0ef41Sopenharmony_ci const x = new Array(101).fill(); 14821cb0ef41Sopenharmony_ci assert(util.inspect(x).endsWith('1 more item\n]')); 14831cb0ef41Sopenharmony_ci assert(!util.inspect(x, { maxArrayLength: 101 }).endsWith('1 more item\n]')); 14841cb0ef41Sopenharmony_ci assert.strictEqual( 14851cb0ef41Sopenharmony_ci util.inspect(x, { maxArrayLength: -1 }), 14861cb0ef41Sopenharmony_ci '[ ... 101 more items ]' 14871cb0ef41Sopenharmony_ci ); 14881cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(x, { maxArrayLength: 0 }), 14891cb0ef41Sopenharmony_ci '[ ... 101 more items ]'); 14901cb0ef41Sopenharmony_ci} 14911cb0ef41Sopenharmony_ci 14921cb0ef41Sopenharmony_ci{ 14931cb0ef41Sopenharmony_ci const x = Array(101); 14941cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(x, { maxArrayLength: 0 }), 14951cb0ef41Sopenharmony_ci '[ ... 101 more items ]'); 14961cb0ef41Sopenharmony_ci assert(!util.inspect(x, { maxArrayLength: null }).endsWith('1 more item\n]')); 14971cb0ef41Sopenharmony_ci assert(!util.inspect( 14981cb0ef41Sopenharmony_ci x, { maxArrayLength: Infinity } 14991cb0ef41Sopenharmony_ci ).endsWith('1 more item ]')); 15001cb0ef41Sopenharmony_ci} 15011cb0ef41Sopenharmony_ci 15021cb0ef41Sopenharmony_ci{ 15031cb0ef41Sopenharmony_ci const x = new Uint8Array(101); 15041cb0ef41Sopenharmony_ci assert(util.inspect(x).endsWith('1 more item\n]')); 15051cb0ef41Sopenharmony_ci assert(!util.inspect(x, { maxArrayLength: 101 }).includes('1 more item')); 15061cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(x, { maxArrayLength: 0 }), 15071cb0ef41Sopenharmony_ci 'Uint8Array(101) [ ... 101 more items ]'); 15081cb0ef41Sopenharmony_ci assert(!util.inspect(x, { maxArrayLength: null }).includes('1 more item')); 15091cb0ef41Sopenharmony_ci assert(util.inspect(x, { maxArrayLength: Infinity }).endsWith(' 0, 0\n]')); 15101cb0ef41Sopenharmony_ci} 15111cb0ef41Sopenharmony_ci 15121cb0ef41Sopenharmony_ci{ 15131cb0ef41Sopenharmony_ci const obj = { foo: 'abc', bar: 'xyz' }; 15141cb0ef41Sopenharmony_ci const oneLine = util.inspect(obj, { breakLength: Infinity }); 15151cb0ef41Sopenharmony_ci // Subtract four for the object's two curly braces and two spaces of padding. 15161cb0ef41Sopenharmony_ci // Add one more to satisfy the strictly greater than condition in the code. 15171cb0ef41Sopenharmony_ci const breakpoint = oneLine.length - 5; 15181cb0ef41Sopenharmony_ci const twoLines = util.inspect(obj, { breakLength: breakpoint }); 15191cb0ef41Sopenharmony_ci 15201cb0ef41Sopenharmony_ci assert.strictEqual(oneLine, "{ foo: 'abc', bar: 'xyz' }"); 15211cb0ef41Sopenharmony_ci assert.strictEqual( 15221cb0ef41Sopenharmony_ci util.inspect(obj, { breakLength: breakpoint + 1 }), 15231cb0ef41Sopenharmony_ci twoLines 15241cb0ef41Sopenharmony_ci ); 15251cb0ef41Sopenharmony_ci assert.strictEqual(twoLines, "{\n foo: 'abc',\n bar: 'xyz'\n}"); 15261cb0ef41Sopenharmony_ci} 15271cb0ef41Sopenharmony_ci 15281cb0ef41Sopenharmony_ci// util.inspect.defaultOptions tests. 15291cb0ef41Sopenharmony_ci{ 15301cb0ef41Sopenharmony_ci const arr = new Array(101).fill(); 15311cb0ef41Sopenharmony_ci const obj = { a: { a: { a: { a: 1 } } } }; 15321cb0ef41Sopenharmony_ci 15331cb0ef41Sopenharmony_ci const oldOptions = { ...util.inspect.defaultOptions }; 15341cb0ef41Sopenharmony_ci 15351cb0ef41Sopenharmony_ci // Set single option through property assignment. 15361cb0ef41Sopenharmony_ci util.inspect.defaultOptions.maxArrayLength = null; 15371cb0ef41Sopenharmony_ci assert.doesNotMatch(util.inspect(arr), /1 more item/); 15381cb0ef41Sopenharmony_ci util.inspect.defaultOptions.maxArrayLength = oldOptions.maxArrayLength; 15391cb0ef41Sopenharmony_ci assert.match(util.inspect(arr), /1 more item/); 15401cb0ef41Sopenharmony_ci util.inspect.defaultOptions.depth = null; 15411cb0ef41Sopenharmony_ci assert.doesNotMatch(util.inspect(obj), /Object/); 15421cb0ef41Sopenharmony_ci util.inspect.defaultOptions.depth = oldOptions.depth; 15431cb0ef41Sopenharmony_ci assert.match(util.inspect(obj), /Object/); 15441cb0ef41Sopenharmony_ci assert.strictEqual( 15451cb0ef41Sopenharmony_ci JSON.stringify(util.inspect.defaultOptions), 15461cb0ef41Sopenharmony_ci JSON.stringify(oldOptions) 15471cb0ef41Sopenharmony_ci ); 15481cb0ef41Sopenharmony_ci 15491cb0ef41Sopenharmony_ci // Set multiple options through object assignment. 15501cb0ef41Sopenharmony_ci util.inspect.defaultOptions = { maxArrayLength: null, depth: 2 }; 15511cb0ef41Sopenharmony_ci assert.doesNotMatch(util.inspect(arr), /1 more item/); 15521cb0ef41Sopenharmony_ci assert.match(util.inspect(obj), /Object/); 15531cb0ef41Sopenharmony_ci util.inspect.defaultOptions = oldOptions; 15541cb0ef41Sopenharmony_ci assert.match(util.inspect(arr), /1 more item/); 15551cb0ef41Sopenharmony_ci assert.match(util.inspect(obj), /Object/); 15561cb0ef41Sopenharmony_ci assert.strictEqual( 15571cb0ef41Sopenharmony_ci JSON.stringify(util.inspect.defaultOptions), 15581cb0ef41Sopenharmony_ci JSON.stringify(oldOptions) 15591cb0ef41Sopenharmony_ci ); 15601cb0ef41Sopenharmony_ci 15611cb0ef41Sopenharmony_ci assert.throws(() => { 15621cb0ef41Sopenharmony_ci util.inspect.defaultOptions = null; 15631cb0ef41Sopenharmony_ci }, { 15641cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 15651cb0ef41Sopenharmony_ci name: 'TypeError', 15661cb0ef41Sopenharmony_ci message: 'The "options" argument must be of type object. ' + 15671cb0ef41Sopenharmony_ci 'Received null' 15681cb0ef41Sopenharmony_ci } 15691cb0ef41Sopenharmony_ci ); 15701cb0ef41Sopenharmony_ci 15711cb0ef41Sopenharmony_ci assert.throws(() => { 15721cb0ef41Sopenharmony_ci util.inspect.defaultOptions = 'bad'; 15731cb0ef41Sopenharmony_ci }, { 15741cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 15751cb0ef41Sopenharmony_ci name: 'TypeError', 15761cb0ef41Sopenharmony_ci message: 'The "options" argument must be of type object. ' + 15771cb0ef41Sopenharmony_ci "Received type string ('bad')" 15781cb0ef41Sopenharmony_ci } 15791cb0ef41Sopenharmony_ci ); 15801cb0ef41Sopenharmony_ci} 15811cb0ef41Sopenharmony_ci 15821cb0ef41Sopenharmony_ciutil.inspect(process); 15831cb0ef41Sopenharmony_ci 15841cb0ef41Sopenharmony_ci// Setting custom inspect property to a non-function should do nothing. 15851cb0ef41Sopenharmony_ci{ 15861cb0ef41Sopenharmony_ci const obj = { [util.inspect.custom]: 'fhqwhgads' }; 15871cb0ef41Sopenharmony_ci assert.strictEqual( 15881cb0ef41Sopenharmony_ci util.inspect(obj), 15891cb0ef41Sopenharmony_ci "{ [Symbol(nodejs.util.inspect.custom)]: 'fhqwhgads' }" 15901cb0ef41Sopenharmony_ci ); 15911cb0ef41Sopenharmony_ci} 15921cb0ef41Sopenharmony_ci 15931cb0ef41Sopenharmony_ci{ 15941cb0ef41Sopenharmony_ci // @@toStringTag 15951cb0ef41Sopenharmony_ci const obj = { [Symbol.toStringTag]: 'a' }; 15961cb0ef41Sopenharmony_ci assert.strictEqual( 15971cb0ef41Sopenharmony_ci util.inspect(obj), 15981cb0ef41Sopenharmony_ci "{ [Symbol(Symbol.toStringTag)]: 'a' }" 15991cb0ef41Sopenharmony_ci ); 16001cb0ef41Sopenharmony_ci Object.defineProperty(obj, Symbol.toStringTag, { 16011cb0ef41Sopenharmony_ci value: 'a', 16021cb0ef41Sopenharmony_ci enumerable: false 16031cb0ef41Sopenharmony_ci }); 16041cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(obj), 'Object [a] {}'); 16051cb0ef41Sopenharmony_ci assert.strictEqual( 16061cb0ef41Sopenharmony_ci util.inspect(obj, { showHidden: true }), 16071cb0ef41Sopenharmony_ci "{ [Symbol(Symbol.toStringTag)]: 'a' }" 16081cb0ef41Sopenharmony_ci ); 16091cb0ef41Sopenharmony_ci 16101cb0ef41Sopenharmony_ci class Foo { 16111cb0ef41Sopenharmony_ci constructor() { 16121cb0ef41Sopenharmony_ci this.foo = 'bar'; 16131cb0ef41Sopenharmony_ci } 16141cb0ef41Sopenharmony_ci 16151cb0ef41Sopenharmony_ci get [Symbol.toStringTag]() { 16161cb0ef41Sopenharmony_ci return this.foo; 16171cb0ef41Sopenharmony_ci } 16181cb0ef41Sopenharmony_ci } 16191cb0ef41Sopenharmony_ci 16201cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect( 16211cb0ef41Sopenharmony_ci Object.create(null, { [Symbol.toStringTag]: { value: 'foo' } })), 16221cb0ef41Sopenharmony_ci '[Object: null prototype] [foo] {}'); 16231cb0ef41Sopenharmony_ci 16241cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(new Foo()), "Foo [bar] { foo: 'bar' }"); 16251cb0ef41Sopenharmony_ci 16261cb0ef41Sopenharmony_ci assert.strictEqual( 16271cb0ef41Sopenharmony_ci util.inspect(new (class extends Foo {})()), 16281cb0ef41Sopenharmony_ci "Foo [bar] { foo: 'bar' }"); 16291cb0ef41Sopenharmony_ci 16301cb0ef41Sopenharmony_ci assert.strictEqual( 16311cb0ef41Sopenharmony_ci util.inspect(Object.create(Object.create(Foo.prototype), { 16321cb0ef41Sopenharmony_ci foo: { value: 'bar', enumerable: true } 16331cb0ef41Sopenharmony_ci })), 16341cb0ef41Sopenharmony_ci "Foo [bar] { foo: 'bar' }"); 16351cb0ef41Sopenharmony_ci 16361cb0ef41Sopenharmony_ci class ThrowingClass { 16371cb0ef41Sopenharmony_ci get [Symbol.toStringTag]() { 16381cb0ef41Sopenharmony_ci throw new Error('toStringTag error'); 16391cb0ef41Sopenharmony_ci } 16401cb0ef41Sopenharmony_ci } 16411cb0ef41Sopenharmony_ci 16421cb0ef41Sopenharmony_ci assert.throws(() => util.inspect(new ThrowingClass()), /toStringTag error/); 16431cb0ef41Sopenharmony_ci 16441cb0ef41Sopenharmony_ci class NotStringClass { 16451cb0ef41Sopenharmony_ci get [Symbol.toStringTag]() { 16461cb0ef41Sopenharmony_ci return null; 16471cb0ef41Sopenharmony_ci } 16481cb0ef41Sopenharmony_ci } 16491cb0ef41Sopenharmony_ci 16501cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(new NotStringClass()), 16511cb0ef41Sopenharmony_ci 'NotStringClass {}'); 16521cb0ef41Sopenharmony_ci} 16531cb0ef41Sopenharmony_ci 16541cb0ef41Sopenharmony_ci{ 16551cb0ef41Sopenharmony_ci const o = { 16561cb0ef41Sopenharmony_ci a: [1, 2, [[ 16571cb0ef41Sopenharmony_ci 'Lorem ipsum dolor\nsit amet,\tconsectetur adipiscing elit, sed do ' + 16581cb0ef41Sopenharmony_ci 'eiusmod tempor incididunt ut labore et dolore magna aliqua.', 16591cb0ef41Sopenharmony_ci 'test', 16601cb0ef41Sopenharmony_ci 'foo']], 4], 16611cb0ef41Sopenharmony_ci b: new Map([['za', 1], ['zb', 'test']]) 16621cb0ef41Sopenharmony_ci }; 16631cb0ef41Sopenharmony_ci 16641cb0ef41Sopenharmony_ci let out = util.inspect(o, { compact: true, depth: 5, breakLength: 80 }); 16651cb0ef41Sopenharmony_ci let expect = [ 16661cb0ef41Sopenharmony_ci '{ a:', 16671cb0ef41Sopenharmony_ci ' [ 1,', 16681cb0ef41Sopenharmony_ci ' 2,', 16691cb0ef41Sopenharmony_ci " [ [ 'Lorem ipsum dolor\\nsit amet,\\tconsectetur adipiscing elit, " + 16701cb0ef41Sopenharmony_ci "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',", 16711cb0ef41Sopenharmony_ci " 'test',", 16721cb0ef41Sopenharmony_ci " 'foo' ] ],", 16731cb0ef41Sopenharmony_ci ' 4 ],', 16741cb0ef41Sopenharmony_ci " b: Map(2) { 'za' => 1, 'zb' => 'test' } }", 16751cb0ef41Sopenharmony_ci ].join('\n'); 16761cb0ef41Sopenharmony_ci assert.strictEqual(out, expect); 16771cb0ef41Sopenharmony_ci 16781cb0ef41Sopenharmony_ci out = util.inspect(o, { compact: false, depth: 5, breakLength: 60 }); 16791cb0ef41Sopenharmony_ci expect = [ 16801cb0ef41Sopenharmony_ci '{', 16811cb0ef41Sopenharmony_ci ' a: [', 16821cb0ef41Sopenharmony_ci ' 1,', 16831cb0ef41Sopenharmony_ci ' 2,', 16841cb0ef41Sopenharmony_ci ' [', 16851cb0ef41Sopenharmony_ci ' [', 16861cb0ef41Sopenharmony_ci " 'Lorem ipsum dolor\\n' +", 16871cb0ef41Sopenharmony_ci " 'sit amet,\\tconsectetur adipiscing elit, sed do eiusmod " + 16881cb0ef41Sopenharmony_ci "tempor incididunt ut labore et dolore magna aliqua.',", 16891cb0ef41Sopenharmony_ci " 'test',", 16901cb0ef41Sopenharmony_ci " 'foo'", 16911cb0ef41Sopenharmony_ci ' ]', 16921cb0ef41Sopenharmony_ci ' ],', 16931cb0ef41Sopenharmony_ci ' 4', 16941cb0ef41Sopenharmony_ci ' ],', 16951cb0ef41Sopenharmony_ci ' b: Map(2) {', 16961cb0ef41Sopenharmony_ci " 'za' => 1,", 16971cb0ef41Sopenharmony_ci " 'zb' => 'test'", 16981cb0ef41Sopenharmony_ci ' }', 16991cb0ef41Sopenharmony_ci '}', 17001cb0ef41Sopenharmony_ci ].join('\n'); 17011cb0ef41Sopenharmony_ci assert.strictEqual(out, expect); 17021cb0ef41Sopenharmony_ci 17031cb0ef41Sopenharmony_ci out = util.inspect(o.a[2][0][0], { compact: false, breakLength: 30 }); 17041cb0ef41Sopenharmony_ci expect = [ 17051cb0ef41Sopenharmony_ci "'Lorem ipsum dolor\\n' +", 17061cb0ef41Sopenharmony_ci " 'sit amet,\\tconsectetur adipiscing elit, sed do eiusmod tempor " + 17071cb0ef41Sopenharmony_ci "incididunt ut labore et dolore magna aliqua.'", 17081cb0ef41Sopenharmony_ci ].join('\n'); 17091cb0ef41Sopenharmony_ci assert.strictEqual(out, expect); 17101cb0ef41Sopenharmony_ci 17111cb0ef41Sopenharmony_ci out = util.inspect( 17121cb0ef41Sopenharmony_ci '12345678901234567890123456789012345678901234567890', 17131cb0ef41Sopenharmony_ci { compact: false, breakLength: 3 }); 17141cb0ef41Sopenharmony_ci expect = "'12345678901234567890123456789012345678901234567890'"; 17151cb0ef41Sopenharmony_ci assert.strictEqual(out, expect); 17161cb0ef41Sopenharmony_ci 17171cb0ef41Sopenharmony_ci out = util.inspect( 17181cb0ef41Sopenharmony_ci '12 45 78 01 34 67 90 23 56 89 123456789012345678901234567890', 17191cb0ef41Sopenharmony_ci { compact: false, breakLength: 3 }); 17201cb0ef41Sopenharmony_ci expect = [ 17211cb0ef41Sopenharmony_ci "'12 45 78 01 34 67 90 23 56 89 123456789012345678901234567890'", 17221cb0ef41Sopenharmony_ci ].join('\n'); 17231cb0ef41Sopenharmony_ci assert.strictEqual(out, expect); 17241cb0ef41Sopenharmony_ci 17251cb0ef41Sopenharmony_ci o.a = () => {}; 17261cb0ef41Sopenharmony_ci o.b = new Number(3); 17271cb0ef41Sopenharmony_ci out = util.inspect(o, { compact: false, breakLength: 3 }); 17281cb0ef41Sopenharmony_ci expect = [ 17291cb0ef41Sopenharmony_ci '{', 17301cb0ef41Sopenharmony_ci ' a: [Function (anonymous)],', 17311cb0ef41Sopenharmony_ci ' b: [Number: 3]', 17321cb0ef41Sopenharmony_ci '}', 17331cb0ef41Sopenharmony_ci ].join('\n'); 17341cb0ef41Sopenharmony_ci assert.strictEqual(out, expect); 17351cb0ef41Sopenharmony_ci 17361cb0ef41Sopenharmony_ci out = util.inspect(o, { compact: false, breakLength: 3, showHidden: true }); 17371cb0ef41Sopenharmony_ci expect = [ 17381cb0ef41Sopenharmony_ci '{', 17391cb0ef41Sopenharmony_ci ' a: [Function (anonymous)] {', 17401cb0ef41Sopenharmony_ci ' [length]: 0,', 17411cb0ef41Sopenharmony_ci " [name]: ''", 17421cb0ef41Sopenharmony_ci ' },', 17431cb0ef41Sopenharmony_ci ' b: [Number: 3]', 17441cb0ef41Sopenharmony_ci '}', 17451cb0ef41Sopenharmony_ci ].join('\n'); 17461cb0ef41Sopenharmony_ci assert.strictEqual(out, expect); 17471cb0ef41Sopenharmony_ci 17481cb0ef41Sopenharmony_ci o[util.inspect.custom] = () => 42; 17491cb0ef41Sopenharmony_ci out = util.inspect(o, { compact: false, breakLength: 3 }); 17501cb0ef41Sopenharmony_ci expect = '42'; 17511cb0ef41Sopenharmony_ci assert.strictEqual(out, expect); 17521cb0ef41Sopenharmony_ci 17531cb0ef41Sopenharmony_ci o[util.inspect.custom] = () => '12 45 78 01 34 67 90 23'; 17541cb0ef41Sopenharmony_ci out = util.inspect(o, { compact: false, breakLength: 3 }); 17551cb0ef41Sopenharmony_ci expect = '12 45 78 01 34 67 90 23'; 17561cb0ef41Sopenharmony_ci assert.strictEqual(out, expect); 17571cb0ef41Sopenharmony_ci 17581cb0ef41Sopenharmony_ci o[util.inspect.custom] = () => ({ a: '12 45 78 01 34 67 90 23' }); 17591cb0ef41Sopenharmony_ci out = util.inspect(o, { compact: false, breakLength: 3 }); 17601cb0ef41Sopenharmony_ci expect = "{\n a: '12 45 78 01 34 67 90 23'\n}"; 17611cb0ef41Sopenharmony_ci assert.strictEqual(out, expect); 17621cb0ef41Sopenharmony_ci} 17631cb0ef41Sopenharmony_ci 17641cb0ef41Sopenharmony_ci// Check compact indentation. 17651cb0ef41Sopenharmony_ci{ 17661cb0ef41Sopenharmony_ci const typed = new Uint8Array(); 17671cb0ef41Sopenharmony_ci typed.buffer.foo = true; 17681cb0ef41Sopenharmony_ci const set = new Set([[1, 2]]); 17691cb0ef41Sopenharmony_ci const promise = Promise.resolve([[1, set]]); 17701cb0ef41Sopenharmony_ci const map = new Map([[promise, typed]]); 17711cb0ef41Sopenharmony_ci map.set(set.values(), map.values()); 17721cb0ef41Sopenharmony_ci 17731cb0ef41Sopenharmony_ci let out = util.inspect(map, { compact: false, showHidden: true, depth: 9 }); 17741cb0ef41Sopenharmony_ci let expected = [ 17751cb0ef41Sopenharmony_ci 'Map(2) {', 17761cb0ef41Sopenharmony_ci ' Promise {', 17771cb0ef41Sopenharmony_ci ' [', 17781cb0ef41Sopenharmony_ci ' [', 17791cb0ef41Sopenharmony_ci ' 1,', 17801cb0ef41Sopenharmony_ci ' Set(1) {', 17811cb0ef41Sopenharmony_ci ' [', 17821cb0ef41Sopenharmony_ci ' 1,', 17831cb0ef41Sopenharmony_ci ' 2,', 17841cb0ef41Sopenharmony_ci ' [length]: 2', 17851cb0ef41Sopenharmony_ci ' ]', 17861cb0ef41Sopenharmony_ci ' },', 17871cb0ef41Sopenharmony_ci ' [length]: 2', 17881cb0ef41Sopenharmony_ci ' ],', 17891cb0ef41Sopenharmony_ci ' [length]: 1', 17901cb0ef41Sopenharmony_ci ' ]', 17911cb0ef41Sopenharmony_ci ' } => Uint8Array(0) [', 17921cb0ef41Sopenharmony_ci ' [BYTES_PER_ELEMENT]: 1,', 17931cb0ef41Sopenharmony_ci ' [length]: 0,', 17941cb0ef41Sopenharmony_ci ' [byteLength]: 0,', 17951cb0ef41Sopenharmony_ci ' [byteOffset]: 0,', 17961cb0ef41Sopenharmony_ci ' [buffer]: ArrayBuffer {', 17971cb0ef41Sopenharmony_ci ' byteLength: 0,', 17981cb0ef41Sopenharmony_ci ' foo: true', 17991cb0ef41Sopenharmony_ci ' }', 18001cb0ef41Sopenharmony_ci ' ],', 18011cb0ef41Sopenharmony_ci ' [Set Iterator] {', 18021cb0ef41Sopenharmony_ci ' [', 18031cb0ef41Sopenharmony_ci ' 1,', 18041cb0ef41Sopenharmony_ci ' 2,', 18051cb0ef41Sopenharmony_ci ' [length]: 2', 18061cb0ef41Sopenharmony_ci ' ],', 18071cb0ef41Sopenharmony_ci " [Symbol(Symbol.toStringTag)]: 'Set Iterator'", 18081cb0ef41Sopenharmony_ci ' } => <ref *1> [Map Iterator] {', 18091cb0ef41Sopenharmony_ci ' Uint8Array(0) [', 18101cb0ef41Sopenharmony_ci ' [BYTES_PER_ELEMENT]: 1,', 18111cb0ef41Sopenharmony_ci ' [length]: 0,', 18121cb0ef41Sopenharmony_ci ' [byteLength]: 0,', 18131cb0ef41Sopenharmony_ci ' [byteOffset]: 0,', 18141cb0ef41Sopenharmony_ci ' [buffer]: ArrayBuffer {', 18151cb0ef41Sopenharmony_ci ' byteLength: 0,', 18161cb0ef41Sopenharmony_ci ' foo: true', 18171cb0ef41Sopenharmony_ci ' }', 18181cb0ef41Sopenharmony_ci ' ],', 18191cb0ef41Sopenharmony_ci ' [Circular *1],', 18201cb0ef41Sopenharmony_ci " [Symbol(Symbol.toStringTag)]: 'Map Iterator'", 18211cb0ef41Sopenharmony_ci ' }', 18221cb0ef41Sopenharmony_ci '}', 18231cb0ef41Sopenharmony_ci ].join('\n'); 18241cb0ef41Sopenharmony_ci 18251cb0ef41Sopenharmony_ci assert.strict.equal(out, expected); 18261cb0ef41Sopenharmony_ci 18271cb0ef41Sopenharmony_ci out = util.inspect(map, { compact: 2, showHidden: true, depth: 9 }); 18281cb0ef41Sopenharmony_ci 18291cb0ef41Sopenharmony_ci expected = [ 18301cb0ef41Sopenharmony_ci 'Map(2) {', 18311cb0ef41Sopenharmony_ci ' Promise {', 18321cb0ef41Sopenharmony_ci ' [', 18331cb0ef41Sopenharmony_ci ' [', 18341cb0ef41Sopenharmony_ci ' 1,', 18351cb0ef41Sopenharmony_ci ' Set(1) { [ 1, 2, [length]: 2 ] },', 18361cb0ef41Sopenharmony_ci ' [length]: 2', 18371cb0ef41Sopenharmony_ci ' ],', 18381cb0ef41Sopenharmony_ci ' [length]: 1', 18391cb0ef41Sopenharmony_ci ' ]', 18401cb0ef41Sopenharmony_ci ' } => Uint8Array(0) [', 18411cb0ef41Sopenharmony_ci ' [BYTES_PER_ELEMENT]: 1,', 18421cb0ef41Sopenharmony_ci ' [length]: 0,', 18431cb0ef41Sopenharmony_ci ' [byteLength]: 0,', 18441cb0ef41Sopenharmony_ci ' [byteOffset]: 0,', 18451cb0ef41Sopenharmony_ci ' [buffer]: ArrayBuffer { byteLength: 0, foo: true }', 18461cb0ef41Sopenharmony_ci ' ],', 18471cb0ef41Sopenharmony_ci ' [Set Iterator] {', 18481cb0ef41Sopenharmony_ci ' [ 1, 2, [length]: 2 ],', 18491cb0ef41Sopenharmony_ci " [Symbol(Symbol.toStringTag)]: 'Set Iterator'", 18501cb0ef41Sopenharmony_ci ' } => <ref *1> [Map Iterator] {', 18511cb0ef41Sopenharmony_ci ' Uint8Array(0) [', 18521cb0ef41Sopenharmony_ci ' [BYTES_PER_ELEMENT]: 1,', 18531cb0ef41Sopenharmony_ci ' [length]: 0,', 18541cb0ef41Sopenharmony_ci ' [byteLength]: 0,', 18551cb0ef41Sopenharmony_ci ' [byteOffset]: 0,', 18561cb0ef41Sopenharmony_ci ' [buffer]: ArrayBuffer { byteLength: 0, foo: true }', 18571cb0ef41Sopenharmony_ci ' ],', 18581cb0ef41Sopenharmony_ci ' [Circular *1],', 18591cb0ef41Sopenharmony_ci " [Symbol(Symbol.toStringTag)]: 'Map Iterator'", 18601cb0ef41Sopenharmony_ci ' }', 18611cb0ef41Sopenharmony_ci '}', 18621cb0ef41Sopenharmony_ci ].join('\n'); 18631cb0ef41Sopenharmony_ci 18641cb0ef41Sopenharmony_ci assert.strict.equal(out, expected); 18651cb0ef41Sopenharmony_ci 18661cb0ef41Sopenharmony_ci out = util.inspect(map, { 18671cb0ef41Sopenharmony_ci showHidden: true, depth: 9, breakLength: 4, compact: true 18681cb0ef41Sopenharmony_ci }); 18691cb0ef41Sopenharmony_ci expected = [ 18701cb0ef41Sopenharmony_ci 'Map(2) {', 18711cb0ef41Sopenharmony_ci ' Promise {', 18721cb0ef41Sopenharmony_ci ' [ [ 1,', 18731cb0ef41Sopenharmony_ci ' Set(1) {', 18741cb0ef41Sopenharmony_ci ' [ 1,', 18751cb0ef41Sopenharmony_ci ' 2,', 18761cb0ef41Sopenharmony_ci ' [length]: 2 ] },', 18771cb0ef41Sopenharmony_ci ' [length]: 2 ],', 18781cb0ef41Sopenharmony_ci ' [length]: 1 ] } => Uint8Array(0) [', 18791cb0ef41Sopenharmony_ci ' [BYTES_PER_ELEMENT]: 1,', 18801cb0ef41Sopenharmony_ci ' [length]: 0,', 18811cb0ef41Sopenharmony_ci ' [byteLength]: 0,', 18821cb0ef41Sopenharmony_ci ' [byteOffset]: 0,', 18831cb0ef41Sopenharmony_ci ' [buffer]: ArrayBuffer {', 18841cb0ef41Sopenharmony_ci ' byteLength: 0,', 18851cb0ef41Sopenharmony_ci ' foo: true } ],', 18861cb0ef41Sopenharmony_ci ' [Set Iterator] {', 18871cb0ef41Sopenharmony_ci ' [ 1,', 18881cb0ef41Sopenharmony_ci ' 2,', 18891cb0ef41Sopenharmony_ci ' [length]: 2 ],', 18901cb0ef41Sopenharmony_ci ' [Symbol(Symbol.toStringTag)]:', 18911cb0ef41Sopenharmony_ci " 'Set Iterator' } => <ref *1> [Map Iterator] {", 18921cb0ef41Sopenharmony_ci ' Uint8Array(0) [', 18931cb0ef41Sopenharmony_ci ' [BYTES_PER_ELEMENT]: 1,', 18941cb0ef41Sopenharmony_ci ' [length]: 0,', 18951cb0ef41Sopenharmony_ci ' [byteLength]: 0,', 18961cb0ef41Sopenharmony_ci ' [byteOffset]: 0,', 18971cb0ef41Sopenharmony_ci ' [buffer]: ArrayBuffer {', 18981cb0ef41Sopenharmony_ci ' byteLength: 0,', 18991cb0ef41Sopenharmony_ci ' foo: true } ],', 19001cb0ef41Sopenharmony_ci ' [Circular *1],', 19011cb0ef41Sopenharmony_ci ' [Symbol(Symbol.toStringTag)]:', 19021cb0ef41Sopenharmony_ci " 'Map Iterator' } }", 19031cb0ef41Sopenharmony_ci ].join('\n'); 19041cb0ef41Sopenharmony_ci 19051cb0ef41Sopenharmony_ci assert.strict.equal(out, expected); 19061cb0ef41Sopenharmony_ci} 19071cb0ef41Sopenharmony_ci 19081cb0ef41Sopenharmony_ci{ // Test WeakMap && WeakSet 19091cb0ef41Sopenharmony_ci const obj = {}; 19101cb0ef41Sopenharmony_ci const arr = []; 19111cb0ef41Sopenharmony_ci const weakMap = new WeakMap([[obj, arr], [arr, obj]]); 19121cb0ef41Sopenharmony_ci let out = util.inspect(weakMap, { showHidden: true }); 19131cb0ef41Sopenharmony_ci let expect = 'WeakMap { [ [length]: 0 ] => {}, {} => [ [length]: 0 ] }'; 19141cb0ef41Sopenharmony_ci assert.strictEqual(out, expect); 19151cb0ef41Sopenharmony_ci 19161cb0ef41Sopenharmony_ci out = util.inspect(weakMap); 19171cb0ef41Sopenharmony_ci expect = 'WeakMap { <items unknown> }'; 19181cb0ef41Sopenharmony_ci assert.strictEqual(out, expect); 19191cb0ef41Sopenharmony_ci 19201cb0ef41Sopenharmony_ci out = util.inspect(weakMap, { maxArrayLength: 0, showHidden: true }); 19211cb0ef41Sopenharmony_ci expect = 'WeakMap { ... 2 more items }'; 19221cb0ef41Sopenharmony_ci assert.strictEqual(out, expect); 19231cb0ef41Sopenharmony_ci 19241cb0ef41Sopenharmony_ci weakMap.extra = true; 19251cb0ef41Sopenharmony_ci out = util.inspect(weakMap, { maxArrayLength: 1, showHidden: true }); 19261cb0ef41Sopenharmony_ci // It is not possible to determine the output reliable. 19271cb0ef41Sopenharmony_ci expect = 'WeakMap { [ [length]: 0 ] => {}, ... 1 more item, extra: true }'; 19281cb0ef41Sopenharmony_ci let expectAlt = 'WeakMap { {} => [ [length]: 0 ], ... 1 more item, ' + 19291cb0ef41Sopenharmony_ci 'extra: true }'; 19301cb0ef41Sopenharmony_ci assert(out === expect || out === expectAlt, 19311cb0ef41Sopenharmony_ci `Found: "${out}"\nrather than: "${expect}"\nor: "${expectAlt}"`); 19321cb0ef41Sopenharmony_ci 19331cb0ef41Sopenharmony_ci // Test WeakSet 19341cb0ef41Sopenharmony_ci arr.push(1); 19351cb0ef41Sopenharmony_ci const weakSet = new WeakSet([obj, arr]); 19361cb0ef41Sopenharmony_ci out = util.inspect(weakSet, { showHidden: true }); 19371cb0ef41Sopenharmony_ci expect = 'WeakSet { [ 1, [length]: 1 ], {} }'; 19381cb0ef41Sopenharmony_ci assert.strictEqual(out, expect); 19391cb0ef41Sopenharmony_ci 19401cb0ef41Sopenharmony_ci out = util.inspect(weakSet); 19411cb0ef41Sopenharmony_ci expect = 'WeakSet { <items unknown> }'; 19421cb0ef41Sopenharmony_ci assert.strictEqual(out, expect); 19431cb0ef41Sopenharmony_ci 19441cb0ef41Sopenharmony_ci out = util.inspect(weakSet, { maxArrayLength: -2, showHidden: true }); 19451cb0ef41Sopenharmony_ci expect = 'WeakSet { ... 2 more items }'; 19461cb0ef41Sopenharmony_ci assert.strictEqual(out, expect); 19471cb0ef41Sopenharmony_ci 19481cb0ef41Sopenharmony_ci weakSet.extra = true; 19491cb0ef41Sopenharmony_ci out = util.inspect(weakSet, { maxArrayLength: 1, showHidden: true }); 19501cb0ef41Sopenharmony_ci // It is not possible to determine the output reliable. 19511cb0ef41Sopenharmony_ci expect = 'WeakSet { {}, ... 1 more item, extra: true }'; 19521cb0ef41Sopenharmony_ci expectAlt = 'WeakSet { [ 1, [length]: 1 ], ... 1 more item, extra: true }'; 19531cb0ef41Sopenharmony_ci assert(out === expect || out === expectAlt, 19541cb0ef41Sopenharmony_ci `Found: "${out}"\nrather than: "${expect}"\nor: "${expectAlt}"`); 19551cb0ef41Sopenharmony_ci // Keep references to the WeakMap entries, otherwise they could be GCed too 19561cb0ef41Sopenharmony_ci // early. 19571cb0ef41Sopenharmony_ci assert(obj && arr); 19581cb0ef41Sopenharmony_ci} 19591cb0ef41Sopenharmony_ci 19601cb0ef41Sopenharmony_ci{ // Test argument objects. 19611cb0ef41Sopenharmony_ci const args = (function() { return arguments; })('a'); 19621cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(args), "[Arguments] { '0': 'a' }"); 19631cb0ef41Sopenharmony_ci} 19641cb0ef41Sopenharmony_ci 19651cb0ef41Sopenharmony_ci{ 19661cb0ef41Sopenharmony_ci // Test that a long linked list can be inspected without throwing an error. 19671cb0ef41Sopenharmony_ci const list = {}; 19681cb0ef41Sopenharmony_ci let head = list; 19691cb0ef41Sopenharmony_ci // A linked list of length 100k should be inspectable in some way, even though 19701cb0ef41Sopenharmony_ci // the real cutoff value is much lower than 100k. 19711cb0ef41Sopenharmony_ci for (let i = 0; i < 100000; i++) 19721cb0ef41Sopenharmony_ci head = head.next = {}; 19731cb0ef41Sopenharmony_ci assert.strictEqual( 19741cb0ef41Sopenharmony_ci util.inspect(list), 19751cb0ef41Sopenharmony_ci '{ next: { next: { next: [Object] } } }' 19761cb0ef41Sopenharmony_ci ); 19771cb0ef41Sopenharmony_ci const longList = util.inspect(list, { depth: Infinity }); 19781cb0ef41Sopenharmony_ci const match = longList.match(/next/g); 19791cb0ef41Sopenharmony_ci assert(match.length > 500 && match.length < 10000); 19801cb0ef41Sopenharmony_ci assert(longList.includes('[Object: Inspection interrupted ' + 19811cb0ef41Sopenharmony_ci 'prematurely. Maximum call stack size exceeded.]')); 19821cb0ef41Sopenharmony_ci} 19831cb0ef41Sopenharmony_ci 19841cb0ef41Sopenharmony_ci// Do not escape single quotes if no double quote or backtick is present. 19851cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect("'"), '"\'"'); 19861cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect('"\''), '`"\'`'); 19871cb0ef41Sopenharmony_ci// eslint-disable-next-line no-template-curly-in-string 19881cb0ef41Sopenharmony_ciassert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'"); 19891cb0ef41Sopenharmony_ci 19901cb0ef41Sopenharmony_ci// Errors should visualize as much information as possible. 19911cb0ef41Sopenharmony_ci// If the name is not included in the stack, visualize it as well. 19921cb0ef41Sopenharmony_ci[ 19931cb0ef41Sopenharmony_ci [class Foo extends TypeError {}, 'test'], 19941cb0ef41Sopenharmony_ci [class Foo extends TypeError {}, undefined], 19951cb0ef41Sopenharmony_ci [class BarError extends Error {}, 'test'], 19961cb0ef41Sopenharmony_ci [class BazError extends Error { 19971cb0ef41Sopenharmony_ci get name() { 19981cb0ef41Sopenharmony_ci return 'BazError'; 19991cb0ef41Sopenharmony_ci } 20001cb0ef41Sopenharmony_ci }, undefined], 20011cb0ef41Sopenharmony_ci].forEach(([Class, message], i) => { 20021cb0ef41Sopenharmony_ci console.log('Test %i', i); 20031cb0ef41Sopenharmony_ci const foo = new Class(message); 20041cb0ef41Sopenharmony_ci const name = foo.name; 20051cb0ef41Sopenharmony_ci const extra = Class.name.includes('Error') ? '' : ` [${foo.name}]`; 20061cb0ef41Sopenharmony_ci assert( 20071cb0ef41Sopenharmony_ci util.inspect(foo).startsWith( 20081cb0ef41Sopenharmony_ci `${Class.name}${extra}${message ? `: ${message}` : '\n'}`), 20091cb0ef41Sopenharmony_ci util.inspect(foo) 20101cb0ef41Sopenharmony_ci ); 20111cb0ef41Sopenharmony_ci Object.defineProperty(foo, Symbol.toStringTag, { 20121cb0ef41Sopenharmony_ci value: 'WOW', 20131cb0ef41Sopenharmony_ci writable: true, 20141cb0ef41Sopenharmony_ci configurable: true 20151cb0ef41Sopenharmony_ci }); 20161cb0ef41Sopenharmony_ci const stack = foo.stack; 20171cb0ef41Sopenharmony_ci foo.stack = 'This is a stack'; 20181cb0ef41Sopenharmony_ci assert.strictEqual( 20191cb0ef41Sopenharmony_ci util.inspect(foo), 20201cb0ef41Sopenharmony_ci '[This is a stack]' 20211cb0ef41Sopenharmony_ci ); 20221cb0ef41Sopenharmony_ci foo.stack = stack; 20231cb0ef41Sopenharmony_ci assert( 20241cb0ef41Sopenharmony_ci util.inspect(foo).startsWith( 20251cb0ef41Sopenharmony_ci `${Class.name} [WOW]${extra}${message ? `: ${message}` : '\n'}`), 20261cb0ef41Sopenharmony_ci util.inspect(foo) 20271cb0ef41Sopenharmony_ci ); 20281cb0ef41Sopenharmony_ci Object.setPrototypeOf(foo, null); 20291cb0ef41Sopenharmony_ci assert( 20301cb0ef41Sopenharmony_ci util.inspect(foo).startsWith( 20311cb0ef41Sopenharmony_ci `[${name}: null prototype] [WOW]${message ? `: ${message}` : '\n'}` 20321cb0ef41Sopenharmony_ci ), 20331cb0ef41Sopenharmony_ci util.inspect(foo) 20341cb0ef41Sopenharmony_ci ); 20351cb0ef41Sopenharmony_ci foo.bar = true; 20361cb0ef41Sopenharmony_ci delete foo[Symbol.toStringTag]; 20371cb0ef41Sopenharmony_ci assert( 20381cb0ef41Sopenharmony_ci util.inspect(foo).startsWith( 20391cb0ef41Sopenharmony_ci `[${name}: null prototype]${message ? `: ${message}` : '\n'}`), 20401cb0ef41Sopenharmony_ci util.inspect(foo) 20411cb0ef41Sopenharmony_ci ); 20421cb0ef41Sopenharmony_ci foo.stack = 'This is a stack'; 20431cb0ef41Sopenharmony_ci assert.strictEqual( 20441cb0ef41Sopenharmony_ci util.inspect(foo), 20451cb0ef41Sopenharmony_ci '[[Error: null prototype]: This is a stack] { bar: true }' 20461cb0ef41Sopenharmony_ci ); 20471cb0ef41Sopenharmony_ci foo.stack = stack.split('\n')[0]; 20481cb0ef41Sopenharmony_ci assert.strictEqual( 20491cb0ef41Sopenharmony_ci util.inspect(foo), 20501cb0ef41Sopenharmony_ci `[[${name}: null prototype]${message ? `: ${message}` : ''}] { bar: true }` 20511cb0ef41Sopenharmony_ci ); 20521cb0ef41Sopenharmony_ci}); 20531cb0ef41Sopenharmony_ci 20541cb0ef41Sopenharmony_ci// Verify that classes are properly inspected. 20551cb0ef41Sopenharmony_ci[ 20561cb0ef41Sopenharmony_ci /* eslint-disable spaced-comment, no-multi-spaces, brace-style */ 20571cb0ef41Sopenharmony_ci // The whitespace is intentional. 20581cb0ef41Sopenharmony_ci [class { }, '[class (anonymous)]'], 20591cb0ef41Sopenharmony_ci [class extends Error { log() {} }, '[class (anonymous) extends Error]'], 20601cb0ef41Sopenharmony_ci [class A { constructor(a) { this.a = a; } log() { return this.a; } }, 20611cb0ef41Sopenharmony_ci '[class A]'], 20621cb0ef41Sopenharmony_ci [class 20631cb0ef41Sopenharmony_ci // Random { // comments /* */ are part of the toString() result 20641cb0ef41Sopenharmony_ci /* eslint-disable-next-line space-before-blocks */ 20651cb0ef41Sopenharmony_ci äß/**/extends/*{*/TypeError{}, '[class äß extends TypeError]'], 20661cb0ef41Sopenharmony_ci /* The whitespace and new line is intended! */ 20671cb0ef41Sopenharmony_ci // Foobar !!! 20681cb0ef41Sopenharmony_ci [class X extends /****/ Error 20691cb0ef41Sopenharmony_ci // More comments 20701cb0ef41Sopenharmony_ci {}, '[class X extends Error]'], 20711cb0ef41Sopenharmony_ci /* eslint-enable spaced-comment, no-multi-spaces, brace-style */ 20721cb0ef41Sopenharmony_ci].forEach(([clazz, string]) => { 20731cb0ef41Sopenharmony_ci const inspected = util.inspect(clazz); 20741cb0ef41Sopenharmony_ci assert.strictEqual(inspected, string); 20751cb0ef41Sopenharmony_ci Object.defineProperty(clazz, Symbol.toStringTag, { 20761cb0ef41Sopenharmony_ci value: 'Woohoo' 20771cb0ef41Sopenharmony_ci }); 20781cb0ef41Sopenharmony_ci const parts = inspected.slice(0, -1).split(' '); 20791cb0ef41Sopenharmony_ci const [, name, ...rest] = parts; 20801cb0ef41Sopenharmony_ci rest.unshift('[Woohoo]'); 20811cb0ef41Sopenharmony_ci if (rest.length) { 20821cb0ef41Sopenharmony_ci rest[rest.length - 1] += ']'; 20831cb0ef41Sopenharmony_ci } 20841cb0ef41Sopenharmony_ci assert.strictEqual( 20851cb0ef41Sopenharmony_ci util.inspect(clazz), 20861cb0ef41Sopenharmony_ci ['[class', name, ...rest].join(' ') 20871cb0ef41Sopenharmony_ci ); 20881cb0ef41Sopenharmony_ci if (rest.length) { 20891cb0ef41Sopenharmony_ci rest[rest.length - 1] = rest[rest.length - 1].slice(0, -1); 20901cb0ef41Sopenharmony_ci rest.length = 1; 20911cb0ef41Sopenharmony_ci } 20921cb0ef41Sopenharmony_ci Object.setPrototypeOf(clazz, Map.prototype); 20931cb0ef41Sopenharmony_ci assert.strictEqual( 20941cb0ef41Sopenharmony_ci util.inspect(clazz), 20951cb0ef41Sopenharmony_ci ['[class', name, '[Map]', ...rest].join(' ') + ']' 20961cb0ef41Sopenharmony_ci ); 20971cb0ef41Sopenharmony_ci Object.setPrototypeOf(clazz, null); 20981cb0ef41Sopenharmony_ci assert.strictEqual( 20991cb0ef41Sopenharmony_ci util.inspect(clazz), 21001cb0ef41Sopenharmony_ci ['[class', name, ...rest, 'extends [null prototype]]'].join(' ') 21011cb0ef41Sopenharmony_ci ); 21021cb0ef41Sopenharmony_ci Object.defineProperty(clazz, 'name', { value: 'Foo' }); 21031cb0ef41Sopenharmony_ci const res = ['[class', 'Foo', ...rest, 'extends [null prototype]]'].join(' '); 21041cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(clazz), res); 21051cb0ef41Sopenharmony_ci clazz.foo = true; 21061cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(clazz), `${res} { foo: true }`); 21071cb0ef41Sopenharmony_ci}); 21081cb0ef41Sopenharmony_ci 21091cb0ef41Sopenharmony_ci// "class" properties should not be detected as "class". 21101cb0ef41Sopenharmony_ci{ 21111cb0ef41Sopenharmony_ci // eslint-disable-next-line space-before-function-paren 21121cb0ef41Sopenharmony_ci let obj = { class () {} }; 21131cb0ef41Sopenharmony_ci assert.strictEqual( 21141cb0ef41Sopenharmony_ci util.inspect(obj), 21151cb0ef41Sopenharmony_ci '{ class: [Function: class] }' 21161cb0ef41Sopenharmony_ci ); 21171cb0ef41Sopenharmony_ci obj = { class: () => {} }; 21181cb0ef41Sopenharmony_ci assert.strictEqual( 21191cb0ef41Sopenharmony_ci util.inspect(obj), 21201cb0ef41Sopenharmony_ci '{ class: [Function: class] }' 21211cb0ef41Sopenharmony_ci ); 21221cb0ef41Sopenharmony_ci obj = { ['class Foo {}']() {} }; 21231cb0ef41Sopenharmony_ci assert.strictEqual( 21241cb0ef41Sopenharmony_ci util.inspect(obj), 21251cb0ef41Sopenharmony_ci "{ 'class Foo {}': [Function: class Foo {}] }" 21261cb0ef41Sopenharmony_ci ); 21271cb0ef41Sopenharmony_ci function Foo() {} 21281cb0ef41Sopenharmony_ci Object.defineProperty(Foo, 'toString', { value: () => 'class Foo {}' }); 21291cb0ef41Sopenharmony_ci assert.strictEqual( 21301cb0ef41Sopenharmony_ci util.inspect(Foo), 21311cb0ef41Sopenharmony_ci '[Function: Foo]' 21321cb0ef41Sopenharmony_ci ); 21331cb0ef41Sopenharmony_ci function fn() {} 21341cb0ef41Sopenharmony_ci Object.defineProperty(fn, 'name', { value: 'class Foo {}' }); 21351cb0ef41Sopenharmony_ci assert.strictEqual( 21361cb0ef41Sopenharmony_ci util.inspect(fn), 21371cb0ef41Sopenharmony_ci '[Function: class Foo {}]' 21381cb0ef41Sopenharmony_ci ); 21391cb0ef41Sopenharmony_ci} 21401cb0ef41Sopenharmony_ci 21411cb0ef41Sopenharmony_ci// Verify that throwing in valueOf and toString still produces nice results. 21421cb0ef41Sopenharmony_ci[ 21431cb0ef41Sopenharmony_ci [new String(55), "[String: '55']"], 21441cb0ef41Sopenharmony_ci [new Boolean(true), '[Boolean: true]'], 21451cb0ef41Sopenharmony_ci [new Number(55), '[Number: 55]'], 21461cb0ef41Sopenharmony_ci [Object(BigInt(55)), '[BigInt: 55n]'], 21471cb0ef41Sopenharmony_ci [Object(Symbol('foo')), '[Symbol: Symbol(foo)]'], 21481cb0ef41Sopenharmony_ci [function() {}, '[Function (anonymous)]'], 21491cb0ef41Sopenharmony_ci [() => {}, '[Function (anonymous)]'], 21501cb0ef41Sopenharmony_ci [[1, 2], '[ 1, 2 ]'], 21511cb0ef41Sopenharmony_ci // eslint-disable-next-line no-sparse-arrays 21521cb0ef41Sopenharmony_ci [[, , 5, , , , ], '[ <2 empty items>, 5, <3 empty items> ]'], 21531cb0ef41Sopenharmony_ci [{ a: 5 }, '{ a: 5 }'], 21541cb0ef41Sopenharmony_ci [new Set([1, 2]), 'Set(2) { 1, 2 }'], 21551cb0ef41Sopenharmony_ci [new Map([[1, 2]]), 'Map(1) { 1 => 2 }'], 21561cb0ef41Sopenharmony_ci [new Set([1, 2]).entries(), '[Set Entries] { [ 1, 1 ], [ 2, 2 ] }'], 21571cb0ef41Sopenharmony_ci [new Map([[1, 2]]).keys(), '[Map Iterator] { 1 }'], 21581cb0ef41Sopenharmony_ci [new Date(2000), '1970-01-01T00:00:02.000Z'], 21591cb0ef41Sopenharmony_ci [new Uint8Array(2), 'Uint8Array(2) [ 0, 0 ]'], 21601cb0ef41Sopenharmony_ci [new Promise((resolve) => setTimeout(resolve, 10)), 'Promise { <pending> }'], 21611cb0ef41Sopenharmony_ci [new WeakSet(), 'WeakSet { <items unknown> }'], 21621cb0ef41Sopenharmony_ci [new WeakMap(), 'WeakMap { <items unknown> }'], 21631cb0ef41Sopenharmony_ci [/foobar/g, '/foobar/g'], 21641cb0ef41Sopenharmony_ci].forEach(([value, expected]) => { 21651cb0ef41Sopenharmony_ci Object.defineProperty(value, 'valueOf', { 21661cb0ef41Sopenharmony_ci get() { 21671cb0ef41Sopenharmony_ci throw new Error('valueOf'); 21681cb0ef41Sopenharmony_ci } 21691cb0ef41Sopenharmony_ci }); 21701cb0ef41Sopenharmony_ci Object.defineProperty(value, 'toString', { 21711cb0ef41Sopenharmony_ci get() { 21721cb0ef41Sopenharmony_ci throw new Error('toString'); 21731cb0ef41Sopenharmony_ci } 21741cb0ef41Sopenharmony_ci }); 21751cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(value), expected); 21761cb0ef41Sopenharmony_ci value.foo = 'bar'; 21771cb0ef41Sopenharmony_ci assert.notStrictEqual(util.inspect(value), expected); 21781cb0ef41Sopenharmony_ci delete value.foo; 21791cb0ef41Sopenharmony_ci value[Symbol('foo')] = 'yeah'; 21801cb0ef41Sopenharmony_ci assert.notStrictEqual(util.inspect(value), expected); 21811cb0ef41Sopenharmony_ci}); 21821cb0ef41Sopenharmony_ci 21831cb0ef41Sopenharmony_ci// Verify that having no prototype still produces nice results. 21841cb0ef41Sopenharmony_ci[ 21851cb0ef41Sopenharmony_ci [[1, 3, 4], '[Array(3): null prototype] [ 1, 3, 4 ]'], 21861cb0ef41Sopenharmony_ci [new Set([1, 2]), '[Set(2): null prototype] { 1, 2 }'], 21871cb0ef41Sopenharmony_ci [new Map([[1, 2]]), '[Map(1): null prototype] { 1 => 2 }'], 21881cb0ef41Sopenharmony_ci [new Promise((resolve) => setTimeout(resolve, 10)), 21891cb0ef41Sopenharmony_ci '[Promise: null prototype] { <pending> }'], 21901cb0ef41Sopenharmony_ci [new WeakSet(), '[WeakSet: null prototype] { <items unknown> }'], 21911cb0ef41Sopenharmony_ci [new WeakMap(), '[WeakMap: null prototype] { <items unknown> }'], 21921cb0ef41Sopenharmony_ci [new Uint8Array(2), '[Uint8Array(2): null prototype] [ 0, 0 ]'], 21931cb0ef41Sopenharmony_ci [new Uint16Array(2), '[Uint16Array(2): null prototype] [ 0, 0 ]'], 21941cb0ef41Sopenharmony_ci [new Uint32Array(2), '[Uint32Array(2): null prototype] [ 0, 0 ]'], 21951cb0ef41Sopenharmony_ci [new Int8Array(2), '[Int8Array(2): null prototype] [ 0, 0 ]'], 21961cb0ef41Sopenharmony_ci [new Int16Array(2), '[Int16Array(2): null prototype] [ 0, 0 ]'], 21971cb0ef41Sopenharmony_ci [new Int32Array(2), '[Int32Array(2): null prototype] [ 0, 0 ]'], 21981cb0ef41Sopenharmony_ci [new Float32Array(2), '[Float32Array(2): null prototype] [ 0, 0 ]'], 21991cb0ef41Sopenharmony_ci [new Float64Array(2), '[Float64Array(2): null prototype] [ 0, 0 ]'], 22001cb0ef41Sopenharmony_ci [new BigInt64Array(2), '[BigInt64Array(2): null prototype] [ 0n, 0n ]'], 22011cb0ef41Sopenharmony_ci [new BigUint64Array(2), '[BigUint64Array(2): null prototype] [ 0n, 0n ]'], 22021cb0ef41Sopenharmony_ci [new ArrayBuffer(16), '[ArrayBuffer: null prototype] {\n' + 22031cb0ef41Sopenharmony_ci ' [Uint8Contents]: <00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00>,\n' + 22041cb0ef41Sopenharmony_ci ' byteLength: undefined\n}'], 22051cb0ef41Sopenharmony_ci [new DataView(new ArrayBuffer(16)), 22061cb0ef41Sopenharmony_ci '[DataView: null prototype] {\n byteLength: undefined,\n ' + 22071cb0ef41Sopenharmony_ci 'byteOffset: undefined,\n buffer: undefined\n}'], 22081cb0ef41Sopenharmony_ci [new SharedArrayBuffer(2), '[SharedArrayBuffer: null prototype] ' + 22091cb0ef41Sopenharmony_ci '{\n [Uint8Contents]: <00 00>,\n byteLength: undefined\n}'], 22101cb0ef41Sopenharmony_ci [/foobar/, '[RegExp: null prototype] /foobar/'], 22111cb0ef41Sopenharmony_ci [new Date('Sun, 14 Feb 2010 11:48:40 GMT'), 22121cb0ef41Sopenharmony_ci '[Date: null prototype] 2010-02-14T11:48:40.000Z'], 22131cb0ef41Sopenharmony_ci].forEach(([value, expected]) => { 22141cb0ef41Sopenharmony_ci assert.strictEqual( 22151cb0ef41Sopenharmony_ci util.inspect(Object.setPrototypeOf(value, null)), 22161cb0ef41Sopenharmony_ci expected 22171cb0ef41Sopenharmony_ci ); 22181cb0ef41Sopenharmony_ci value.foo = 'bar'; 22191cb0ef41Sopenharmony_ci assert.notStrictEqual(util.inspect(value), expected); 22201cb0ef41Sopenharmony_ci delete value.foo; 22211cb0ef41Sopenharmony_ci value[Symbol('foo')] = 'yeah'; 22221cb0ef41Sopenharmony_ci assert.notStrictEqual(util.inspect(value), expected); 22231cb0ef41Sopenharmony_ci}); 22241cb0ef41Sopenharmony_ci 22251cb0ef41Sopenharmony_ci// Verify that subclasses with and without prototype produce nice results. 22261cb0ef41Sopenharmony_ci[ 22271cb0ef41Sopenharmony_ci [RegExp, ['foobar', 'g'], '/foobar/g'], 22281cb0ef41Sopenharmony_ci [WeakSet, [[{}]], '{ <items unknown> }'], 22291cb0ef41Sopenharmony_ci [WeakMap, [[[{}, {}]]], '{ <items unknown> }'], 22301cb0ef41Sopenharmony_ci [BigInt64Array, 22311cb0ef41Sopenharmony_ci [10], 22321cb0ef41Sopenharmony_ci '[\n 0n, 0n, 0n, 0n, 0n,\n 0n, 0n, 0n, 0n, 0n\n]'], 22331cb0ef41Sopenharmony_ci [Date, ['Sun, 14 Feb 2010 11:48:40 GMT'], '2010-02-14T11:48:40.000Z'], 22341cb0ef41Sopenharmony_ci [Date, ['invalid_date'], 'Invalid Date'], 22351cb0ef41Sopenharmony_ci].forEach(([base, input, rawExpected]) => { 22361cb0ef41Sopenharmony_ci class Foo extends base {} 22371cb0ef41Sopenharmony_ci const value = new Foo(...input); 22381cb0ef41Sopenharmony_ci const symbol = value[Symbol.toStringTag]; 22391cb0ef41Sopenharmony_ci const size = base.name.includes('Array') ? `(${input[0]})` : ''; 22401cb0ef41Sopenharmony_ci const expected = `Foo${size} ${symbol ? `[${symbol}] ` : ''}${rawExpected}`; 22411cb0ef41Sopenharmony_ci const expectedWithoutProto = 22421cb0ef41Sopenharmony_ci `[${base.name}${size}: null prototype] ${rawExpected}`; 22431cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(value), expected); 22441cb0ef41Sopenharmony_ci value.foo = 'bar'; 22451cb0ef41Sopenharmony_ci assert.notStrictEqual(util.inspect(value), expected); 22461cb0ef41Sopenharmony_ci delete value.foo; 22471cb0ef41Sopenharmony_ci assert.strictEqual( 22481cb0ef41Sopenharmony_ci util.inspect(Object.setPrototypeOf(value, null)), 22491cb0ef41Sopenharmony_ci expectedWithoutProto 22501cb0ef41Sopenharmony_ci ); 22511cb0ef41Sopenharmony_ci value.foo = 'bar'; 22521cb0ef41Sopenharmony_ci let res = util.inspect(value); 22531cb0ef41Sopenharmony_ci assert.notStrictEqual(res, expectedWithoutProto); 22541cb0ef41Sopenharmony_ci assert.match(res, /foo: 'bar'/); 22551cb0ef41Sopenharmony_ci delete value.foo; 22561cb0ef41Sopenharmony_ci value[Symbol('foo')] = 'yeah'; 22571cb0ef41Sopenharmony_ci res = util.inspect(value); 22581cb0ef41Sopenharmony_ci assert.notStrictEqual(res, expectedWithoutProto); 22591cb0ef41Sopenharmony_ci assert.match(res, /\[Symbol\(foo\)]: 'yeah'/); 22601cb0ef41Sopenharmony_ci}); 22611cb0ef41Sopenharmony_ci 22621cb0ef41Sopenharmony_ciassert.strictEqual(inspect(1n), '1n'); 22631cb0ef41Sopenharmony_ciassert.strictEqual(inspect(Object(-1n)), '[BigInt: -1n]'); 22641cb0ef41Sopenharmony_ciassert.strictEqual(inspect(Object(13n)), '[BigInt: 13n]'); 22651cb0ef41Sopenharmony_ciassert.strictEqual(inspect(new BigInt64Array([0n])), 'BigInt64Array(1) [ 0n ]'); 22661cb0ef41Sopenharmony_ciassert.strictEqual( 22671cb0ef41Sopenharmony_ci inspect(new BigUint64Array([0n])), 'BigUint64Array(1) [ 0n ]'); 22681cb0ef41Sopenharmony_ci 22691cb0ef41Sopenharmony_ci// Verify non-enumerable keys get escaped. 22701cb0ef41Sopenharmony_ci{ 22711cb0ef41Sopenharmony_ci const obj = {}; 22721cb0ef41Sopenharmony_ci Object.defineProperty(obj, 'Non\nenumerable\tkey', { value: true }); 22731cb0ef41Sopenharmony_ci assert.strictEqual( 22741cb0ef41Sopenharmony_ci util.inspect(obj, { showHidden: true }), 22751cb0ef41Sopenharmony_ci '{ [Non\\nenumerable\\tkey]: true }' 22761cb0ef41Sopenharmony_ci ); 22771cb0ef41Sopenharmony_ci} 22781cb0ef41Sopenharmony_ci 22791cb0ef41Sopenharmony_ci// Check for special colors. 22801cb0ef41Sopenharmony_ci{ 22811cb0ef41Sopenharmony_ci const special = inspect.colors[inspect.styles.special]; 22821cb0ef41Sopenharmony_ci const string = inspect.colors[inspect.styles.string]; 22831cb0ef41Sopenharmony_ci 22841cb0ef41Sopenharmony_ci assert.strictEqual( 22851cb0ef41Sopenharmony_ci inspect(new WeakSet(), { colors: true }), 22861cb0ef41Sopenharmony_ci `WeakSet { \u001b[${special[0]}m<items unknown>\u001b[${special[1]}m }` 22871cb0ef41Sopenharmony_ci ); 22881cb0ef41Sopenharmony_ci assert.strictEqual( 22891cb0ef41Sopenharmony_ci inspect(new WeakMap(), { colors: true }), 22901cb0ef41Sopenharmony_ci `WeakMap { \u001b[${special[0]}m<items unknown>\u001b[${special[1]}m }` 22911cb0ef41Sopenharmony_ci ); 22921cb0ef41Sopenharmony_ci assert.strictEqual( 22931cb0ef41Sopenharmony_ci inspect(new Promise(() => {}), { colors: true }), 22941cb0ef41Sopenharmony_ci `Promise { \u001b[${special[0]}m<pending>\u001b[${special[1]}m }` 22951cb0ef41Sopenharmony_ci ); 22961cb0ef41Sopenharmony_ci 22971cb0ef41Sopenharmony_ci const rejection = Promise.reject('Oh no!'); 22981cb0ef41Sopenharmony_ci assert.strictEqual( 22991cb0ef41Sopenharmony_ci inspect(rejection, { colors: true }), 23001cb0ef41Sopenharmony_ci `Promise { \u001b[${special[0]}m<rejected>\u001b[${special[1]}m ` + 23011cb0ef41Sopenharmony_ci `\u001b[${string[0]}m'Oh no!'\u001b[${string[1]}m }` 23021cb0ef41Sopenharmony_ci ); 23031cb0ef41Sopenharmony_ci rejection.catch(() => {}); 23041cb0ef41Sopenharmony_ci 23051cb0ef41Sopenharmony_ci // Verify that aliases do not show up as key while checking `inspect.colors`. 23061cb0ef41Sopenharmony_ci const colors = Object.keys(inspect.colors); 23071cb0ef41Sopenharmony_ci const aliases = Object.getOwnPropertyNames(inspect.colors) 23081cb0ef41Sopenharmony_ci .filter((c) => !colors.includes(c)); 23091cb0ef41Sopenharmony_ci assert(!colors.includes('grey')); 23101cb0ef41Sopenharmony_ci assert(colors.includes('gray')); 23111cb0ef41Sopenharmony_ci // Verify that all aliases are correctly mapped. 23121cb0ef41Sopenharmony_ci for (const alias of aliases) { 23131cb0ef41Sopenharmony_ci assert(Array.isArray(inspect.colors[alias])); 23141cb0ef41Sopenharmony_ci } 23151cb0ef41Sopenharmony_ci // Check consistent naming. 23161cb0ef41Sopenharmony_ci [ 23171cb0ef41Sopenharmony_ci 'black', 23181cb0ef41Sopenharmony_ci 'red', 23191cb0ef41Sopenharmony_ci 'green', 23201cb0ef41Sopenharmony_ci 'yellow', 23211cb0ef41Sopenharmony_ci 'blue', 23221cb0ef41Sopenharmony_ci 'magenta', 23231cb0ef41Sopenharmony_ci 'cyan', 23241cb0ef41Sopenharmony_ci 'white', 23251cb0ef41Sopenharmony_ci ].forEach((color, i) => { 23261cb0ef41Sopenharmony_ci assert.deepStrictEqual(inspect.colors[color], [30 + i, 39]); 23271cb0ef41Sopenharmony_ci assert.deepStrictEqual(inspect.colors[`${color}Bright`], [90 + i, 39]); 23281cb0ef41Sopenharmony_ci const bgColor = `bg${color[0].toUpperCase()}${color.slice(1)}`; 23291cb0ef41Sopenharmony_ci assert.deepStrictEqual(inspect.colors[bgColor], [40 + i, 49]); 23301cb0ef41Sopenharmony_ci assert.deepStrictEqual(inspect.colors[`${bgColor}Bright`], [100 + i, 49]); 23311cb0ef41Sopenharmony_ci }); 23321cb0ef41Sopenharmony_ci 23331cb0ef41Sopenharmony_ci // Unknown colors are handled gracefully: 23341cb0ef41Sopenharmony_ci const stringStyle = inspect.styles.string; 23351cb0ef41Sopenharmony_ci inspect.styles.string = 'UNKNOWN'; 23361cb0ef41Sopenharmony_ci assert.strictEqual(inspect('foobar', { colors: true }), "'foobar'"); 23371cb0ef41Sopenharmony_ci inspect.styles.string = stringStyle; 23381cb0ef41Sopenharmony_ci} 23391cb0ef41Sopenharmony_ci 23401cb0ef41Sopenharmony_ciassert.strictEqual( 23411cb0ef41Sopenharmony_ci inspect([1, 3, 2], { sorted: true }), 23421cb0ef41Sopenharmony_ci inspect([1, 3, 2]) 23431cb0ef41Sopenharmony_ci); 23441cb0ef41Sopenharmony_ciassert.strictEqual( 23451cb0ef41Sopenharmony_ci inspect({ c: 3, a: 1, b: 2 }, { sorted: true }), 23461cb0ef41Sopenharmony_ci '{ a: 1, b: 2, c: 3 }' 23471cb0ef41Sopenharmony_ci); 23481cb0ef41Sopenharmony_ciassert.strictEqual( 23491cb0ef41Sopenharmony_ci inspect( 23501cb0ef41Sopenharmony_ci { a200: 4, a100: 1, a102: 3, a101: 2 }, 23511cb0ef41Sopenharmony_ci { sorted(a, b) { return b.localeCompare(a); } } 23521cb0ef41Sopenharmony_ci ), 23531cb0ef41Sopenharmony_ci '{ a200: 4, a102: 3, a101: 2, a100: 1 }' 23541cb0ef41Sopenharmony_ci); 23551cb0ef41Sopenharmony_ci 23561cb0ef41Sopenharmony_ci// Non-indices array properties are sorted as well. 23571cb0ef41Sopenharmony_ci{ 23581cb0ef41Sopenharmony_ci const arr = [3, 2, 1]; 23591cb0ef41Sopenharmony_ci arr.b = 2; 23601cb0ef41Sopenharmony_ci arr.c = 3; 23611cb0ef41Sopenharmony_ci arr.a = 1; 23621cb0ef41Sopenharmony_ci arr[Symbol('b')] = true; 23631cb0ef41Sopenharmony_ci arr[Symbol('a')] = false; 23641cb0ef41Sopenharmony_ci assert.strictEqual( 23651cb0ef41Sopenharmony_ci inspect(arr, { sorted: true }), 23661cb0ef41Sopenharmony_ci '[ 3, 2, 1, [Symbol(a)]: false, [Symbol(b)]: true, a: 1, b: 2, c: 3 ]' 23671cb0ef41Sopenharmony_ci ); 23681cb0ef41Sopenharmony_ci} 23691cb0ef41Sopenharmony_ci 23701cb0ef41Sopenharmony_ci// Manipulate the prototype in weird ways. 23711cb0ef41Sopenharmony_ci{ 23721cb0ef41Sopenharmony_ci let obj = { a: true }; 23731cb0ef41Sopenharmony_ci let value = (function() { return function() {}; })(); 23741cb0ef41Sopenharmony_ci Object.setPrototypeOf(value, null); 23751cb0ef41Sopenharmony_ci Object.setPrototypeOf(obj, value); 23761cb0ef41Sopenharmony_ci assert.strictEqual( 23771cb0ef41Sopenharmony_ci util.inspect(obj), 23781cb0ef41Sopenharmony_ci 'Object <[Function (null prototype) (anonymous)]> { a: true }' 23791cb0ef41Sopenharmony_ci ); 23801cb0ef41Sopenharmony_ci assert.strictEqual( 23811cb0ef41Sopenharmony_ci util.inspect(obj, { colors: true }), 23821cb0ef41Sopenharmony_ci 'Object <\u001b[36m[Function (null prototype) (anonymous)]\u001b[39m> ' + 23831cb0ef41Sopenharmony_ci '{ a: \u001b[33mtrue\u001b[39m }' 23841cb0ef41Sopenharmony_ci ); 23851cb0ef41Sopenharmony_ci 23861cb0ef41Sopenharmony_ci obj = { a: true }; 23871cb0ef41Sopenharmony_ci value = []; 23881cb0ef41Sopenharmony_ci Object.setPrototypeOf(value, null); 23891cb0ef41Sopenharmony_ci Object.setPrototypeOf(obj, value); 23901cb0ef41Sopenharmony_ci assert.strictEqual( 23911cb0ef41Sopenharmony_ci util.inspect(obj), 23921cb0ef41Sopenharmony_ci 'Object <[Array(0): null prototype] []> { a: true }' 23931cb0ef41Sopenharmony_ci ); 23941cb0ef41Sopenharmony_ci 23951cb0ef41Sopenharmony_ci function StorageObject() {} 23961cb0ef41Sopenharmony_ci StorageObject.prototype = Object.create(null); 23971cb0ef41Sopenharmony_ci assert.strictEqual( 23981cb0ef41Sopenharmony_ci util.inspect(new StorageObject()), 23991cb0ef41Sopenharmony_ci 'StorageObject <[Object: null prototype] {}> {}' 24001cb0ef41Sopenharmony_ci ); 24011cb0ef41Sopenharmony_ci 24021cb0ef41Sopenharmony_ci obj = [1, 2, 3]; 24031cb0ef41Sopenharmony_ci Object.setPrototypeOf(obj, Number.prototype); 24041cb0ef41Sopenharmony_ci assert.strictEqual(inspect(obj), "Number { '0': 1, '1': 2, '2': 3 }"); 24051cb0ef41Sopenharmony_ci 24061cb0ef41Sopenharmony_ci Object.setPrototypeOf(obj, Object.create(null)); 24071cb0ef41Sopenharmony_ci assert.strictEqual( 24081cb0ef41Sopenharmony_ci inspect(obj), 24091cb0ef41Sopenharmony_ci "Array <[Object: null prototype] {}> { '0': 1, '1': 2, '2': 3 }" 24101cb0ef41Sopenharmony_ci ); 24111cb0ef41Sopenharmony_ci 24121cb0ef41Sopenharmony_ci StorageObject.prototype = Object.create(null); 24131cb0ef41Sopenharmony_ci Object.setPrototypeOf(StorageObject.prototype, Object.create(null)); 24141cb0ef41Sopenharmony_ci Object.setPrototypeOf( 24151cb0ef41Sopenharmony_ci Object.getPrototypeOf(StorageObject.prototype), 24161cb0ef41Sopenharmony_ci Object.create(null) 24171cb0ef41Sopenharmony_ci ); 24181cb0ef41Sopenharmony_ci assert.strictEqual( 24191cb0ef41Sopenharmony_ci util.inspect(new StorageObject()), 24201cb0ef41Sopenharmony_ci 'StorageObject <Object <Object <[Object: null prototype] {}>>> {}' 24211cb0ef41Sopenharmony_ci ); 24221cb0ef41Sopenharmony_ci assert.strictEqual( 24231cb0ef41Sopenharmony_ci util.inspect(new StorageObject(), { depth: 1 }), 24241cb0ef41Sopenharmony_ci 'StorageObject <Object <Object <Complex prototype>>> {}' 24251cb0ef41Sopenharmony_ci ); 24261cb0ef41Sopenharmony_ci} 24271cb0ef41Sopenharmony_ci 24281cb0ef41Sopenharmony_ci// Check that the fallback always works. 24291cb0ef41Sopenharmony_ci{ 24301cb0ef41Sopenharmony_ci const obj = new Set([1, 2]); 24311cb0ef41Sopenharmony_ci const iterator = obj[Symbol.iterator]; 24321cb0ef41Sopenharmony_ci Object.setPrototypeOf(obj, null); 24331cb0ef41Sopenharmony_ci Object.defineProperty(obj, Symbol.iterator, { 24341cb0ef41Sopenharmony_ci value: iterator, 24351cb0ef41Sopenharmony_ci configurable: true 24361cb0ef41Sopenharmony_ci }); 24371cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(obj), '[Set(2): null prototype] { 1, 2 }'); 24381cb0ef41Sopenharmony_ci Object.defineProperty(obj, Symbol.iterator, { 24391cb0ef41Sopenharmony_ci value: true, 24401cb0ef41Sopenharmony_ci configurable: true 24411cb0ef41Sopenharmony_ci }); 24421cb0ef41Sopenharmony_ci Object.defineProperty(obj, 'size', { 24431cb0ef41Sopenharmony_ci value: NaN, 24441cb0ef41Sopenharmony_ci configurable: true, 24451cb0ef41Sopenharmony_ci enumerable: true 24461cb0ef41Sopenharmony_ci }); 24471cb0ef41Sopenharmony_ci assert.strictEqual( 24481cb0ef41Sopenharmony_ci util.inspect(obj), 24491cb0ef41Sopenharmony_ci '[Set(2): null prototype] { 1, 2, size: NaN }' 24501cb0ef41Sopenharmony_ci ); 24511cb0ef41Sopenharmony_ci} 24521cb0ef41Sopenharmony_ci 24531cb0ef41Sopenharmony_ci// Check the getter option. 24541cb0ef41Sopenharmony_ci{ 24551cb0ef41Sopenharmony_ci let foo = 1; 24561cb0ef41Sopenharmony_ci const get = { get foo() { return foo; } }; 24571cb0ef41Sopenharmony_ci const getset = { 24581cb0ef41Sopenharmony_ci get foo() { return foo; }, 24591cb0ef41Sopenharmony_ci set foo(val) { foo = val; }, 24601cb0ef41Sopenharmony_ci get inc() { return ++foo; } 24611cb0ef41Sopenharmony_ci }; 24621cb0ef41Sopenharmony_ci const thrower = { get foo() { throw new Error('Oops'); } }; 24631cb0ef41Sopenharmony_ci assert.strictEqual( 24641cb0ef41Sopenharmony_ci inspect(get, { getters: true, colors: true }), 24651cb0ef41Sopenharmony_ci '{ foo: \u001b[36m[Getter:\u001b[39m ' + 24661cb0ef41Sopenharmony_ci '\u001b[33m1\u001b[39m\u001b[36m]\u001b[39m }'); 24671cb0ef41Sopenharmony_ci assert.strictEqual( 24681cb0ef41Sopenharmony_ci inspect(thrower, { getters: true }), 24691cb0ef41Sopenharmony_ci '{ foo: [Getter: <Inspection threw (Oops)>] }'); 24701cb0ef41Sopenharmony_ci assert.strictEqual( 24711cb0ef41Sopenharmony_ci inspect(getset, { getters: true }), 24721cb0ef41Sopenharmony_ci '{ foo: [Getter/Setter: 1], inc: [Getter: 2] }'); 24731cb0ef41Sopenharmony_ci assert.strictEqual( 24741cb0ef41Sopenharmony_ci inspect(getset, { getters: 'get' }), 24751cb0ef41Sopenharmony_ci '{ foo: [Getter/Setter], inc: [Getter: 3] }'); 24761cb0ef41Sopenharmony_ci assert.strictEqual( 24771cb0ef41Sopenharmony_ci inspect(getset, { getters: 'set' }), 24781cb0ef41Sopenharmony_ci '{ foo: [Getter/Setter: 3], inc: [Getter] }'); 24791cb0ef41Sopenharmony_ci getset.foo = new Set([[{ a: true }, 2, {}], 'foobar', { x: 1 }]); 24801cb0ef41Sopenharmony_ci assert.strictEqual( 24811cb0ef41Sopenharmony_ci inspect(getset, { getters: true }), 24821cb0ef41Sopenharmony_ci '{\n foo: [Getter/Setter] Set(3) { [ [Object], 2, {} ], ' + 24831cb0ef41Sopenharmony_ci "'foobar', { x: 1 } },\n inc: [Getter: NaN]\n}"); 24841cb0ef41Sopenharmony_ci} 24851cb0ef41Sopenharmony_ci 24861cb0ef41Sopenharmony_ci// Check compact number mode. 24871cb0ef41Sopenharmony_ci{ 24881cb0ef41Sopenharmony_ci let obj = { 24891cb0ef41Sopenharmony_ci a: { 24901cb0ef41Sopenharmony_ci b: { 24911cb0ef41Sopenharmony_ci x: 5, 24921cb0ef41Sopenharmony_ci c: { 24931cb0ef41Sopenharmony_ci x: '10000000000000000 00000000000000000 '.repeat(1e1), 24941cb0ef41Sopenharmony_ci d: 2, 24951cb0ef41Sopenharmony_ci e: 3 24961cb0ef41Sopenharmony_ci } 24971cb0ef41Sopenharmony_ci } 24981cb0ef41Sopenharmony_ci }, 24991cb0ef41Sopenharmony_ci b: [ 25001cb0ef41Sopenharmony_ci 1, 25011cb0ef41Sopenharmony_ci 2, 25021cb0ef41Sopenharmony_ci [ 1, 2, { a: 1, b: 2, c: 3 } ], 25031cb0ef41Sopenharmony_ci ], 25041cb0ef41Sopenharmony_ci c: ['foo', 4, 444444], 25051cb0ef41Sopenharmony_ci d: Array.from({ length: 101 }).map((e, i) => { 25061cb0ef41Sopenharmony_ci return i % 2 === 0 ? i * i : i; 25071cb0ef41Sopenharmony_ci }), 25081cb0ef41Sopenharmony_ci e: Array(6).fill('foobar'), 25091cb0ef41Sopenharmony_ci f: Array(9).fill('foobar'), 25101cb0ef41Sopenharmony_ci g: Array(21).fill('foobar baz'), 25111cb0ef41Sopenharmony_ci h: [100].concat(Array.from({ length: 9 }).map((e, n) => (n))), 25121cb0ef41Sopenharmony_ci long: Array(9).fill('This text is too long for grouping!') 25131cb0ef41Sopenharmony_ci }; 25141cb0ef41Sopenharmony_ci 25151cb0ef41Sopenharmony_ci let out = util.inspect(obj, { compact: 3, depth: 10, breakLength: 60 }); 25161cb0ef41Sopenharmony_ci let expected = [ 25171cb0ef41Sopenharmony_ci '{', 25181cb0ef41Sopenharmony_ci ' a: {', 25191cb0ef41Sopenharmony_ci ' b: {', 25201cb0ef41Sopenharmony_ci ' x: 5,', 25211cb0ef41Sopenharmony_ci ' c: {', 25221cb0ef41Sopenharmony_ci " x: '10000000000000000 00000000000000000 10000000000000000 " + 25231cb0ef41Sopenharmony_ci '00000000000000000 10000000000000000 00000000000000000 ' + 25241cb0ef41Sopenharmony_ci '10000000000000000 00000000000000000 10000000000000000 ' + 25251cb0ef41Sopenharmony_ci '00000000000000000 10000000000000000 00000000000000000 ' + 25261cb0ef41Sopenharmony_ci '10000000000000000 00000000000000000 10000000000000000 ' + 25271cb0ef41Sopenharmony_ci '00000000000000000 10000000000000000 00000000000000000 ' + 25281cb0ef41Sopenharmony_ci "10000000000000000 00000000000000000 ',", 25291cb0ef41Sopenharmony_ci ' d: 2,', 25301cb0ef41Sopenharmony_ci ' e: 3', 25311cb0ef41Sopenharmony_ci ' }', 25321cb0ef41Sopenharmony_ci ' }', 25331cb0ef41Sopenharmony_ci ' },', 25341cb0ef41Sopenharmony_ci ' b: [ 1, 2, [ 1, 2, { a: 1, b: 2, c: 3 } ] ],', 25351cb0ef41Sopenharmony_ci " c: [ 'foo', 4, 444444 ],", 25361cb0ef41Sopenharmony_ci ' d: [', 25371cb0ef41Sopenharmony_ci ' 0, 1, 4, 3, 16, 5, 36, 7, 64,', 25381cb0ef41Sopenharmony_ci ' 9, 100, 11, 144, 13, 196, 15, 256, 17,', 25391cb0ef41Sopenharmony_ci ' 324, 19, 400, 21, 484, 23, 576, 25, 676,', 25401cb0ef41Sopenharmony_ci ' 27, 784, 29, 900, 31, 1024, 33, 1156, 35,', 25411cb0ef41Sopenharmony_ci ' 1296, 37, 1444, 39, 1600, 41, 1764, 43, 1936,', 25421cb0ef41Sopenharmony_ci ' 45, 2116, 47, 2304, 49, 2500, 51, 2704, 53,', 25431cb0ef41Sopenharmony_ci ' 2916, 55, 3136, 57, 3364, 59, 3600, 61, 3844,', 25441cb0ef41Sopenharmony_ci ' 63, 4096, 65, 4356, 67, 4624, 69, 4900, 71,', 25451cb0ef41Sopenharmony_ci ' 5184, 73, 5476, 75, 5776, 77, 6084, 79, 6400,', 25461cb0ef41Sopenharmony_ci ' 81, 6724, 83, 7056, 85, 7396, 87, 7744, 89,', 25471cb0ef41Sopenharmony_ci ' 8100, 91, 8464, 93, 8836, 95, 9216, 97, 9604,', 25481cb0ef41Sopenharmony_ci ' 99,', 25491cb0ef41Sopenharmony_ci ' ... 1 more item', 25501cb0ef41Sopenharmony_ci ' ],', 25511cb0ef41Sopenharmony_ci ' e: [', 25521cb0ef41Sopenharmony_ci " 'foobar',", 25531cb0ef41Sopenharmony_ci " 'foobar',", 25541cb0ef41Sopenharmony_ci " 'foobar',", 25551cb0ef41Sopenharmony_ci " 'foobar',", 25561cb0ef41Sopenharmony_ci " 'foobar',", 25571cb0ef41Sopenharmony_ci " 'foobar'", 25581cb0ef41Sopenharmony_ci ' ],', 25591cb0ef41Sopenharmony_ci ' f: [', 25601cb0ef41Sopenharmony_ci " 'foobar', 'foobar',", 25611cb0ef41Sopenharmony_ci " 'foobar', 'foobar',", 25621cb0ef41Sopenharmony_ci " 'foobar', 'foobar',", 25631cb0ef41Sopenharmony_ci " 'foobar', 'foobar',", 25641cb0ef41Sopenharmony_ci " 'foobar'", 25651cb0ef41Sopenharmony_ci ' ],', 25661cb0ef41Sopenharmony_ci ' g: [', 25671cb0ef41Sopenharmony_ci " 'foobar baz', 'foobar baz',", 25681cb0ef41Sopenharmony_ci " 'foobar baz', 'foobar baz',", 25691cb0ef41Sopenharmony_ci " 'foobar baz', 'foobar baz',", 25701cb0ef41Sopenharmony_ci " 'foobar baz', 'foobar baz',", 25711cb0ef41Sopenharmony_ci " 'foobar baz', 'foobar baz',", 25721cb0ef41Sopenharmony_ci " 'foobar baz', 'foobar baz',", 25731cb0ef41Sopenharmony_ci " 'foobar baz', 'foobar baz',", 25741cb0ef41Sopenharmony_ci " 'foobar baz', 'foobar baz',", 25751cb0ef41Sopenharmony_ci " 'foobar baz', 'foobar baz',", 25761cb0ef41Sopenharmony_ci " 'foobar baz', 'foobar baz',", 25771cb0ef41Sopenharmony_ci " 'foobar baz'", 25781cb0ef41Sopenharmony_ci ' ],', 25791cb0ef41Sopenharmony_ci ' h: [', 25801cb0ef41Sopenharmony_ci ' 100, 0, 1, 2, 3,', 25811cb0ef41Sopenharmony_ci ' 4, 5, 6, 7, 8', 25821cb0ef41Sopenharmony_ci ' ],', 25831cb0ef41Sopenharmony_ci ' long: [', 25841cb0ef41Sopenharmony_ci " 'This text is too long for grouping!',", 25851cb0ef41Sopenharmony_ci " 'This text is too long for grouping!',", 25861cb0ef41Sopenharmony_ci " 'This text is too long for grouping!',", 25871cb0ef41Sopenharmony_ci " 'This text is too long for grouping!',", 25881cb0ef41Sopenharmony_ci " 'This text is too long for grouping!',", 25891cb0ef41Sopenharmony_ci " 'This text is too long for grouping!',", 25901cb0ef41Sopenharmony_ci " 'This text is too long for grouping!',", 25911cb0ef41Sopenharmony_ci " 'This text is too long for grouping!',", 25921cb0ef41Sopenharmony_ci " 'This text is too long for grouping!'", 25931cb0ef41Sopenharmony_ci ' ]', 25941cb0ef41Sopenharmony_ci '}', 25951cb0ef41Sopenharmony_ci ].join('\n'); 25961cb0ef41Sopenharmony_ci 25971cb0ef41Sopenharmony_ci assert.strictEqual(out, expected); 25981cb0ef41Sopenharmony_ci 25991cb0ef41Sopenharmony_ci obj = [ 26001cb0ef41Sopenharmony_ci 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 26011cb0ef41Sopenharmony_ci 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 26021cb0ef41Sopenharmony_ci 1, 1, 1, 1, 1, 1, 123456789, 26031cb0ef41Sopenharmony_ci ]; 26041cb0ef41Sopenharmony_ci 26051cb0ef41Sopenharmony_ci out = util.inspect(obj, { compact: 3 }); 26061cb0ef41Sopenharmony_ci 26071cb0ef41Sopenharmony_ci expected = [ 26081cb0ef41Sopenharmony_ci '[', 26091cb0ef41Sopenharmony_ci ' 1, 1, 1, 1,', 26101cb0ef41Sopenharmony_ci ' 1, 1, 1, 1,', 26111cb0ef41Sopenharmony_ci ' 1, 1, 1, 1,', 26121cb0ef41Sopenharmony_ci ' 1, 1, 1, 1,', 26131cb0ef41Sopenharmony_ci ' 1, 1, 1, 1,', 26141cb0ef41Sopenharmony_ci ' 1, 1, 1, 1,', 26151cb0ef41Sopenharmony_ci ' 1, 1, 123456789', 26161cb0ef41Sopenharmony_ci ']', 26171cb0ef41Sopenharmony_ci ].join('\n'); 26181cb0ef41Sopenharmony_ci 26191cb0ef41Sopenharmony_ci assert.strictEqual(out, expected); 26201cb0ef41Sopenharmony_ci 26211cb0ef41Sopenharmony_ci // Unicode support. あ has a length of one and a width of two. 26221cb0ef41Sopenharmony_ci obj = [ 26231cb0ef41Sopenharmony_ci '123', '123', '123', '123', 'あああ', 26241cb0ef41Sopenharmony_ci '123', '123', '123', '123', 'あああ', 26251cb0ef41Sopenharmony_ci ]; 26261cb0ef41Sopenharmony_ci 26271cb0ef41Sopenharmony_ci out = util.inspect(obj, { compact: 3 }); 26281cb0ef41Sopenharmony_ci 26291cb0ef41Sopenharmony_ci expected = [ 26301cb0ef41Sopenharmony_ci '[', 26311cb0ef41Sopenharmony_ci " '123', '123',", 26321cb0ef41Sopenharmony_ci " '123', '123',", 26331cb0ef41Sopenharmony_ci " 'あああ', '123',", 26341cb0ef41Sopenharmony_ci " '123', '123',", 26351cb0ef41Sopenharmony_ci " '123', 'あああ'", 26361cb0ef41Sopenharmony_ci ']', 26371cb0ef41Sopenharmony_ci ].join('\n'); 26381cb0ef41Sopenharmony_ci 26391cb0ef41Sopenharmony_ci assert.strictEqual(out, expected); 26401cb0ef41Sopenharmony_ci 26411cb0ef41Sopenharmony_ci // Array grouping should prevent lining up outer elements on a single line. 26421cb0ef41Sopenharmony_ci obj = [[[1, 2, 3, 4, 5, 6, 7, 8, 9]]]; 26431cb0ef41Sopenharmony_ci 26441cb0ef41Sopenharmony_ci out = util.inspect(obj, { compact: 3 }); 26451cb0ef41Sopenharmony_ci 26461cb0ef41Sopenharmony_ci expected = [ 26471cb0ef41Sopenharmony_ci '[', 26481cb0ef41Sopenharmony_ci ' [', 26491cb0ef41Sopenharmony_ci ' [', 26501cb0ef41Sopenharmony_ci ' 1, 2, 3, 4, 5,', 26511cb0ef41Sopenharmony_ci ' 6, 7, 8, 9', 26521cb0ef41Sopenharmony_ci ' ]', 26531cb0ef41Sopenharmony_ci ' ]', 26541cb0ef41Sopenharmony_ci ']', 26551cb0ef41Sopenharmony_ci ].join('\n'); 26561cb0ef41Sopenharmony_ci 26571cb0ef41Sopenharmony_ci assert.strictEqual(out, expected); 26581cb0ef41Sopenharmony_ci 26591cb0ef41Sopenharmony_ci // Verify that array grouping and line consolidation does not happen together. 26601cb0ef41Sopenharmony_ci obj = { 26611cb0ef41Sopenharmony_ci a: { 26621cb0ef41Sopenharmony_ci b: { 26631cb0ef41Sopenharmony_ci x: 5, 26641cb0ef41Sopenharmony_ci c: { 26651cb0ef41Sopenharmony_ci d: 2, 26661cb0ef41Sopenharmony_ci e: 3 26671cb0ef41Sopenharmony_ci } 26681cb0ef41Sopenharmony_ci } 26691cb0ef41Sopenharmony_ci }, 26701cb0ef41Sopenharmony_ci b: Array.from({ length: 9 }).map((e, n) => { 26711cb0ef41Sopenharmony_ci return n % 2 === 0 ? 'foobar' : 'baz'; 26721cb0ef41Sopenharmony_ci }) 26731cb0ef41Sopenharmony_ci }; 26741cb0ef41Sopenharmony_ci 26751cb0ef41Sopenharmony_ci out = util.inspect(obj, { compact: 1, breakLength: Infinity, colors: true }); 26761cb0ef41Sopenharmony_ci 26771cb0ef41Sopenharmony_ci expected = [ 26781cb0ef41Sopenharmony_ci '{', 26791cb0ef41Sopenharmony_ci ' a: {', 26801cb0ef41Sopenharmony_ci ' b: { x: \u001b[33m5\u001b[39m, c: \u001b[36m[Object]\u001b[39m }', 26811cb0ef41Sopenharmony_ci ' },', 26821cb0ef41Sopenharmony_ci ' b: [', 26831cb0ef41Sopenharmony_ci " \u001b[32m'foobar'\u001b[39m, \u001b[32m'baz'\u001b[39m,", 26841cb0ef41Sopenharmony_ci " \u001b[32m'foobar'\u001b[39m, \u001b[32m'baz'\u001b[39m,", 26851cb0ef41Sopenharmony_ci " \u001b[32m'foobar'\u001b[39m, \u001b[32m'baz'\u001b[39m,", 26861cb0ef41Sopenharmony_ci " \u001b[32m'foobar'\u001b[39m, \u001b[32m'baz'\u001b[39m,", 26871cb0ef41Sopenharmony_ci " \u001b[32m'foobar'\u001b[39m", 26881cb0ef41Sopenharmony_ci ' ]', 26891cb0ef41Sopenharmony_ci '}', 26901cb0ef41Sopenharmony_ci ].join('\n'); 26911cb0ef41Sopenharmony_ci 26921cb0ef41Sopenharmony_ci assert.strictEqual(out, expected); 26931cb0ef41Sopenharmony_ci 26941cb0ef41Sopenharmony_ci obj = Array.from({ length: 60 }).map((e, i) => i); 26951cb0ef41Sopenharmony_ci out = util.inspect(obj, { compact: 1, breakLength: Infinity, colors: true }); 26961cb0ef41Sopenharmony_ci 26971cb0ef41Sopenharmony_ci expected = [ 26981cb0ef41Sopenharmony_ci '[', 26991cb0ef41Sopenharmony_ci ' \u001b[33m0\u001b[39m, \u001b[33m1\u001b[39m, \u001b[33m2\u001b[39m, \u001b[33m3\u001b[39m,', 27001cb0ef41Sopenharmony_ci ' \u001b[33m4\u001b[39m, \u001b[33m5\u001b[39m, \u001b[33m6\u001b[39m, \u001b[33m7\u001b[39m,', 27011cb0ef41Sopenharmony_ci ' \u001b[33m8\u001b[39m, \u001b[33m9\u001b[39m, \u001b[33m10\u001b[39m, \u001b[33m11\u001b[39m,', 27021cb0ef41Sopenharmony_ci ' \u001b[33m12\u001b[39m, \u001b[33m13\u001b[39m, \u001b[33m14\u001b[39m, \u001b[33m15\u001b[39m,', 27031cb0ef41Sopenharmony_ci ' \u001b[33m16\u001b[39m, \u001b[33m17\u001b[39m, \u001b[33m18\u001b[39m, \u001b[33m19\u001b[39m,', 27041cb0ef41Sopenharmony_ci ' \u001b[33m20\u001b[39m, \u001b[33m21\u001b[39m, \u001b[33m22\u001b[39m, \u001b[33m23\u001b[39m,', 27051cb0ef41Sopenharmony_ci ' \u001b[33m24\u001b[39m, \u001b[33m25\u001b[39m, \u001b[33m26\u001b[39m, \u001b[33m27\u001b[39m,', 27061cb0ef41Sopenharmony_ci ' \u001b[33m28\u001b[39m, \u001b[33m29\u001b[39m, \u001b[33m30\u001b[39m, \u001b[33m31\u001b[39m,', 27071cb0ef41Sopenharmony_ci ' \u001b[33m32\u001b[39m, \u001b[33m33\u001b[39m, \u001b[33m34\u001b[39m, \u001b[33m35\u001b[39m,', 27081cb0ef41Sopenharmony_ci ' \u001b[33m36\u001b[39m, \u001b[33m37\u001b[39m, \u001b[33m38\u001b[39m, \u001b[33m39\u001b[39m,', 27091cb0ef41Sopenharmony_ci ' \u001b[33m40\u001b[39m, \u001b[33m41\u001b[39m, \u001b[33m42\u001b[39m, \u001b[33m43\u001b[39m,', 27101cb0ef41Sopenharmony_ci ' \u001b[33m44\u001b[39m, \u001b[33m45\u001b[39m, \u001b[33m46\u001b[39m, \u001b[33m47\u001b[39m,', 27111cb0ef41Sopenharmony_ci ' \u001b[33m48\u001b[39m, \u001b[33m49\u001b[39m, \u001b[33m50\u001b[39m, \u001b[33m51\u001b[39m,', 27121cb0ef41Sopenharmony_ci ' \u001b[33m52\u001b[39m, \u001b[33m53\u001b[39m, \u001b[33m54\u001b[39m, \u001b[33m55\u001b[39m,', 27131cb0ef41Sopenharmony_ci ' \u001b[33m56\u001b[39m, \u001b[33m57\u001b[39m, \u001b[33m58\u001b[39m, \u001b[33m59\u001b[39m', 27141cb0ef41Sopenharmony_ci ']', 27151cb0ef41Sopenharmony_ci ].join('\n'); 27161cb0ef41Sopenharmony_ci 27171cb0ef41Sopenharmony_ci assert.strictEqual(out, expected); 27181cb0ef41Sopenharmony_ci 27191cb0ef41Sopenharmony_ci out = util.inspect([1, 2, 3, 4], { compact: 1, colors: true }); 27201cb0ef41Sopenharmony_ci expected = '[ \u001b[33m1\u001b[39m, \u001b[33m2\u001b[39m, ' + 27211cb0ef41Sopenharmony_ci '\u001b[33m3\u001b[39m, \u001b[33m4\u001b[39m ]'; 27221cb0ef41Sopenharmony_ci 27231cb0ef41Sopenharmony_ci assert.strictEqual(out, expected); 27241cb0ef41Sopenharmony_ci 27251cb0ef41Sopenharmony_ci obj = [ 27261cb0ef41Sopenharmony_ci 'Object', 'Function', 'Array', 27271cb0ef41Sopenharmony_ci 'Number', 'parseFloat', 'parseInt', 27281cb0ef41Sopenharmony_ci 'Infinity', 'NaN', 'undefined', 27291cb0ef41Sopenharmony_ci 'Boolean', 'String', 'Symbol', 27301cb0ef41Sopenharmony_ci 'Date', 'Promise', 'RegExp', 27311cb0ef41Sopenharmony_ci 'Error', 'EvalError', 'RangeError', 27321cb0ef41Sopenharmony_ci 'ReferenceError', 'SyntaxError', 'TypeError', 27331cb0ef41Sopenharmony_ci 'URIError', 'JSON', 'Math', 27341cb0ef41Sopenharmony_ci 'console', 'Intl', 'ArrayBuffer', 27351cb0ef41Sopenharmony_ci 'Uint8Array', 'Int8Array', 'Uint16Array', 27361cb0ef41Sopenharmony_ci 'Int16Array', 'Uint32Array', 'Int32Array', 27371cb0ef41Sopenharmony_ci 'Float32Array', 'Float64Array', 'Uint8ClampedArray', 27381cb0ef41Sopenharmony_ci 'BigUint64Array', 'BigInt64Array', 'DataView', 27391cb0ef41Sopenharmony_ci 'Map', 'BigInt', 'Set', 27401cb0ef41Sopenharmony_ci 'WeakMap', 'WeakSet', 'Proxy', 27411cb0ef41Sopenharmony_ci 'Reflect', 'decodeURI', 'decodeURIComponent', 27421cb0ef41Sopenharmony_ci 'encodeURI', 'encodeURIComponent', 'escape', 27431cb0ef41Sopenharmony_ci 'unescape', 'eval', 'isFinite', 27441cb0ef41Sopenharmony_ci 'isNaN', 'SharedArrayBuffer', 'Atomics', 27451cb0ef41Sopenharmony_ci 'globalThis', 'WebAssembly', 'global', 27461cb0ef41Sopenharmony_ci 'process', 'Buffer', 'URL', 27471cb0ef41Sopenharmony_ci 'URLSearchParams', 'TextEncoder', 'TextDecoder', 27481cb0ef41Sopenharmony_ci 'clearInterval', 'clearTimeout', 'setInterval', 27491cb0ef41Sopenharmony_ci 'setTimeout', 'queueMicrotask', 'clearImmediate', 27501cb0ef41Sopenharmony_ci 'setImmediate', 'module', 'require', 27511cb0ef41Sopenharmony_ci 'assert', 'async_hooks', 'buffer', 27521cb0ef41Sopenharmony_ci 'child_process', 'cluster', 'crypto', 27531cb0ef41Sopenharmony_ci 'dgram', 'dns', 'domain', 27541cb0ef41Sopenharmony_ci 'events', 'fs', 'http', 27551cb0ef41Sopenharmony_ci 'http2', 'https', 'inspector', 27561cb0ef41Sopenharmony_ci 'net', 'os', 'path', 27571cb0ef41Sopenharmony_ci 'perf_hooks', 'punycode', 'querystring', 27581cb0ef41Sopenharmony_ci 'readline', 'repl', 'stream', 27591cb0ef41Sopenharmony_ci 'string_decoder', 'tls', 'trace_events', 27601cb0ef41Sopenharmony_ci 'tty', 'url', 'v8', 27611cb0ef41Sopenharmony_ci 'vm', 'worker_threads', 'zlib', 27621cb0ef41Sopenharmony_ci '_', '_error', 'util', 27631cb0ef41Sopenharmony_ci ]; 27641cb0ef41Sopenharmony_ci 27651cb0ef41Sopenharmony_ci out = util.inspect( 27661cb0ef41Sopenharmony_ci obj, 27671cb0ef41Sopenharmony_ci { compact: 3, breakLength: 80, maxArrayLength: 250 } 27681cb0ef41Sopenharmony_ci ); 27691cb0ef41Sopenharmony_ci expected = [ 27701cb0ef41Sopenharmony_ci '[', 27711cb0ef41Sopenharmony_ci " 'Object', 'Function', 'Array',", 27721cb0ef41Sopenharmony_ci " 'Number', 'parseFloat', 'parseInt',", 27731cb0ef41Sopenharmony_ci " 'Infinity', 'NaN', 'undefined',", 27741cb0ef41Sopenharmony_ci " 'Boolean', 'String', 'Symbol',", 27751cb0ef41Sopenharmony_ci " 'Date', 'Promise', 'RegExp',", 27761cb0ef41Sopenharmony_ci " 'Error', 'EvalError', 'RangeError',", 27771cb0ef41Sopenharmony_ci " 'ReferenceError', 'SyntaxError', 'TypeError',", 27781cb0ef41Sopenharmony_ci " 'URIError', 'JSON', 'Math',", 27791cb0ef41Sopenharmony_ci " 'console', 'Intl', 'ArrayBuffer',", 27801cb0ef41Sopenharmony_ci " 'Uint8Array', 'Int8Array', 'Uint16Array',", 27811cb0ef41Sopenharmony_ci " 'Int16Array', 'Uint32Array', 'Int32Array',", 27821cb0ef41Sopenharmony_ci " 'Float32Array', 'Float64Array', 'Uint8ClampedArray',", 27831cb0ef41Sopenharmony_ci " 'BigUint64Array', 'BigInt64Array', 'DataView',", 27841cb0ef41Sopenharmony_ci " 'Map', 'BigInt', 'Set',", 27851cb0ef41Sopenharmony_ci " 'WeakMap', 'WeakSet', 'Proxy',", 27861cb0ef41Sopenharmony_ci " 'Reflect', 'decodeURI', 'decodeURIComponent',", 27871cb0ef41Sopenharmony_ci " 'encodeURI', 'encodeURIComponent', 'escape',", 27881cb0ef41Sopenharmony_ci " 'unescape', 'eval', 'isFinite',", 27891cb0ef41Sopenharmony_ci " 'isNaN', 'SharedArrayBuffer', 'Atomics',", 27901cb0ef41Sopenharmony_ci " 'globalThis', 'WebAssembly', 'global',", 27911cb0ef41Sopenharmony_ci " 'process', 'Buffer', 'URL',", 27921cb0ef41Sopenharmony_ci " 'URLSearchParams', 'TextEncoder', 'TextDecoder',", 27931cb0ef41Sopenharmony_ci " 'clearInterval', 'clearTimeout', 'setInterval',", 27941cb0ef41Sopenharmony_ci " 'setTimeout', 'queueMicrotask', 'clearImmediate',", 27951cb0ef41Sopenharmony_ci " 'setImmediate', 'module', 'require',", 27961cb0ef41Sopenharmony_ci " 'assert', 'async_hooks', 'buffer',", 27971cb0ef41Sopenharmony_ci " 'child_process', 'cluster', 'crypto',", 27981cb0ef41Sopenharmony_ci " 'dgram', 'dns', 'domain',", 27991cb0ef41Sopenharmony_ci " 'events', 'fs', 'http',", 28001cb0ef41Sopenharmony_ci " 'http2', 'https', 'inspector',", 28011cb0ef41Sopenharmony_ci " 'net', 'os', 'path',", 28021cb0ef41Sopenharmony_ci " 'perf_hooks', 'punycode', 'querystring',", 28031cb0ef41Sopenharmony_ci " 'readline', 'repl', 'stream',", 28041cb0ef41Sopenharmony_ci " 'string_decoder', 'tls', 'trace_events',", 28051cb0ef41Sopenharmony_ci " 'tty', 'url', 'v8',", 28061cb0ef41Sopenharmony_ci " 'vm', 'worker_threads', 'zlib',", 28071cb0ef41Sopenharmony_ci " '_', '_error', 'util'", 28081cb0ef41Sopenharmony_ci ']', 28091cb0ef41Sopenharmony_ci ].join('\n'); 28101cb0ef41Sopenharmony_ci 28111cb0ef41Sopenharmony_ci assert.strictEqual(out, expected); 28121cb0ef41Sopenharmony_ci} 28131cb0ef41Sopenharmony_ci 28141cb0ef41Sopenharmony_ci{ 28151cb0ef41Sopenharmony_ci const originalCWD = process.cwd(); 28161cb0ef41Sopenharmony_ci 28171cb0ef41Sopenharmony_ci process.cwd = () => (process.platform === 'win32' ? 28181cb0ef41Sopenharmony_ci 'C:\\workspace\\node-test-binary-windows js-suites-%percent-encoded\\node' : 28191cb0ef41Sopenharmony_ci '/home/user directory/repository%encoded/node'); 28201cb0ef41Sopenharmony_ci 28211cb0ef41Sopenharmony_ci // Use a fake stack to verify the expected colored outcome. 28221cb0ef41Sopenharmony_ci const stack = [ 28231cb0ef41Sopenharmony_ci 'Error: CWD is grayed out, even cwd that are percent encoded!', 28241cb0ef41Sopenharmony_ci ' at A.<anonymous> (/test/node_modules/foo/node_modules/bar/baz.js:2:7)', 28251cb0ef41Sopenharmony_ci ' at Module._compile (node:internal/modules/cjs/loader:827:30)', 28261cb0ef41Sopenharmony_ci ' at Fancy (node:vm:697:32)', 28271cb0ef41Sopenharmony_ci // This file is not an actual Node.js core file. 28281cb0ef41Sopenharmony_ci ' at tryModuleLoad (node:internal/modules/cjs/foo:629:12)', 28291cb0ef41Sopenharmony_ci ' at Function.Module._load (node:internal/modules/cjs/loader:621:3)', 28301cb0ef41Sopenharmony_ci // This file is not an actual Node.js core file. 28311cb0ef41Sopenharmony_ci ' at Module.require [as weird/name] (node:internal/aaaaa/loader:735:19)', 28321cb0ef41Sopenharmony_ci ' at require (node:internal/modules/helpers:14:16)', 28331cb0ef41Sopenharmony_ci ' at Array.forEach (<anonymous>)', 28341cb0ef41Sopenharmony_ci ` at ${process.cwd()}/test/parallel/test-util-inspect.js:2760:12`, 28351cb0ef41Sopenharmony_ci ` at Object.<anonymous> (${process.cwd()}/node_modules/hyper_module/folder/file.js:2753:10)`, 28361cb0ef41Sopenharmony_ci ' at /test/test-util-inspect.js:2239:9', 28371cb0ef41Sopenharmony_ci ' at getActual (node:assert:592:5)', 28381cb0ef41Sopenharmony_ci ]; 28391cb0ef41Sopenharmony_ci const err = new Error('CWD is grayed out, even cwd that are percent encoded!'); 28401cb0ef41Sopenharmony_ci err.stack = stack.join('\n'); 28411cb0ef41Sopenharmony_ci if (process.platform === 'win32') { 28421cb0ef41Sopenharmony_ci err.stack = stack.map((frame) => (frame.includes('node:') ? 28431cb0ef41Sopenharmony_ci frame : 28441cb0ef41Sopenharmony_ci frame.replaceAll('/', '\\')) 28451cb0ef41Sopenharmony_ci ).join('\n'); 28461cb0ef41Sopenharmony_ci } 28471cb0ef41Sopenharmony_ci const escapedCWD = util.inspect(process.cwd()).slice(1, -1); 28481cb0ef41Sopenharmony_ci util.inspect(err, { colors: true }).split('\n').forEach((line, i) => { 28491cb0ef41Sopenharmony_ci let expected = stack[i].replace(/node_modules\/([^/]+)/gi, (_, m) => { 28501cb0ef41Sopenharmony_ci return `node_modules/\u001b[4m${m}\u001b[24m`; 28511cb0ef41Sopenharmony_ci }).replaceAll(new RegExp(`(\\(?${escapedCWD}(\\\\|/))`, 'gi'), (_, m) => { 28521cb0ef41Sopenharmony_ci return `\x1B[90m${m}\x1B[39m`; 28531cb0ef41Sopenharmony_ci }); 28541cb0ef41Sopenharmony_ci if (expected.includes(process.cwd()) && expected.endsWith(')')) { 28551cb0ef41Sopenharmony_ci expected = `${expected.slice(0, -1)}\x1B[90m)\x1B[39m`; 28561cb0ef41Sopenharmony_ci } 28571cb0ef41Sopenharmony_ci if (line.includes('node:')) { 28581cb0ef41Sopenharmony_ci if (!line.includes('foo') && !line.includes('aaa')) { 28591cb0ef41Sopenharmony_ci expected = `\u001b[90m${expected}\u001b[39m`; 28601cb0ef41Sopenharmony_ci } 28611cb0ef41Sopenharmony_ci } else if (process.platform === 'win32') { 28621cb0ef41Sopenharmony_ci expected = expected.replaceAll('/', '\\'); 28631cb0ef41Sopenharmony_ci } 28641cb0ef41Sopenharmony_ci assert.strictEqual(line, expected); 28651cb0ef41Sopenharmony_ci }); 28661cb0ef41Sopenharmony_ci 28671cb0ef41Sopenharmony_ci // Check ESM 28681cb0ef41Sopenharmony_ci const encodedCwd = url.pathToFileURL(process.cwd()); 28691cb0ef41Sopenharmony_ci const sl = process.platform === 'win32' ? '\\' : '/'; 28701cb0ef41Sopenharmony_ci 28711cb0ef41Sopenharmony_ci // Use a fake stack to verify the expected colored outcome. 28721cb0ef41Sopenharmony_ci err.stack = 'Error: ESM and CJS mixed are both grayed out!\n' + 28731cb0ef41Sopenharmony_ci ` at ${encodedCwd}/test/parallel/test-esm.mjs:2760:12\n` + 28741cb0ef41Sopenharmony_ci ` at Object.<anonymous> (${encodedCwd}/node_modules/esm_module/folder/file.js:2753:10)\n` + 28751cb0ef41Sopenharmony_ci ` at ${process.cwd()}${sl}test${sl}parallel${sl}test-cjs.js:2760:12\n` + 28761cb0ef41Sopenharmony_ci ` at Object.<anonymous> (${process.cwd()}${sl}node_modules${sl}cjs_module${sl}folder${sl}file.js:2753:10)`; 28771cb0ef41Sopenharmony_ci 28781cb0ef41Sopenharmony_ci let actual = util.inspect(err, { colors: true }); 28791cb0ef41Sopenharmony_ci let expected = 'Error: ESM and CJS mixed are both grayed out!\n' + 28801cb0ef41Sopenharmony_ci ` at \x1B[90m${encodedCwd}/\x1B[39mtest/parallel/test-esm.mjs:2760:12\n` + 28811cb0ef41Sopenharmony_ci ` at Object.<anonymous> \x1B[90m(${encodedCwd}/\x1B[39mnode_modules/\x1B[4mesm_module\x1B[24m/folder/file.js:2753:10\x1B[90m)\x1B[39m\n` + 28821cb0ef41Sopenharmony_ci ` at \x1B[90m${process.cwd()}${sl}\x1B[39mtest${sl}parallel${sl}test-cjs.js:2760:12\n` + 28831cb0ef41Sopenharmony_ci ` at Object.<anonymous> \x1B[90m(${process.cwd()}${sl}\x1B[39mnode_modules${sl}\x1B[4mcjs_module\x1B[24m${sl}folder${sl}file.js:2753:10\x1B[90m)\x1B[39m`; 28841cb0ef41Sopenharmony_ci 28851cb0ef41Sopenharmony_ci assert.strictEqual(actual, expected); 28861cb0ef41Sopenharmony_ci 28871cb0ef41Sopenharmony_ci // ESM without need for encoding 28881cb0ef41Sopenharmony_ci process.cwd = () => (process.platform === 'win32' ? 28891cb0ef41Sopenharmony_ci 'C:\\workspace\\node-test-binary-windows-js-suites\\node' : 28901cb0ef41Sopenharmony_ci '/home/user/repository/node'); 28911cb0ef41Sopenharmony_ci let expectedCwd = process.cwd(); 28921cb0ef41Sopenharmony_ci if (process.platform === 'win32') { 28931cb0ef41Sopenharmony_ci expectedCwd = `/${expectedCwd.replaceAll('\\', '/')}`; 28941cb0ef41Sopenharmony_ci } 28951cb0ef41Sopenharmony_ci // Use a fake stack to verify the expected colored outcome. 28961cb0ef41Sopenharmony_ci err.stack = 'Error: ESM without need for encoding!\n' + 28971cb0ef41Sopenharmony_ci ` at file://${expectedCwd}/file.js:15:15`; 28981cb0ef41Sopenharmony_ci 28991cb0ef41Sopenharmony_ci actual = util.inspect(err, { colors: true }); 29001cb0ef41Sopenharmony_ci expected = 'Error: ESM without need for encoding!\n' + 29011cb0ef41Sopenharmony_ci ` at \x1B[90mfile://${expectedCwd}/\x1B[39mfile.js:15:15`; 29021cb0ef41Sopenharmony_ci assert.strictEqual(actual, expected); 29031cb0ef41Sopenharmony_ci 29041cb0ef41Sopenharmony_ci process.cwd = originalCWD; 29051cb0ef41Sopenharmony_ci} 29061cb0ef41Sopenharmony_ci 29071cb0ef41Sopenharmony_ci{ 29081cb0ef41Sopenharmony_ci // Cross platform checks. 29091cb0ef41Sopenharmony_ci const err = new Error('foo'); 29101cb0ef41Sopenharmony_ci util.inspect(err, { colors: true }).split('\n').forEach((line, i) => { 29111cb0ef41Sopenharmony_ci assert(i < 2 || line.startsWith('\u001b[90m')); 29121cb0ef41Sopenharmony_ci }); 29131cb0ef41Sopenharmony_ci} 29141cb0ef41Sopenharmony_ci 29151cb0ef41Sopenharmony_ci{ 29161cb0ef41Sopenharmony_ci // Tracing class respects inspect depth. 29171cb0ef41Sopenharmony_ci try { 29181cb0ef41Sopenharmony_ci const trace = require('trace_events').createTracing({ categories: ['fo'] }); 29191cb0ef41Sopenharmony_ci const actualDepth0 = util.inspect({ trace }, { depth: 0 }); 29201cb0ef41Sopenharmony_ci assert.strictEqual(actualDepth0, '{ trace: [Tracing] }'); 29211cb0ef41Sopenharmony_ci const actualDepth1 = util.inspect({ trace }, { depth: 1 }); 29221cb0ef41Sopenharmony_ci assert.strictEqual( 29231cb0ef41Sopenharmony_ci actualDepth1, 29241cb0ef41Sopenharmony_ci "{ trace: Tracing { enabled: false, categories: 'fo' } }" 29251cb0ef41Sopenharmony_ci ); 29261cb0ef41Sopenharmony_ci } catch (err) { 29271cb0ef41Sopenharmony_ci if (err.code !== 'ERR_TRACE_EVENTS_UNAVAILABLE') 29281cb0ef41Sopenharmony_ci throw err; 29291cb0ef41Sopenharmony_ci } 29301cb0ef41Sopenharmony_ci} 29311cb0ef41Sopenharmony_ci 29321cb0ef41Sopenharmony_ci// Inspect prototype properties. 29331cb0ef41Sopenharmony_ci{ 29341cb0ef41Sopenharmony_ci class Foo extends Map { 29351cb0ef41Sopenharmony_ci prop = false; 29361cb0ef41Sopenharmony_ci prop2 = true; 29371cb0ef41Sopenharmony_ci get abc() { 29381cb0ef41Sopenharmony_ci return true; 29391cb0ef41Sopenharmony_ci } 29401cb0ef41Sopenharmony_ci get def() { 29411cb0ef41Sopenharmony_ci return false; 29421cb0ef41Sopenharmony_ci } 29431cb0ef41Sopenharmony_ci set def(v) {} 29441cb0ef41Sopenharmony_ci get xyz() { 29451cb0ef41Sopenharmony_ci return 'Should be ignored'; 29461cb0ef41Sopenharmony_ci } 29471cb0ef41Sopenharmony_ci func(a) {} 29481cb0ef41Sopenharmony_ci [util.inspect.custom]() { 29491cb0ef41Sopenharmony_ci return this; 29501cb0ef41Sopenharmony_ci } 29511cb0ef41Sopenharmony_ci } 29521cb0ef41Sopenharmony_ci 29531cb0ef41Sopenharmony_ci class Bar extends Foo { 29541cb0ef41Sopenharmony_ci abc = true; 29551cb0ef41Sopenharmony_ci prop = true; 29561cb0ef41Sopenharmony_ci get xyz() { 29571cb0ef41Sopenharmony_ci return 'YES!'; 29581cb0ef41Sopenharmony_ci } 29591cb0ef41Sopenharmony_ci [util.inspect.custom]() { 29601cb0ef41Sopenharmony_ci return this; 29611cb0ef41Sopenharmony_ci } 29621cb0ef41Sopenharmony_ci } 29631cb0ef41Sopenharmony_ci 29641cb0ef41Sopenharmony_ci const bar = new Bar(); 29651cb0ef41Sopenharmony_ci 29661cb0ef41Sopenharmony_ci assert.strictEqual( 29671cb0ef41Sopenharmony_ci inspect(bar), 29681cb0ef41Sopenharmony_ci 'Bar(0) [Map] { prop: true, prop2: true, abc: true }' 29691cb0ef41Sopenharmony_ci ); 29701cb0ef41Sopenharmony_ci assert.strictEqual( 29711cb0ef41Sopenharmony_ci inspect(bar, { showHidden: true, getters: true, colors: false }), 29721cb0ef41Sopenharmony_ci 'Bar(0) [Map] {\n' + 29731cb0ef41Sopenharmony_ci ' prop: true,\n' + 29741cb0ef41Sopenharmony_ci ' prop2: true,\n' + 29751cb0ef41Sopenharmony_ci ' abc: true,\n' + 29761cb0ef41Sopenharmony_ci " [xyz]: [Getter: 'YES!'],\n" + 29771cb0ef41Sopenharmony_ci ' [def]: [Getter/Setter: false]\n' + 29781cb0ef41Sopenharmony_ci '}' 29791cb0ef41Sopenharmony_ci ); 29801cb0ef41Sopenharmony_ci assert.strictEqual( 29811cb0ef41Sopenharmony_ci inspect(bar, { showHidden: true, getters: false, colors: true }), 29821cb0ef41Sopenharmony_ci 'Bar(0) [Map] {\n' + 29831cb0ef41Sopenharmony_ci ' prop: \x1B[33mtrue\x1B[39m,\n' + 29841cb0ef41Sopenharmony_ci ' prop2: \x1B[33mtrue\x1B[39m,\n' + 29851cb0ef41Sopenharmony_ci ' abc: \x1B[33mtrue\x1B[39m,\n' + 29861cb0ef41Sopenharmony_ci ' \x1B[2m[xyz]: \x1B[36m[Getter]\x1B[39m\x1B[22m,\n' + 29871cb0ef41Sopenharmony_ci ' \x1B[2m[def]: \x1B[36m[Getter/Setter]\x1B[39m\x1B[22m\n' + 29881cb0ef41Sopenharmony_ci '}' 29891cb0ef41Sopenharmony_ci ); 29901cb0ef41Sopenharmony_ci 29911cb0ef41Sopenharmony_ci const obj = Object.create({ abc: true, def: 5, toString() {} }); 29921cb0ef41Sopenharmony_ci assert.strictEqual( 29931cb0ef41Sopenharmony_ci inspect(obj, { showHidden: true, colors: true }), 29941cb0ef41Sopenharmony_ci '{ \x1B[2mabc: \x1B[33mtrue\x1B[39m\x1B[22m, ' + 29951cb0ef41Sopenharmony_ci '\x1B[2mdef: \x1B[33m5\x1B[39m\x1B[22m }' 29961cb0ef41Sopenharmony_ci ); 29971cb0ef41Sopenharmony_ci 29981cb0ef41Sopenharmony_ci assert.strictEqual( 29991cb0ef41Sopenharmony_ci inspect(Object.getPrototypeOf(bar), { showHidden: true, getters: true }), 30001cb0ef41Sopenharmony_ci '<ref *1> Foo [Map] {\n' + 30011cb0ef41Sopenharmony_ci ' [constructor]: [class Bar extends Foo] {\n' + 30021cb0ef41Sopenharmony_ci ' [length]: 0,\n' + 30031cb0ef41Sopenharmony_ci " [name]: 'Bar',\n" + 30041cb0ef41Sopenharmony_ci ' [prototype]: [Circular *1],\n' + 30051cb0ef41Sopenharmony_ci ' [Symbol(Symbol.species)]: [Getter: <Inspection threw ' + 30061cb0ef41Sopenharmony_ci "(Symbol.prototype.toString requires that 'this' be a Symbol)>]\n" + 30071cb0ef41Sopenharmony_ci ' },\n' + 30081cb0ef41Sopenharmony_ci " [xyz]: [Getter: 'YES!'],\n" + 30091cb0ef41Sopenharmony_ci ' [Symbol(nodejs.util.inspect.custom)]: ' + 30101cb0ef41Sopenharmony_ci '[Function: [nodejs.util.inspect.custom]] {\n' + 30111cb0ef41Sopenharmony_ci ' [length]: 0,\n' + 30121cb0ef41Sopenharmony_ci " [name]: '[nodejs.util.inspect.custom]'\n" + 30131cb0ef41Sopenharmony_ci ' },\n' + 30141cb0ef41Sopenharmony_ci ' [abc]: [Getter: true],\n' + 30151cb0ef41Sopenharmony_ci ' [def]: [Getter/Setter: false]\n' + 30161cb0ef41Sopenharmony_ci ' }' 30171cb0ef41Sopenharmony_ci ); 30181cb0ef41Sopenharmony_ci 30191cb0ef41Sopenharmony_ci assert.strictEqual( 30201cb0ef41Sopenharmony_ci inspect(Object.getPrototypeOf(bar)), 30211cb0ef41Sopenharmony_ci 'Foo [Map] {}' 30221cb0ef41Sopenharmony_ci ); 30231cb0ef41Sopenharmony_ci 30241cb0ef41Sopenharmony_ci assert.strictEqual( 30251cb0ef41Sopenharmony_ci inspect(Object.getPrototypeOf(new Foo())), 30261cb0ef41Sopenharmony_ci 'Map {}' 30271cb0ef41Sopenharmony_ci ); 30281cb0ef41Sopenharmony_ci} 30291cb0ef41Sopenharmony_ci 30301cb0ef41Sopenharmony_ci// Check that prototypes with a null prototype are inspectable. 30311cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node/issues/35730 30321cb0ef41Sopenharmony_ci{ 30331cb0ef41Sopenharmony_ci function Func() {} 30341cb0ef41Sopenharmony_ci Func.prototype = null; 30351cb0ef41Sopenharmony_ci const object = {}; 30361cb0ef41Sopenharmony_ci object.constructor = Func; 30371cb0ef41Sopenharmony_ci 30381cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(object), '{ constructor: [Function: Func] }'); 30391cb0ef41Sopenharmony_ci} 30401cb0ef41Sopenharmony_ci 30411cb0ef41Sopenharmony_ci// Test changing util.inspect.colors colors and aliases. 30421cb0ef41Sopenharmony_ci{ 30431cb0ef41Sopenharmony_ci const colors = util.inspect.colors; 30441cb0ef41Sopenharmony_ci 30451cb0ef41Sopenharmony_ci const originalValue = colors.gray; 30461cb0ef41Sopenharmony_ci 30471cb0ef41Sopenharmony_ci // "grey" is reference-equal alias of "gray". 30481cb0ef41Sopenharmony_ci assert.strictEqual(colors.grey, colors.gray); 30491cb0ef41Sopenharmony_ci 30501cb0ef41Sopenharmony_ci // Assigninging one should assign the other. This tests that the alias setter 30511cb0ef41Sopenharmony_ci // function keeps things reference-equal. 30521cb0ef41Sopenharmony_ci colors.gray = [0, 0]; 30531cb0ef41Sopenharmony_ci assert.deepStrictEqual(colors.gray, [0, 0]); 30541cb0ef41Sopenharmony_ci assert.strictEqual(colors.grey, colors.gray); 30551cb0ef41Sopenharmony_ci 30561cb0ef41Sopenharmony_ci colors.grey = [1, 1]; 30571cb0ef41Sopenharmony_ci assert.deepStrictEqual(colors.grey, [1, 1]); 30581cb0ef41Sopenharmony_ci assert.strictEqual(colors.grey, colors.gray); 30591cb0ef41Sopenharmony_ci 30601cb0ef41Sopenharmony_ci // Restore original value to avoid side effects in other tests. 30611cb0ef41Sopenharmony_ci colors.gray = originalValue; 30621cb0ef41Sopenharmony_ci assert.deepStrictEqual(colors.gray, originalValue); 30631cb0ef41Sopenharmony_ci assert.strictEqual(colors.grey, colors.gray); 30641cb0ef41Sopenharmony_ci} 30651cb0ef41Sopenharmony_ci 30661cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/31889 30671cb0ef41Sopenharmony_ci{ 30681cb0ef41Sopenharmony_ci v8.setFlagsFromString('--allow-natives-syntax'); 30691cb0ef41Sopenharmony_ci const undetectable = vm.runInThisContext('%GetUndetectable()'); 30701cb0ef41Sopenharmony_ci v8.setFlagsFromString('--no-allow-natives-syntax'); 30711cb0ef41Sopenharmony_ci assert.strictEqual(inspect(undetectable), '{}'); 30721cb0ef41Sopenharmony_ci} 30731cb0ef41Sopenharmony_ci 30741cb0ef41Sopenharmony_ci// Truncate output for Primitives with 1 character left 30751cb0ef41Sopenharmony_ci{ 30761cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect('bl', { maxStringLength: 1 }), 30771cb0ef41Sopenharmony_ci "'b'... 1 more character"); 30781cb0ef41Sopenharmony_ci} 30791cb0ef41Sopenharmony_ci 30801cb0ef41Sopenharmony_ci{ 30811cb0ef41Sopenharmony_ci const x = 'a'.repeat(1e6); 30821cb0ef41Sopenharmony_ci assert(util.inspect(x).endsWith('... 990000 more characters')); 30831cb0ef41Sopenharmony_ci assert.strictEqual( 30841cb0ef41Sopenharmony_ci util.inspect(x, { maxStringLength: 4 }), 30851cb0ef41Sopenharmony_ci "'aaaa'... 999996 more characters" 30861cb0ef41Sopenharmony_ci ); 30871cb0ef41Sopenharmony_ci assert.match(util.inspect(x, { maxStringLength: null }), /a'$/); 30881cb0ef41Sopenharmony_ci} 30891cb0ef41Sopenharmony_ci 30901cb0ef41Sopenharmony_ci{ 30911cb0ef41Sopenharmony_ci // Verify that util.inspect() invokes custom inspect functions on objects 30921cb0ef41Sopenharmony_ci // from other vm.Contexts but does not pass data from its own Context to that 30931cb0ef41Sopenharmony_ci // function. 30941cb0ef41Sopenharmony_ci const target = vm.runInNewContext(` 30951cb0ef41Sopenharmony_ci ({ 30961cb0ef41Sopenharmony_ci [Symbol.for('nodejs.util.inspect.custom')](depth, ctx) { 30971cb0ef41Sopenharmony_ci this.depth = depth; 30981cb0ef41Sopenharmony_ci this.ctx = ctx; 30991cb0ef41Sopenharmony_ci try { 31001cb0ef41Sopenharmony_ci this.stylized = ctx.stylize(''); 31011cb0ef41Sopenharmony_ci } catch (e) { 31021cb0ef41Sopenharmony_ci this.stylizeException = e; 31031cb0ef41Sopenharmony_ci } 31041cb0ef41Sopenharmony_ci return this.stylized; 31051cb0ef41Sopenharmony_ci } 31061cb0ef41Sopenharmony_ci }) 31071cb0ef41Sopenharmony_ci `, Object.create(null)); 31081cb0ef41Sopenharmony_ci assert.strictEqual(target.ctx, undefined); 31091cb0ef41Sopenharmony_ci 31101cb0ef41Sopenharmony_ci { 31111cb0ef41Sopenharmony_ci // Subtest 1: Just try to inspect the object with default options. 31121cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(target), ''); 31131cb0ef41Sopenharmony_ci assert.strictEqual(typeof target.ctx, 'object'); 31141cb0ef41Sopenharmony_ci const objectGraph = fullObjectGraph(target); 31151cb0ef41Sopenharmony_ci assert(!objectGraph.has(Object)); 31161cb0ef41Sopenharmony_ci assert(!objectGraph.has(Function)); 31171cb0ef41Sopenharmony_ci } 31181cb0ef41Sopenharmony_ci 31191cb0ef41Sopenharmony_ci { 31201cb0ef41Sopenharmony_ci // Subtest 2: Use a stylize function that returns a non-primitive. 31211cb0ef41Sopenharmony_ci const output = util.inspect(target, { 31221cb0ef41Sopenharmony_ci stylize: common.mustCall((str) => { 31231cb0ef41Sopenharmony_ci return {}; 31241cb0ef41Sopenharmony_ci }) 31251cb0ef41Sopenharmony_ci }); 31261cb0ef41Sopenharmony_ci assert.strictEqual(output, '[object Object]'); 31271cb0ef41Sopenharmony_ci assert.strictEqual(typeof target.ctx, 'object'); 31281cb0ef41Sopenharmony_ci const objectGraph = fullObjectGraph(target); 31291cb0ef41Sopenharmony_ci assert(!objectGraph.has(Object)); 31301cb0ef41Sopenharmony_ci assert(!objectGraph.has(Function)); 31311cb0ef41Sopenharmony_ci } 31321cb0ef41Sopenharmony_ci 31331cb0ef41Sopenharmony_ci { 31341cb0ef41Sopenharmony_ci // Subtest 3: Use a stylize function that throws an exception. 31351cb0ef41Sopenharmony_ci const output = util.inspect(target, { 31361cb0ef41Sopenharmony_ci stylize: common.mustCall((str) => { 31371cb0ef41Sopenharmony_ci throw new Error('oops'); 31381cb0ef41Sopenharmony_ci }) 31391cb0ef41Sopenharmony_ci }); 31401cb0ef41Sopenharmony_ci assert.strictEqual(output, ''); 31411cb0ef41Sopenharmony_ci assert.strictEqual(typeof target.ctx, 'object'); 31421cb0ef41Sopenharmony_ci const objectGraph = fullObjectGraph(target); 31431cb0ef41Sopenharmony_ci assert(!objectGraph.has(Object)); 31441cb0ef41Sopenharmony_ci assert(!objectGraph.has(Function)); 31451cb0ef41Sopenharmony_ci } 31461cb0ef41Sopenharmony_ci 31471cb0ef41Sopenharmony_ci function fullObjectGraph(value) { 31481cb0ef41Sopenharmony_ci const graph = new Set([value]); 31491cb0ef41Sopenharmony_ci 31501cb0ef41Sopenharmony_ci for (const entry of graph) { 31511cb0ef41Sopenharmony_ci if ((typeof entry !== 'object' && typeof entry !== 'function') || 31521cb0ef41Sopenharmony_ci entry === null) { 31531cb0ef41Sopenharmony_ci continue; 31541cb0ef41Sopenharmony_ci } 31551cb0ef41Sopenharmony_ci 31561cb0ef41Sopenharmony_ci graph.add(Object.getPrototypeOf(entry)); 31571cb0ef41Sopenharmony_ci const descriptors = Object.values( 31581cb0ef41Sopenharmony_ci Object.getOwnPropertyDescriptors(entry)); 31591cb0ef41Sopenharmony_ci for (const descriptor of descriptors) { 31601cb0ef41Sopenharmony_ci graph.add(descriptor.value); 31611cb0ef41Sopenharmony_ci graph.add(descriptor.set); 31621cb0ef41Sopenharmony_ci graph.add(descriptor.get); 31631cb0ef41Sopenharmony_ci } 31641cb0ef41Sopenharmony_ci } 31651cb0ef41Sopenharmony_ci 31661cb0ef41Sopenharmony_ci return graph; 31671cb0ef41Sopenharmony_ci } 31681cb0ef41Sopenharmony_ci 31691cb0ef41Sopenharmony_ci // Consistency check. 31701cb0ef41Sopenharmony_ci assert(fullObjectGraph(global).has(Function.prototype)); 31711cb0ef41Sopenharmony_ci} 31721cb0ef41Sopenharmony_ci 31731cb0ef41Sopenharmony_ci{ 31741cb0ef41Sopenharmony_ci // Confirm that own constructor value displays correctly. 31751cb0ef41Sopenharmony_ci 31761cb0ef41Sopenharmony_ci function Fhqwhgads() {} 31771cb0ef41Sopenharmony_ci 31781cb0ef41Sopenharmony_ci const sterrance = new Fhqwhgads(); 31791cb0ef41Sopenharmony_ci sterrance.constructor = Fhqwhgads; 31801cb0ef41Sopenharmony_ci 31811cb0ef41Sopenharmony_ci assert.strictEqual( 31821cb0ef41Sopenharmony_ci util.inspect(sterrance, { showHidden: true }), 31831cb0ef41Sopenharmony_ci 'Fhqwhgads {\n' + 31841cb0ef41Sopenharmony_ci ' constructor: <ref *1> [Function: Fhqwhgads] {\n' + 31851cb0ef41Sopenharmony_ci ' [length]: 0,\n' + 31861cb0ef41Sopenharmony_ci " [name]: 'Fhqwhgads',\n" + 31871cb0ef41Sopenharmony_ci ' [prototype]: { [constructor]: [Circular *1] }\n' + 31881cb0ef41Sopenharmony_ci ' }\n' + 31891cb0ef41Sopenharmony_ci '}' 31901cb0ef41Sopenharmony_ci ); 31911cb0ef41Sopenharmony_ci} 31921cb0ef41Sopenharmony_ci 31931cb0ef41Sopenharmony_ci{ 31941cb0ef41Sopenharmony_ci // Confirm null prototype of generator prototype displays as expected. 31951cb0ef41Sopenharmony_ci 31961cb0ef41Sopenharmony_ci function getProtoOfProto() { 31971cb0ef41Sopenharmony_ci return Object.getPrototypeOf(Object.getPrototypeOf(function* () {})); 31981cb0ef41Sopenharmony_ci } 31991cb0ef41Sopenharmony_ci 32001cb0ef41Sopenharmony_ci function* generator() {} 32011cb0ef41Sopenharmony_ci 32021cb0ef41Sopenharmony_ci const generatorPrototype = Object.getPrototypeOf(generator); 32031cb0ef41Sopenharmony_ci const originalProtoOfProto = Object.getPrototypeOf(generatorPrototype); 32041cb0ef41Sopenharmony_ci assert.strictEqual(getProtoOfProto(), originalProtoOfProto); 32051cb0ef41Sopenharmony_ci Object.setPrototypeOf(generatorPrototype, null); 32061cb0ef41Sopenharmony_ci assert.notStrictEqual(getProtoOfProto, originalProtoOfProto); 32071cb0ef41Sopenharmony_ci 32081cb0ef41Sopenharmony_ci // This is the actual test. The other assertions in this block are about 32091cb0ef41Sopenharmony_ci // making sure the test is set up correctly and isn't polluting other tests. 32101cb0ef41Sopenharmony_ci assert.strictEqual( 32111cb0ef41Sopenharmony_ci util.inspect(generator, { showHidden: true }), 32121cb0ef41Sopenharmony_ci '[GeneratorFunction: generator] {\n' + 32131cb0ef41Sopenharmony_ci ' [length]: 0,\n' + 32141cb0ef41Sopenharmony_ci " [name]: 'generator',\n" + 32151cb0ef41Sopenharmony_ci " [prototype]: Object [Generator] { [Symbol(Symbol.toStringTag)]: 'Generator' },\n" + // eslint-disable-line max-len 32161cb0ef41Sopenharmony_ci " [Symbol(Symbol.toStringTag)]: 'GeneratorFunction'\n" + 32171cb0ef41Sopenharmony_ci '}' 32181cb0ef41Sopenharmony_ci ); 32191cb0ef41Sopenharmony_ci 32201cb0ef41Sopenharmony_ci // Reset so we don't pollute other tests 32211cb0ef41Sopenharmony_ci Object.setPrototypeOf(generatorPrototype, originalProtoOfProto); 32221cb0ef41Sopenharmony_ci assert.strictEqual(getProtoOfProto(), originalProtoOfProto); 32231cb0ef41Sopenharmony_ci} 32241cb0ef41Sopenharmony_ci 32251cb0ef41Sopenharmony_ci{ 32261cb0ef41Sopenharmony_ci // Test for when breakLength results in a single column. 32271cb0ef41Sopenharmony_ci const obj = Array(9).fill('fhqwhgadshgnsdhjsdbkhsdabkfabkveybvf'); 32281cb0ef41Sopenharmony_ci assert.strictEqual( 32291cb0ef41Sopenharmony_ci util.inspect(obj, { breakLength: 256 }), 32301cb0ef41Sopenharmony_ci '[\n' + 32311cb0ef41Sopenharmony_ci " 'fhqwhgadshgnsdhjsdbkhsdabkfabkveybvf',\n" + 32321cb0ef41Sopenharmony_ci " 'fhqwhgadshgnsdhjsdbkhsdabkfabkveybvf',\n" + 32331cb0ef41Sopenharmony_ci " 'fhqwhgadshgnsdhjsdbkhsdabkfabkveybvf',\n" + 32341cb0ef41Sopenharmony_ci " 'fhqwhgadshgnsdhjsdbkhsdabkfabkveybvf',\n" + 32351cb0ef41Sopenharmony_ci " 'fhqwhgadshgnsdhjsdbkhsdabkfabkveybvf',\n" + 32361cb0ef41Sopenharmony_ci " 'fhqwhgadshgnsdhjsdbkhsdabkfabkveybvf',\n" + 32371cb0ef41Sopenharmony_ci " 'fhqwhgadshgnsdhjsdbkhsdabkfabkveybvf',\n" + 32381cb0ef41Sopenharmony_ci " 'fhqwhgadshgnsdhjsdbkhsdabkfabkveybvf',\n" + 32391cb0ef41Sopenharmony_ci " 'fhqwhgadshgnsdhjsdbkhsdabkfabkveybvf'\n" + 32401cb0ef41Sopenharmony_ci ']' 32411cb0ef41Sopenharmony_ci ); 32421cb0ef41Sopenharmony_ci} 32431cb0ef41Sopenharmony_ci 32441cb0ef41Sopenharmony_ci{ 32451cb0ef41Sopenharmony_ci assert.strictEqual( 32461cb0ef41Sopenharmony_ci util.inspect({ ['__proto__']: { a: 1 } }), 32471cb0ef41Sopenharmony_ci "{ ['__proto__']: { a: 1 } }" 32481cb0ef41Sopenharmony_ci ); 32491cb0ef41Sopenharmony_ci} 32501cb0ef41Sopenharmony_ci 32511cb0ef41Sopenharmony_ci{ 32521cb0ef41Sopenharmony_ci const { numericSeparator } = util.inspect.defaultOptions; 32531cb0ef41Sopenharmony_ci util.inspect.defaultOptions.numericSeparator = true; 32541cb0ef41Sopenharmony_ci 32551cb0ef41Sopenharmony_ci assert.strictEqual( 32561cb0ef41Sopenharmony_ci // eslint-disable-next-line no-loss-of-precision 32571cb0ef41Sopenharmony_ci util.inspect(1234567891234567891234), 32581cb0ef41Sopenharmony_ci '1.234567891234568e+21' 32591cb0ef41Sopenharmony_ci ); 32601cb0ef41Sopenharmony_ci assert.strictEqual( 32611cb0ef41Sopenharmony_ci util.inspect(123456789.12345678), 32621cb0ef41Sopenharmony_ci '123_456_789.123_456_78' 32631cb0ef41Sopenharmony_ci ); 32641cb0ef41Sopenharmony_ci 32651cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(10_000_000), '10_000_000'); 32661cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(1_000_000), '1_000_000'); 32671cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(100_000), '100_000'); 32681cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(99_999.9), '99_999.9'); 32691cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(9_999), '9_999'); 32701cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(999), '999'); 32711cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(NaN), 'NaN'); 32721cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(Infinity), 'Infinity'); 32731cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect(-Infinity), '-Infinity'); 32741cb0ef41Sopenharmony_ci 32751cb0ef41Sopenharmony_ci assert.strictEqual( 32761cb0ef41Sopenharmony_ci util.inspect(new Float64Array([100_000_000])), 32771cb0ef41Sopenharmony_ci 'Float64Array(1) [ 100_000_000 ]' 32781cb0ef41Sopenharmony_ci ); 32791cb0ef41Sopenharmony_ci assert.strictEqual( 32801cb0ef41Sopenharmony_ci util.inspect(new BigInt64Array([9_100_000_100n])), 32811cb0ef41Sopenharmony_ci 'BigInt64Array(1) [ 9_100_000_100n ]' 32821cb0ef41Sopenharmony_ci ); 32831cb0ef41Sopenharmony_ci 32841cb0ef41Sopenharmony_ci assert.strictEqual( 32851cb0ef41Sopenharmony_ci util.inspect(123456789), 32861cb0ef41Sopenharmony_ci '123_456_789' 32871cb0ef41Sopenharmony_ci ); 32881cb0ef41Sopenharmony_ci assert.strictEqual( 32891cb0ef41Sopenharmony_ci util.inspect(123456789n), 32901cb0ef41Sopenharmony_ci '123_456_789n' 32911cb0ef41Sopenharmony_ci ); 32921cb0ef41Sopenharmony_ci 32931cb0ef41Sopenharmony_ci util.inspect.defaultOptions.numericSeparator = numericSeparator; 32941cb0ef41Sopenharmony_ci 32951cb0ef41Sopenharmony_ci assert.strictEqual( 32961cb0ef41Sopenharmony_ci util.inspect(123456789.12345678, { numericSeparator: true }), 32971cb0ef41Sopenharmony_ci '123_456_789.123_456_78' 32981cb0ef41Sopenharmony_ci ); 32991cb0ef41Sopenharmony_ci 33001cb0ef41Sopenharmony_ci assert.strictEqual( 33011cb0ef41Sopenharmony_ci util.inspect(-123456789.12345678, { numericSeparator: true }), 33021cb0ef41Sopenharmony_ci '-123_456_789.123_456_78' 33031cb0ef41Sopenharmony_ci ); 33041cb0ef41Sopenharmony_ci} 33051cb0ef41Sopenharmony_ci 33061cb0ef41Sopenharmony_ci// Regression test for https://github.com/nodejs/node/issues/41244 33071cb0ef41Sopenharmony_ci{ 33081cb0ef41Sopenharmony_ci assert.strictEqual(util.inspect({ 33091cb0ef41Sopenharmony_ci get [Symbol.iterator]() { 33101cb0ef41Sopenharmony_ci throw new Error(); 33111cb0ef41Sopenharmony_ci } 33121cb0ef41Sopenharmony_ci }), '{ [Symbol(Symbol.iterator)]: [Getter] }'); 33131cb0ef41Sopenharmony_ci} 3314