From 49e111ed111fd952e6285d7adea629200fffa3ed Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 15 Oct 2024 10:20:51 -0700 Subject: [PATCH] add `Error.isError` tests See https://github.com/tc39/proposal-is-error/issues/7 --- test/built-ins/Error/isError/bigints.js | 14 ++++++++ .../built-ins/Error/isError/error-subclass.js | 36 +++++++++++++++++++ .../Error/isError/errors-other-realm.js | 26 ++++++++++++++ test/built-ins/Error/isError/errors.js | 23 ++++++++++++ test/built-ins/Error/isError/fake-errors.js | 21 +++++++++++ .../Error/isError/is-a-constructor.js | 24 +++++++++++++ test/built-ins/Error/isError/name.js | 11 ++++++ .../isError/non-error-objects-other-realm.js | 31 ++++++++++++++++ .../Error/isError/non-error-objects.js | 28 +++++++++++++++ test/built-ins/Error/isError/primitives.js | 22 ++++++++++++ test/built-ins/Error/isError/prop-desc.js | 18 ++++++++++ test/built-ins/Error/isError/symbols.js | 11 ++++++ 12 files changed, 265 insertions(+) create mode 100644 test/built-ins/Error/isError/bigints.js create mode 100644 test/built-ins/Error/isError/error-subclass.js create mode 100644 test/built-ins/Error/isError/errors-other-realm.js create mode 100644 test/built-ins/Error/isError/errors.js create mode 100644 test/built-ins/Error/isError/fake-errors.js create mode 100644 test/built-ins/Error/isError/is-a-constructor.js create mode 100644 test/built-ins/Error/isError/name.js create mode 100644 test/built-ins/Error/isError/non-error-objects-other-realm.js create mode 100644 test/built-ins/Error/isError/non-error-objects.js create mode 100644 test/built-ins/Error/isError/primitives.js create mode 100644 test/built-ins/Error/isError/prop-desc.js create mode 100644 test/built-ins/Error/isError/symbols.js diff --git a/test/built-ins/Error/isError/bigints.js b/test/built-ins/Error/isError/bigints.js new file mode 100644 index 0000000000..80f5f9a743 --- /dev/null +++ b/test/built-ins/Error/isError/bigints.js @@ -0,0 +1,14 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-error.iserror +description: > + Returns false on bigints +features: [BigInt] +---*/ + +assert.equal(Error.isError(0n), false); +assert.equal(Error.isError(42n), false); +assert.equal(Error.isError(new BigInt(0)), false); +assert.equal(Error.isError(new BigInt(42)), false); diff --git a/test/built-ins/Error/isError/error-subclass.js b/test/built-ins/Error/isError/error-subclass.js new file mode 100644 index 0000000000..98829ffbaf --- /dev/null +++ b/test/built-ins/Error/isError/error-subclass.js @@ -0,0 +1,36 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-error.iserror +description: > + Returns true on userland Error subclasses +---*/ + +class MyError extends Error {} +class MyEvalError extends EvalError {} +class MyRangeError extends RangeError {} +class MyReferenceError extends ReferenceError {} +class MySyntaxError extends SyntaxError {} +class MyTypeError extends TypeError {} +class MyURIError extends URIError {} + +assert.equal(Error.isError(new MyError()), true); +assert.equal(Error.isError(new MyEvalError()), true); +assert.equal(Error.isError(new MyRangeError()), true); +assert.equal(Error.isError(new MyReferenceError()), true); +assert.equal(Error.isError(new MySyntaxError()), true); +assert.equal(Error.isError(new MyTypeError()), true); +assert.equal(Error.isError(new MyURIError()), true); + +if (typeof AggregateError !== 'undefined') { + class MyAggregateError extends AggregateError {} + + assert.equal(Error.isError(new MyAggregateError()), true); +} + +if (typeof SuppressedError !== 'undefined') { + class MySuppressedError extends SuppressedError {} + + assert.equal(Error.isError(new MySuppressedError()), true); +} diff --git a/test/built-ins/Error/isError/errors-other-realm.js b/test/built-ins/Error/isError/errors-other-realm.js new file mode 100644 index 0000000000..cd963e7e37 --- /dev/null +++ b/test/built-ins/Error/isError/errors-other-realm.js @@ -0,0 +1,26 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-error.iserror +description: > + Returns true on Error and Error subclass instances from a different realm +features: [cross-realm] +---*/ + +var other = $262.createRealm().global; + +assert.equal(Error.isError(new other.Error()), true); +assert.equal(Error.isError(new other.EvalError()), true); +assert.equal(Error.isError(new other.RangeError()), true); +assert.equal(Error.isError(new other.ReferenceError()), true); +assert.equal(Error.isError(new other.SyntaxError()), true); +assert.equal(Error.isError(new other.TypeError()), true); +assert.equal(Error.isError(new other.URIError()), true); + +if (typeof AggregateError !== 'undefined') { + assert.equal(Error.isError(new other.AggregateError()), true); +} +if (typeof SuppressedError !== 'undefined') { + assert.equal(Error.isError(new other.SuppressedError()), true); +} diff --git a/test/built-ins/Error/isError/errors.js b/test/built-ins/Error/isError/errors.js new file mode 100644 index 0000000000..7c13e84dfc --- /dev/null +++ b/test/built-ins/Error/isError/errors.js @@ -0,0 +1,23 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-error.iserror +description: > + Returns true on Error and Error subclass instances +---*/ + +assert.equal(Error.isError(new Error()), true); +assert.equal(Error.isError(new EvalError()), true); +assert.equal(Error.isError(new RangeError()), true); +assert.equal(Error.isError(new ReferenceError()), true); +assert.equal(Error.isError(new SyntaxError()), true); +assert.equal(Error.isError(new TypeError()), true); +assert.equal(Error.isError(new URIError()), true); + +if (typeof AggregateError !== 'undefined') { + assert.equal(Error.isError(new AggregateError()), true); +} +if (typeof SuppressedError !== 'undefined') { + assert.equal(Error.isError(new SuppressedError()), true); +} diff --git a/test/built-ins/Error/isError/fake-errors.js b/test/built-ins/Error/isError/fake-errors.js new file mode 100644 index 0000000000..35739c5909 --- /dev/null +++ b/test/built-ins/Error/isError/fake-errors.js @@ -0,0 +1,21 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-error.iserror +description: > + Returns false on non-Error objects pretending to be an Error +---*/ + +var fakeError = { + __proto__: Error.prototype, + constructor: Error, + message: '', + stack: new Error().stack +}; + +if (typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol') { + fakeError[Symbol.toStringTag] = 'Error'; +} + +assert.equal(Error.isError(fakeError), false); diff --git a/test/built-ins/Error/isError/is-a-constructor.js b/test/built-ins/Error/isError/is-a-constructor.js new file mode 100644 index 0000000000..87cc12fe3c --- /dev/null +++ b/test/built-ins/Error/isError/is-a-constructor.js @@ -0,0 +1,24 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-error.iserror +description: > + Error.isError does not implement [[Construct]] +info: | + IsConstructor ( argument ) + + The abstract operation IsConstructor takes argument argument (an ECMAScript language value). + It determines if argument is a function object with a [[Construct]] internal method. + It performs the following steps when called: + + If Type(argument) is not Object, return false. + If argument has a [[Construct]] internal method, return true. + Return false. +includes: [isConstructor.js] +features: [Reflect.construct] +---*/ + +assert.sameValue(isConstructor(Error.isError), false, 'isConstructor(Error.isError) must return false'); +assert.throws(function () { new Error.isError(); }); + diff --git a/test/built-ins/Error/isError/name.js b/test/built-ins/Error/isError/name.js new file mode 100644 index 0000000000..ba00cd2c12 --- /dev/null +++ b/test/built-ins/Error/isError/name.js @@ -0,0 +1,11 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-error.iserror +description: > + The initial value of Error.isError.name is "isError". +---*/ + +assert.sameValue(Error.isError.name, 'isError'); + diff --git a/test/built-ins/Error/isError/non-error-objects-other-realm.js b/test/built-ins/Error/isError/non-error-objects-other-realm.js new file mode 100644 index 0000000000..927b26f083 --- /dev/null +++ b/test/built-ins/Error/isError/non-error-objects-other-realm.js @@ -0,0 +1,31 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-error.iserror +description: > + Returns false on non-Error objects from a different realm +features: [cross-realm] +---*/ + +var other = $262.createRealm().global; + +assert.equal(Error.isError(new other.Object()), false); +assert.equal(Error.isError(new other.Array()), false); +assert.equal(Error.isError(new other.Function('')), false); +assert.equal(Error.isError(new other.RegExp('a')), false); + +assert.equal(Error.isError(other.Error), false); +assert.equal(Error.isError(other.EvalError), false); +assert.equal(Error.isError(other.RangeError), false); +assert.equal(Error.isError(other.ReferenceError), false); +assert.equal(Error.isError(other.SyntaxError), false); +assert.equal(Error.isError(other.TypeError), false); +assert.equal(Error.isError(other.URIError), false); + +if (typeof other.AggregateError !== 'undefined') { + assert.equal(Error.isError(other.AggregateError), false); +} +if (typeof other.SuppressedError !== 'undefined') { + assert.equal(Error.isError(other.SuppressedError), false); +} diff --git a/test/built-ins/Error/isError/non-error-objects.js b/test/built-ins/Error/isError/non-error-objects.js new file mode 100644 index 0000000000..8a136b50ac --- /dev/null +++ b/test/built-ins/Error/isError/non-error-objects.js @@ -0,0 +1,28 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-error.iserror +description: > + Returns false on non-Error objects +---*/ + +assert.equal(Error.isError({}), false); +assert.equal(Error.isError([]), false); +assert.equal(Error.isError(function () {}), false); +assert.equal(Error.isError(/a/g), false); + +assert.equal(Error.isError(Error), false); +assert.equal(Error.isError(EvalError), false); +assert.equal(Error.isError(RangeError), false); +assert.equal(Error.isError(ReferenceError), false); +assert.equal(Error.isError(SyntaxError), false); +assert.equal(Error.isError(TypeError), false); +assert.equal(Error.isError(URIError), false); + +if (typeof AggregateError !== 'undefined') { + assert.equal(Error.isError(AggregateError), false); +} +if (typeof SuppressedError !== 'undefined') { + assert.equal(Error.isError(SuppressedError), false); +} diff --git a/test/built-ins/Error/isError/primitives.js b/test/built-ins/Error/isError/primitives.js new file mode 100644 index 0000000000..ce40a30e17 --- /dev/null +++ b/test/built-ins/Error/isError/primitives.js @@ -0,0 +1,22 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-error.iserror +description: > + Returns false on primitives +---*/ + +assert.equal(Error.isError(), false); +assert.equal(Error.isError(undefined), false); +assert.equal(Error.isError(null), false); +assert.equal(Error.isError(true), false); +assert.equal(Error.isError(false), false); +assert.equal(Error.isError(0), false); +assert.equal(Error.isError(-0), false); +assert.equal(Error.isError(NaN), false); +assert.equal(Error.isError(Infinity), false); +assert.equal(Error.isError(-Infinity), false); +assert.equal(Error.isError(42), false); +assert.equal(Error.isError(''), false); +assert.equal(Error.isError('foo'), false); diff --git a/test/built-ins/Error/isError/prop-desc.js b/test/built-ins/Error/isError/prop-desc.js new file mode 100644 index 0000000000..3d582e7a77 --- /dev/null +++ b/test/built-ins/Error/isError/prop-desc.js @@ -0,0 +1,18 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-error.iserror +description: Property descriptor for Error.isError +info: | + Every other data property described in clauses 18 through 26 and in Annex B.2 + has the attributes { [[Writable]]: true, [[Enumerable]]: false, + [[Configurable]]: true } unless otherwise specified. +includes: [propertyHelper.js] +---*/ + +verifyProperty(Error, "isError", { + writable: true, + enumerable: false, + configurable: true +}); diff --git a/test/built-ins/Error/isError/symbols.js b/test/built-ins/Error/isError/symbols.js new file mode 100644 index 0000000000..91a4078c1d --- /dev/null +++ b/test/built-ins/Error/isError/symbols.js @@ -0,0 +1,11 @@ +// Copyright (C) 2024 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-error.iserror +description: > + Returns false on symbols +features: [Symbol] +---*/ + +assert.equal(Error.isError(new Symbol()), false);