Merge pull request #100 from smikes/promise-tests

add initial set of tests for Promise
This commit is contained in:
Brian Terlson 2014-11-12 11:29:11 -08:00
commit 0caf4eec31
85 changed files with 1849 additions and 62 deletions

View File

@ -0,0 +1,18 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise reaction jobs have predictable environment
author: Sam Mikes
description: argument passes through "Identity"
---*/
var obj = {};
var p = Promise.resolve(obj).then(/*Identity, Thrower*/)
.then(function (arg) {
if (arg !== obj) {
$ERROR("Expected promise to be fulfilled with obj, actually " + arg);
}
}).then($DONE, $DONE);

View File

@ -0,0 +1,20 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise reaction jobs have predictable environment
author: Sam Mikes
description: argument thrown through "Thrower"
---*/
var obj = {};
var p = Promise.reject(obj).then(/*Identity, Thrower*/)
.then(function () {
$ERROR("Unexpected fulfillment - promise should reject.");
}, function (arg) {
if (arg !== obj) {
$ERROR("Expected reject reason to be obj, actually " + arg);
}
}).then($DONE, $DONE);

View File

@ -0,0 +1,23 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise reaction jobs have predictable environment
author: Sam Mikes
description: Promise.onFulfilled gets undefined as 'this'
flags: [noStrict]
includes: [fnGlobalObject.js]
---*/
var expectedThis = fnGlobalObject(),
obj = {};
var p = Promise.resolve(obj).then(function(arg) {
if (this !== expectedThis) {
$ERROR("'this' must be global object, got " + this);
}
if (arg !== obj) {
$ERROR("Expected promise to be fulfilled by obj, actually " + arg);
}
}).then($DONE, $DONE);

View File

@ -0,0 +1,22 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise reaction jobs have predictable environment
author: Sam Mikes
description: Promise.onFulfilled gets undefined as 'this'
flags: [onlyStrict]
---*/
var expectedThis = undefined,
obj = {};
var p = Promise.resolve(obj).then(function(arg) {
if (this !== expectedThis) {
$ERROR("'this' must be undefined, got " + this);
}
if (arg !== obj) {
$ERROR("Expected promise to be fulfilled by obj, actually " + arg);
}
}).then($DONE, $DONE);

View File

@ -0,0 +1,28 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise reaction jobs have predictable environment
'this' is global object in sloppy mode,
undefined in strict mode
author: Sam Mikes
description: onRejected gets default 'this'
flags: [noStrict]
includes: [fnGlobalObject.js]
---*/
var expectedThis = fnGlobalObject(),
obj = {};
var p = Promise.reject(obj).then(function () {
$ERROR("Unexpected fulfillment; expected rejection.");
}, function(arg) {
if (this !== expectedThis) {
$ERROR("'this' must be global object, got " + this);
}
if (arg !== obj) {
$ERROR("Expected promise to be rejected with obj, actually " + arg);
}
}).then($DONE, $DONE);

View File

@ -0,0 +1,27 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise reaction jobs have predictable environment
'this' is global object in sloppy mode,
undefined in strict mode
author: Sam Mikes
description: onRejected gets default 'this'
flags: [onlyStrict]
---*/
var expectedThis = undefined,
obj = {};
var p = Promise.reject(obj).then(function () {
$ERROR("Unexpected fulfillment; expected rejection.");
}, function(arg) {
if (this !== expectedThis) {
$ERROR("'this' must be undefined, got " + this);
}
if (arg !== obj) {
$ERROR("Expected promise to be rejected with obj, actually " + arg);
}
}).then($DONE, $DONE);

View File

@ -0,0 +1,16 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise is the Promise property of the global object
author: Sam Mikes
description: Promise === global.Promise
includes: [fnGlobalObject.js]
---*/
var global = fnGlobalObject();
if (Promise !== global.Promise) {
$ERROR("Expected Promise === global.Promise.");
}

View File

@ -0,0 +1,12 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise throws TypeError when 'this' is not Object
author: Sam Mikes
description: Promise.call("non-object") throws TypeError
negative: TypeError
---*/
Promise.call("non-object", function () {});

View File

@ -0,0 +1,14 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise throws TypeError when 'this' is constructed but unsettled promise
author: Sam Mikes
description: Promise.call(new Promise()) throws TypeError
negative: TypeError
---*/
var p = new Promise(function() {});
Promise.call(p, function () {});

View File

@ -0,0 +1,21 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise throws TypeError when 'this' is resolved promise
author: Sam Mikes
description: Promise.call(resolved Promise) throws TypeError
---*/
var p = new Promise(function(resolve) { resolve(1); });
p.then(function () {
Promise.call(p, function () {});
}).then(function () {
$ERROR("Unexpected resolution - expected TypeError");
}, function (err) {
if (!(err instanceof TypeError)) {
$ERROR("Expected TypeError, got " + err);
}
}).then($DONE, $DONE);

View File

@ -0,0 +1,21 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise throws TypeError when 'this' is rejected promise
author: Sam Mikes
description: Promise.call(rejected Promise) throws TypeError
---*/
var p = new Promise(function(resolve, reject) { reject(1) });
p.catch(function () {
Promise.call(p, function () {});
}).then(function () {
$ERROR("Unexpected resolution - expected TypeError");
}, function (err) {
if (!(err instanceof TypeError)) {
$ERROR("Expected TypeError, got " + err);
}
}).then($DONE, $DONE);

View File

@ -0,0 +1,12 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise throws TypeError when executor is not callable
author: Sam Mikes
description: new Promise("not callable") throws TypeError
negative: TypeError
---*/
new Promise("not callable");

View File

@ -0,0 +1,24 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise catches exceptions thrown from executor and turns
them into reject
author: Sam Mikes
description: new Promise(function () { throw }) should reject
---*/
var errorObject = {},
p = new Promise(function () {
throw errorObject;
});
p.then(function() {
$ERROR("Unexpected fulfill -- promise should reject.");
}, function (err) {
if (err !== errorObject) {
$ERROR("Expected promise rejection reason to be thrown errorObject, actually " + err);
}
}).then($DONE, $DONE);

View File

@ -0,0 +1,23 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise executor has predictable environment
'this' should be global object in sloppy mode,
undefined in strict mode
author: Sam Mikes
description: Promise executor gets default handling for 'this'
flags: [noStrict]
includes: [fnGlobalObject.js]
---*/
var expectedThis = fnGlobalObject();
var p = new Promise(function (resolve) {
if (this !== expectedThis) {
$ERROR("'this' must be global object, got " + this);
}
resolve();
}).then($DONE, $DONE);

View File

@ -0,0 +1,22 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise executor has predictable environment
'this' should be global object in sloppy mode,
undefined in strict mode
author: Sam Mikes
description: Promise executor gets default handling for 'this'
flags: [onlyStrict]
---*/
var expectedThis = undefined;
var p = new Promise(function (resolve) {
if (this !== expectedThis) {
$ERROR("'this' must be undefined, got " + this);
}
resolve();
}).then($DONE, $DONE);

View File

@ -1,17 +1,13 @@
// Copyright 2014 Ecma International. All rights reserved.
// Ecma International makes this code available under the terms and conditions set
// forth on http://hg.ecmascript.org/tests/test262/raw-file/tip/LICENSE (the
// "Use Terms"). Any redistribution of this code must retain the above
// copyright and this notice and otherwise comply with the Use Terms.
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: Promise.all is callable
es5id: 25.4.4.1_A1.1_T1
author: Sam Mikes
description: Promise.all is callable
---*/
// CHECK#1
var x = typeof Promise.all;
if (x !== "function") {
$ERROR('#1: x = typeof Promise.all; x === "function". Actual: ' + (x));
if ((typeof Promise.all) !== "function") {
$ERROR('Expected Promise.all to be a function');
}

View File

@ -1,17 +1,14 @@
// Copyright 2012 Ecma International. All rights reserved.
// Ecma International makes this code available under the terms and conditions set
// forth on http://hg.ecmascript.org/tests/test262/raw-file/tip/LICENSE (the
// "Use Terms"). Any redistribution of this code must retain the above
// copyright and this notice and otherwise comply with the Use Terms.
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: Promise.all expects 1 argument
es5id: 25.4.4.1_A1.2_T1
author: Sam Mikes
description: Promise.all expects 1 argument
---*/
// CHECK#1
var x = Promise.all.length;
if (x !== 1) {
$ERROR('#1: x = Promise.all.length; x === 1. Actual: ' + (x));
if (Promise.all.length !== 1) {
$ERROR('Expected Promise.all to be a function of one argument.');
}

View File

@ -1,17 +1,14 @@
// Copyright 2014 Ecma International. All rights reserved.
// Ecma International makes this code available under the terms and conditions set
// forth on http://hg.ecmascript.org/tests/test262/raw-file/tip/LICENSE (the
// "Use Terms"). Any redistribution of this code must retain the above
// copyright and this notice and otherwise comply with the Use Terms.
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: Promise.all([]) is a Promise
es5id: 25.4.4.1_A2.1_T1
author: Sam Mikes
description: Promise.all returns a Promise
---*/
// CHECK#1
var x = (Promise.all([]) instanceof Promise);
if (x !== true) {
$ERROR('#1: x (Promise.all([]) instanceof Promise); x === true. Actual: ' + (x));
var p = Promise.all([]);
if (!(p instanceof Promise)) {
$ERROR('Expected p to be a Promise');
}

View File

@ -1,25 +1,25 @@
// Copyright 2014 Ecma International. All rights reserved.
// Ecma International makes this code available under the terms and conditions set
// forth on http://hg.ecmascript.org/tests/test262/raw-file/tip/LICENSE (the
// "Use Terms"). Any redistribution of this code must retain the above
// copyright and this notice and otherwise comply with the Use Terms.
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: Promise.all([]) is resolved immediately
es5id: 25.4.4.1_A2.2_T1
author: Sam Mikes
includes: [PromiseHelper.js]
description: Promise.all([]) returns immediately
---*/
var sequence = [];
Promise.all([]).then(function () {
sequence.push(1);
sequence.push(2);
}).catch($DONE);
Promise.resolve().then(function() {
sequence.push(2);
}).then(function () {
sequence.push(3);
}).then(function () {
sequence.push(4);
checkSequence(sequence, "Promises resolved in unexpected sequence");
}).then($DONE,$DONE);
}).then($DONE, $DONE);
sequence.push(1);

View File

@ -1,19 +1,17 @@
// Copyright 2014 Ecma International. All rights reserved.
// Ecma International makes this code available under the terms and conditions set
// forth on http://hg.ecmascript.org/tests/test262/raw-file/tip/LICENSE (the
// "Use Terms"). Any redistribution of this code must retain the above
// copyright and this notice and otherwise comply with the Use Terms.
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: Promise.all is resolved with a new empty array
info: Promise.all([]) returns a promise for a new empty array
es5id: 25.4.4.1_A2.3_T1
author: Sam Mikes
description: Promise.all([]) returns a promise for an array
---*/
var arg = [];
Promise.all(arg).then(function (result) {
if((result instanceof Array) !== true) {
if(!(result instanceof Array)) {
$ERROR("expected an array from Promise.all, got " + result);
}
}).then($DONE,$DONE);
}).then($DONE, $DONE);

View File

@ -1,13 +1,11 @@
// Copyright 2014 Ecma International. All rights reserved.
// Ecma International makes this code available under the terms and conditions set
// forth on http://hg.ecmascript.org/tests/test262/raw-file/tip/LICENSE (the
// "Use Terms"). Any redistribution of this code must retain the above
// copyright and this notice and otherwise comply with the Use Terms.
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: Promise.all is resolved with a new empty array
es5id: 25.4.4.1_A2.3_T2
author: Sam Mikes
description: Promise.all([]) returns a Promise for an empty array
---*/
var arg = [];
@ -16,4 +14,4 @@ Promise.all(arg).then(function (result) {
if(result.length !== 0) {
$ERROR("expected an empty array from Promise.all([]), got " + result);
}
}).then($DONE,$DONE);
}).then($DONE, $DONE);

View File

@ -1,13 +1,11 @@
// Copyright 2014 Ecma International. All rights reserved.
// Ecma International makes this code available under the terms and conditions set
// forth on http://hg.ecmascript.org/tests/test262/raw-file/tip/LICENSE (the
// "Use Terms"). Any redistribution of this code must retain the above
// copyright and this notice and otherwise comply with the Use Terms.
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: Promise.all is resolved with a new empty array
info: Promise.all([]) is resolved with Promise for a new empty array
es5id: 25.4.4.1_A2.3_T3
author: Sam Mikes
description: Promise.all([]) is resolved with a Promise for a new array
---*/
var arg = [];
@ -16,4 +14,4 @@ Promise.all(arg).then(function (result) {
if(result === arg) {
$ERROR("expected a new array from Promise.all but argument was re-used");
}
}).then($DONE,$DONE);
}).then($DONE, $DONE);

View File

@ -1,8 +1,5 @@
// Copyright 2014 Ecma International. All rights reserved.
// Ecma International makes this code available under the terms and conditions set
// forth on http://hg.ecmascript.org/tests/test262/raw-file/tip/LICENSE (the
// "Use Terms"). Any redistribution of this code must retain the above
// copyright and this notice and otherwise comply with the Use Terms.
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
@ -11,6 +8,7 @@ info: >
ref 7.4.2 GetIterator throws TypeError if CheckIterable fails
es5id: 25.4.4.1_A3.1_T1
author: Sam Mikes
description: Promise.all(3) returns Promise rejected with TypeError
---*/
var nonIterable = 3;
@ -21,4 +19,4 @@ Promise.all(nonIterable).then(function () {
if (!(err instanceof TypeError)) {
$ERROR('Expected TypeError, got ' + err);
}
}).then($DONE,$DONE);
}).then($DONE, $DONE);

View File

@ -0,0 +1,20 @@
// 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);

View File

@ -0,0 +1,25 @@
// 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);

View File

@ -0,0 +1,15 @@
// 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, []);

View File

@ -0,0 +1,29 @@
// 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);

View File

@ -0,0 +1,15 @@
// 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);
}

View File

@ -0,0 +1,21 @@
// 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);

View File

@ -0,0 +1,29 @@
// 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);

View File

@ -0,0 +1,32 @@
// 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);

View File

@ -0,0 +1,32 @@
// 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);

View File

@ -0,0 +1,26 @@
// 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);

View File

@ -0,0 +1,26 @@
// 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);

View File

@ -0,0 +1,19 @@
// 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.");
}

View File

@ -0,0 +1,16 @@
// 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.");
}

View File

@ -0,0 +1,14 @@
// 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.");
}

View File

@ -0,0 +1,19 @@
// 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);

View File

@ -0,0 +1,17 @@
// 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);

View File

@ -0,0 +1,28 @@
// 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);

View File

@ -0,0 +1,17 @@
// 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]);

View File

@ -0,0 +1,16 @@
// 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, []);

View File

@ -0,0 +1,27 @@
// 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);

View File

@ -0,0 +1,33 @@
// 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 () {
var v = {};
Object.defineProperty(v, 'value', {
get: function () {
throw new Error("abrupt completion");
}
});
return v;
}
};
}
});
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);

View File

@ -0,0 +1,19 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
author: Sam Mikes
description: Promise.race([]) never settles
---*/
var p = Promise.race([]);
p.then(function () {
$ERROR("Never settles.");
}, function () {
$ERROR("Never settles.");
}).then($DONE, $DONE);
// use three 'then's to allow above to settle
// if this is a buggy Promise.race implementation
Promise.resolve().then().then().then($DONE, $DONE);

View File

@ -0,0 +1,29 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
author: Sam Mikes
description: Promise.race([1]) settles immediately
includes: [PromiseHelper.js]
---*/
var sequence = [];
var p = Promise.race([1]);
sequence.push(1);
p.then(function () {
sequence.push(4);
checkSequence(sequence, "This happens second");
}).catch($DONE);
Promise.resolve().then(function () {
sequence.push(3);
checkSequence(sequence, "This happens first");
}).then(function () {
sequence.push(5);
checkSequence(sequence, "This happens third");
}).then($DONE, $DONE);
sequence.push(2);

View File

@ -0,0 +1,32 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
author: Sam Mikes
description: Promise.race([p1]) settles immediately
includes: [PromiseHelper.js]
---*/
var sequence = [];
var p1 = Promise.reject(1),
p = Promise.race([p1]);
sequence.push(1);
p.then(function () {
$ERROR("Should not fulfill.");
}, function () {
sequence.push(4);
checkSequence(sequence, "This happens second");
}).catch($DONE);
Promise.resolve().then(function () {
sequence.push(3);
checkSequence(sequence, "This happens first");
}).then(function () {
sequence.push(5);
checkSequence(sequence, "This happens third");
}).then($DONE, $DONE);
sequence.push(2);

View File

@ -0,0 +1,35 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
author: Sam Mikes
description: Promise.race([p1, p2]) settles when first settles
includes: [PromiseHelper.js]
---*/
var sequence = [];
var p1 = Promise.resolve(1),
p2 = Promise.resolve(2),
p = Promise.race([p1, p2]);
sequence.push(1);
p.then(function (arg) {
if (arg !== 1) {
$ERROR("Expected promise to be fulfilled with 1, got " + arg);
}
sequence.push(4);
checkSequence(sequence, "This happens second");
}).catch($DONE);
Promise.resolve().then(function () {
sequence.push(3);
checkSequence(sequence, "This happens first");
}).then(function () {
sequence.push(5);
checkSequence(sequence, "This happens third");
}).then($DONE, $DONE);
sequence.push(2);

View File

@ -0,0 +1,35 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
author: Sam Mikes
description: Promise.race([p1, p2]) settles when first settles
includes: [PromiseHelper.js]
---*/
var sequence = [];
var p1 = Promise.resolve(1),
p2 = new Promise(function () {}),
p = Promise.race([p1, p2]);
sequence.push(1);
p.then(function (arg) {
if (arg !== 1) {
$ERROR("Expected promise to be fulfilled with 1, got " + arg);
}
sequence.push(4);
checkSequence(sequence, "This happens second");
}).catch($DONE);
Promise.resolve().then(function () {
sequence.push(3);
checkSequence(sequence, "This happens first");
}).then(function () {
sequence.push(5);
checkSequence(sequence, "This happens third");
}).then($DONE, $DONE);
sequence.push(2);

View File

@ -0,0 +1,35 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
author: Sam Mikes
description: Promise.race([p1, p2]) settles when first settles
includes: [PromiseHelper.js]
---*/
var sequence = [];
var p1 = new Promise(function () {}),
p2 = Promise.resolve(2),
p = Promise.race([p1, p2]);
sequence.push(1);
p.then(function (arg) {
if (arg !== 2) {
$ERROR("Expected promise to be fulfilled with 2, got " + arg);
}
sequence.push(4);
checkSequence(sequence, "This happens second");
}).catch($DONE);
Promise.resolve().then(function () {
sequence.push(3);
checkSequence(sequence, "This happens first");
}).then(function () {
sequence.push(5);
checkSequence(sequence, "This happens third");
}).then($DONE, $DONE);
sequence.push(2);

View File

@ -0,0 +1,37 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
author: Sam Mikes
description: Promise.race([p1, p2]) settles when first settles
includes: [PromiseHelper.js]
---*/
var sequence = [];
var p1 = Promise.reject(1),
p2 = Promise.resolve(2),
p = Promise.race([p1, p2]);
sequence.push(1);
p.then(function () {
$ERROR("Should not be fulfilled - expected rejection.");
}, function (arg) {
if (arg !== 1) {
$ERROR("Expected rejection reason to be 1, got " + arg);
}
sequence.push(4);
checkSequence(sequence, "This happens second");
}).catch($DONE);
Promise.resolve().then(function () {
sequence.push(3);
checkSequence(sequence, "This happens first");
}).then(function () {
sequence.push(5);
checkSequence(sequence, "This happens third");
}).then($DONE, $DONE);
sequence.push(2);

View File

@ -0,0 +1,20 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
author: Sam Mikes
description: Promise.race([p1, p2]) settles when first settles
---*/
var resolveP1, rejectP2,
p1 = new Promise(function (resolve) { resolveP1 = resolve; }),
p2 = new Promise(function (resolve, reject) { rejectP2 = reject; });
rejectP2(new Error("Promise.race should not see this if P1 already resolved"));
resolveP1(1);
Promise.race([p1, p2]).then(function (arg) {
if (arg !== 1) {
$ERROR("Expected fulfillment with 1, got " + arg);
}
}).then($DONE, $DONE);

View File

@ -0,0 +1,23 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
author: Sam Mikes
description: Promise.race([p1, p2]) settles when first settles
---*/
var resolveP1, rejectP2,
p1 = new Promise(function (resolve) { resolveP1 = resolve; }),
p2 = new Promise(function (resolve, reject) { rejectP2 = reject; });
Promise.race([p1, p2]).then(function () {
$ERROR("Should not be fulfilled: expected rejection.");
}, function (arg) {
if (arg !== 2) {
$ERROR("Expected rejection reason to be 2, got " + arg);
}
}).then($DONE, $DONE);
rejectP2(2);
resolveP1(1);

View File

@ -0,0 +1,17 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.reject
author: Sam Mikes
description: Promise.reject is a function
---*/
if ((typeof Promise.reject) !== "function") {
$ERROR("Expected Promise.reject to be a function");
}
if (Promise.reject.length !== 1) {
$ERROR("Expected Promise.reject to be a function of one argument");
}

View File

@ -0,0 +1,23 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.reject
author: Sam Mikes
description: Promise.reject creates a new settled promise
---*/
var p = Promise.reject(3);
if (!(p instanceof Promise)) {
$ERROR("Expected Promise.reject to return a promise.");
}
p.then(function () {
$ERROR("Promise should not be fulfilled.");
}, function (arg) {
if (arg !== 3) {
$ERROR("Expected promise to be rejected with supplied arg, got " + arg);
}
}).then($DONE, $DONE);

View File

@ -0,0 +1,15 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.reject
author: Sam Mikes
description: Promise.reject throws TypeError for bad 'this'
negative: TypeError
---*/
function ZeroArgConstructor() {
}
Promise.reject.call(ZeroArgConstructor, 4);

View File

@ -0,0 +1,17 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.resolve
author: Sam Mikes
description: Promise.resolve is a function
---*/
if ((typeof Promise.resolve) !== "function") {
$ERROR("Expected Promise.resolve to be a function");
}
if (Promise.resolve.length !== 1) {
$ERROR("Expected Promise.resolve to be a function of one argument");
}

View File

@ -0,0 +1,14 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
author: Sam Mikes
description: Promise.resolve passes through a promise w/ same Constructor
---*/
var p1 = Promise.resolve(1),
p2 = Promise.resolve(p1);
if (p1 !== p2) {
$ERROR("Expected p1 === Promise.resolve(p1) because they have same constructor");
}

View File

@ -0,0 +1,24 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
author: Sam Mikes
description: Promise.resolve passes through an unsettled promise w/ same Constructor
---*/
var resolveP1,
p1 = new Promise(function (resolve) { resolveP1 = resolve; }),
p2 = Promise.resolve(p1),
obj = {};
if (p1 !== p2) {
$ERROR("Expected p1 === Promise.resolve(p1) because they have same constructor");
}
p2.then(function (arg) {
if (arg !== obj) {
$ERROR("Expected promise to be resolved with obj, actually " + arg);
}
}).then($DONE, $DONE);
resolveP1(obj);

View File

@ -0,0 +1,26 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
author: Sam Mikes
description: Promise.resolve passes through an unsettled promise w/ same Constructor
---*/
var rejectP1,
p1 = new Promise(function (resolve, reject) { rejectP1 = reject; }),
p2 = Promise.resolve(p1),
obj = {};
if (p1 !== p2) {
$ERROR("Expected p1 === Promise.resolve(p1) because they have same constructor");
}
p2.then(function () {
$ERROR("Expected p2 to be rejected, not fulfilled.");
}, function (arg) {
if (arg !== obj) {
$ERROR("Expected promise to be rejected with reason obj, actually " + arg);
}
}).then($DONE, $DONE);
rejectP1(obj);

View File

@ -0,0 +1,32 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.resolve
author: Sam Mikes
description: Promise.resolve delegates to foreign thenable
includes: [PromiseHelper.js]
---*/
var sequence = [];
var thenable = {
then: function(onResolve, onReject) {
sequence.push(3);
checkSequence(sequence, "thenable.then called");
Promise.resolve().then(function () {
sequence.push(4);
checkSequence(sequence, "all done");
}).then($DONE, $DONE);
}
};
sequence.push(1);
checkSequence(sequence, "no async calls yet");
var p1 = Promise.resolve(thenable);
sequence.push(2);
checkSequence(sequence, "thenable.then queued but not yet called");

View File

@ -0,0 +1,22 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Section 25.4.1.4.2
author: Sam Mikes
description: self-resolved Promise throws TypeError
---*/
var resolveP,
p = new Promise(function (resolve) { resolveP = resolve; });
resolveP(p);
p.then(function () {
$ERROR("Should not fulfill: should reject with TypeError.");
}, function (err) {
if (!(err instanceof TypeError)) {
$ERROR("Expected TypeError, got " + err);
}
}).then($DONE, $DONE);

View File

@ -0,0 +1,38 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Misc sequencing tests
inspired by https://github.com/getify/native-promise-only/issues/34#issuecomment-54282002
author: Sam Mikes
description: Promise onResolved functions are called in predictable sequence
includes: [PromiseHelper.js]
---*/
var sequence = [];
var p = new Promise(function(resolve, reject){
sequence.push(1);
resolve("");
});
p.then(function () {
sequence.push(3);
}).then(function () {
sequence.push(5);
}).then(function () {
sequence.push(7);
});
p.then(function () {
sequence.push(4);
}).then(function () {
sequence.push(6);
}).then(function () {
sequence.push(8);
}).then(function () {
checkSequence(sequence, "Sequence should be as expected");
}).then($DONE, $DONE);
sequence.push(2);

View File

@ -0,0 +1,33 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Misc sequencing tests
inspired by https://github.com/promises-aplus/promises-tests/issues/61
Case "T1"
author: Sam Mikes
description: Promise onResolved functions are called in predictable sequence
includes: [PromiseHelper.js]
---*/
var resolveP1, rejectP2, sequence = [];
(new Promise(function (resolve, reject) {
resolveP1 = resolve;
})).then(function (msg) {
sequence.push(msg);
}).then(function () {
checkSequence(sequence, "Expected 1,2,3");
}).then($DONE, $DONE);
(new Promise(function (resolve, reject) {
rejectP2 = reject;
})).catch(function (msg) {
sequence.push(msg);
});
rejectP2(2);
resolveP1(3);
sequence.push(1);

View File

@ -0,0 +1,37 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Misc sequencing tests
inspired by https://github.com/promises-aplus/promises-tests/issues/61
Case "T2a"
author: Sam Mikes
description: Promise onResolved functions are called in predictable sequence
includes: [PromiseHelper.js]
---*/
var resolveP1, rejectP2, p1, p2,
sequence = [];
p1 = new Promise(function (resolve, reject) {
resolveP1 = resolve;
});
p2 = new Promise(function (resolve, reject) {
rejectP2 = reject;
});
rejectP2(3);
resolveP1(2);
p1.then(function (msg) {
sequence.push(msg);
});
p2.catch(function (msg) {
sequence.push(msg);
}).then(function () {
checkSequence(sequence, "Expected 1,2,3");
}).then($DONE, $DONE);
sequence.push(1);

View File

@ -0,0 +1,41 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Misc sequencing tests
inspired by https://github.com/promises-aplus/promises-tests/issues/61
Case "T2b"
author: Sam Mikes
description: Promise onResolved functions are called in predictable sequence
includes: [PromiseHelper.js]
---*/
var resolveP1, rejectP2, p1, p2,
sequence = [];
p1 = new Promise(function (resolve, reject) {
resolveP1 = resolve;
});
p2 = new Promise(function (resolve, reject) {
rejectP2 = reject;
});
rejectP2(3);
resolveP1(2);
Promise.resolve().then(function () {
p1.then(function (msg) {
sequence.push(msg);
});
p2.catch(function (msg) {
sequence.push(msg);
}).then(function () {
checkSequence(sequence, "Expected 1,2,3");
}).then($DONE, $DONE);
});
sequence.push(1);

View File

@ -0,0 +1,13 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise prototype.catch is a function
author: Sam Mikes
description: Promise.prototype.catch is a function
---*/
if (!(Promise.prototype.catch instanceof Function)) {
$ERROR("Expected Promise.prototype.catch to be a function");
}

View File

@ -0,0 +1,21 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
catch is a method on a Promise
author: Sam Mikes
description: catch is a method on a Promise
---*/
var p = Promise.resolve(3);
if (!(p.catch instanceof Function)) {
$ERROR("Expected p.catch to be a function");
}
if (p.catch.length !== 1) {
$ERROR("Expected p.catch to take one argument");
}

View File

@ -0,0 +1,22 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
catch(arg) is equivalent to then(undefined, arg)
author: Sam Mikes
description: catch is implemented in terms of then
---*/
var obj = {};
var p = Promise.resolve(obj);
p.catch(function () {
$ERROR("Should not be called - promise is fulfilled");
}).then(function (arg) {
if (arg !== obj) {
$ERROR("Expected promise to be fulfilled with obj, got " + arg);
}
}).then($DONE, $DONE);

View File

@ -0,0 +1,22 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
catch(arg) is equivalent to then(undefined, arg)
author: Sam Mikes
description: catch is implemented in terms of then
---*/
var obj = {};
var p = Promise.reject(obj);
p.then(function () {
$ERROR("Should not be called: did not expect promise to be fulfilled");
}).catch(function (arg) {
if (arg !== obj) {
$ERROR("Should have been rejected with reason obj, got " + arg);
}
}).then($DONE, $DONE);

View File

@ -0,0 +1,17 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.prototype.then is a function of two arguments
author: Sam Mikes
description: Promise.prototype.then is a function of two arguments
---*/
if (!(Promise.prototype.then instanceof Function)) {
$ERROR("Expected Promise.prototype.then to be a function");
}
if (Promise.prototype.then.length !== 2) {
$ERROR("Expected Promise.prototype.then to be a function of two arguments");
}

View File

@ -0,0 +1,21 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.prototype.then is a function of two arguments
author: Sam Mikes
description: Promise.prototype.then is a function of two arguments
---*/
var p = new Promise(function () {});
if (!(p.then instanceof Function)) {
$ERROR("Expected p.then to be a function");
}
if (p.then.length !== 2) {
$ERROR("Expected p.then to be a function of two arguments");
}

View File

@ -0,0 +1,15 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.prototype.then expects a constructor conforming to Promise as 'this'
author: Sam Mikes
description: Promise.prototype.then throw if 'this' is non-Object
negative: TypeError
---*/
var p = new Promise(function () {});
p.then.call(3, function () {}, function () {});

View File

@ -0,0 +1,17 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.prototype.then expects a Promise as 'this'
author: Sam Mikes
description: Promise.prototype.then throw if 'this' is non-Promise Object
negative: TypeError
---*/
function ZeroArgConstructor() {
}
var z = new ZeroArgConstructor();
Promise.then.call(z, function () {}, function () {});

View File

@ -0,0 +1,33 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.prototype.then throws TypeError if Get(promise, "constructor") throws
Ref 25.4.5.3 step 4 ReturnIfAbrupt(C)
author: Sam Mikes
description: Promise.prototype.then throws if Get(promise, "constructor") throws
---*/
var p = Promise.resolve("foo");
Object.defineProperty(p, "constructor", {
get: function () {
throw new Error("abrupt completion");
}
});
try {
p.then(function () {
$ERROR("Should never be called.");
}, function () {
$ERROR("Should never be called.");
});
} catch (e) {
if (!(e instanceof Error)) {
$ERROR("Expected Error, got " + e);
}
if (e.message !== "abrupt completion") {
$ERROR("Expected the Error we threw, got " + e);
}
}

View File

@ -0,0 +1,20 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
PerformPromiseThen
Ref 25.4.5.3.1
author: Sam Mikes
description: Promise.prototype.then accepts 'undefined' as arg1, arg2
---*/
var obj = {};
var p = Promise.resolve(obj);
p.then(undefined, undefined)
.then(function (arg) {
if (arg !== obj) {
$ERROR("Expected resolution object to be passed through, got " + arg);
}
}).then($DONE, $DONE);

View File

@ -0,0 +1,21 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
PerformPromiseThen
Ref 25.4.5.3.1
author: Sam Mikes
description: Promise.prototype.then accepts 'undefined' as arg1, arg2
---*/
var obj = {};
var p = Promise.reject(obj);
p.then(undefined, undefined).then(function () {
$ERROR("Should not be called -- promise was rejected.");
}, function (arg) {
if (arg !== obj) {
$ERROR("Expected resolution object to be passed through, got " + arg);
}
}).then($DONE, $DONE);

View File

@ -0,0 +1,20 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
PerformPromiseThen
Ref 25.4.5.3.1
author: Sam Mikes
description: Promise.prototype.then treats non-callable arg1, arg2 as undefined
---*/
var obj = {};
var p = Promise.resolve(obj);
p.then(3, 5)
.then(function (arg) {
if (arg !== obj) {
$ERROR("Expected resolution object to be passed through, got " + arg);
}
}).then($DONE, $DONE);

View File

@ -0,0 +1,21 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
PerformPromiseThen
Ref 25.4.5.3.1
author: Sam Mikes
description: Promise.prototype.then treats non-callable arg1, arg2 as undefined
---*/
var obj = {};
var p = Promise.reject(obj);
p.then(3, 5).then(function () {
$ERROR("Should not be called -- promise was rejected.");
}, function (arg) {
if (arg !== obj) {
$ERROR("Expected resolution object to be passed through, got " + arg);
}
}).then($DONE, $DONE);

View File

@ -0,0 +1,37 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
PerformPromiseThen
Ref 25.4.5.3.1
author: Sam Mikes
description: Promise.prototype.then enqueues handler if pending
includes: [PromiseHelper.js]
---*/
var sequence = [],
pResolve,
p = new Promise(function (resolve, reject) {
pResolve = resolve;
});
sequence.push(1);
p.then(function () {
sequence.push(3);
checkSequence(sequence, "Should be second");
}).catch($DONE);
Promise.resolve().then(function () {
// enqueue another then-handler
p.then(function () {
sequence.push(4);
checkSequence(sequence, "Should be third");
}).then($DONE, $DONE);
sequence.push(2);
checkSequence(sequence, "Should be first");
pResolve();
}).catch($DONE);

View File

@ -0,0 +1,39 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
PerformPromiseThen
Ref 25.4.5.3.1
author: Sam Mikes
description: Promise.prototype.then immediately queues handler if fulfilled
includes: [PromiseHelper.js]
---*/
var sequence = [],
pResolve,
p = new Promise(function (resolve, reject) {
pResolve = resolve;
});
sequence.push(1);
pResolve();
p.then(function () {
sequence.push(3);
checkSequence(sequence, "Should be first");
}).catch($DONE);
Promise.resolve().then(function () {
// enqueue another then-handler
p.then(function () {
sequence.push(5);
checkSequence(sequence, "Should be third");
}).then($DONE, $DONE);
sequence.push(4);
checkSequence(sequence, "Should be second");
}).catch($DONE);
sequence.push(2);

View File

@ -0,0 +1,43 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
PerformPromiseThen
Ref 25.4.5.3.1
author: Sam Mikes
description: Promise.prototype.then immediately queues handler if rejected
includes: [PromiseHelper.js]
---*/
var sequence = [],
pReject,
p = new Promise(function (resolve, reject) {
pReject = reject;
});
sequence.push(1);
pReject();
p.then(function () {
$ERROR("Should not be called -- Promise rejected.");
}, function () {
sequence.push(3);
checkSequence(sequence, "Should be first");
}).catch($DONE);
Promise.resolve().then(function () {
// enqueue another then-handler
p.then(function () {
$ERROR("Should not be called (2) -- Promise rejected.");
}, function () {
sequence.push(5);
checkSequence(sequence, "Should be third");
}).then($DONE, $DONE);
sequence.push(4);
checkSequence(sequence, "Should be second");
}).catch($DONE);
sequence.push(2);

View File

@ -0,0 +1,12 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise prototype object is an ordinary object
author: Sam Mikes
description: Promise prototype does not have [[PromiseState]] internal slot
negative: TypeError
---*/
Promise.call(Promise.prototype, function () {});

View File

@ -0,0 +1,14 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise prototype object is an ordinary object
author: Sam Mikes
description: Promise prototype is a standard built-in Object
---*/
if (!(Promise.prototype instanceof Object)) {
$ERROR("Expected Promise.prototype to be an Object");
}

View File

@ -0,0 +1,14 @@
// Copyright 2014 Cubane Canada, Inc. All rights reserved.
// See LICENSE for details.
/*---
info: >
Promise.prototype.constructor is the Promise constructor
author: Sam Mikes
description: Promise.prototype.constructor is the Promise constructor
---*/
if (Promise.prototype.constructor !== Promise) {
$ERROR("Expected Promise.prototype.constructor to be Promise");
}