11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_cirequire('../common');
241cb0ef41Sopenharmony_ciconst assert = require('assert');
251cb0ef41Sopenharmony_ciconst util = require('util');
261cb0ef41Sopenharmony_ciconst symbol = Symbol('foo');
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciassert.strictEqual(util.format(), '');
291cb0ef41Sopenharmony_ciassert.strictEqual(util.format(''), '');
301cb0ef41Sopenharmony_ciassert.strictEqual(util.format([]), '[]');
311cb0ef41Sopenharmony_ciassert.strictEqual(util.format([0]), '[ 0 ]');
321cb0ef41Sopenharmony_ciassert.strictEqual(util.format({}), '{}');
331cb0ef41Sopenharmony_ciassert.strictEqual(util.format({ foo: 42 }), '{ foo: 42 }');
341cb0ef41Sopenharmony_ciassert.strictEqual(util.format(null), 'null');
351cb0ef41Sopenharmony_ciassert.strictEqual(util.format(true), 'true');
361cb0ef41Sopenharmony_ciassert.strictEqual(util.format(false), 'false');
371cb0ef41Sopenharmony_ciassert.strictEqual(util.format('test'), 'test');
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci// CHECKME this is for console.log() compatibility - but is it *right*?
401cb0ef41Sopenharmony_ciassert.strictEqual(util.format('foo', 'bar', 'baz'), 'foo bar baz');
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci// ES6 Symbol handling
431cb0ef41Sopenharmony_ciassert.strictEqual(util.format(symbol), 'Symbol(foo)');
441cb0ef41Sopenharmony_ciassert.strictEqual(util.format('foo', symbol), 'foo Symbol(foo)');
451cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s', symbol), 'Symbol(foo)');
461cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%j', symbol), 'undefined');
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci// Number format specifier
491cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%d'), '%d');
501cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%d', 42.0), '42');
511cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%d', 42), '42');
521cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%d', '42'), '42');
531cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%d', '42.0'), '42');
541cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%d', 1.5), '1.5');
551cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%d', -0.5), '-0.5');
561cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%d', -0.0), '-0');
571cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%d', ''), '0');
581cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%d', ' -0.000'), '-0');
591cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%d', Symbol()), 'NaN');
601cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%d', Infinity), 'Infinity');
611cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%d', -Infinity), '-Infinity');
621cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%d %d', 42, 43), '42 43');
631cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%d %d', 42), '42 %d');
641cb0ef41Sopenharmony_ciassert.strictEqual(
651cb0ef41Sopenharmony_ci  util.format('%d', 1180591620717411303424),
661cb0ef41Sopenharmony_ci  '1.1805916207174113e+21'
671cb0ef41Sopenharmony_ci);
681cb0ef41Sopenharmony_ciassert.strictEqual(
691cb0ef41Sopenharmony_ci  util.format('%d', 1180591620717411303424n),
701cb0ef41Sopenharmony_ci  '1180591620717411303424n'
711cb0ef41Sopenharmony_ci);
721cb0ef41Sopenharmony_ciassert.strictEqual(
731cb0ef41Sopenharmony_ci  util.format('%d %d', 1180591620717411303424n, 12345678901234567890123n),
741cb0ef41Sopenharmony_ci  '1180591620717411303424n 12345678901234567890123n'
751cb0ef41Sopenharmony_ci);
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci{
781cb0ef41Sopenharmony_ci  const { numericSeparator } = util.inspect.defaultOptions;
791cb0ef41Sopenharmony_ci  util.inspect.defaultOptions.numericSeparator = true;
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  assert.strictEqual(
821cb0ef41Sopenharmony_ci    util.format('%d', 1180591620717411303424),
831cb0ef41Sopenharmony_ci    '1.1805916207174113e+21'
841cb0ef41Sopenharmony_ci  );
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  assert.strictEqual(
871cb0ef41Sopenharmony_ci    util.format(
881cb0ef41Sopenharmony_ci      // eslint-disable-next-line no-loss-of-precision
891cb0ef41Sopenharmony_ci      '%d %s %i', 118059162071741130342, 118059162071741130342, 123_123_123),
901cb0ef41Sopenharmony_ci    '118_059_162_071_741_140_000 118_059_162_071_741_140_000 123_123_123'
911cb0ef41Sopenharmony_ci  );
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci  assert.strictEqual(
941cb0ef41Sopenharmony_ci    util.format(
951cb0ef41Sopenharmony_ci      '%d %s',
961cb0ef41Sopenharmony_ci      1_180_591_620_717_411_303_424n,
971cb0ef41Sopenharmony_ci      12_345_678_901_234_567_890_123n
981cb0ef41Sopenharmony_ci    ),
991cb0ef41Sopenharmony_ci    '1_180_591_620_717_411_303_424n 12_345_678_901_234_567_890_123n'
1001cb0ef41Sopenharmony_ci  );
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  assert.strictEqual(
1031cb0ef41Sopenharmony_ci    util.format('%i', 1_180_591_620_717_411_303_424n),
1041cb0ef41Sopenharmony_ci    '1_180_591_620_717_411_303_424n'
1051cb0ef41Sopenharmony_ci  );
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci  util.inspect.defaultOptions.numericSeparator = numericSeparator;
1081cb0ef41Sopenharmony_ci}
1091cb0ef41Sopenharmony_ci// Integer format specifier
1101cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%i'), '%i');
1111cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%i', 42.0), '42');
1121cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%i', 42), '42');
1131cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%i', '42'), '42');
1141cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%i', '42.0'), '42');
1151cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%i', 1.5), '1');
1161cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%i', -0.5), '-0');
1171cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%i', ''), 'NaN');
1181cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%i', Infinity), 'NaN');
1191cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%i', -Infinity), 'NaN');
1201cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%i', Symbol()), 'NaN');
1211cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%i %i', 42, 43), '42 43');
1221cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%i %i', 42), '42 %i');
1231cb0ef41Sopenharmony_ciassert.strictEqual(
1241cb0ef41Sopenharmony_ci  util.format('%i', 1180591620717411303424),
1251cb0ef41Sopenharmony_ci  '1'
1261cb0ef41Sopenharmony_ci);
1271cb0ef41Sopenharmony_ciassert.strictEqual(
1281cb0ef41Sopenharmony_ci  util.format('%i', 1180591620717411303424n),
1291cb0ef41Sopenharmony_ci  '1180591620717411303424n'
1301cb0ef41Sopenharmony_ci);
1311cb0ef41Sopenharmony_ciassert.strictEqual(
1321cb0ef41Sopenharmony_ci  util.format('%i %i', 1180591620717411303424n, 12345678901234567890123n),
1331cb0ef41Sopenharmony_ci  '1180591620717411303424n 12345678901234567890123n'
1341cb0ef41Sopenharmony_ci);
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ciassert.strictEqual(
1371cb0ef41Sopenharmony_ci  util.format('%d %i', 1180591620717411303424n, 12345678901234567890123n),
1381cb0ef41Sopenharmony_ci  '1180591620717411303424n 12345678901234567890123n'
1391cb0ef41Sopenharmony_ci);
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ciassert.strictEqual(
1421cb0ef41Sopenharmony_ci  util.format('%i %d', 1180591620717411303424n, 12345678901234567890123n),
1431cb0ef41Sopenharmony_ci  '1180591620717411303424n 12345678901234567890123n'
1441cb0ef41Sopenharmony_ci);
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ciassert.strictEqual(
1471cb0ef41Sopenharmony_ci  util.formatWithOptions(
1481cb0ef41Sopenharmony_ci    { numericSeparator: true },
1491cb0ef41Sopenharmony_ci    '%i %d', 1180591620717411303424n, 12345678901234567890123n),
1501cb0ef41Sopenharmony_ci  '1_180_591_620_717_411_303_424n 12_345_678_901_234_567_890_123n'
1511cb0ef41Sopenharmony_ci);
1521cb0ef41Sopenharmony_ci
1531cb0ef41Sopenharmony_ci// Float format specifier
1541cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f'), '%f');
1551cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f', 42.0), '42');
1561cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f', 42), '42');
1571cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f', '42'), '42');
1581cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f', '-0.0'), '-0');
1591cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f', '42.0'), '42');
1601cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f', 1.5), '1.5');
1611cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f', -0.5), '-0.5');
1621cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f', Math.PI), '3.141592653589793');
1631cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f', ''), 'NaN');
1641cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f', Symbol('foo')), 'NaN');
1651cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f', 5n), '5');
1661cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f', Infinity), 'Infinity');
1671cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f', -Infinity), '-Infinity');
1681cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f %f', 42, 43), '42 43');
1691cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f %f', 42), '42 %f');
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci// String format specifier
1721cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s'), '%s');
1731cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s', undefined), 'undefined');
1741cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s', null), 'null');
1751cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s', 'foo'), 'foo');
1761cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s', 42), '42');
1771cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s', '42'), '42');
1781cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s', -0), '-0');
1791cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s', '-0.0'), '-0.0');
1801cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s %s', 42, 43), '42 43');
1811cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s %s', 42), '42 %s');
1821cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s', 42n), '42n');
1831cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s', Symbol('foo')), 'Symbol(foo)');
1841cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s', true), 'true');
1851cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s', { a: [1, 2, 3] }), '{ a: [Array] }');
1861cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s', { toString() { return 'Foo'; } }), 'Foo');
1871cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s', { toString: 5 }), '{ toString: 5 }');
1881cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s', () => 5), '() => 5');
1891cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s', Infinity), 'Infinity');
1901cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s', -Infinity), '-Infinity');
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci// String format specifier including `toString` properties on the prototype.
1931cb0ef41Sopenharmony_ci{
1941cb0ef41Sopenharmony_ci  class Foo { toString() { return 'Bar'; } }
1951cb0ef41Sopenharmony_ci  assert.strictEqual(util.format('%s', new Foo()), 'Bar');
1961cb0ef41Sopenharmony_ci  assert.strictEqual(
1971cb0ef41Sopenharmony_ci    util.format('%s', Object.setPrototypeOf(new Foo(), null)),
1981cb0ef41Sopenharmony_ci    '[Foo: null prototype] {}'
1991cb0ef41Sopenharmony_ci  );
2001cb0ef41Sopenharmony_ci  global.Foo = Foo;
2011cb0ef41Sopenharmony_ci  assert.strictEqual(util.format('%s', new Foo()), 'Bar');
2021cb0ef41Sopenharmony_ci  delete global.Foo;
2031cb0ef41Sopenharmony_ci  class Bar { abc = true; }
2041cb0ef41Sopenharmony_ci  assert.strictEqual(util.format('%s', new Bar()), 'Bar { abc: true }');
2051cb0ef41Sopenharmony_ci  class Foobar extends Array { aaa = true; }
2061cb0ef41Sopenharmony_ci  assert.strictEqual(
2071cb0ef41Sopenharmony_ci    util.format('%s', new Foobar(5)),
2081cb0ef41Sopenharmony_ci    'Foobar(5) [ <5 empty items>, aaa: true ]'
2091cb0ef41Sopenharmony_ci  );
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ci  // Subclassing:
2121cb0ef41Sopenharmony_ci  class B extends Foo {}
2131cb0ef41Sopenharmony_ci
2141cb0ef41Sopenharmony_ci  function C() {}
2151cb0ef41Sopenharmony_ci  C.prototype.toString = function() {
2161cb0ef41Sopenharmony_ci    return 'Custom';
2171cb0ef41Sopenharmony_ci  };
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_ci  function D() {
2201cb0ef41Sopenharmony_ci    C.call(this);
2211cb0ef41Sopenharmony_ci  }
2221cb0ef41Sopenharmony_ci  D.prototype = Object.create(C.prototype);
2231cb0ef41Sopenharmony_ci
2241cb0ef41Sopenharmony_ci  assert.strictEqual(
2251cb0ef41Sopenharmony_ci    util.format('%s', new B()),
2261cb0ef41Sopenharmony_ci    'Bar'
2271cb0ef41Sopenharmony_ci  );
2281cb0ef41Sopenharmony_ci  assert.strictEqual(
2291cb0ef41Sopenharmony_ci    util.format('%s', new C()),
2301cb0ef41Sopenharmony_ci    'Custom'
2311cb0ef41Sopenharmony_ci  );
2321cb0ef41Sopenharmony_ci  assert.strictEqual(
2331cb0ef41Sopenharmony_ci    util.format('%s', new D()),
2341cb0ef41Sopenharmony_ci    'Custom'
2351cb0ef41Sopenharmony_ci  );
2361cb0ef41Sopenharmony_ci
2371cb0ef41Sopenharmony_ci  D.prototype.constructor = D;
2381cb0ef41Sopenharmony_ci  assert.strictEqual(
2391cb0ef41Sopenharmony_ci    util.format('%s', new D()),
2401cb0ef41Sopenharmony_ci    'Custom'
2411cb0ef41Sopenharmony_ci  );
2421cb0ef41Sopenharmony_ci
2431cb0ef41Sopenharmony_ci  D.prototype.constructor = null;
2441cb0ef41Sopenharmony_ci  assert.strictEqual(
2451cb0ef41Sopenharmony_ci    util.format('%s', new D()),
2461cb0ef41Sopenharmony_ci    'Custom'
2471cb0ef41Sopenharmony_ci  );
2481cb0ef41Sopenharmony_ci
2491cb0ef41Sopenharmony_ci  D.prototype.constructor = { name: 'Foobar' };
2501cb0ef41Sopenharmony_ci  assert.strictEqual(
2511cb0ef41Sopenharmony_ci    util.format('%s', new D()),
2521cb0ef41Sopenharmony_ci    'Custom'
2531cb0ef41Sopenharmony_ci  );
2541cb0ef41Sopenharmony_ci
2551cb0ef41Sopenharmony_ci  Object.defineProperty(D.prototype, 'constructor', {
2561cb0ef41Sopenharmony_ci    get() {
2571cb0ef41Sopenharmony_ci      throw new Error();
2581cb0ef41Sopenharmony_ci    },
2591cb0ef41Sopenharmony_ci    configurable: true
2601cb0ef41Sopenharmony_ci  });
2611cb0ef41Sopenharmony_ci  assert.strictEqual(
2621cb0ef41Sopenharmony_ci    util.format('%s', new D()),
2631cb0ef41Sopenharmony_ci    'Custom'
2641cb0ef41Sopenharmony_ci  );
2651cb0ef41Sopenharmony_ci
2661cb0ef41Sopenharmony_ci  assert.strictEqual(
2671cb0ef41Sopenharmony_ci    util.format('%s', Object.create(null)),
2681cb0ef41Sopenharmony_ci    '[Object: null prototype] {}'
2691cb0ef41Sopenharmony_ci  );
2701cb0ef41Sopenharmony_ci}
2711cb0ef41Sopenharmony_ci
2721cb0ef41Sopenharmony_ci// JSON format specifier
2731cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%j'), '%j');
2741cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%j', 42), '42');
2751cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%j', '42'), '"42"');
2761cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%j %j', 42, 43), '42 43');
2771cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%j %j', 42), '42 %j');
2781cb0ef41Sopenharmony_ci
2791cb0ef41Sopenharmony_ci// Object format specifier
2801cb0ef41Sopenharmony_ciconst obj = {
2811cb0ef41Sopenharmony_ci  foo: 'bar',
2821cb0ef41Sopenharmony_ci  foobar: 1,
2831cb0ef41Sopenharmony_ci  func: function() {}
2841cb0ef41Sopenharmony_ci};
2851cb0ef41Sopenharmony_ciconst nestedObj = {
2861cb0ef41Sopenharmony_ci  foo: 'bar',
2871cb0ef41Sopenharmony_ci  foobar: {
2881cb0ef41Sopenharmony_ci    foo: 'bar',
2891cb0ef41Sopenharmony_ci    func: function() {}
2901cb0ef41Sopenharmony_ci  }
2911cb0ef41Sopenharmony_ci};
2921cb0ef41Sopenharmony_ciconst nestedObj2 = {
2931cb0ef41Sopenharmony_ci  foo: 'bar',
2941cb0ef41Sopenharmony_ci  foobar: 1,
2951cb0ef41Sopenharmony_ci  func: [{ a: function() {} }]
2961cb0ef41Sopenharmony_ci};
2971cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%o'), '%o');
2981cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%o', 42), '42');
2991cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%o', 'foo'), '\'foo\'');
3001cb0ef41Sopenharmony_ciassert.strictEqual(
3011cb0ef41Sopenharmony_ci  util.format('%o', obj),
3021cb0ef41Sopenharmony_ci  '{\n' +
3031cb0ef41Sopenharmony_ci  '  foo: \'bar\',\n' +
3041cb0ef41Sopenharmony_ci  '  foobar: 1,\n' +
3051cb0ef41Sopenharmony_ci  '  func: <ref *1> [Function: func] {\n' +
3061cb0ef41Sopenharmony_ci  '    [length]: 0,\n' +
3071cb0ef41Sopenharmony_ci  '    [name]: \'func\',\n' +
3081cb0ef41Sopenharmony_ci  '    [prototype]: { [constructor]: [Circular *1] }\n' +
3091cb0ef41Sopenharmony_ci  '  }\n' +
3101cb0ef41Sopenharmony_ci  '}');
3111cb0ef41Sopenharmony_ciassert.strictEqual(
3121cb0ef41Sopenharmony_ci  util.format('%o', nestedObj2),
3131cb0ef41Sopenharmony_ci  '{\n' +
3141cb0ef41Sopenharmony_ci  '  foo: \'bar\',\n' +
3151cb0ef41Sopenharmony_ci  '  foobar: 1,\n' +
3161cb0ef41Sopenharmony_ci  '  func: [\n' +
3171cb0ef41Sopenharmony_ci  '    {\n' +
3181cb0ef41Sopenharmony_ci  '      a: <ref *1> [Function: a] {\n' +
3191cb0ef41Sopenharmony_ci  '        [length]: 0,\n' +
3201cb0ef41Sopenharmony_ci  '        [name]: \'a\',\n' +
3211cb0ef41Sopenharmony_ci  '        [prototype]: { [constructor]: [Circular *1] }\n' +
3221cb0ef41Sopenharmony_ci  '      }\n' +
3231cb0ef41Sopenharmony_ci  '    },\n' +
3241cb0ef41Sopenharmony_ci  '    [length]: 1\n' +
3251cb0ef41Sopenharmony_ci  '  ]\n' +
3261cb0ef41Sopenharmony_ci  '}');
3271cb0ef41Sopenharmony_ciassert.strictEqual(
3281cb0ef41Sopenharmony_ci  util.format('%o', nestedObj),
3291cb0ef41Sopenharmony_ci  '{\n' +
3301cb0ef41Sopenharmony_ci  '  foo: \'bar\',\n' +
3311cb0ef41Sopenharmony_ci  '  foobar: {\n' +
3321cb0ef41Sopenharmony_ci  '    foo: \'bar\',\n' +
3331cb0ef41Sopenharmony_ci  '    func: <ref *1> [Function: func] {\n' +
3341cb0ef41Sopenharmony_ci  '      [length]: 0,\n' +
3351cb0ef41Sopenharmony_ci  '      [name]: \'func\',\n' +
3361cb0ef41Sopenharmony_ci  '      [prototype]: { [constructor]: [Circular *1] }\n' +
3371cb0ef41Sopenharmony_ci  '    }\n' +
3381cb0ef41Sopenharmony_ci  '  }\n' +
3391cb0ef41Sopenharmony_ci  '}');
3401cb0ef41Sopenharmony_ciassert.strictEqual(
3411cb0ef41Sopenharmony_ci  util.format('%o %o', obj, obj),
3421cb0ef41Sopenharmony_ci  '{\n' +
3431cb0ef41Sopenharmony_ci  '  foo: \'bar\',\n' +
3441cb0ef41Sopenharmony_ci  '  foobar: 1,\n' +
3451cb0ef41Sopenharmony_ci  '  func: <ref *1> [Function: func] {\n' +
3461cb0ef41Sopenharmony_ci  '    [length]: 0,\n' +
3471cb0ef41Sopenharmony_ci  '    [name]: \'func\',\n' +
3481cb0ef41Sopenharmony_ci  '    [prototype]: { [constructor]: [Circular *1] }\n' +
3491cb0ef41Sopenharmony_ci  '  }\n' +
3501cb0ef41Sopenharmony_ci  '} {\n' +
3511cb0ef41Sopenharmony_ci  '  foo: \'bar\',\n' +
3521cb0ef41Sopenharmony_ci  '  foobar: 1,\n' +
3531cb0ef41Sopenharmony_ci  '  func: <ref *1> [Function: func] {\n' +
3541cb0ef41Sopenharmony_ci  '    [length]: 0,\n' +
3551cb0ef41Sopenharmony_ci  '    [name]: \'func\',\n' +
3561cb0ef41Sopenharmony_ci  '    [prototype]: { [constructor]: [Circular *1] }\n' +
3571cb0ef41Sopenharmony_ci  '  }\n' +
3581cb0ef41Sopenharmony_ci  '}');
3591cb0ef41Sopenharmony_ciassert.strictEqual(
3601cb0ef41Sopenharmony_ci  util.format('%o %o', obj),
3611cb0ef41Sopenharmony_ci  '{\n' +
3621cb0ef41Sopenharmony_ci  '  foo: \'bar\',\n' +
3631cb0ef41Sopenharmony_ci  '  foobar: 1,\n' +
3641cb0ef41Sopenharmony_ci  '  func: <ref *1> [Function: func] {\n' +
3651cb0ef41Sopenharmony_ci  '    [length]: 0,\n' +
3661cb0ef41Sopenharmony_ci  '    [name]: \'func\',\n' +
3671cb0ef41Sopenharmony_ci  '    [prototype]: { [constructor]: [Circular *1] }\n' +
3681cb0ef41Sopenharmony_ci  '  }\n' +
3691cb0ef41Sopenharmony_ci  '} %o');
3701cb0ef41Sopenharmony_ci
3711cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%O'), '%O');
3721cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%O', 42), '42');
3731cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%O', 'foo'), '\'foo\'');
3741cb0ef41Sopenharmony_ciassert.strictEqual(
3751cb0ef41Sopenharmony_ci  util.format('%O', obj),
3761cb0ef41Sopenharmony_ci  '{ foo: \'bar\', foobar: 1, func: [Function: func] }');
3771cb0ef41Sopenharmony_ciassert.strictEqual(
3781cb0ef41Sopenharmony_ci  util.format('%O', nestedObj),
3791cb0ef41Sopenharmony_ci  '{ foo: \'bar\', foobar: { foo: \'bar\', func: [Function: func] } }');
3801cb0ef41Sopenharmony_ciassert.strictEqual(
3811cb0ef41Sopenharmony_ci  util.format('%O %O', obj, obj),
3821cb0ef41Sopenharmony_ci  '{ foo: \'bar\', foobar: 1, func: [Function: func] } ' +
3831cb0ef41Sopenharmony_ci  '{ foo: \'bar\', foobar: 1, func: [Function: func] }');
3841cb0ef41Sopenharmony_ciassert.strictEqual(
3851cb0ef41Sopenharmony_ci  util.format('%O %O', obj),
3861cb0ef41Sopenharmony_ci  '{ foo: \'bar\', foobar: 1, func: [Function: func] } %O');
3871cb0ef41Sopenharmony_ci
3881cb0ef41Sopenharmony_ci// Various format specifiers
3891cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%%s%s', 'foo'), '%sfoo');
3901cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s:%s'), '%s:%s');
3911cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s:%s', undefined), 'undefined:%s');
3921cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s:%s', 'foo'), 'foo:%s');
3931cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s:%i', 'foo'), 'foo:%i');
3941cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s:%f', 'foo'), 'foo:%f');
3951cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s:%s', 'foo', 'bar'), 'foo:bar');
3961cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%s:%s', 'foo', 'bar', 'baz'), 'foo:bar baz');
3971cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%%%s%%', 'hi'), '%hi%');
3981cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%%%s%%%%', 'hi'), '%hi%%');
3991cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%sbc%%def', 'a'), 'abc%def');
4001cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%d:%d', 12, 30), '12:30');
4011cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%d:%d', 12), '12:%d');
4021cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%d:%d'), '%d:%d');
4031cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%i:%i', 12, 30), '12:30');
4041cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%i:%i', 12), '12:%i');
4051cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%i:%i'), '%i:%i');
4061cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f:%f', 12, 30), '12:30');
4071cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f:%f', 12), '12:%f');
4081cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%f:%f'), '%f:%f');
4091cb0ef41Sopenharmony_ciassert.strictEqual(util.format('o: %j, a: %j', {}, []), 'o: {}, a: []');
4101cb0ef41Sopenharmony_ciassert.strictEqual(util.format('o: %j, a: %j', {}), 'o: {}, a: %j');
4111cb0ef41Sopenharmony_ciassert.strictEqual(util.format('o: %j, a: %j'), 'o: %j, a: %j');
4121cb0ef41Sopenharmony_ciassert.strictEqual(util.format('o: %o, a: %O', {}, []), 'o: {}, a: []');
4131cb0ef41Sopenharmony_ciassert.strictEqual(util.format('o: %o, a: %o', {}), 'o: {}, a: %o');
4141cb0ef41Sopenharmony_ciassert.strictEqual(util.format('o: %O, a: %O'), 'o: %O, a: %O');
4151cb0ef41Sopenharmony_ci
4161cb0ef41Sopenharmony_ci
4171cb0ef41Sopenharmony_ci// Invalid format specifiers
4181cb0ef41Sopenharmony_ciassert.strictEqual(util.format('a% b', 'x'), 'a% b x');
4191cb0ef41Sopenharmony_ciassert.strictEqual(util.format('percent: %d%, fraction: %d', 10, 0.1),
4201cb0ef41Sopenharmony_ci                   'percent: 10%, fraction: 0.1');
4211cb0ef41Sopenharmony_ciassert.strictEqual(util.format('abc%', 1), 'abc% 1');
4221cb0ef41Sopenharmony_ci
4231cb0ef41Sopenharmony_ci// Additional arguments after format specifiers
4241cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%i', 1, 'number'), '1 number');
4251cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%i', 1, () => {}), '1 [Function (anonymous)]');
4261cb0ef41Sopenharmony_ci
4271cb0ef41Sopenharmony_ci// %c from https://console.spec.whatwg.org/
4281cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%c'), '%c');
4291cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%cab'), '%cab');
4301cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%cab', 'color: blue'), 'ab');
4311cb0ef41Sopenharmony_ciassert.strictEqual(util.format('%cab', 'color: blue', 'c'), 'ab c');
4321cb0ef41Sopenharmony_ci
4331cb0ef41Sopenharmony_ci{
4341cb0ef41Sopenharmony_ci  const o = {};
4351cb0ef41Sopenharmony_ci  o.o = o;
4361cb0ef41Sopenharmony_ci  assert.strictEqual(util.format('%j', o), '[Circular]');
4371cb0ef41Sopenharmony_ci}
4381cb0ef41Sopenharmony_ci
4391cb0ef41Sopenharmony_ci{
4401cb0ef41Sopenharmony_ci  const o = {
4411cb0ef41Sopenharmony_ci    toJSON() {
4421cb0ef41Sopenharmony_ci      throw new Error('Not a circular object but still not serializable');
4431cb0ef41Sopenharmony_ci    }
4441cb0ef41Sopenharmony_ci  };
4451cb0ef41Sopenharmony_ci  assert.throws(() => util.format('%j', o),
4461cb0ef41Sopenharmony_ci                /^Error: Not a circular object but still not serializable$/);
4471cb0ef41Sopenharmony_ci}
4481cb0ef41Sopenharmony_ci
4491cb0ef41Sopenharmony_ci// Errors
4501cb0ef41Sopenharmony_ciconst err = new Error('foo');
4511cb0ef41Sopenharmony_ciassert.strictEqual(util.format(err), err.stack);
4521cb0ef41Sopenharmony_ciclass CustomError extends Error {
4531cb0ef41Sopenharmony_ci  constructor(msg) {
4541cb0ef41Sopenharmony_ci    super();
4551cb0ef41Sopenharmony_ci    Object.defineProperty(this, 'message',
4561cb0ef41Sopenharmony_ci                          { value: msg, enumerable: false });
4571cb0ef41Sopenharmony_ci    Object.defineProperty(this, 'name',
4581cb0ef41Sopenharmony_ci                          { value: 'CustomError', enumerable: false });
4591cb0ef41Sopenharmony_ci    Error.captureStackTrace(this, CustomError);
4601cb0ef41Sopenharmony_ci  }
4611cb0ef41Sopenharmony_ci}
4621cb0ef41Sopenharmony_ciconst customError = new CustomError('bar');
4631cb0ef41Sopenharmony_ciassert.strictEqual(util.format(customError), customError.stack);
4641cb0ef41Sopenharmony_ci// Doesn't capture stack trace
4651cb0ef41Sopenharmony_cifunction BadCustomError(msg) {
4661cb0ef41Sopenharmony_ci  Error.call(this);
4671cb0ef41Sopenharmony_ci  Object.defineProperty(this, 'message',
4681cb0ef41Sopenharmony_ci                        { value: msg, enumerable: false });
4691cb0ef41Sopenharmony_ci  Object.defineProperty(this, 'name',
4701cb0ef41Sopenharmony_ci                        { value: 'BadCustomError', enumerable: false });
4711cb0ef41Sopenharmony_ci}
4721cb0ef41Sopenharmony_ciObject.setPrototypeOf(BadCustomError.prototype, Error.prototype);
4731cb0ef41Sopenharmony_ciObject.setPrototypeOf(BadCustomError, Error);
4741cb0ef41Sopenharmony_ciassert.strictEqual(util.format(new BadCustomError('foo')),
4751cb0ef41Sopenharmony_ci                   '[BadCustomError: foo]');
4761cb0ef41Sopenharmony_ci
4771cb0ef41Sopenharmony_ci// The format of arguments should not depend on type of the first argument
4781cb0ef41Sopenharmony_ciassert.strictEqual(util.format('1', '1'), '1 1');
4791cb0ef41Sopenharmony_ciassert.strictEqual(util.format(1, '1'), '1 1');
4801cb0ef41Sopenharmony_ciassert.strictEqual(util.format('1', 1), '1 1');
4811cb0ef41Sopenharmony_ciassert.strictEqual(util.format(1, -0), '1 -0');
4821cb0ef41Sopenharmony_ciassert.strictEqual(util.format('1', () => {}), '1 [Function (anonymous)]');
4831cb0ef41Sopenharmony_ciassert.strictEqual(util.format(1, () => {}), '1 [Function (anonymous)]');
4841cb0ef41Sopenharmony_ciassert.strictEqual(util.format('1', "'"), "1 '");
4851cb0ef41Sopenharmony_ciassert.strictEqual(util.format(1, "'"), "1 '");
4861cb0ef41Sopenharmony_ciassert.strictEqual(util.format('1', 'number'), '1 number');
4871cb0ef41Sopenharmony_ciassert.strictEqual(util.format(1, 'number'), '1 number');
4881cb0ef41Sopenharmony_ciassert.strictEqual(util.format(5n), '5n');
4891cb0ef41Sopenharmony_ciassert.strictEqual(util.format(5n, 5n), '5n 5n');
4901cb0ef41Sopenharmony_ci
4911cb0ef41Sopenharmony_ci// Check `formatWithOptions`.
4921cb0ef41Sopenharmony_ciassert.strictEqual(
4931cb0ef41Sopenharmony_ci  util.formatWithOptions(
4941cb0ef41Sopenharmony_ci    { colors: true },
4951cb0ef41Sopenharmony_ci    true, undefined, Symbol(), 1, 5n, null, 'foobar'
4961cb0ef41Sopenharmony_ci  ),
4971cb0ef41Sopenharmony_ci  '\u001b[33mtrue\u001b[39m ' +
4981cb0ef41Sopenharmony_ci    '\u001b[90mundefined\u001b[39m ' +
4991cb0ef41Sopenharmony_ci    '\u001b[32mSymbol()\u001b[39m ' +
5001cb0ef41Sopenharmony_ci    '\u001b[33m1\u001b[39m ' +
5011cb0ef41Sopenharmony_ci    '\u001b[33m5n\u001b[39m ' +
5021cb0ef41Sopenharmony_ci    '\u001b[1mnull\u001b[22m ' +
5031cb0ef41Sopenharmony_ci    'foobar'
5041cb0ef41Sopenharmony_ci);
5051cb0ef41Sopenharmony_ci
5061cb0ef41Sopenharmony_ciassert.strictEqual(
5071cb0ef41Sopenharmony_ci  util.format(new SharedArrayBuffer(4)),
5081cb0ef41Sopenharmony_ci  'SharedArrayBuffer { [Uint8Contents]: <00 00 00 00>, byteLength: 4 }'
5091cb0ef41Sopenharmony_ci);
5101cb0ef41Sopenharmony_ci
5111cb0ef41Sopenharmony_ciassert.strictEqual(
5121cb0ef41Sopenharmony_ci  util.formatWithOptions(
5131cb0ef41Sopenharmony_ci    { colors: true, compact: 3 },
5141cb0ef41Sopenharmony_ci    '%s', [ 1, { a: true }]
5151cb0ef41Sopenharmony_ci  ),
5161cb0ef41Sopenharmony_ci  '[ 1, [Object] ]'
5171cb0ef41Sopenharmony_ci);
5181cb0ef41Sopenharmony_ci
5191cb0ef41Sopenharmony_ci[
5201cb0ef41Sopenharmony_ci  undefined,
5211cb0ef41Sopenharmony_ci  null,
5221cb0ef41Sopenharmony_ci  false,
5231cb0ef41Sopenharmony_ci  5n,
5241cb0ef41Sopenharmony_ci  5,
5251cb0ef41Sopenharmony_ci  'test',
5261cb0ef41Sopenharmony_ci  Symbol(),
5271cb0ef41Sopenharmony_ci].forEach((invalidOptions) => {
5281cb0ef41Sopenharmony_ci  assert.throws(() => {
5291cb0ef41Sopenharmony_ci    util.formatWithOptions(invalidOptions, { a: true });
5301cb0ef41Sopenharmony_ci  }, {
5311cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
5321cb0ef41Sopenharmony_ci    message: /"inspectOptions".+object/
5331cb0ef41Sopenharmony_ci  });
5341cb0ef41Sopenharmony_ci});
535