From 9cfcd7bcb0d8d2ac11c947d19deb3c81a5c30481 Mon Sep 17 00:00:00 2001 From: Alexey Shvayka Date: Wed, 27 May 2020 12:32:44 +0300 Subject: [PATCH] Add yield* tests --- ...tar-getiter-async-return-method-is-null.js | 55 ++++++++++++++ ...star-getiter-async-throw-method-is-null.js | 75 +++++++++++++++++++ .../expressions/yield/star-return-is-null.js | 55 ++++++++++++++ .../expressions/yield/star-throw-is-null.js | 71 ++++++++++++++++++ 4 files changed, 256 insertions(+) create mode 100644 test/language/expressions/async-generator/yield-star-getiter-async-return-method-is-null.js create mode 100644 test/language/expressions/async-generator/yield-star-getiter-async-throw-method-is-null.js create mode 100644 test/language/expressions/yield/star-return-is-null.js create mode 100644 test/language/expressions/yield/star-throw-is-null.js diff --git a/test/language/expressions/async-generator/yield-star-getiter-async-return-method-is-null.js b/test/language/expressions/async-generator/yield-star-getiter-async-return-method-is-null.js new file mode 100644 index 0000000000..c77692e301 --- /dev/null +++ b/test/language/expressions/async-generator/yield-star-getiter-async-return-method-is-null.js @@ -0,0 +1,55 @@ +// Copyright (C) 2020 Alexey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-generator-function-definitions-runtime-semantics-evaluation +description: > + If iterator's "return" method is `null`, + received completion is forwarded to the runtime. +info: | + YieldExpression : yield * AssignmentExpression + + [...] + 7. Repeat, + [...] + c. Else, + i. Assert: received.[[Type]] is return. + ii. Let return be ? GetMethod(iterator, "return"). + iii. If return is undefined, then + [...] + 2. Return Completion(received). + + GetMethod ( V, P ) + + [...] + 2. Let func be ? GetV(V, P). + 3. If func is either undefined or null, return undefined. +features: [Symbol.asyncIterator, async-iteration] +flags: [async] +---*/ + +var returnGets = 0; +var asyncIterable = { + [Symbol.asyncIterator]: function() { + return this; + }, + next: function() { + return {value: 1, done: false}; + }, + get return() { + returnGets += 1; + return null; + }, +}; + +async function* asyncGenerator() { + yield* asyncIterable; +} + +var asyncIterator = asyncGenerator(); +asyncIterator.next().then(function() { + asyncIterator.return(2).then(function(result) { + assert.sameValue(result.value, 2); + assert.sameValue(result.done, true); + assert.sameValue(returnGets, 1); + }).then($DONE, $DONE); +}).catch($DONE); diff --git a/test/language/expressions/async-generator/yield-star-getiter-async-throw-method-is-null.js b/test/language/expressions/async-generator/yield-star-getiter-async-throw-method-is-null.js new file mode 100644 index 0000000000..e4cc1c1faa --- /dev/null +++ b/test/language/expressions/async-generator/yield-star-getiter-async-throw-method-is-null.js @@ -0,0 +1,75 @@ +// Copyright (C) 2020 Alexey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-generator-function-definitions-runtime-semantics-evaluation +description: > + If iterator's "throw" method is `null`, + AsyncIteratorClose is called before rising TypeError. +info: | + YieldExpression : yield * AssignmentExpression + + [...] + 7. Repeat, + [...] + b. Else if received.[[Type]] is throw, then + i. Let throw be ? GetMethod(iterator, "throw"). + ii. If throw is not undefined, then + [...] + iii. Else, + [...] + 3. If generatorKind is async, perform ? AsyncIteratorClose(iteratorRecord, closeCompletion). + [...] + 6. Throw a TypeError exception. + + GetMethod ( V, P ) + + [...] + 2. Let func be ? GetV(V, P). + 3. If func is either undefined or null, return undefined. + + AsyncIteratorClose ( iteratorRecord, completion ) + + [...] + 4. Let innerResult be GetMethod(iterator, "return"). + 5. If innerResult.[[Type]] is normal, then + a. Let return be innerResult.[[Value]]. + b. If return is undefined, return Completion(completion). +features: [Symbol.asyncIterator, async-iteration] +flags: [async] +---*/ + +var throwGets = 0; +var returnGets = 0; +var asyncIterable = { + [Symbol.asyncIterator]: function() { + return this; + }, + next: function() { + return {value: 1, done: false}; + }, + get throw() { + throwGets += 1; + return null; + }, + get return() { + returnGets += 1; + }, +}; + +async function* asyncGenerator() { + yield* asyncIterable; +} + +var asyncIterator = asyncGenerator(); +asyncIterator.next().then(function() { + asyncIterator.throw().then( + function(result) { + throw new Test262Error("Promise should be rejected, got: " + result.value); + }, + function(err) { + assert.sameValue(err.constructor, TypeError); + assert.sameValue(throwGets, 1); + assert.sameValue(returnGets, 1); + }, + ).then($DONE, $DONE); +}).catch($DONE); diff --git a/test/language/expressions/yield/star-return-is-null.js b/test/language/expressions/yield/star-return-is-null.js new file mode 100644 index 0000000000..e8d18f5e59 --- /dev/null +++ b/test/language/expressions/yield/star-return-is-null.js @@ -0,0 +1,55 @@ +// Copyright (C) 2020 Alexey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-generator-function-definitions-runtime-semantics-evaluation +description: > + If iterator's "return" method is `null`, + received completion is forwarded to the runtime. +info: | + YieldExpression : yield * AssignmentExpression + + [...] + 7. Repeat, + [...] + c. Else, + i. Assert: received.[[Type]] is return. + ii. Let return be ? GetMethod(iterator, "return"). + iii. If return is undefined, then + [...] + 2. Return Completion(received). + + GetMethod ( V, P ) + + [...] + 2. Let func be ? GetV(V, P). + 3. If func is either undefined or null, return undefined. +features: [generators, Symbol.iterator] +---*/ + +var returnGets = 0; +var iterable = { + next: function() { + return {value: 1, done: false}; + }, + get return() { + returnGets += 1; + return null; + }, +}; + +iterable[Symbol.iterator] = function() { + return iterable; +}; + +function* generator() { + yield* iterable; +} + +var iterator = generator(); +iterator.next(); + +var result = iterator.return(2); +assert.sameValue(result.value, 2); +assert.sameValue(result.done, true); + +assert.sameValue(returnGets, 1); diff --git a/test/language/expressions/yield/star-throw-is-null.js b/test/language/expressions/yield/star-throw-is-null.js new file mode 100644 index 0000000000..041b799c11 --- /dev/null +++ b/test/language/expressions/yield/star-throw-is-null.js @@ -0,0 +1,71 @@ +// Copyright (C) 2020 Alexey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-generator-function-definitions-runtime-semantics-evaluation +description: > + If iterator's "throw" method is `null`, + IteratorClose is called before rising TypeError. +info: | + YieldExpression : yield * AssignmentExpression + + [...] + 7. Repeat, + [...] + b. Else if received.[[Type]] is throw, then + i. Let throw be ? GetMethod(iterator, "throw"). + ii. If throw is not undefined, then + [...] + iii. Else, + [...] + 4. Else, perform ? IteratorClose(iteratorRecord, closeCompletion). + [...] + 6. Throw a TypeError exception. + + GetMethod ( V, P ) + + [...] + 2. Let func be ? GetV(V, P). + 3. If func is either undefined or null, return undefined. + + IteratorClose ( iteratorRecord, completion ) + + [...] + 4. Let innerResult be GetMethod(iterator, "return"). + 5. If innerResult.[[Type]] is normal, then + a. Let return be innerResult.[[Value]]. + b. If return is undefined, return Completion(completion). +features: [generators, Symbol.iterator] +---*/ + +var throwGets = 0; +var returnGets = 0; +var iterable = { + next: function() { + return {value: 1, done: false}; + }, + get throw() { + throwGets += 1; + return null; + }, + get return() { + returnGets += 1; + }, +}; + +iterable[Symbol.iterator] = function() { + return iterable; +}; + +function* generator() { + yield* iterable; +} + +var iterator = generator(); +iterator.next(); + +assert.throws(TypeError, function() { + iterator.throw(); +}); + +assert.sameValue(throwGets, 1); +assert.sameValue(returnGets, 1);