Add tests for stage 3 proposal error cause (#2965)

* Add tests for error cause

* Correct invocation of Proxy constructor

Co-authored-by: Mike Pennisi <mike@mikepennisi.com>
This commit is contained in:
legendecas 2021-04-24 02:45:27 +08:00 committed by GitHub
parent eca69e2c95
commit 3e4e07c836
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 204 additions and 0 deletions

View File

@ -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");
//////////////////////////////////////////////////////////////////////////////

View File

@ -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 });

View File

@ -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");

View File

@ -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 });

View File

@ -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 });
}