Promise.all, Promise.allSettled: coverage updates (#2657)

This commit is contained in:
Rick Waldron 2020-06-12 12:54:52 -04:00 committed by GitHub
parent 7ed6238a89
commit c4c978b7ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 442 additions and 0 deletions

View File

@ -0,0 +1,43 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Invocation of the constructor's `resolve` method for iterable with promise values
esid: sec-promise.all
info: |
Let result be PerformPromiseAll(iteratorRecord, C, promiseCapability).
Runtime Semantics: PerformPromiseAll
Repeat
...
Let nextPromise be ? Call(promiseResolve, constructor, « nextValue »).
flags: [async]
features: [class, arrow-function]
---*/
class Custom extends Promise {}
let values = [1, 1, 1];
let cresolveCallCount = 0;
let presolveCallCount = 0;
let boundCustomResolve = Custom.resolve.bind(Custom);
let boundPromiseResolve = Promise.resolve.bind(Promise);
Custom.resolve = function(...args) {
cresolveCallCount += 1;
return boundCustomResolve(...args);
};
Promise.resolve = function(...args) {
presolveCallCount += 1;
return boundPromiseResolve(...args);
};
Promise.all.call(Custom, values)
.then(() => {
assert.sameValue(presolveCallCount, 0, '`Promise.resolve` is never invoked');
assert.sameValue(cresolveCallCount, 3, '`Custom.resolve` invoked once for every iterated promise');
}, $DONE).then($DONE, $DONE);

View File

@ -0,0 +1,34 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Invocation of the constructor's `resolve` method for iterable with promise values
esid: sec-promise.all
info: |
Let result be PerformPromiseAll(iteratorRecord, C, promiseCapability).
Runtime Semantics: PerformPromiseAll
Repeat
...
i. Let nextPromise be ? Call(promiseResolve, constructor, « nextValue »).
flags: [async]
features: [arrow-function]
---*/
let values = [1,1,1];
let callCount = 0;
let boundPromiseResolve = Promise.resolve.bind(Promise);
Promise.resolve = function(...args) {
callCount += 1;
return boundPromiseResolve(...args);
};
Promise.all(values)
.then(() => {
assert.sameValue(callCount, 3, '`then` invoked once for every iterated promise');
}, $DONE).then($DONE, $DONE);

View File

@ -0,0 +1,34 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Invocation of the constructor's `resolve` method for iterable with non-promise values
esid: sec-promise.all
info: |
Let result be PerformPromiseAll(iteratorRecord, C, promiseCapability).
Runtime Semantics: PerformPromiseAll
Repeat
...
i. Let nextPromise be ? Call(promiseResolve, constructor, « nextValue »).
flags: [async]
features: [arrow-function]
---*/
let values = [1, 2, 3];
let callCount = 0;
let boundPromiseResolve = Promise.resolve.bind(Promise);
Promise.resolve = function(...args) {
callCount += 1;
return boundPromiseResolve(...args);
};
Promise.all(values)
.then(() => {
assert.sameValue(callCount, 3, '`Promise.resolve` invoked once for every iterated value');
}, $DONE).then($DONE, $DONE);

View File

@ -0,0 +1,42 @@
// Copyright (C) 2019 Leo Balter, 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Resolved promises ignore rejections through deferred invocation of the
provided resolving function
esid: sec-promise.any
info: |
Let result be PerformPromiseAll(iteratorRecord, C, promiseCapability).
Runtime Semantics: PerformPromiseAll
Repeat
...
r. Perform ? Invoke(nextPromise, "then", « resultCapability.[[Resolve]], rejectElement »).
flags: [async]
features: [arrow-function]
---*/
var resolver = {
then(resolve) {
new Promise((resolve) => resolve())
.then(() => resolve(42));
}
};
var lateRejector = {
then(resolve, reject) {
new Promise((resolve) => resolve())
.then(() => {
resolve(9);
reject();
});
}
};
Promise.all([resolver, lateRejector])
.then(resolution => {
assert.sameValue(resolution[0], 42);
assert.sameValue(resolution[1], 9);
}).then($DONE, $DONE);

View File

@ -0,0 +1,38 @@
// Copyright (C) 2019 Leo Balter, 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Resolved promises ignore rejections through immediate invocation of the
provided resolving function
esid: sec-promise.all
info: |
Let result be PerformPromiseAll(iteratorRecord, C, promiseCapability).
Runtime Semantics: PerformPromiseAll
Repeat
...
r. Perform ? Invoke(nextPromise, "then", « resultCapability.[[Resolve]], rejectElement »).
flags: [async]
features: [arrow-function]
---*/
var resolver = {
then(resolve) {
resolve(42);
}
};
var lateRejector = {
then(resolve, reject) {
resolve(33);
reject();
}
};
Promise.all([resolver, lateRejector])
.then(resolution => {
assert.sameValue(resolution[0], 42);
assert.sameValue(resolution[1], 33);
}).then($DONE, $DONE);

View File

@ -0,0 +1,28 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-promise.all
description: >
If the constructor's `resolve` method is not callable, reject with a TypeError.
info: |
Let result be PerformPromiseAny(iteratorRecord, C, promiseCapability).
Runtime Semantics: PerformPromiseAny
Let promiseResolve be ? Get(constructor, "resolve").
If ! IsCallable(promiseResolve) is false, throw a TypeError exception.
flags: [async]
features: [arrow-function]
---*/
Promise.resolve = null;
Promise.all([1])
.then(
() => $DONE('The promise should not be resolved.'),
error => {
assert(error instanceof TypeError);
}
).then($DONE, $DONE);

View File

@ -0,0 +1,43 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Invocation of the constructor's `resolve` method for iterable with promise values
esid: sec-promise.allSettled
info: |
7. Let result be PerformPromiseAllSettled(iteratorRecord, C, promiseCapability).
Runtime Semantics: PerformPromiseAllSettled
7. Repeat
...
i. Let nextPromise be ? Call(promiseResolve, constructor, « nextValue »).
flags: [async]
features: [Promise.allSettled, class, arrow-function]
---*/
class Custom extends Promise {}
let values = [1, 1, 1];
let cresolveCallCount = 0;
let presolveCallCount = 0;
let boundCustomResolve = Custom.resolve.bind(Custom);
let boundPromiseResolve = Promise.resolve.bind(Promise);
Custom.resolve = function(...args) {
cresolveCallCount += 1;
return boundCustomResolve(...args);
};
Promise.resolve = function(...args) {
presolveCallCount += 1;
return boundPromiseResolve(...args);
};
Promise.allSettled.call(Custom, values)
.then(() => {
assert.sameValue(presolveCallCount, 0, '`Promise.resolve` is never invoked');
assert.sameValue(cresolveCallCount, 3, '`Custom.resolve` invoked once for every iterated promise');
}, $DONE).then($DONE, $DONE);

View File

@ -0,0 +1,34 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Invocation of the constructor's `resolve` method for iterable with promise values
esid: sec-promise.allSettled
info: |
7. Let result be PerformPromiseAllSettled(iteratorRecord, C, promiseCapability).
Runtime Semantics: PerformPromiseAllSettled
7. Repeat
...
i. Let nextPromise be ? Call(promiseResolve, constructor, « nextValue »).
flags: [async]
features: [Promise.allSettled, arrow-function]
---*/
let values = [1,1,1];
let callCount = 0;
let boundPromiseResolve = Promise.resolve.bind(Promise);
Promise.resolve = function(...args) {
callCount += 1;
return boundPromiseResolve(...args);
};
Promise.allSettled(values)
.then(() => {
assert.sameValue(callCount, 3, '`then` invoked once for every iterated promise');
}, $DONE).then($DONE, $DONE);

View File

@ -0,0 +1,34 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Invocation of the constructor's `resolve` method for iterable with non-promise values
esid: sec-promise.allSettled
info: |
5. Let result be PerformPromiseAllSettled(iteratorRecord, C, promiseCapability).
Runtime Semantics: PerformPromiseAllSettled
8. Repeat
...
i. Let nextPromise be ? Call(promiseResolve, constructor, « nextValue »).
flags: [async]
features: [Promise.allSettled, arrow-function]
---*/
let values = [1, 2, 3];
let callCount = 0;
let boundPromiseResolve = Promise.resolve.bind(Promise);
Promise.resolve = function(...args) {
callCount += 1;
return boundPromiseResolve(...args);
};
Promise.allSettled(values)
.then(() => {
assert.sameValue(callCount, 3, '`Promise.resolve` invoked once for every iterated value');
}, $DONE).then($DONE, $DONE);

View File

@ -0,0 +1,44 @@
// Copyright (C) 2019 Leo Balter, 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Resolved promises ignore rejections through deferred invocation of the
provided resolving function
esid: sec-promise.allSettled
info: |
Let result be PerformPromiseAllSettled(iteratorRecord, C, promiseCapability).
Runtime Semantics: PerformPromiseAllSettled
Repeat
...
r. Perform ? Invoke(nextPromise, "then", « resultCapability.[[Resolve]], rejectElement »).
flags: [async]
features: [Promise.allSettled, arrow-function]
---*/
var resolver = {
then(resolve) {
new Promise((resolve) => resolve())
.then(() => resolve(42));
}
};
var lateRejector = {
then(resolve, reject) {
new Promise((resolve) => resolve())
.then(() => {
resolve(9);
reject();
});
}
};
Promise.allSettled([resolver, lateRejector])
.then(resolution => {
assert.sameValue(resolution[0].value, 42);
assert.sameValue(resolution[0].status, 'fulfilled');
assert.sameValue(resolution[1].value, 9);
assert.sameValue(resolution[1].status, 'fulfilled');
}).then($DONE, $DONE);

View File

@ -0,0 +1,40 @@
// Copyright (C) 2019 Leo Balter, 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Resolved promises ignore rejections through immediate invocation of the
provided resolving function
esid: sec-promise.allSettled
info: |
Let result be PerformPromiseAllSettled(iteratorRecord, C, promiseCapability).
Runtime Semantics: PerformPromiseAllSettled
Repeat
...
r. Perform ? Invoke(nextPromise, "then", « resultCapability.[[Resolve]], rejectElement »).
flags: [async]
features: [Promise.allSettled, arrow-function]
---*/
var resolver = {
then(resolve) {
resolve(42);
}
};
var lateRejector = {
then(resolve, reject) {
resolve(33);
reject();
}
};
Promise.allSettled([resolver, lateRejector])
.then(resolution => {
assert.sameValue(resolution[0].value, 42);
assert.sameValue(resolution[0].status, 'fulfilled');
assert.sameValue(resolution[1].value, 33);
assert.sameValue(resolution[1].status, 'fulfilled');
}).then($DONE, $DONE);

View File

@ -0,0 +1,28 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-promise.allSettled
description: >
If the constructor's `resolve` method is not callable, reject with a TypeError.
info: |
Let result be PerformPromiseAllSettled(iteratorRecord, C, promiseCapability).
Runtime Semantics: PerformPromiseAllSettled
Let promiseResolve be ? Get(constructor, "resolve").
If ! IsCallable(promiseResolve) is false, throw a TypeError exception.
flags: [async]
features: [Promise.allSettled, arrow-function]
---*/
Promise.resolve = null;
Promise.allSettled([1])
.then(
() => $DONE('The promise should not be resolved.'),
error => {
assert(error instanceof TypeError);
}
).then($DONE, $DONE);