From 964ea7c8387ba68556ffabc24510240c0f2a4515 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 20 Dec 2022 10:43:33 -0800 Subject: [PATCH] [Tests] add more test coverage from https://github.com/tc39/test262/pull/3755 --- .eslintrc | 1 + package.json | 3 +++ test/tests.js | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/.eslintrc b/.eslintrc index de8c630..e611ab4 100644 --- a/.eslintrc +++ b/.eslintrc @@ -5,6 +5,7 @@ "rules": { "id-length": 0, + "max-lines-per-function": 0, "new-cap": [2, { "capIsNewExceptions": [ "CodePointAt", diff --git a/package.json b/package.json index 4bd776b..a88448c 100644 --- a/package.json +++ b/package.json @@ -69,8 +69,11 @@ "es-value-fixtures": "^1.4.2", "eslint": "=8.8.0", "functions-have-names": "^1.2.3", + "has-bigints": "^1.0.2", "has-strict-mode": "^1.0.1", + "has-symbols": "^1.0.3", "in-publish": "^2.0.1", + "mock-property": "^1.0.0", "npmignore": "^0.3.0", "nyc": "^10.3.2", "safe-publish-latest": "^2.0.0", diff --git a/test/tests.js b/test/tests.js index c52d88e..27499c4 100644 --- a/test/tests.js +++ b/test/tests.js @@ -3,6 +3,9 @@ var v = require('es-value-fixtures'); var forEach = require('for-each'); var inspect = require('object-inspect'); +var mockProperty = require('mock-property'); +var hasSymbols = require('has-symbols')(); +var hasBigInts = require('has-bigints')(); var SymbolDescriptiveString = require('es-abstract/2022/SymbolDescriptiveString'); @@ -13,6 +16,38 @@ var wholePoo = leadingPoo + trailingPoo; var replacementChar = '\uFFFD'; module.exports = function (toWellFormed, t) { + t.test('does not call prototype toString methods on primitives', function (st) { + st.teardown(mockProperty(Boolean.prototype, 'toString', function fakeToString() { + st.fail('Boolean.prototype.toString should not be called'); + })); + st.teardown(mockProperty(Number.prototype, 'toString', function fakeToString() { + st.fail('Number.prototype.toString should not be called'); + })); + if (hasBigInts) { + st.teardown(mockProperty(BigInt.prototype, 'toString', function fakeToString() { + st.fail('BigInt.prototype.toString should not be called'); + })); + } + if (hasSymbols) { + st.teardown(mockProperty(Symbol.prototype, 'toString', function fakeToString() { + st.fail('Symbol.prototype.toString should not be called'); + })); + } + + forEach(v.nonNullPrimitives, function (nonNullPrimitive) { + if (typeof nonNullPrimitive === 'symbol') { + st['throws']( + function () { toWellFormed(nonNullPrimitive); }, + inspect(nonNullPrimitive) + ' throws when implicitly coerced to a string, and does not call the proto method' + ); + } else if (typeof nonNullPrimitive !== 'string') { + st.equal(toWellFormed(nonNullPrimitive), String(nonNullPrimitive), inspect(nonNullPrimitive) + ' stringifies without calling the proto method'); + } + }); + + st.end(); + }); + t.test('well-formed strings', function (st) { forEach(v.nonStrings.concat( v.strings,