Skip to content

Commit

Permalink
add Promise.try tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed May 1, 2024
1 parent a7e796a commit 8b6b0f5
Show file tree
Hide file tree
Showing 15 changed files with 225 additions and 2 deletions.
4 changes: 4 additions & 0 deletions features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ iterator-helpers
# https://github.com/tc39/proposal-promise-with-resolvers
promise-with-resolvers

# Promise.try
# https://github.com/tc39/proposal-promise-try
promise-try

# Set methods
# https://github.com/tc39/proposal-set-methods
set-methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ info: |
Perform ? Invoke(nextPromise, "then", « resultCapability.[[Resolve]], resultCapability.[[Reject]] »).
flags: [async]
includes: [compareArray.js,promiseHelper.js]
includes: [compareArray.js, promiseHelper.js]
---*/

let a = new Promise((_, reject) => reject('a'));
Expand Down
22 changes: 22 additions & 0 deletions test/built-ins/Promise/try/args.js
Original file line number Diff line number Diff line change
@@ -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.

/*---
description: Promise.try forwards arguments
esid: sec-promise.try
features: [promise-try]
flags: [async]
includes: [asyncHelpers.js, compareArray.js]
---*/

var sentinel = { sentinel: true };

asyncTest(
Promise.try(function () {
assert.compareArray(
Array.prototype.slice.call(arguments),
[1, 2, Test262Error, sentinel]
);
}, 1, 2, Test262Error, sentinel)
);

16 changes: 16 additions & 0 deletions test/built-ins/Promise/try/ctx-ctor-throws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2024 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
`Promise.try` invoked on a constructor value that throws an error
features: [promise-try]
---*/

var CustomPromise = function () {
throw new Test262Error();
};

assert.throws(Test262Error, function () {
Promise.try.call(CustomPromise, function () {});
});
27 changes: 27 additions & 0 deletions test/built-ins/Promise/try/ctx-ctor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (C) 2024 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Promise.try produces instances of the receiver
esid: sec-promise.try
features: [promise-try, class]
---*/

var executor = null;
var callCount = 0;

class SubPromise extends Promise {
constructor(a) {
super(a);
executor = a;
callCount += 1;
}
}

var instance = Promise.try.call(SubPromise, function () {});

assert.sameValue(instance.promise.constructor, SubPromise);
assert.sameValue(instance.promise instanceof SubPromise, true);

assert.sameValue(callCount, 1);
assert.sameValue(typeof executor, 'function');
12 changes: 12 additions & 0 deletions test/built-ins/Promise/try/ctx-non-ctor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (C) 2024 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Promise.try errors when the receiver is not a constructor
esid: sec-promise.try
features: [promise-try]
---*/

assert.throws(TypeError, function () {
Promise.try.call(eval);
});
32 changes: 32 additions & 0 deletions test/built-ins/Promise/try/ctx-non-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (C) 2024 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Promise.try errors when the receiver is not an object
esid: sec-promise.try
features: [promise-try]
---*/

assert.throws(TypeError, function () {
Promise.try.call(undefined);
});

assert.throws(TypeError, function () {
Promise.try.call(null);
});

assert.throws(TypeError, function () {
Promise.try.call(86);
});

assert.throws(TypeError, function () {
Promise.try.call('string');
});

assert.throws(TypeError, function () {
Promise.try.call(true);
});

assert.throws(TypeError, function () {
Promise.try.call(Symbol());
});
14 changes: 14 additions & 0 deletions test/built-ins/Promise/try/length.js
Original file line number Diff line number Diff line change
@@ -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.
/*---
description: Promise.try `length` property
includes: [propertyHelper.js]
features: [promise-try]
---*/

verifyProperty(Promise.try, "length", {
value: 1,
writable: false,
enumerable: false,
configurable: true
});
14 changes: 14 additions & 0 deletions test/built-ins/Promise/try/name.js
Original file line number Diff line number Diff line change
@@ -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.
/*---
description: Promise.try `name` property
includes: [propertyHelper.js]
features: [promise-try]
---*/

verifyProperty(Promise.try, "name", {
value: "try",
writable: false,
enumerable: false,
configurable: true
});
16 changes: 16 additions & 0 deletions test/built-ins/Promise/try/not-a-constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2024 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
Promise.try does not implement [[Construct]], is not new-able
includes: [isConstructor.js]
features: [Reflect.construct, promise-try]
---*/

assert.sameValue(isConstructor(Promise.try), false, 'isConstructor(Promise.all) must return false');

assert.throws(TypeError, function () {
new Promise.try(function () {});
});

12 changes: 12 additions & 0 deletions test/built-ins/Promise/try/promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (C) 2024 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Promise.try return value is a Promise
features: [promise-try]
---*/

var instance = Promise.try(function () {});

assert.sameValue(instance.constructor, Promise);
assert.sameValue(instance instanceof Promise, true);
16 changes: 16 additions & 0 deletions test/built-ins/Promise/try/prop-desc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2024 Jordan Harband. All rights reserved.
// See LICENSE for details.

/*---
author: Jordan Harband
description: Promise.try property descriptor
features: [promise-try]
includes: [propertyHelper.js]
---*/

verifyProperty(Promise, 'try', {
value: Promise.try,
writable: true,
enumerable: false,
configurable: true
})
21 changes: 21 additions & 0 deletions test/built-ins/Promise/try/return-value.js
Original file line number Diff line number Diff line change
@@ -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.

/*---
description: Promise.try returns a Promise resolved to the callback's return value
esid: sec-promise.try
features: [promise-try]
flags: [async]
includes: [asyncHelpers.js]
---*/

var sentinel = { sentinel: true };

asyncTest(
Promise.try(function () {
return sentinel;
}).then(function (v) {
assert.sameValue(v, sentinel);
})
);

17 changes: 17 additions & 0 deletions test/built-ins/Promise/try/throws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) 2024 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Promise.try returns a Promise that rejects when the function throws
esid: sec-promise.try
features: [promise-try]
flags: [async]
---*/

assert.throwsAsync(
Test262Error,
function () {
Promise.try(function () { throw new Test262Error(); })
},
"error thrown from callback must become a rejection"
);
2 changes: 1 addition & 1 deletion test/built-ins/Promise/withResolvers/ctx-ctor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/*---
description: Promise.withResolvers produces instances of the receiver
esid: sec-promise.withresolvers
features: [promise-with-resolvers]
features: [promise-with-resolvers, class]
---*/

class SubPromise extends Promise {}
Expand Down

0 comments on commit 8b6b0f5

Please sign in to comment.