From e14a9ad9fefaf94454695e74db9873282ea22880 Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 2 Mar 2018 09:44:23 -0500 Subject: [PATCH] async-iteration: AsyncGeneratorPrototype tests (#1451) --- .../Symbol.toStringTag.js | 28 ++++++ .../AsyncGeneratorPrototype/constructor.js | 30 ++++++ .../next/iterator-result-prototype.js | 46 +++++++++ .../AsyncGeneratorPrototype/next/length.js | 35 +++++++ .../AsyncGeneratorPrototype/next/name.js | 31 ++++++ .../AsyncGeneratorPrototype/next/prop-desc.js | 21 ++++ .../next/request-queue-await-order.js | 66 +++++++++++++ .../request-queue-order-state-executing.js | 71 +++++++++++++ .../next/request-queue-order.js | 71 +++++++++++++ .../request-queue-promise-resolve-order.js | 82 +++++++++++++++ .../next/return-promise.js | 26 +++++ .../next/this-val-not-async-generator.js | 88 +++++++++++++++++ .../next/this-val-not-object.js | 98 ++++++++++++++++++ .../return/iterator-result-prototype.js | 47 +++++++++ .../AsyncGeneratorPrototype/return/length.js | 35 +++++++ .../AsyncGeneratorPrototype/return/name.js | 31 ++++++ .../return/prop-desc.js | 21 ++++ .../request-queue-order-state-executing.js | 66 +++++++++++++ .../return/return-promise.js | 28 ++++++ .../return/return-state-completed.js | 38 +++++++ .../return/this-val-not-async-generator.js | 89 +++++++++++++++++ .../return/this-val-not-object.js | 99 +++++++++++++++++++ .../AsyncGeneratorPrototype/throw/length.js | 35 +++++++ .../AsyncGeneratorPrototype/throw/name.js | 31 ++++++ .../throw/prop-desc.js | 21 ++++ .../request-queue-order-state-executing.js | 68 +++++++++++++ .../throw/return-rejected-promise.js | 54 ++++++++++ .../throw/this-val-not-async-generator.js | 89 +++++++++++++++++ .../throw/this-val-not-object.js | 99 +++++++++++++++++++ .../throw/throw-state-completed.js | 38 +++++++ 30 files changed, 1582 insertions(+) create mode 100644 test/built-ins/AsyncGeneratorPrototype/Symbol.toStringTag.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/constructor.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/next/iterator-result-prototype.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/next/length.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/next/name.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/next/prop-desc.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/next/request-queue-await-order.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/next/request-queue-order-state-executing.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/next/request-queue-order.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/next/request-queue-promise-resolve-order.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/next/return-promise.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/next/this-val-not-async-generator.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/next/this-val-not-object.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/return/iterator-result-prototype.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/return/length.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/return/name.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/return/prop-desc.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/return/request-queue-order-state-executing.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/return/return-promise.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/return/return-state-completed.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/return/this-val-not-async-generator.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/return/this-val-not-object.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/throw/length.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/throw/name.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/throw/prop-desc.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/throw/request-queue-order-state-executing.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/throw/return-rejected-promise.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/throw/this-val-not-async-generator.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/throw/this-val-not-object.js create mode 100644 test/built-ins/AsyncGeneratorPrototype/throw/throw-state-completed.js diff --git a/test/built-ins/AsyncGeneratorPrototype/Symbol.toStringTag.js b/test/built-ins/AsyncGeneratorPrototype/Symbol.toStringTag.js new file mode 100644 index 0000000000..b30c09ca8b --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/Symbol.toStringTag.js @@ -0,0 +1,28 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-tostringtag +description: > + `Symbol.toStringTag` property descriptor +info: | + The initial value of the @@toStringTag property is the String value + "AsyncGenerator". + + This property has the attributes { [[Writable]]: false, [[Enumerable]]: + false, [[Configurable]]: true }. + +includes: [propertyHelper.js] +features: [async-iteration, Symbol.toStringTag] +---*/ + +var AsyncGeneratorPrototype = Object.getPrototypeOf( + Object.getPrototypeOf(async function*() {}()) +); + +verifyProperty(AsyncGeneratorPrototype, Symbol.toStringTag, { + value: 'AsyncGenerator', + enumerable: false, + writable: false, + configurable: true, +}); diff --git a/test/built-ins/AsyncGeneratorPrototype/constructor.js b/test/built-ins/AsyncGeneratorPrototype/constructor.js new file mode 100644 index 0000000000..2dfac98369 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/constructor.js @@ -0,0 +1,30 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-constructor +description: > + The GeneratorPrototype intrinsic's constructor. +info: | + AsyncGenerator.prototype.constructor + + The initial value of AsyncGenerator.prototype.constructor is the + intrinsic object %AsyncGenerator%. + + This property has the attributes { [[Writable]]: false, + [[Enumerable]]: false, [[Configurable]]: true }. + +includes: [propertyHelper.js] +features: [async-iteration] +---*/ + +async function* g() {} +var AsyncGenerator = Object.getPrototypeOf(g); +var AsyncGeneratorPrototype = AsyncGenerator.prototype; + +verifyProperty(AsyncGeneratorPrototype, 'constructor', { + value: AsyncGenerator, + enumerable: false, + writable: false, + configurable: true, +}); diff --git a/test/built-ins/AsyncGeneratorPrototype/next/iterator-result-prototype.js b/test/built-ins/AsyncGeneratorPrototype/next/iterator-result-prototype.js new file mode 100644 index 0000000000..47b3ddd44a --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/next/iterator-result-prototype.js @@ -0,0 +1,46 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-next +description: > + "next" returns a promise for an IteratorResult object +info: | + AsyncGenerator.prototype.next ( value ) + 1. Let generator be the this value. + 2. Let completion be NormalCompletion(value). + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 4. Let queue be generator.[[AsyncGeneratorQueue]]. + 5. Let request be AsyncGeneratorRequest{[[Completion]]: completion, + [[Capability]]: promiseCapability}. + 6. Append request to the end of queue. + ... + + AsyncGeneratorResolve ( generator, value, done ) + 1. Assert: generator is an AsyncGenerator instance. + 2. Let queue be generator.[[AsyncGeneratorQueue]]. + 3. Assert: queue is not an empty List. + 4. Remove the first element from queue and let next be the value of that element. + 5. Let promiseCapability be next.[[Capability]]. + 6. Let iteratorResult be ! CreateIterResultObject(value, done). + 7. Perform ! Call(promiseCapability.[[Resolve]], undefined, « iteratorResult »). + ... + +flags: [async] +features: [async-iteration] +---*/ + +async function* g() {} + +g().next().then(function (result) { + assert( + Object.hasOwnProperty.call(result, 'value'), 'Has "own" property `value`' + ); + assert( + Object.hasOwnProperty.call(result, 'done'), 'Has "own" property `done`' + ); + assert.sameValue(Object.getPrototypeOf(result), Object.prototype); +}).then($DONE, $DONE) diff --git a/test/built-ins/AsyncGeneratorPrototype/next/length.js b/test/built-ins/AsyncGeneratorPrototype/next/length.js new file mode 100644 index 0000000000..7252e1b634 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/next/length.js @@ -0,0 +1,35 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-next +description: > + AsyncGenerator.prototype.next.length is 1. +info: | + AsyncGenerator.prototype.next ( value ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, has a length + property whose value is an integer. Unless otherwise specified, this + value is equal to the largest number of named arguments shown in the + subclause headings for the function description, including optional + parameters. However, rest parameters shown using the form “...name” + are not included in the default argument count. + + Unless otherwise specified, the length property of a built-in Function + object has the attributes { [[Writable]]: false, [[Enumerable]]: false, + [[Configurable]]: true }. +includes: [propertyHelper.js] +features: [async-iteration] +---*/ + +async function* g() {} +var AsyncGeneratorPrototype = Object.getPrototypeOf(g).prototype; + +verifyProperty(AsyncGeneratorPrototype.next, "length", { + value: 1, + enumerable: false, + writable: false, + configurable: true, +}); + diff --git a/test/built-ins/AsyncGeneratorPrototype/next/name.js b/test/built-ins/AsyncGeneratorPrototype/next/name.js new file mode 100644 index 0000000000..f292515c4b --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/next/name.js @@ -0,0 +1,31 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-next +description: > + Generator.prototype.next.name is "next". +info: | + Generator.prototype.next ( value ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, that is not + identified as an anonymous function has a name property whose value + is a String. + + Unless otherwise specified, the name property of a built-in Function + object, if it exists, has the attributes { [[Writable]]: false, + [[Enumerable]]: false, [[Configurable]]: true }. +includes: [propertyHelper.js] +features: [generators] +---*/ + +async function* g() {} +var AsyncGeneratorPrototype = Object.getPrototypeOf(g).prototype; + +verifyProperty(AsyncGeneratorPrototype.next, "name", { + value: "next", + enumerable: false, + writable: false, + configurable: true, +}); diff --git a/test/built-ins/AsyncGeneratorPrototype/next/prop-desc.js b/test/built-ins/AsyncGeneratorPrototype/next/prop-desc.js new file mode 100644 index 0000000000..b064e4f789 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/next/prop-desc.js @@ -0,0 +1,21 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-next +description: GeneratorPrototype.next property description +info: | + This property has the attributes { [[Writable]]: false, [[Enumerable]]: + false, [[Configurable]]: false }. +includes: [propertyHelper.js] +features: [async-iteration] +---*/ + +async function* g() {} +var AsyncGeneratorPrototype = Object.getPrototypeOf(g).prototype; + +verifyProperty(AsyncGeneratorPrototype, "next", { + enumerable: false, + writable: true, + configurable: true, +}); diff --git a/test/built-ins/AsyncGeneratorPrototype/next/request-queue-await-order.js b/test/built-ins/AsyncGeneratorPrototype/next/request-queue-await-order.js new file mode 100644 index 0000000000..23ce911b53 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/next/request-queue-await-order.js @@ -0,0 +1,66 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-next +description: next() requests from iterator processed in order, await +info: > + AsyncGenerator.prototype.next ( value ) + 1. Let generator be the this value. + 2. Let completion be NormalCompletion(value). + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 4. Let queue be generator.[[AsyncGeneratorQueue]]. + 5. Let request be AsyncGeneratorRequest{[[Completion]]: completion, + [[Capability]]: promiseCapability}. + 6. Append request to the end of queue. + ... + + AsyncGeneratorResolve ( generator, value, done ) + ... + 2. Let queue be generator.[[AsyncGeneratorQueue]]. + 3. Assert: queue is not an empty List. + 4. Remove the first element from queue and let next be the value of that element. + ... + +flags: [async] +features: [async-iteration] +---*/ + +var yieldorder = 0; +var resolveLatePromise; + +function resolveLater() { + return new Promise(resolve => { + resolveLatePromise = resolve; + }); +} + +async function* g() { + yield resolveLater(); + yield ++yieldorder; +} + +var iter = g(); + +assert.sameValue(yieldorder, 0); + +var item1 = iter.next(); +var item2 = iter.next(); +var item3 = iter.next(); + +async function awaitnexts() { + assert.sameValue((await item3).value, undefined) + assert.sameValue(yieldorder, 2, "All next requests have been proccessed.") + assert.sameValue((await item2).value, 2) + assert.sameValue((await item1).value, 1) +} + +awaitnexts().then($DONE, $DONE); + +// At this point: +// yieldorder == 0 +// item1 is an unresolved promise +resolveLatePromise(++yieldorder); diff --git a/test/built-ins/AsyncGeneratorPrototype/next/request-queue-order-state-executing.js b/test/built-ins/AsyncGeneratorPrototype/next/request-queue-order-state-executing.js new file mode 100644 index 0000000000..82a9baf13e --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/next/request-queue-order-state-executing.js @@ -0,0 +1,71 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-asyncgenerator-prototype-throw +description: next() call while iterator is in state executing +info: | + AsyncGenerator.prototype.next ( value ) + 1. Let generator be the this value. + 2. Let completion be NormalCompletion(value). + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 4. Let queue be generator.[[AsyncGeneratorQueue]]. + 5. Let request be AsyncGeneratorRequest{[[Completion]]: completion, + [[Capability]]: promiseCapability}. + 6. Append request to the end of queue. + ... + + AsyncGeneratorResolve ( generator, value, done ) + ... + 2. Let queue be generator.[[AsyncGeneratorQueue]]. + 3. Assert: queue is not an empty List. + 4. Remove the first element from queue and let next be the value of that element. + ... + +flags: [async] +features: [async-iteration] +---*/ + +var iter, result; +var executionorder = 0; +var valueisset = false; + +async function* g() { + + iter.next().then( + function(result) { + assert(valueisset, "variable valueisset should be set to true"); + assert.sameValue(++executionorder, 2); + assert.sameValue(result.value, 2); + assert.sameValue(result.done, false); + }, + function() { + $DONE(new Test262Error("next() should result in resolved promise.")); + } + ).catch($DONE) + + valueisset = true; + + yield 1; + yield 2; +} + +iter = g(); + +iter.next().then(function(result) { + assert.sameValue(++executionorder, 1); + assert.sameValue(result.value, 1); + assert.sameValue(result.done, false); + + iter.next().then(function(result) { + assert.sameValue(++executionorder, 3); + assert.sameValue(result.value, undefined); + assert.sameValue(result.done, true); + }).then($DONE, $DONE); + +}).catch($DONE); + + + diff --git a/test/built-ins/AsyncGeneratorPrototype/next/request-queue-order.js b/test/built-ins/AsyncGeneratorPrototype/next/request-queue-order.js new file mode 100644 index 0000000000..5edc01ec3d --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/next/request-queue-order.js @@ -0,0 +1,71 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-next +description: queue +info: > + AsyncGenerator.prototype.next ( value ) + 1. Let generator be the this value. + 2. Let completion be NormalCompletion(value). + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 4. Let queue be generator.[[AsyncGeneratorQueue]]. + 5. Let request be AsyncGeneratorRequest{[[Completion]]: completion, + [[Capability]]: promiseCapability}. + 6. Append request to the end of queue. + ... + + AsyncGeneratorResolve ( generator, value, done ) + ... + 2. Let queue be generator.[[AsyncGeneratorQueue]]. + 3. Assert: queue is not an empty List. + 4. Remove the first element from queue and let next be the value of that element. + ... + +flags: [async] +features: [async-iteration] +---*/ + +var order = 0; + +async function* g() { + yield 'first'; + yield 'second'; +} + +var iter = g(); + +var item1 = iter.next(); +var item2 = iter.next(); +var item3 = iter.next(); + +var resolvedorder = 0; + +Promise.all([ + + item3.then(function(result) { + resolvedorder++; + assert.sameValue(resolvedorder, 3); + assert.sameValue(result.value, undefined); + assert.sameValue(result.done, true); + }), + + item2.then(function(result) { + resolvedorder++; + assert.sameValue(resolvedorder, 2); + assert.sameValue(result.value, "second"); + assert.sameValue(result.done, false); + }), + + item1.then(function(result) { + resolvedorder++; + assert.sameValue(resolvedorder, 1); + assert.sameValue(result.value, "first"); + assert.sameValue(result.done, false); + }) + +]).then(function() { $DONE(); }, $DONE); + diff --git a/test/built-ins/AsyncGeneratorPrototype/next/request-queue-promise-resolve-order.js b/test/built-ins/AsyncGeneratorPrototype/next/request-queue-promise-resolve-order.js new file mode 100644 index 0000000000..f459cd5eb9 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/next/request-queue-promise-resolve-order.js @@ -0,0 +1,82 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-next +description: next() requests from iterator processed in order, then +info: > + AsyncGenerator.prototype.next ( value ) + 1. Let generator be the this value. + 2. Let completion be NormalCompletion(value). + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 4. Let queue be generator.[[AsyncGeneratorQueue]]. + 5. Let request be AsyncGeneratorRequest{[[Completion]]: completion, + [[Capability]]: promiseCapability}. + 6. Append request to the end of queue. + ... + + AsyncGeneratorResolve ( generator, value, done ) + ... + 2. Let queue be generator.[[AsyncGeneratorQueue]]. + 3. Assert: queue is not an empty List. + 4. Remove the first element from queue and let next be the value of that element. + ... + +flags: [async] +features: [async-iteration] +---*/ + +var yieldorder = 0; +var resolveLatePromise; + +function resolveLater() { + return new Promise(resolve => { + resolveLatePromise = resolve; + }); +} + +async function* g() { + yield resolveLater(); + yield ++yieldorder; +} + +var iter = g(); + +var item1 = iter.next(); +var item2 = iter.next(); +var item3 = iter.next(); + +var resolvedorder = 0; + +Promise.all([ + + item3.then(function(result) { + resolvedorder++; + assert.sameValue(resolvedorder, 3); + assert.sameValue(result.value, undefined); + assert.sameValue(result.done, true); + }), + + item2.then(function(result) { + resolvedorder++; + assert.sameValue(resolvedorder, 2); + assert.sameValue(result.value, 2); + assert.sameValue(result.done, false); + }), + + item1.then(function(result) { + resolvedorder++; + assert.sameValue(resolvedorder, 1); + assert.sameValue(result.value, 1); + assert.sameValue(result.done, false); + }) + +]).then(function() { $DONE(); }, $DONE); + +// At this point: +// yieldorder == 0 +// item1 is an unresolved promise +resolveLatePromise(++yieldorder); diff --git a/test/built-ins/AsyncGeneratorPrototype/next/return-promise.js b/test/built-ins/AsyncGeneratorPrototype/next/return-promise.js new file mode 100644 index 0000000000..e727ab69d2 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/next/return-promise.js @@ -0,0 +1,26 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-next +description: > + "next" returns a promise for an IteratorResult object +info: | + AsyncGenerator.prototype.next ( value ) + 1. Let generator be the this value. + 2. Let completion be NormalCompletion(value). + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 2. Let promiseCapability be ! NewPromiseCapability(%Promise%). + ... + 9. Return promiseCapability.[[Promise]]. + +features: [async-iteration] +---*/ + +async function* g() {} +var result = g().next() + +assert(result instanceof Promise) diff --git a/test/built-ins/AsyncGeneratorPrototype/next/this-val-not-async-generator.js b/test/built-ins/AsyncGeneratorPrototype/next/this-val-not-async-generator.js new file mode 100644 index 0000000000..f011442c36 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/next/this-val-not-async-generator.js @@ -0,0 +1,88 @@ +// Copyright 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-asyncgenerator-prototype-next +description: next rejects promise when `this` value is not an async generator +info: | + AsyncGenerator.prototype.next ( value ) + 1. Let generator be the this value. + 2. Let completion be NormalCompletion(value). + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 3. If Type(generator) is not Object, or if generator does not have an + [[AsyncGeneratorState]] internal slot, then + a. Let badGeneratorError be a newly created TypeError object. + b. Perform ! Call(promiseCapability.[[Reject]], undefined, « badGeneratorError »). + c. Return promiseCapability.[[Promise]]. + +flags: [async] +features: [async-iteration] +---*/ + +async function* g() {} +var AsyncGeneratorPrototype = Object.getPrototypeOf(g).prototype; + +function* syncGenerator() {} +var syncIterator = syncGenerator() + +var testPromises = [ + AsyncGeneratorPrototype.next.call({}).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.next should reject promise" + + " when `this` value is an object"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(object) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.next.call(function() {}).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.next should reject promise" + + " when `this` value is a function"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(function) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.next.call(g).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.next should reject promise" + + " when `this` value is an async generator function"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(async generator function) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.next.call(g.prototype).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.next should reject promise" + + " when `this` value is an async generator function prototype object"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(async generator function prototype) expected TypeError but got " + e); + } + }, + ), + AsyncGeneratorPrototype.next.call(syncIterator).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.next should reject promise" + + " when `this` value is a generator"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(generator) expected TypeError but got " + e); + } + } + ) +] + +Promise.all(testPromises).then(() => {}).then($DONE, $DONE) diff --git a/test/built-ins/AsyncGeneratorPrototype/next/this-val-not-object.js b/test/built-ins/AsyncGeneratorPrototype/next/this-val-not-object.js new file mode 100644 index 0000000000..7fd254f2d4 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/next/this-val-not-object.js @@ -0,0 +1,98 @@ +// Copyright 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-asyncgenerator-prototype-next +description: next rejects promise when `this` value not an object +info: | + AsyncGenerator.prototype.next ( value ) + 1. Let generator be the this value. + 2. Let completion be NormalCompletion(value). + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 3. If Type(generator) is not Object, or if generator does not have an + [[AsyncGeneratorState]] internal slot, then + a. Let badGeneratorError be a newly created TypeError object. + b. Perform ! Call(promiseCapability.[[Reject]], undefined, « badGeneratorError »). + c. Return promiseCapability.[[Promise]]. + +flags: [async] +features: [async-iteration] +---*/ + +async function* g() {} +var AsyncGeneratorPrototype = Object.getPrototypeOf(g).prototype; + +var symbol = Symbol(); + +var testPromises = [ + AsyncGeneratorPrototype.next.call(undefined).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.next should reject promise" + + " when `this` value `undefined`"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(undefined) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.next.call(1).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.next should reject promise" + + " when `this` value is a Number"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(Number) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.next.call("string").then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.next should reject promise" + + " when `this` value is a String"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(String) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.next.call(null).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.next should reject promise" + + " when `this` value `null`"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(null) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.next.call(true).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.next should reject promise" + + " when `this` value is a Boolean"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(Boolean) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.next.call(symbol).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.next should reject promise" + + " when `this` value is a Symbol"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(Symbol) expected TypeError but got " + e); + } + } + ) +] + +Promise.all(testPromises).then(() => {}).then($DONE, $DONE) diff --git a/test/built-ins/AsyncGeneratorPrototype/return/iterator-result-prototype.js b/test/built-ins/AsyncGeneratorPrototype/return/iterator-result-prototype.js new file mode 100644 index 0000000000..efb01f7beb --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/return/iterator-result-prototype.js @@ -0,0 +1,47 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-return +description: > + "return" returns a promise for an IteratorResult object +info: | + AsyncGenerator.prototype.return ( value ) + 1. Let generator be the this value. + 2. Let completion be Completion{[[Type]]: return, [[Value]]: value, + [[Target]]: empty}. + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 4. Let queue be generator.[[AsyncGeneratorQueue]]. + 5. Let request be AsyncGeneratorRequest{[[Completion]]: completion, + [[Capability]]: promiseCapability}. + 6. Append request to the end of queue. + ... + + AsyncGeneratorResolve ( generator, value, done ) + 1. Assert: generator is an AsyncGenerator instance. + 2. Let queue be generator.[[AsyncGeneratorQueue]]. + 3. Assert: queue is not an empty List. + 4. Remove the first element from queue and let next be the value of that element. + 5. Let promiseCapability be next.[[Capability]]. + 6. Let iteratorResult be ! CreateIterResultObject(value, done). + 7. Perform ! Call(promiseCapability.[[Resolve]], undefined, « iteratorResult »). + ... + +flags: [async] +features: [async-iteration] +---*/ + +async function* g() {} + +g().return().then(function (result) { + assert( + Object.hasOwnProperty.call(result, 'value'), 'Has "own" property `value`' + ); + assert( + Object.hasOwnProperty.call(result, 'done'), 'Has "own" property `done`' + ); + assert.sameValue(Object.getPrototypeOf(result), Object.prototype); +}).then($DONE, $DONE) diff --git a/test/built-ins/AsyncGeneratorPrototype/return/length.js b/test/built-ins/AsyncGeneratorPrototype/return/length.js new file mode 100644 index 0000000000..048efe5327 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/return/length.js @@ -0,0 +1,35 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-return +description: > + AsyncGenerator.prototype.return.length is 1. +info: | + AsyncGenerator.prototype.return ( value ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, has a length + property whose value is an integer. Unless otherwise specified, this + value is equal to the largest number of named arguments shown in the + subclause headings for the function description, including optional + parameters. However, rest parameters shown using the form “...name” + are not included in the default argument count. + + Unless otherwise specified, the length property of a built-in Function + object has the attributes { [[Writable]]: false, [[Enumerable]]: false, + [[Configurable]]: true }. +includes: [propertyHelper.js] +features: [async-iteration] +---*/ + +async function* g() {} +var AsyncGeneratorPrototype = Object.getPrototypeOf(g).prototype; + +verifyProperty(AsyncGeneratorPrototype.return, "length", { + value: 1, + enumerable: false, + writable: false, + configurable: true, +}); + diff --git a/test/built-ins/AsyncGeneratorPrototype/return/name.js b/test/built-ins/AsyncGeneratorPrototype/return/name.js new file mode 100644 index 0000000000..c14f4ee235 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/return/name.js @@ -0,0 +1,31 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-return +description: > + Generator.prototype.next.name is "return". +info: | + Generator.prototype.return ( value ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, that is not + identified as an anonymous function has a name property whose value + is a String. + + Unless otherwise specified, the name property of a built-in Function + object, if it exists, has the attributes { [[Writable]]: false, + [[Enumerable]]: false, [[Configurable]]: true }. +includes: [propertyHelper.js] +features: [generators] +---*/ + +async function* g() {} +var AsyncGeneratorPrototype = Object.getPrototypeOf(g).prototype; + +verifyProperty(AsyncGeneratorPrototype.return, "name", { + value: "return", + enumerable: false, + writable: false, + configurable: true, +}); diff --git a/test/built-ins/AsyncGeneratorPrototype/return/prop-desc.js b/test/built-ins/AsyncGeneratorPrototype/return/prop-desc.js new file mode 100644 index 0000000000..aaf893be17 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/return/prop-desc.js @@ -0,0 +1,21 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-thow +description: GeneratorPrototype.return property description +info: | + This property has the attributes { [[Writable]]: false, [[Enumerable]]: + false, [[Configurable]]: false }. +includes: [propertyHelper.js] +features: [async-iteration] +---*/ + +async function* g() {} +var AsyncGeneratorPrototype = Object.getPrototypeOf(g).prototype; + +verifyProperty(AsyncGeneratorPrototype, "return", { + enumerable: false, + writable: true, + configurable: true, +}); diff --git a/test/built-ins/AsyncGeneratorPrototype/return/request-queue-order-state-executing.js b/test/built-ins/AsyncGeneratorPrototype/return/request-queue-order-state-executing.js new file mode 100644 index 0000000000..0d469e6784 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/return/request-queue-order-state-executing.js @@ -0,0 +1,66 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-asyncgenerator-prototype-throw +description: return() call while iterator is in state executing +info: | + AsyncGenerator.prototype.return ( value ) + 1. Let generator be the this value. + 2. Let completion be Completion{[[Type]]: return, [[Value]]: value, [[Target]]: empty}. + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 4. Let queue be generator.[[AsyncGeneratorQueue]]. + 5. Let request be AsyncGeneratorRequest{[[Completion]]: completion, + [[Capability]]: promiseCapability}. + 6. Append request to the end of queue. + ... + + AsyncGeneratorResolve ( generator, value, done ) + ... + 2. Let queue be generator.[[AsyncGeneratorQueue]]. + 3. Assert: queue is not an empty List. + 4. Remove the first element from queue and let next be the value of that element. + ... + +flags: [async] +features: [async-iteration] +---*/ + +var iter; +var executionorder = 0; +var valueisset = false; + +async function* g() { + + iter.return(42).then( + function(result) { + assert(valueisset, "variable valueisset should be set to true"); + assert.sameValue(++executionorder, 2); + assert.sameValue(result.value, 42); + assert.sameValue(result.done, true); + } + ).catch($DONE); + + valueisset = true; + + yield 1; + throw new Test262Error("This line should no be reached: generator closed by return"); +} + +iter = g(); + +iter.next().then(function(result) { + + assert.sameValue(++executionorder, 1); + assert.sameValue(result.value, 1); + assert.sameValue(result.done, false); + + iter.next().then(function(result) { + assert.sameValue(++executionorder, 3); + assert.sameValue(result.value, undefined); + assert.sameValue(result.done, true); + }).then($DONE, $DONE); + +}).catch($DONE); diff --git a/test/built-ins/AsyncGeneratorPrototype/return/return-promise.js b/test/built-ins/AsyncGeneratorPrototype/return/return-promise.js new file mode 100644 index 0000000000..07dff9f652 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/return/return-promise.js @@ -0,0 +1,28 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-next +description: > + "return" returns a promise +info: | + AsyncGenerator.prototype.return ( value ) + 1. Let generator be the this value. + 2. Let completion be Completion{[[Type]]: return, [[Value]]: value, + [[Target]]: empty}. + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 2. Let promiseCapability be ! NewPromiseCapability(%Promise%). + ... + 9. Return promiseCapability.[[Promise]]. + +includes: [propertyHelper.js] +features: [async-iteration] +---*/ + +async function* g() {} +var result = g().return() + +assert(result instanceof Promise) diff --git a/test/built-ins/AsyncGeneratorPrototype/return/return-state-completed.js b/test/built-ins/AsyncGeneratorPrototype/return/return-state-completed.js new file mode 100644 index 0000000000..5accb7e4d4 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/return/return-state-completed.js @@ -0,0 +1,38 @@ +// Copyright 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-return +description: return() results in fullfilled promise when called on completed iterator +info: | + AsyncGenerator.prototype.return ( value ) + 1. Let generator be the this value. + 2. Let completion be Completion{[[Type]]: return, [[Value]]: value, [[Target]]: empty}. + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 8. If state is not "executing", then + a. Perform ! AsyncGeneratorResumeNext(generator). + ... + + AsyncGeneratorResumeNext: + If completion.[[Type]] is throw, and generator.[[AsyncGeneratorState]] is + "completed" +flags: [async] +features: [async-iteration] +---*/ + +var g = async function*() {}; + +var iter = g(); +iter.next().then(function(result) { + assert.sameValue(result.value, undefined); + assert.sameValue(result.done, true); + + iter.return(42).then(function(result) { + assert.sameValue(result.value, 42) + assert.sameValue(result.done, true) + }).then($DONE, $DONE); + +}).catch($DONE); diff --git a/test/built-ins/AsyncGeneratorPrototype/return/this-val-not-async-generator.js b/test/built-ins/AsyncGeneratorPrototype/return/this-val-not-async-generator.js new file mode 100644 index 0000000000..07ae37cdd2 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/return/this-val-not-async-generator.js @@ -0,0 +1,89 @@ +// Copyright 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-asyncgenerator-prototype-return +description: return rejects promise when `this` value is not an async generator +info: | + AsyncGenerator.prototype.return ( exception ) + 1. Let generator be the this value. + 2. Let completion be Completion{[[Type]]: return, [[Value]]: value, + [[Target]]: empty}. + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 3. If Type(generator) is not Object, or if generator does not have an + [[AsyncGeneratorState]] internal slot, then + a. Let badGeneratorError be a newly created TypeError object. + b. Perform ! Call(promiseCapability.[[Reject]], undefined, « badGeneratorError »). + c. Return promiseCapability.[[Promise]]. + +flags: [async] +features: [async-iteration] +---*/ + +async function* g() {} +var AsyncGeneratorPrototype = Object.getPrototypeOf(g).prototype; + +function* syncGenerator() {} +var syncIterator = syncGenerator() + +var testPromises = [ + AsyncGeneratorPrototype.return.call({}).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.throw should reject promise" + + " when `this` value is an object"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(object) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.return.call(function() {}).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.throw should reject promise" + + " when `this` value is a function"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(function) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.return.call(g).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.throw should reject promise" + + " when `this` value is an async generator function"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(async generator function) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.return.call(g.prototype).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.throw should reject promise" + + " when `this` value is an async generator function prototype object"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(async generator function prototype) expected TypeError but got " + e); + } + }, + ), + AsyncGeneratorPrototype.return.call(syncIterator).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.throw should reject promise" + + " when `this` value is a generator"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(generator) expected TypeError but got " + e); + } + } + ) +] + +Promise.all(testPromises).then(() => {}).then($DONE, $DONE) diff --git a/test/built-ins/AsyncGeneratorPrototype/return/this-val-not-object.js b/test/built-ins/AsyncGeneratorPrototype/return/this-val-not-object.js new file mode 100644 index 0000000000..9b8ef05e68 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/return/this-val-not-object.js @@ -0,0 +1,99 @@ +// Copyright 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-asyncgenerator-prototype-return +description: return rejects promise when `this` value not an object +info: | + AsyncGenerator.prototype.return ( value ) + 1. Let generator be the this value. + 2. Let completion be Completion{[[Type]]: return, [[Value]]: value, + [[Target]]: empty}. + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 3. If Type(generator) is not Object, or if generator does not have an + [[AsyncGeneratorState]] internal slot, then + a. Let badGeneratorError be a newly created TypeError object. + b. Perform ! Call(promiseCapability.[[Reject]], undefined, « badGeneratorError »). + c. Return promiseCapability.[[Promise]]. + +flags: [async] +features: [async-iteration] +---*/ + +async function* g() {} +var AsyncGeneratorPrototype = Object.getPrototypeOf(g).prototype; + +var symbol = Symbol(); + +var testPromises = [ + AsyncGeneratorPrototype.return.call(undefined).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.return should reject promise" + + " when `this` value `undefined`"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(undefined) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.return.call(1).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.return should reject promise" + + " when `this` value is a Number"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(Number) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.return.call("string").then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.return should reject promise" + + " when `this` value is a String"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(String) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.return.call(null).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.return should reject promise" + + " when `this` value `null`"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(null) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.return.call(true).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.return should reject promise" + + " when `this` value is a Boolean"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(Boolean) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.return.call(symbol).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.return should reject promise" + + " when `this` value is a Symbol"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(Symbol) expected TypeError but got " + e); + } + } + ) +] + +Promise.all(testPromises).then(() => {}).then($DONE, $DONE) diff --git a/test/built-ins/AsyncGeneratorPrototype/throw/length.js b/test/built-ins/AsyncGeneratorPrototype/throw/length.js new file mode 100644 index 0000000000..eb7092182e --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/throw/length.js @@ -0,0 +1,35 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-throw +description: > + AsyncGenerator.prototype.throw.length is 1. +info: | + AsyncGenerator.prototype.throw ( value ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, has a length + property whose value is an integer. Unless otherwise specified, this + value is equal to the largest number of named arguments shown in the + subclause headings for the function description, including optional + parameters. However, rest parameters shown using the form “...name” + are not included in the default argument count. + + Unless otherwise specified, the length property of a built-in Function + object has the attributes { [[Writable]]: false, [[Enumerable]]: false, + [[Configurable]]: true }. +includes: [propertyHelper.js] +features: [async-iteration] +---*/ + +async function* g() {} +var AsyncGeneratorPrototype = Object.getPrototypeOf(g).prototype; + +verifyProperty(AsyncGeneratorPrototype.throw, "length", { + value: 1, + enumerable: false, + writable: false, + configurable: true, +}); + diff --git a/test/built-ins/AsyncGeneratorPrototype/throw/name.js b/test/built-ins/AsyncGeneratorPrototype/throw/name.js new file mode 100644 index 0000000000..d15df4e806 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/throw/name.js @@ -0,0 +1,31 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-throw +description: > + Generator.prototype.next.name is "throw". +info: | + Generator.prototype.throw ( value ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, that is not + identified as an anonymous function has a name property whose value + is a String. + + Unless otherwise specified, the name property of a built-in Function + object, if it exists, has the attributes { [[Writable]]: false, + [[Enumerable]]: false, [[Configurable]]: true }. +includes: [propertyHelper.js] +features: [generators] +---*/ + +async function* g() {} +var AsyncGeneratorPrototype = Object.getPrototypeOf(g).prototype; + +verifyProperty(AsyncGeneratorPrototype.throw, "name", { + value: "throw", + enumerable: false, + writable: false, + configurable: true, +}); diff --git a/test/built-ins/AsyncGeneratorPrototype/throw/prop-desc.js b/test/built-ins/AsyncGeneratorPrototype/throw/prop-desc.js new file mode 100644 index 0000000000..143e712dbc --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/throw/prop-desc.js @@ -0,0 +1,21 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-thow +description: GeneratorPrototype.throw property description +info: | + This property has the attributes { [[Writable]]: false, [[Enumerable]]: + false, [[Configurable]]: false }. +includes: [propertyHelper.js] +features: [async-iteration] +---*/ + +async function* g() {} +var AsyncGeneratorPrototype = Object.getPrototypeOf(g).prototype; + +verifyProperty(AsyncGeneratorPrototype, "throw", { + enumerable: false, + writable: true, + configurable: true, +}); diff --git a/test/built-ins/AsyncGeneratorPrototype/throw/request-queue-order-state-executing.js b/test/built-ins/AsyncGeneratorPrototype/throw/request-queue-order-state-executing.js new file mode 100644 index 0000000000..6c16bcbe33 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/throw/request-queue-order-state-executing.js @@ -0,0 +1,68 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-asyncgenerator-prototype-throw +description: return() call while iterator is in state executing +info: | + AsyncGenerator.prototype.throw ( error ) + 1. Let generator be the this value. + 2. Let completion be Completion{[[Type]]: throw, [[Value]]: error, [[Target]]: empty}. + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 4. Let queue be generator.[[AsyncGeneratorQueue]]. + 5. Let request be AsyncGeneratorRequest{[[Completion]]: completion, + [[Capability]]: promiseCapability}. + 6. Append request to the end of queue. + ... + + AsyncGeneratorResolve ( generator, value, done ) + ... + 2. Let queue be generator.[[AsyncGeneratorQueue]]. + 3. Assert: queue is not an empty List. + 4. Remove the first element from queue and let next be the value of that element. + ... + +flags: [async] +features: [async-iteration] +---*/ + +var iter, result; +var thrownErr = new Error("Catch me."); +var caughtErr; + +var order = 0; +var promises = [] + +async function* g() { + + iter.throw(thrownErr).then( + function() { + $DONE(new Test262Error("throw() should result in reject promise.")); + }, + function(e) { + caughtErr = e; + order++; + } + ); + + yield 1; + yield 2; +} + +iter = g(); + +iter.next().then(function(result) { + + assert.sameValue(++order, 1); + assert.sameValue(result.value, 1); + assert.sameValue(result.done, false); + + iter.next().then(function(result) { + assert.sameValue(++order, 3); + assert.sameValue(caughtErr, thrownErr); + assert.sameValue(result.done, true); + }).then($DONE, $DONE); + +}).catch($DONE) diff --git a/test/built-ins/AsyncGeneratorPrototype/throw/return-rejected-promise.js b/test/built-ins/AsyncGeneratorPrototype/throw/return-rejected-promise.js new file mode 100644 index 0000000000..37fc0a7a43 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/throw/return-rejected-promise.js @@ -0,0 +1,54 @@ +// Copyright (C) 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-throw +description: > + "throw" returns a rejected promise +info: | + AsyncGenerator.prototype.next ( value ) + 1. Let generator be the this value. + 2. Let completion be NormalCompletion(value). + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 2. Let promiseCapability be ! NewPromiseCapability(%Promise%). + ... + 4. Let queue be generator.[[AsyncGeneratorQueue]]. + 5. Let request be AsyncGeneratorRequest{[[Completion]]: completion, + [[Capability]]: promiseCapability}. + 6. Append request to the end of queue. + ... + 9. Return promiseCapability.[[Promise]]. + + AsyncGeneratorReject ( generator, exception ) + 1. Assert: generator is an AsyncGenerator instance. + 2. Let queue be generator.[[AsyncGeneratorQueue]]. + 3. Assert: queue is not an empty List. + 4. Remove the first element from queue and let next be the value of that element. + 5. Let promiseCapability be next.[[Capability]]. + 6. Perform ! Call(promiseCapability.[[Reject]], undefined, « exception »). + ... + +flags: [async] +features: [async-iteration] +---*/ + +async function* g() {} + +var errormessage = "Promise rejected." +var result = g().throw(new Test262Error(errormessage)) + +assert(result instanceof Promise, "Expected result to be an instanceof Promise") + +result.then( + function () { + throw new Test262Error("Expected result to be rejected promise."); + }, + function (e) { + if (!(e.message = errormessage)) { + throw new Test262Error("Expected thrown custom error, got " + e); + } + } +).then($DONE, $DONE) diff --git a/test/built-ins/AsyncGeneratorPrototype/throw/this-val-not-async-generator.js b/test/built-ins/AsyncGeneratorPrototype/throw/this-val-not-async-generator.js new file mode 100644 index 0000000000..cb9b287366 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/throw/this-val-not-async-generator.js @@ -0,0 +1,89 @@ +// Copyright 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-asyncgenerator-prototype-throw +description: throw rejects promise when `this` value is not an async generator +info: | + AsyncGenerator.prototype.throw ( exception ) + 1. Let generator be the this value. + 2. Let completion be Completion{[[Type]]: throw, [[Value]]: exception, + [[Target]]: empty}. + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 3. If Type(generator) is not Object, or if generator does not have an + [[AsyncGeneratorState]] internal slot, then + a. Let badGeneratorError be a newly created TypeError object. + b. Perform ! Call(promiseCapability.[[Reject]], undefined, « badGeneratorError »). + c. Return promiseCapability.[[Promise]]. + +flags: [async] +features: [async-iteration] +---*/ + +async function* g() {} +var AsyncGeneratorPrototype = Object.getPrototypeOf(g).prototype; + +function* syncGenerator() {} +var syncIterator = syncGenerator() + +var testPromises = [ + AsyncGeneratorPrototype.throw.call({}).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.throw should reject promise" + + " when `this` value is an object"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(object) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.throw.call(function() {}).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.throw should reject promise" + + " when `this` value is a function"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(function) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.throw.call(g).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.throw should reject promise" + + " when `this` value is an async generator function"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(async generator function) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.throw.call(g.prototype).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.throw should reject promise" + + " when `this` value is an async generator function prototype object"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(async generator function prototype) expected TypeError but got " + e); + } + }, + ), + AsyncGeneratorPrototype.throw.call(syncIterator).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.throw should reject promise" + + " when `this` value is a generator"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(generator) expected TypeError but got " + e); + } + } + ) +] + +Promise.all(testPromises).then(() => {}).then($DONE, $DONE) diff --git a/test/built-ins/AsyncGeneratorPrototype/throw/this-val-not-object.js b/test/built-ins/AsyncGeneratorPrototype/throw/this-val-not-object.js new file mode 100644 index 0000000000..c82f060db0 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/throw/this-val-not-object.js @@ -0,0 +1,99 @@ +// Copyright 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-asyncgenerator-prototype-throw +description: throw rejects promise when `this` value not an object +info: | + AsyncGenerator.prototype.throw ( exception ) + 1. Let generator be the this value. + 2. Let completion be Completion{[[Type]]: throw, [[Value]]: exception, + [[Target]]: empty}. + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 3. If Type(generator) is not Object, or if generator does not have an + [[AsyncGeneratorState]] internal slot, then + a. Let badGeneratorError be a newly created TypeError object. + b. Perform ! Call(promiseCapability.[[Reject]], undefined, « badGeneratorError »). + c. Return promiseCapability.[[Promise]]. + +flags: [async] +features: [async-iteration] +---*/ + +async function* g() {} +var AsyncGeneratorPrototype = Object.getPrototypeOf(g).prototype; + +var symbol = Symbol(); + +var testPromises = [ + AsyncGeneratorPrototype.throw.call(undefined).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.throw should reject promise" + + " when `this` value `undefined`"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(undefined) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.throw.call(1).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.throw should reject promise" + + " when `this` value is a Number"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(Number) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.throw.call("string").then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.throw should reject promise" + + " when `this` value is a String"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(String) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.throw.call(null).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.throw should reject promise" + + " when `this` value `null`"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(null) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.throw.call(true).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.throw should reject promise" + + " when `this` value is a Boolean"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(Boolean) expected TypeError but got " + e); + } + } + ), + AsyncGeneratorPrototype.throw.call(symbol).then( + function () { + throw new Test262Error("AsyncGeneratorPrototype.throw should reject promise" + + " when `this` value is a Symbol"); + }, + function (e) { + if (!(e instanceof TypeError)) { + throw new Test262Error("(Symbol) expected TypeError but got " + e); + } + } + ) +] + +Promise.all(testPromises).then(() => {}).then($DONE, $DONE) diff --git a/test/built-ins/AsyncGeneratorPrototype/throw/throw-state-completed.js b/test/built-ins/AsyncGeneratorPrototype/throw/throw-state-completed.js new file mode 100644 index 0000000000..8c382542e4 --- /dev/null +++ b/test/built-ins/AsyncGeneratorPrototype/throw/throw-state-completed.js @@ -0,0 +1,38 @@ +// Copyright 2018 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-asyncgenerator-prototype-throw +description: throw() results in rejected promise when called on completed iterator +info: | + AsyncGenerator.prototype.throw ( exception ) + 1. Let generator be the this value. + 2. Let completion be Completion{[[Type]]: throw, [[Value]]: exception, [[Target]]: empty}. + 3. Return ! AsyncGeneratorEnqueue(generator, completion). + + AsyncGeneratorEnqueue ( generator, completion ) + ... + 8. If state is not "executing", then + a. Perform ! AsyncGeneratorResumeNext(generator). + ... + + AsyncGeneratorResumeNext: + If completion.[[Type]] is throw, and generator.[[AsyncGeneratorState]] is + "completed", the resulting promise is rejected with the error. +flags: [async] +features: [async-iteration] +---*/ + +var throwError = new Error('Catch me'); +var g = async function*() {}; + +var iter = g(); +iter.next().then(function(result) { + assert.sameValue(result.value, undefined); + assert.sameValue(result.done, true); + + iter.throw(throwError).then($DONE, function(err) { + assert.sameValue(err, throwError) + }).then($DONE, $DONE); + +}).catch($DONE);