From 3e4e07c836d7471cf59c1537c5ac1a0722e6a915 Mon Sep 17 00:00:00 2001 From: legendecas Date: Sat, 24 Apr 2021 02:45:27 +0800 Subject: [PATCH] Add tests for stage 3 proposal error cause (#2965) * Add tests for error cause * Correct invocation of Proxy constructor Co-authored-by: Mike Pennisi --- test/built-ins/Error/cause_abrupt.js | 49 +++++++++++++++++++ test/built-ins/Error/cause_property.js | 36 ++++++++++++++ test/built-ins/Error/constructor.js | 36 ++++++++++++++ .../AggregateError/cause-property.js | 39 +++++++++++++++ .../cause_property_native_error.js | 44 +++++++++++++++++ 5 files changed, 204 insertions(+) create mode 100644 test/built-ins/Error/cause_abrupt.js create mode 100644 test/built-ins/Error/cause_property.js create mode 100644 test/built-ins/Error/constructor.js create mode 100644 test/built-ins/NativeErrors/AggregateError/cause-property.js create mode 100644 test/built-ins/NativeErrors/cause_property_native_error.js diff --git a/test/built-ins/Error/cause_abrupt.js b/test/built-ins/Error/cause_abrupt.js new file mode 100644 index 0000000000..3ed9bac001 --- /dev/null +++ b/test/built-ins/Error/cause_abrupt.js @@ -0,0 +1,49 @@ +// Copyright (C) 2021 Chengzhong Wu. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: InstallErrorCause on abrupt completions +info: | + Error ( message [ , options ] ) + + ... + 4. Perform ? InstallErrorCause(O, options). + ... + + 20.5.8.1 InstallErrorCause ( O, options ) + + 1. If Type(options) is Object and ? HasProperty(options, "cause") is true, then + a. Let cause be ? Get(options, "cause"). + b. Perform ! CreateNonEnumerableDataPropertyOrThrow(O, "cause", cause). + ... + +esid: sec-error-message +---*/ + +var message = "my-message"; +////////////////////////////////////////////////////////////////////////////// +// CHECK#0 +assert.throws(Test262Error, function () { + var options = new Proxy({}, { + has(target, prop) { + if (prop === "cause") { + throw new Test262Error("HasProperty"); + } + return prop in target; + }, + }); + var error = new Error(message, options); +}, "HasProperty"); +////////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////////// +// CHECK#1 +assert.throws(Test262Error, function () { + var options = { + get cause() { + throw new Test262Error("Get Cause"); + }, + }; + var error = new Error(message, options); +}, "Get Cause"); +////////////////////////////////////////////////////////////////////////////// diff --git a/test/built-ins/Error/cause_property.js b/test/built-ins/Error/cause_property.js new file mode 100644 index 0000000000..6bd510c3a2 --- /dev/null +++ b/test/built-ins/Error/cause_property.js @@ -0,0 +1,36 @@ +// Copyright (C) 2021 Chengzhong Wu. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Error constructor creates own cause property +info: | + Error ( message [ , options ] ) + + ... + 4. Perform ? InstallErrorCause(O, options). + ... + + 20.5.8.1 InstallErrorCause ( O, options ) + + 1. If Type(options) is Object and ? HasProperty(options, "cause") is true, then + a. Let cause be ? Get(options, "cause"). + b. Perform ! CreateNonEnumerableDataPropertyOrThrow(O, "cause", cause). + ... + +esid: sec-error-message +includes: [propertyHelper.js] +---*/ + +var message = "my-message"; +var cause = { message: "my-cause" }; +var error = new Error(message, { cause }); + +verifyProperty(error, "cause", { + configurable: true, + enumerable: false, + writeable: true, + value: cause, +}); + +verifyProperty(new Error(message), "cause", undefined); +verifyProperty(new Error(message, { cause: undefined }), "cause", { value: undefined }); diff --git a/test/built-ins/Error/constructor.js b/test/built-ins/Error/constructor.js new file mode 100644 index 0000000000..4764487ccf --- /dev/null +++ b/test/built-ins/Error/constructor.js @@ -0,0 +1,36 @@ +// Copyright (C) 2021 Chengzhong Wu. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Error constructor creates own properties in sequence +info: | + Error ( message [ , options ] ) + + ... + 4. Perform ? InstallErrorCause(O, options). + ... + +esid: sec-error-message +includes: [deepEqual.js] +---*/ + +var message = "my-message"; +var cause = { message: "my-cause" }; + +var sequence = []; +new Error( + { + toString() { + sequence.push("toString"); + return message; + }, + }, + { + get cause() { + sequence.push("cause"); + return cause; + }, + }, +); + +assert.deepEqual(sequence, [ "toString", "cause" ], "accessing own properties on sequence"); diff --git a/test/built-ins/NativeErrors/AggregateError/cause-property.js b/test/built-ins/NativeErrors/AggregateError/cause-property.js new file mode 100644 index 0000000000..68a5cbb546 --- /dev/null +++ b/test/built-ins/NativeErrors/AggregateError/cause-property.js @@ -0,0 +1,39 @@ +// Copyright (C) 2021 Chengzhong Wu. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-aggregate-error +description: > + AggregateError constructor creates own cause property +info: | + AggregateError ( errors, message[ , options ] ) + + ... + 4. Perform ? InstallErrorCause(O, options). + ... + + InstallErrorCause ( O, options ) + + 1. If Type(options) is Object and ? HasProperty(options, "cause") is true, then + a. Let cause be ? Get(options, "cause"). + b. Perform ! CreateNonEnumerableDataPropertyOrThrow(O, "cause", cause). + ... + +features: [AggregateError] +includes: [propertyHelper.js] +---*/ + +var errors = []; +var message = "my-message"; +var cause = { message: "my-cause" }; +var error = new AggregateError(errors, message, { cause }); + +verifyProperty(error, "cause", { + configurable: true, + enumerable: false, + writeable: true, + value: cause, +}); + +verifyProperty(new AggregateError(errors, message), "cause", undefined); +verifyProperty(new AggregateError(errors, message, { cause: undefined }), "cause", { value: undefined }); diff --git a/test/built-ins/NativeErrors/cause_property_native_error.js b/test/built-ins/NativeErrors/cause_property_native_error.js new file mode 100644 index 0000000000..0270ad7f09 --- /dev/null +++ b/test/built-ins/NativeErrors/cause_property_native_error.js @@ -0,0 +1,44 @@ +// Copyright (C) 2021 Chengzhong Wu. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: NativeError constructor creates own cause property +info: | + NativeError ( message[ , options ] ) + + ... + 4. Perform ? InstallErrorCause(O, options). + ... + + 20.5.8.1 InstallErrorCause ( O, options ) + + 1. If Type(options) is Object and ? HasProperty(options, "cause") is true, then + a. Let cause be ? Get(options, "cause"). + b. Perform ! CreateNonEnumerableDataPropertyOrThrow(O, "cause", cause). + ... + +esid: sec-nativeerror +includes: [propertyHelper.js] +---*/ + +var nativeErrors = [ + EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError +]; + +for (var i = 0; i < nativeErrors.length; ++i) { + var nativeError = nativeErrors[i]; + + var message = "my-message"; + var cause = { message: "my-cause" }; + var error = new nativeError(message, { cause }); + + verifyProperty(error, "cause", { + configurable: true, + enumerable: false, + writeable: true, + value: cause, + }); + + verifyProperty(new nativeError(message), "cause", undefined); + verifyProperty(new nativeError(message, { cause: undefined }), "cause", { value: undefined }); +}