Promises: whenever evaluating checkSequence(sequence), also assert length of sequence (#2672)

This commit is contained in:
Rick Waldron 2020-06-24 15:18:35 -04:00 committed by GitHub
parent 20a1345bbe
commit 040eb5393a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
36 changed files with 115 additions and 27 deletions

View File

@ -20,6 +20,7 @@ Promise.resolve().then(function() {
sequence.push(3); sequence.push(3);
}).then(function() { }).then(function() {
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "Promises resolved in unexpected sequence"); checkSequence(sequence, "Promises resolved in unexpected sequence");
}).then($DONE, $DONE); }).then($DONE, $DONE);

View File

@ -22,14 +22,17 @@ sequence.push(1);
Promise.all([p1]).then(function(resolved) { Promise.all([p1]).then(function(resolved) {
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "Expected Promise.all().then to queue second"); checkSequence(sequence, "Expected Promise.all().then to queue second");
}).catch($DONE); }).catch($DONE);
p1.then(function() { p1.then(function() {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "Expected p1.then to queue first"); checkSequence(sequence, "Expected p1.then to queue first");
}).then(function() { }).then(function() {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "Expected final then to queue last"); checkSequence(sequence, "Expected final then to queue last");
}).then($DONE, $DONE); }).then($DONE, $DONE);

View File

@ -22,16 +22,19 @@ sequence.push(1);
p1.then(function() { p1.then(function() {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "Expected to be called first."); checkSequence(sequence, "Expected to be called first.");
}).catch($DONE); }).catch($DONE);
Promise.all([p1, p2]).then(function() { Promise.all([p1, p2]).then(function() {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "Expected to be called third."); checkSequence(sequence, "Expected to be called third.");
}).then($DONE, $DONE); }).then($DONE, $DONE);
p2.then(function() { p2.then(function() {
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "Expected to be called second."); checkSequence(sequence, "Expected to be called second.");
}).catch($DONE); }).catch($DONE);

View File

@ -19,7 +19,8 @@ Promise.resolve().then(function() {
sequence.push(3); sequence.push(3);
}).then(function() { }).then(function() {
sequence.push(4); sequence.push(4);
checkSequence(sequence, 'Promises resolved in unexpected sequence'); assert.sameValue(sequence.length, 4);
checkSequence(sequence, 'Promises resolved in unexpected sequence');
}).then($DONE, $DONE); }).then($DONE, $DONE);
sequence.push(1); sequence.push(1);

View File

@ -30,14 +30,17 @@ sequence.push(1);
Promise.allSettled([p1]).then(function(resolved) { Promise.allSettled([p1]).then(function(resolved) {
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, 'Expected Promise.allSettled().then to queue second'); checkSequence(sequence, 'Expected Promise.allSettled().then to queue second');
}).catch($DONE); }).catch($DONE);
p1.then(function() { p1.then(function() {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, 'Expected p1.then to queue first'); checkSequence(sequence, 'Expected p1.then to queue first');
}).then(function() { }).then(function() {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, 'Expected final then to queue last'); checkSequence(sequence, 'Expected final then to queue last');
}).then($DONE, $DONE); }).then($DONE, $DONE);

View File

@ -37,22 +37,26 @@ sequence.push(1);
p1.catch(function() { p1.catch(function() {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, 'Expected to be called first.'); checkSequence(sequence, 'Expected to be called first.');
}).catch($DONE); });
Promise.allSettled([p1, p2, p3]).then(function() { Promise.allSettled([p1, p2, p3]).then(function() {
sequence.push(6); sequence.push(6);
assert.sameValue(sequence.length, 6);
checkSequence(sequence, 'Expected to be called fourth.'); checkSequence(sequence, 'Expected to be called fourth.');
}).then($DONE, $DONE); }).then($DONE, $DONE);
p2.then(function() { p2.then(function() {
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, 'Expected to be called second.'); checkSequence(sequence, 'Expected to be called second.');
}).catch($DONE); });
sequence.push(2); sequence.push(2);
p3.catch(function() { p3.catch(function() {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, 'Expected to be called third.'); checkSequence(sequence, 'Expected to be called third.');
}).catch($DONE); });

View File

@ -33,16 +33,19 @@ sequence.push(1);
p1.catch(function() { p1.catch(function() {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, 'Expected to be called first.'); checkSequence(sequence, 'Expected to be called first.');
}).catch($DONE); }).catch($DONE);
Promise.allSettled([p1, p2]).then(function() { Promise.allSettled([p1, p2]).then(function() {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, 'Expected to be called third.'); checkSequence(sequence, 'Expected to be called third.');
}).then($DONE, $DONE); }).then($DONE, $DONE);
p2.catch(function() { p2.catch(function() {
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, 'Expected to be called second.'); checkSequence(sequence, 'Expected to be called second.');
}).catch($DONE); }).catch($DONE);

View File

@ -33,16 +33,19 @@ sequence.push(1);
p1.then(function() { p1.then(function() {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, 'Expected to be called first.'); checkSequence(sequence, 'Expected to be called first.');
}).catch($DONE); }).catch($DONE);
Promise.allSettled([p1, p2]).then(function() { Promise.allSettled([p1, p2]).then(function() {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, 'Expected to be called third.'); checkSequence(sequence, 'Expected to be called third.');
}).then($DONE, $DONE); }).then($DONE, $DONE);
p2.then(function() { p2.then(function() {
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, 'Expected to be called second.'); checkSequence(sequence, 'Expected to be called second.');
}).catch($DONE); }).catch($DONE);

View File

@ -37,14 +37,17 @@ sequence.push(1);
Promise.any([p1]).then((resolved) => { Promise.any([p1]).then((resolved) => {
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, 'Expected Promise.any().then to queue second'); checkSequence(sequence, 'Expected Promise.any().then to queue second');
}).catch($DONE); }).catch($DONE);
p1.then(() => { p1.then(() => {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, 'Expected p1.then to queue first'); checkSequence(sequence, 'Expected p1.then to queue first');
}).then(() => { }).then(() => {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, 'Expected final then to queue last'); checkSequence(sequence, 'Expected final then to queue last');
}).then($DONE, $DONE); }).then($DONE, $DONE);

View File

@ -44,16 +44,19 @@ sequence.push(1);
p1.catch(() => { p1.catch(() => {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, 'Expected to be called first.'); checkSequence(sequence, 'Expected to be called first.');
}).catch($DONE); }).catch($DONE);
Promise.any([p1, p2, p3]).then(() => { Promise.any([p1, p2, p3]).then(() => {
sequence.push(6); sequence.push(6);
assert.sameValue(sequence.length, 6);
checkSequence(sequence, 'Expected to be called fourth.'); checkSequence(sequence, 'Expected to be called fourth.');
}).then($DONE, $DONE); }).then($DONE, $DONE);
p2.then(() => { p2.then(() => {
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, 'Expected to be called second.'); checkSequence(sequence, 'Expected to be called second.');
}).catch($DONE); }).catch($DONE);
@ -61,5 +64,6 @@ sequence.push(2);
p3.catch(() => { p3.catch(() => {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, 'Expected to be called third.'); checkSequence(sequence, 'Expected to be called third.');
}).catch($DONE); }).catch($DONE);

View File

@ -40,11 +40,13 @@ sequence.push(1);
p1.catch(() => { p1.catch(() => {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, 'Expected to be called first.'); checkSequence(sequence, 'Expected to be called first.');
}).catch($DONE); }).catch($DONE);
Promise.any([p1, p2]).then(() => { Promise.any([p1, p2]).then(() => {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, 'Expected to be called third.'); checkSequence(sequence, 'Expected to be called third.');
}).then($DONE, outcome => { }).then($DONE, outcome => {
assert(outcome instanceof AggregateError); assert(outcome instanceof AggregateError);
@ -55,6 +57,7 @@ Promise.any([p1, p2]).then(() => {
p2.catch(() => { p2.catch(() => {
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, 'Expected to be called second.'); checkSequence(sequence, 'Expected to be called second.');
}).catch($DONE); }).catch($DONE);

View File

@ -35,16 +35,19 @@ sequence.push(1);
p1.then(function() { p1.then(function() {
sequence.push(3); sequence.push(3);
checkSequence(sequence, 'Expected to be called first.'); assert.sameValue(sequence.length, 3);
checkSequence(sequence, 'Expected to be called first.');
}).catch($DONE); }).catch($DONE);
Promise.any([p1, p2]).then(function() { Promise.any([p1, p2]).then(function() {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, 'Expected to be called third.'); checkSequence(sequence, 'Expected to be called third.');
}).then($DONE, $DONE); }).then($DONE, $DONE);
p2.then(function() { p2.then(function() {
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, 'Expected to be called second.'); checkSequence(sequence, 'Expected to be called second.');
}).catch($DONE); }).catch($DONE);

View File

@ -34,6 +34,6 @@ no.catch(function(e) {
sequence.push(5); sequence.push(5);
assert.sameValue(e, noReason); assert.sameValue(e, noReason);
}).then(function() { }).then(function() {
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "All expected callbacks called in correct order"); checkSequence(sequence, "All expected callbacks called in correct order");
$DONE(); }).then($DONE, $DONE);
}).catch($ERROR);

View File

@ -24,6 +24,6 @@ p.finally(function() {
sequence.push(2); sequence.push(2);
assert.sameValue(reason, original, 'onFinally can not override the rejection value by returning'); assert.sameValue(reason, original, 'onFinally can not override the rejection value by returning');
}).then(function() { }).then(function() {
assert.sameValue(sequence.length, 2);
checkSequence(sequence, "All expected callbacks called in correct order"); checkSequence(sequence, "All expected callbacks called in correct order");
$DONE(); }).then($DONE, $DONE);
}).catch($ERROR);

View File

@ -24,6 +24,6 @@ p.finally(function() {
sequence.push(2); sequence.push(2);
assert.sameValue(reason, thrown, 'onFinally can override the rejection reason by throwing'); assert.sameValue(reason, thrown, 'onFinally can override the rejection reason by throwing');
}).then(function() { }).then(function() {
assert.sameValue(sequence.length, 2);
checkSequence(sequence, "All expected callbacks called in correct order"); checkSequence(sequence, "All expected callbacks called in correct order");
$DONE(); }).then($DONE, $DONE);
}).catch($ERROR);

View File

@ -20,6 +20,6 @@ p.finally(function() {
sequence.push(2); sequence.push(2);
assert.sameValue(x, obj, 'onFinally can not override the resolution value'); assert.sameValue(x, obj, 'onFinally can not override the resolution value');
}).then(function() { }).then(function() {
assert.sameValue(sequence.length, 2);
checkSequence(sequence, "All expected callbacks called in correct order"); checkSequence(sequence, "All expected callbacks called in correct order");
$DONE(); }).then($DONE, $DONE);
}).catch($ERROR);

View File

@ -34,6 +34,7 @@ yes.then(function(x) {
sequence.push(5); sequence.push(5);
assert.sameValue(e, noReason); assert.sameValue(e, noReason);
}).then(function() { }).then(function() {
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "All expected callbacks called in correct order"); checkSequence(sequence, "All expected callbacks called in correct order");
$DONE(); $DONE();
}).catch($ERROR); }).catch($ERROR);

View File

@ -34,6 +34,7 @@ p.then(function() {
}).then(function() { }).then(function() {
sequence.push(8); sequence.push(8);
}).then(function() { }).then(function() {
assert.sameValue(sequence.length, 8);
checkSequence(sequence, "Sequence should be as expected"); checkSequence(sequence, "Sequence should be as expected");
}).then($DONE, $DONE); }).then($DONE, $DONE);

View File

@ -20,7 +20,8 @@ var resolveP1, rejectP2, sequence = [];
})).then(function(msg) { })).then(function(msg) {
sequence.push(msg); sequence.push(msg);
}).then(function() { }).then(function() {
checkSequence(sequence, "Expected 1,2,3"); assert.sameValue(sequence.length, 3);
checkSequence(sequence, "Expected 1,2,3");
}).then($DONE, $DONE); }).then($DONE, $DONE);
(new Promise(function(resolve, reject) { (new Promise(function(resolve, reject) {

View File

@ -33,6 +33,7 @@ p1.then(function(msg) {
p2.catch(function(msg) { p2.catch(function(msg) {
sequence.push(msg); sequence.push(msg);
}).then(function() { }).then(function() {
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "Expected 1,2,3"); checkSequence(sequence, "Expected 1,2,3");
}).then($DONE, $DONE); }).then($DONE, $DONE);

View File

@ -34,8 +34,9 @@ Promise.resolve().then(function() {
p2.catch(function(msg) { p2.catch(function(msg) {
sequence.push(msg); sequence.push(msg);
}).then(function() { }).then(function() {
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "Expected 1,2,3"); checkSequence(sequence, "Expected 1,2,3");
}).then($DONE, $DONE); }).then($DONE, $DONE);
}); }).then($DONE, $DONE);
sequence.push(1); sequence.push(1);

View File

@ -22,6 +22,7 @@ sequence.push(1);
p.then(function() { p.then(function() {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "Should be second"); checkSequence(sequence, "Should be second");
}).catch($DONE); }).catch($DONE);
@ -29,10 +30,12 @@ Promise.resolve().then(function() {
// enqueue another then-handler // enqueue another then-handler
p.then(function() { p.then(function() {
sequence.push(4); sequence.push(4);
checkSequence(sequence, "Should be third"); assert.sameValue(sequence.length, 4);
checkSequence(sequence, "Should be third");
}).then($DONE, $DONE); }).then($DONE, $DONE);
sequence.push(2); sequence.push(2);
assert.sameValue(sequence.length, 2);
checkSequence(sequence, "Should be first"); checkSequence(sequence, "Should be first");
pResolve(); pResolve();

View File

@ -24,18 +24,21 @@ pResolve();
p.then(function() { p.then(function() {
sequence.push(3); sequence.push(3);
checkSequence(sequence, "Should be first"); assert.sameValue(sequence.length, 3);
checkSequence(sequence, "Should be first");
}).catch($DONE); }).catch($DONE);
Promise.resolve().then(function() { Promise.resolve().then(function() {
// enqueue another then-handler // enqueue another then-handler
p.then(function() { p.then(function() {
sequence.push(5); sequence.push(5);
checkSequence(sequence, "Should be third"); assert.sameValue(sequence.length, 5);
checkSequence(sequence, "Should be third");
}).then($DONE, $DONE); }).then($DONE, $DONE);
sequence.push(4); sequence.push(4);
checkSequence(sequence, "Should be second"); assert.sameValue(sequence.length, 4);
checkSequence(sequence, "Should be second");
}).catch($DONE); }).catch($DONE);
sequence.push(2); sequence.push(2);

View File

@ -26,6 +26,7 @@ p.then(function() {
$ERROR("Should not be called -- Promise rejected."); $ERROR("Should not be called -- Promise rejected.");
}, function() { }, function() {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "Should be first"); checkSequence(sequence, "Should be first");
}).catch($DONE); }).catch($DONE);
@ -35,10 +36,12 @@ Promise.resolve().then(function() {
$ERROR("Should not be called (2) -- Promise rejected."); $ERROR("Should not be called (2) -- Promise rejected.");
}, function() { }, function() {
sequence.push(5); sequence.push(5);
checkSequence(sequence, "Should be third"); assert.sameValue(sequence.length, 5);
checkSequence(sequence, "Should be third");
}).then($DONE, $DONE); }).then($DONE, $DONE);
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "Should be second"); checkSequence(sequence, "Should be second");
}).catch($DONE); }).catch($DONE);

View File

@ -17,14 +17,17 @@ sequence.push(1);
p.then(function() { p.then(function() {
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "This happens second"); checkSequence(sequence, "This happens second");
}).catch($DONE); }).catch($DONE);
Promise.resolve().then(function() { Promise.resolve().then(function() {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "This happens first"); checkSequence(sequence, "This happens first");
}).then(function() { }).then(function() {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "This happens third"); checkSequence(sequence, "This happens third");
}).then($DONE, $DONE); }).then($DONE, $DONE);

View File

@ -20,14 +20,17 @@ p.then(function() {
$ERROR("Should not fulfill."); $ERROR("Should not fulfill.");
}, function() { }, function() {
sequence.push(4); sequence.push(4);
checkSequence(sequence, "This happens second"); assert.sameValue(sequence.length, 4);
checkSequence(sequence, "This happens second");
}).catch($DONE); }).catch($DONE);
Promise.resolve().then(function() { Promise.resolve().then(function() {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "This happens first"); checkSequence(sequence, "This happens first");
}).then(function() { }).then(function() {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "This happens third"); checkSequence(sequence, "This happens third");
}).then($DONE, $DONE); }).then($DONE, $DONE);

View File

@ -23,14 +23,17 @@ p.then(function(arg) {
} }
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "This happens second"); checkSequence(sequence, "This happens second");
}).catch($DONE); }).catch($DONE);
Promise.resolve().then(function() { Promise.resolve().then(function() {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "This happens first"); checkSequence(sequence, "This happens first");
}).then(function() { }).then(function() {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "This happens third"); checkSequence(sequence, "This happens third");
}).then($DONE, $DONE); }).then($DONE, $DONE);

View File

@ -23,14 +23,17 @@ p.then(function(arg) {
} }
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "This happens second"); checkSequence(sequence, "This happens second");
}).catch($DONE); }).catch($DONE);
Promise.resolve().then(function() { Promise.resolve().then(function() {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "This happens first"); checkSequence(sequence, "This happens first");
}).then(function() { }).then(function() {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "This happens third"); checkSequence(sequence, "This happens third");
}).then($DONE, $DONE); }).then($DONE, $DONE);

View File

@ -23,14 +23,17 @@ p.then(function(arg) {
} }
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "This happens second"); checkSequence(sequence, "This happens second");
}).catch($DONE); }).catch($DONE);
Promise.resolve().then(function() { Promise.resolve().then(function() {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "This happens first"); checkSequence(sequence, "This happens first");
}).then(function() { }).then(function() {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "This happens third"); checkSequence(sequence, "This happens third");
}).then($DONE, $DONE); }).then($DONE, $DONE);

View File

@ -25,14 +25,17 @@ p.then(function() {
} }
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "This happens second"); checkSequence(sequence, "This happens second");
}).catch($DONE); }).catch($DONE);
Promise.resolve().then(function() { Promise.resolve().then(function() {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "This happens first"); checkSequence(sequence, "This happens first");
}).then(function() { }).then(function() {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "This happens third"); checkSequence(sequence, "This happens third");
}).then($DONE, $DONE); }).then($DONE, $DONE);

View File

@ -25,12 +25,12 @@ includes: [promiseHelper.js]
---*/ ---*/
let callCount = 0; let callCount = 0;
let values = []; let sequence = [];
function Constructor(executor) { function Constructor(executor) {
function reject(value) { function reject(value) {
callCount += 1; callCount += 1;
values.push(value); sequence.push(value);
} }
executor(() => { executor(() => {
throw new Test262Error(); throw new Test262Error();
@ -59,4 +59,5 @@ pReject(2);
pReject(3); pReject(3);
assert.sameValue(callCount, 3, 'callCount after resolving a'); assert.sameValue(callCount, 3, 'callCount after resolving a');
checkSequence(values); assert.sameValue(sequence.length, 3);
checkSequence(sequence);

View File

@ -25,12 +25,12 @@ includes: [promiseHelper.js]
---*/ ---*/
let callCount = 0; let callCount = 0;
let values = []; let sequence = [];
function Constructor(executor) { function Constructor(executor) {
function resolve(value) { function resolve(value) {
callCount += 1; callCount += 1;
values.push(value); sequence.push(value);
} }
executor(resolve, $ERROR); executor(resolve, $ERROR);
} }
@ -56,4 +56,5 @@ pResolve(2);
pResolve(3); pResolve(3);
assert.sameValue(callCount, 3, 'callCount after resolving a'); assert.sameValue(callCount, 3, 'callCount after resolving a');
checkSequence(values); assert.sameValue(sequence.length, 3);
checkSequence(sequence);

View File

@ -30,18 +30,23 @@ let sequence = [1];
Promise.all([ Promise.all([
a.catch(() => { a.catch(() => {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
return checkSequence(sequence, 'Expected to be called first.'); return checkSequence(sequence, 'Expected to be called first.');
}), }),
Promise.race([a, b]).catch(() => { Promise.race([a, b]).catch(() => {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
return checkSequence(sequence, 'Expected to be called third.'); return checkSequence(sequence, 'Expected to be called third.');
}), }),
b.catch(() => { b.catch(() => {
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
return checkSequence(sequence, 'Expected to be called second.'); return checkSequence(sequence, 'Expected to be called second.');
}) })
]).then(result => { ]).then(result => {
compareArray(result, [true, true, true]); compareArray(result, [true, true, true]);
assert.sameValue(sequence.length, 5);
checkSequence(sequence);
}).then($DONE, $DONE); }).then($DONE, $DONE);
sequence.push(2); sequence.push(2);

View File

@ -30,17 +30,22 @@ let sequence = [1];
Promise.all([ Promise.all([
a.then(() => { a.then(() => {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
return checkSequence(sequence, 'Expected to be called first.'); return checkSequence(sequence, 'Expected to be called first.');
}), }),
Promise.race([a, b]).then(() => { Promise.race([a, b]).then(() => {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
return checkSequence(sequence, 'Expected to be called third.'); return checkSequence(sequence, 'Expected to be called third.');
}), }),
b.then(() => { b.then(() => {
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
return checkSequence(sequence, 'Expected to be called second.'); return checkSequence(sequence, 'Expected to be called second.');
}) })
]).then(result => { ]).then(result => {
compareArray(result, [true, true, true]); compareArray(result, [true, true, true]);
assert.sameValue(sequence.length, 5);
checkSequence(sequence)
}).then($DONE, $DONE); }).then($DONE, $DONE);
sequence.push(2); sequence.push(2);

View File

@ -16,6 +16,7 @@ var sequence = [];
var thenable = { var thenable = {
then: function(onResolve, onReject) { then: function(onResolve, onReject) {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "thenable.then called"); checkSequence(sequence, "thenable.then called");
assert.sameValue(this, thenable); assert.sameValue(this, thenable);
@ -23,25 +24,30 @@ var thenable = {
onResolve('resolved'); onResolve('resolved');
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "after resolved"); checkSequence(sequence, "after resolved");
throw new Error('interrupt flow'); throw new Error('interrupt flow');
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "duplicate sequence point not pushed"); checkSequence(sequence, "duplicate sequence point not pushed");
} }
}; };
sequence.push(1); sequence.push(1);
assert.sameValue(sequence.length, 1);
checkSequence(sequence, "no async calls yet"); checkSequence(sequence, "no async calls yet");
var p1 = Promise.resolve(thenable); var p1 = Promise.resolve(thenable);
sequence.push(2); sequence.push(2);
assert.sameValue(sequence.length, 2);
checkSequence(sequence, "thenable.then queued but not yet called"); checkSequence(sequence, "thenable.then queued but not yet called");
p1.then(function(q) { p1.then(function(q) {
sequence.push(5); sequence.push(5);
assert.sameValue(sequence.length, 5);
checkSequence(sequence, "all done"); checkSequence(sequence, "all done");
assert.sameValue(q, 'resolved'); assert.sameValue(q, 'resolved');

View File

@ -17,6 +17,7 @@ var thenable = {
then: function(onResolve, onReject) { then: function(onResolve, onReject) {
sequence.push(3); sequence.push(3);
assert.sameValue(sequence.length, 3);
checkSequence(sequence, "thenable.then called"); checkSequence(sequence, "thenable.then called");
assert.sameValue(this, thenable, "thenable.then called with `thenable` as `this`"); assert.sameValue(this, thenable, "thenable.then called with `thenable` as `this`");
@ -26,15 +27,18 @@ var thenable = {
}; };
sequence.push(1); sequence.push(1);
assert.sameValue(sequence.length, 1);
checkSequence(sequence, "no async calls yet"); checkSequence(sequence, "no async calls yet");
var p = Promise.resolve(thenable); var p = Promise.resolve(thenable);
sequence.push(2); sequence.push(2);
assert.sameValue(sequence.length, 2);
checkSequence(sequence, "thenable.then queued but not yet called"); checkSequence(sequence, "thenable.then queued but not yet called");
p.then(function(r) { p.then(function(r) {
sequence.push(4); sequence.push(4);
assert.sameValue(sequence.length, 4);
checkSequence(sequence, "all done"); checkSequence(sequence, "all done");
assert.sameValue(r, 'resolved'); assert.sameValue(r, 'resolved');