diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2018-12-23 17:16:14 +0100 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-12-27 22:30:31 +0100 |
commit | bd13afb04a551b762d45d041d30790d4ac6d7f4a (patch) | |
tree | 9fe0db24d6d0a4526d727849fe83c5fb3951eaf2 /test | |
parent | 728777d2d0634a9c5ea0726b4d08ca91ed5ab503 (diff) | |
download | android-node-v8-bd13afb04a551b762d45d041d30790d4ac6d7f4a.tar.gz android-node-v8-bd13afb04a551b762d45d041d30790d4ac6d7f4a.tar.bz2 android-node-v8-bd13afb04a551b762d45d041d30790d4ac6d7f4a.zip |
util: make inspect aware of RegExp subclasses and null prototype
This adds support for inspect to distinguish regular expression
subclasses and ones with null prototype from "normal" regular
expressions.
PR-URL: https://github.com/nodejs/node/pull/25192
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/parallel/test-assert-deep.js | 2 | ||||
-rw-r--r-- | test/parallel/test-util-inspect.js | 43 |
2 files changed, 34 insertions, 11 deletions
diff --git a/test/parallel/test-assert-deep.js b/test/parallel/test-assert-deep.js index 68b3ee5497..b16b0f3ffc 100644 --- a/test/parallel/test-assert-deep.js +++ b/test/parallel/test-assert-deep.js @@ -149,7 +149,7 @@ assert.throws( { code: 'ERR_ASSERTION', message: `${defaultMsgStartFull}\n\n` + - "+ /test/\n- /test/ {\n- '0': '1'\n- }" + "+ /test/\n- MyRegExp /test/ {\n- '0': '1'\n- }" } ); diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 0d9cbdbe9d..9ee0272089 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -1580,15 +1580,7 @@ assert.strictEqual(util.inspect('"\''), '`"\'`'); // eslint-disable-next-line no-template-curly-in-string assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'"); -{ - assert.strictEqual( - util.inspect(Object.setPrototypeOf(/a/, null)), - '/undefined/undefined' - ); -} - -// Verify that throwing in valueOf and having no prototype still produces nice -// results. +// Verify that throwing in valueOf and toString still produces nice results. [ [new String(55), "[String: '55']"], [new Boolean(true), '[Boolean: true]'], @@ -1609,6 +1601,7 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'"); [new Promise((resolve) => setTimeout(resolve, 10)), 'Promise { <pending> }'], [new WeakSet(), 'WeakSet { <items unknown> }'], [new WeakMap(), 'WeakMap { <items unknown> }'], + [/foobar/g, '/foobar/g'] ].forEach(([value, expected]) => { Object.defineProperty(value, 'valueOf', { get() { @@ -1628,6 +1621,7 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'"); assert.notStrictEqual(util.inspect(value), expected); }); +// Verify that having no prototype still produces nice results. [ [[1, 3, 4], '[Array: null prototype] [ 1, 3, 4 ]'], [new Set([1, 2]), '[Set: null prototype] { 1, 2 }'], @@ -1652,7 +1646,8 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'"); '[DataView: null prototype] {\n byteLength: undefined,\n ' + 'byteOffset: undefined,\n buffer: undefined }'], [new SharedArrayBuffer(2), '[SharedArrayBuffer: null prototype] ' + - '{ byteLength: undefined }'] + '{ byteLength: undefined }'], + [/foobar/, '[RegExp: null prototype] /foobar/'] ].forEach(([value, expected]) => { assert.strictEqual( util.inspect(Object.setPrototypeOf(value, null)), @@ -1665,6 +1660,34 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'"); assert.notStrictEqual(util.inspect(value), expected); }); +// Verify that subclasses with and without prototype produce nice results. +[ + [RegExp, ['foobar', 'g'], '/foobar/g'] +].forEach(([base, input, rawExpected]) => { + class Foo extends base {} + const value = new Foo(...input); + const symbol = value[Symbol.toStringTag]; + const expected = `Foo ${symbol ? `[${symbol}] ` : ''}${rawExpected}`; + const expectedWithoutProto = `[${base.name}: null prototype] ${rawExpected}`; + assert.strictEqual(util.inspect(value), expected); + value.foo = 'bar'; + assert.notStrictEqual(util.inspect(value), expected); + delete value.foo; + assert.strictEqual( + util.inspect(Object.setPrototypeOf(value, null)), + expectedWithoutProto + ); + value.foo = 'bar'; + let res = util.inspect(value); + assert.notStrictEqual(res, expectedWithoutProto); + assert(/foo: 'bar'/.test(res), res); + delete value.foo; + value[Symbol('foo')] = 'yeah'; + res = util.inspect(value); + assert.notStrictEqual(res, expectedWithoutProto); + assert(/\[Symbol\(foo\)]: 'yeah'/.test(res), res); +}); + assert.strictEqual(inspect(1n), '1n'); assert.strictEqual(inspect(Object(-1n)), '[BigInt: -1n]'); assert.strictEqual(inspect(Object(13n)), '[BigInt: 13n]'); |