Skip to content
Snippets Groups Projects
Commit 26787ab0 authored by smikes's avatar smikes
Browse files

add tests for 24.4.4.1 - Promise.all

add more tests of Promise.all

additional Promise test

add tests of Promise.prototype

add some tests for Promise.race

additional Promise tests

add Promise.reject and Promise.resolve tests

correct test description

rename badly-named files

use current license and minor style cleanup

correct understanding of undefined as thisArgument

incorporate line notes

Is this enough to make a constructor in ES6?

more accurate use of resolved,fulfilled etc.
remove some redundant tests
add new tests

remove "constant array" unclear language

better description

Update S25.4.2.1_A3.2_T1.js

address dangling ()

changes per @anba line notes
 - if GetIterator is abrupt, the Promise.race / Promise.all should reject
 - if Promise.race is called with nonconforming constructor as 'this',
   TypeError should be thrown (cannot reject if exeption is thrown from
   NewPromiseCapability; no promise exists yet...)

correct description of "this" testing in callbacks

fix whitespace, formatting

remove tab
add "next-turn" checking to sequencers
parent 61113dbf
No related branches found
No related tags found
No related merge requests found
Showing
with 443 additions and 0 deletions
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.all expects an iterable argument;
fails if recieves an abrupt completion
ref 7.4.1 non-Object fails CheckIterable
ref 7.4.2 GetIterator throws TypeError if CheckIterable fails
author: Sam Mikes
description: Promise.all(new Error()) returns Promise rejected with TypeError
---*/
Promise.all(new Error("abrupt")).then(function () {
$ERROR('Promise unexpectedly resolved: Promise.all(abruptCompletion) should throw TypeError');
},function (err) {
if (!(err instanceof TypeError)) {
$ERROR('Expected TypeError, got ' + err);
}
}).then($DONE,$DONE);
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.all expects an iterable argument;
fails if GetIterator returns an abrupt completion.
author: Sam Mikes
description: Promise.all((throw on GetIterator)) returns Promise rejected with TypeError
---*/
var iterThrows = {};
Object.defineProperty(iterThrows, Symbol.iterator, {
get: function () {
throw new Error("abrupt completion");
}
});
Promise.all(iterThrows).then(function () {
$ERROR('Promise unexpectedly fulfilled: Promise.all(iterThrows) should throw TypeError');
},function (err) {
if (!(err instanceof Error)) {
$ERROR('Expected promise to be rejected with error, got ' + err);
}
}).then($DONE,$DONE);
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.all should throw if 'this' does not conform to Promise constructor
negative: TypeError
description: this must conform to Promise constructor in Promise.all
author: Sam Mikes
---*/
function ZeroArgConstructor() {
}
Promise.all.call(ZeroArgConstructor, []);
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.all expects an iterable argument;
rejects if IteratorStep() throws
author: Sam Mikes
description: iterator.next throws, causing Promise.all to reject
---*/
var iterThrows = {};
Object.defineProperty(iterThrows, Symbol.iterator, {
get: function () {
return {
next: function () {
throw new Error("abrupt completion");
}
};
}
});
Promise.all(iterThrows).then(function () {
$ERROR('Promise unexpectedly resolved: Promise.all(iterThrows) should throw TypeError');
},function (err) {
if (!(err instanceof TypeError)) {
$ERROR('Expected TypeError, got ' + err);
}
}).then($DONE,$DONE);
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.all with 0-element array
author: Sam Mikes
description: Promise.all([]) produces a promise
---*/
var p = Promise.all([]);
if (!(p instanceof Promise)) {
$ERROR('Expected Promise.all([]) to be instanceof Promise' + err);
}
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.all with 0-element array
should accept an empty array
author: Sam Mikes
description: Promise.all([]) returns a promise for an empty array
---*/
var p = Promise.all([]);
p.then(function (result) {
if (!(result instanceof Array)) {
$ERROR("Expected Promise.all([]) to be Array, actually " + result);
}
if (result.length !== 0) {
$ERROR("Expected Promise.all([]) to be empty Array, actually " + result);
}
}).then($DONE, $DONE);
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.all with 1-element array
should accept an array with settled promise
author: Sam Mikes
description: Promise.all([p1]) is resolved with a promise for a one-element array
---*/
var p1 = Promise.resolve(3);
var pAll = Promise.all([p1]);
pAll.then(function (result) {
if (!(pAll instanceof Promise)) {
$ERROR("Expected Promise.all() to be promise, actually " + pAll);
}
if (!(result instanceof Array)) {
$ERROR("Expected Promise.all() to be promise for an Array, actually " + result);
}
if (result.length !== 1) {
$ERROR("Expected Promise.all([p1]) to be a promise for one-element Array, actually " + result);
}
if (result[0] !== 3) {
$ERROR("Expected result[0] to be 3, actually " + result[0]);
}
}).then($DONE, $DONE);
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.all with 1-element array
should accept an array with settled promise
author: Sam Mikes
description: Promise.all() accepts a one-element array
includes: [PromiseHelper.js]
---*/
var sequence = [];
var p1 = new Promise(function (resolve) { resolve({}); } );
sequence.push(1);
Promise.all([p1]).then(function (resolved) {
sequence.push(4);
checkSequence(sequence, "Expected Promise.all().then to queue second");
}).catch($DONE);
p1.then(function () {
sequence.push(3);
checkSequence(sequence, "Expected p1.then to queue first");
}).then(function () {
sequence.push(5);
checkSequence(sequence, "Expected final then to queue last");
}).then($DONE, $DONE);
sequence.push(2);
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
author: Sam Mikes
description: Promise.all([p1, p2]) resolution functions are called in predictable sequence
includes: [PromiseHelper.js]
---*/
var sequence = [];
var p1 = new Promise(function (resolve) { resolve(1); } );
var p2 = new Promise(function (resolve) { resolve(2); } );
sequence.push(1);
p1.then(function () {
sequence.push(3);
checkSequence(sequence, "Expected to be called first.");
}).catch($DONE);
Promise.all([p1, p2]).then(function () {
sequence.push(5);
checkSequence(sequence, "Expected to be called third.");
}).then($DONE, $DONE);
p2.then(function () {
sequence.push(4);
checkSequence(sequence, "Expected to be called second.");
}).catch($DONE);
sequence.push(2);
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.all with 2-element array
author: Sam Mikes
description: Promise.all() rejects when a promise in its array rejects
includes: [PromiseHelper.js]
---*/
var rejectP1,
p1 = new Promise(function (resolve, reject) {
rejectP1 = reject;
}),
p2 = Promise.resolve(2);
Promise.all([p1, p2]).then(function (resolve) {
$ERROR("Did not expect promise to be fulfilled.");
}, function (rejected) {
if (rejected !== 1) {
$ERROR("Expected promise to be rejected with 1, actually " + rejected);
}
}).then($DONE, $DONE);
rejectP1(1);
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.all with 2-element array
author: Sam Mikes
description: Promise.all() rejects when second promise in array rejects
includes: [PromiseHelper.js]
---*/
var rejectP2,
p1 = Promise.resolve(1),
p2 = new Promise(function (resolve, reject) {
rejectP2 = reject;
});
Promise.all([p1, p2]).then(function () {
$ERROR("Did not expect promise to be fulfilled.");
}, function (rejected) {
if (rejected !== 2) {
$ERROR("Expected promise to be rejected with 2, actually " + rejected);
}
}).then($DONE, $DONE);
rejectP2(2);
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise prototype object exists, is object, not enumerable, writable,
or configurable
author: Sam Mikes
description: Promise prototype exists
---*/
if (Promise.prototype === undefined) {
$ERROR("Expected Promise.prototype to be defined.");
}
if (!(Promise.prototype instanceof Object)) {
$ERROR("Expected Promise.prototype to be an object.");
}
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: Promise.race is callable
author: Sam Mikes
description: Promise.race is callable
---*/
if (typeof Promise.race !== "function") {
$ERROR("Expected Promise.race to be a function, actually " + typeof Promise.race);
}
if (Promise.race.length !== 1) {
$ERROR("Expected Promise.race to be a function of 1 argument.");
}
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: Promise.race returns a new promise
author: Sam Mikes
description: Promise.race returns a new promise
---*/
var p = Promise.race([]);
if (!(p instanceof Promise)) {
$ERROR("Expected Promise.race([]) to return a promise.");
}
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: Promise.race rejects on non-iterable argument
author: Sam Mikes
description: Promise.race rejects if argument is not object or is non-iterable
---*/
var nonIterable = 3;
Promise.race(nonIterable).then(function () {
$ERROR('Promise unexpectedly fulfilled: Promise.race(nonIterable) should throw TypeError');
}, function (err) {
if (!(err instanceof TypeError)) {
$ERROR('Expected TypeError, got ' + err);
}
}).then($DONE, $DONE);
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: Promise.race rejects on non-iterable argument
author: Sam Mikes
description: Promise.race rejects if argument is not object or is non-iterable
---*/
Promise.race(new Error("abrupt")).then(function () {
$ERROR('Promise unexpectedly resolved: Promise.race(abruptCompletion) should throw TypeError');
}, function (err) {
if (!(err instanceof TypeError)) {
$ERROR('Expected TypeError, got ' + err);
}
}).then($DONE, $DONE);
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.race rejects when GetIterator() returns an abrupt completion
4. Let iterator be GetIterator(iterable).
5. IfAbruptRejectPromise(iterator, promiseCapability)
author: Sam Mikes
description: Promise.race rejects if GetIterator throws
---*/
var iterThrows = {};
Object.defineProperty(iterThrows, Symbol.iterator, {
get: function () {
throw new Error("abrupt completion");
}
});
Promise.race(iterThrows).then(function () {
$ERROR('Promise unexpectedly fulfilled: Promise.race(iterThrows) should throw');
}, function (err) {
if (!(err instanceof Error)) {
$ERROR('Expected Promise to be rejected with an error, got ' + err);
}
}).then($DONE, $DONE);
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.race throws on invalid 'this'
Note: must have at least one element in array, or else Promise.race
never exercises the code that throws
author: Sam Mikes
description: Promise.race throws if 'this' does not conform to Promise constructor
negative: TypeError
---*/
function ZeroArgConstructor() {
}
Promise.race.call(ZeroArgConstructor, [3]);
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.race must throw TypeError per
CreatePromiseCapabilityRecord step 8 when
promiseCapabliity.[[Resolve]] is not callable
author: Sam Mikes
description: Promise.race throws TypeError, even on empty array, when 'this' does not conform to Promise constructor
negative: TypeError
---*/
function BadPromiseConstructor(f) { f(undefined, undefined); }
Promise.race.call(BadPromiseConstructor, []);
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
author: Sam Mikes
description: Promise.race rejects if IteratorStep throws
---*/
var iterThrows = {};
Object.defineProperty(iterThrows, Symbol.iterator, {
get: function () {
return {
next: function () {
throw new Error("abrupt completion");
}
};
}
});
Promise.race(iterThrows).then(function () {
$ERROR('Promise unexpectedly fulfilled: Promise.race(iterThrows) should throw TypeError');
},function (err) {
if (!(err instanceof TypeError)) {
$ERROR('Expected TypeError, got ' + err);
}
}).then($DONE,$DONE);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment