diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2019-11-09 16:14:24 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-11-30 01:19:14 +0100 |
commit | be3091136161d8c3793dfe53f6e82f96e2b6a177 (patch) | |
tree | fc136ae454dac879e39b5411d04b0f154a4a7437 /test/parallel/test-util-format.js | |
parent | f0181d9980a0268eb0183bdee28abcc79b2d740c (diff) | |
download | android-node-v8-be3091136161d8c3793dfe53f6e82f96e2b6a177.tar.gz android-node-v8-be3091136161d8c3793dfe53f6e82f96e2b6a177.tar.bz2 android-node-v8-be3091136161d8c3793dfe53f6e82f96e2b6a177.zip |
util: fix .format() not always calling toString when it should be
This makes sure that `util.format('%s', object)` will always call
a user defined `toString` function. It was formerly not the case
when the object had the function declared on the super class.
At the same time this also makes sure that getters won't be
triggered accessing the `constructor` property.
PR-URL: https://github.com/nodejs/node/pull/30343
Fixes: https://github.com/nodejs/node/issues/30333
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Diffstat (limited to 'test/parallel/test-util-format.js')
-rw-r--r-- | test/parallel/test-util-format.js | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/test/parallel/test-util-format.js b/test/parallel/test-util-format.js index 2ef0528490..e07ec6d6a3 100644 --- a/test/parallel/test-util-format.js +++ b/test/parallel/test-util-format.js @@ -160,6 +160,66 @@ assert.strictEqual(util.format('%s', () => 5), '() => 5'); util.format('%s', new Foobar(5)), 'Foobar [ <5 empty items>, aaa: true ]' ); + + // Subclassing: + class B extends Foo {} + + function C() {} + C.prototype.toString = function() { + return 'Custom'; + }; + + function D() { + C.call(this); + } + D.prototype = Object.create(C.prototype); + + assert.strictEqual( + util.format('%s', new B()), + 'Bar' + ); + assert.strictEqual( + util.format('%s', new C()), + 'Custom' + ); + assert.strictEqual( + util.format('%s', new D()), + 'Custom' + ); + + D.prototype.constructor = D; + assert.strictEqual( + util.format('%s', new D()), + 'Custom' + ); + + D.prototype.constructor = null; + assert.strictEqual( + util.format('%s', new D()), + 'Custom' + ); + + D.prototype.constructor = { name: 'Foobar' }; + assert.strictEqual( + util.format('%s', new D()), + 'Custom' + ); + + Object.defineProperty(D.prototype, 'constructor', { + get() { + throw new Error(); + }, + configurable: true + }); + assert.strictEqual( + util.format('%s', new D()), + 'Custom' + ); + + assert.strictEqual( + util.format('%s', Object.create(null)), + '[Object: null prototype] {}' + ); } // JSON format specifier |