diff options
Diffstat (limited to 'js/src/tests/non262/AsyncIterator/prototype/toArray')
12 files changed, 262 insertions, 0 deletions
diff --git a/js/src/tests/non262/AsyncIterator/prototype/toArray/async-writes.js b/js/src/tests/non262/AsyncIterator/prototype/toArray/async-writes.js new file mode 100644 index 0000000000..8246d8d4ed --- /dev/null +++ b/js/src/tests/non262/AsyncIterator/prototype/toArray/async-writes.js @@ -0,0 +1,20 @@ +// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator')) + +let x = {a: () => true}; + +async function* gen() { + yield x.a(); + yield x.a(); +} + +gen().toArray().then( + () => assertEq(true, false, 'expected error'), + err => assertEq(err instanceof Error, true), +); + +x.a = () => { + throw Error(); +}; + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/AsyncIterator/prototype/toArray/create-in-current-realm.js b/js/src/tests/non262/AsyncIterator/prototype/toArray/create-in-current-realm.js new file mode 100644 index 0000000000..6f0b69f8ad --- /dev/null +++ b/js/src/tests/non262/AsyncIterator/prototype/toArray/create-in-current-realm.js @@ -0,0 +1,22 @@ +// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator')) + +const otherGlobal = newGlobal({newCompartment: true}); + +async function* gen() { + yield 1; + yield 2; + yield 3; +} + +gen().toArray().then(array => { + assertEq(array instanceof Array, true); + assertEq(array instanceof otherGlobal.Array, false); +}); + +otherGlobal.AsyncIterator.prototype.toArray.call(gen()).then(array => { + assertEq(array instanceof Array, false); + assertEq(array instanceof otherGlobal.Array, true); +}); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/AsyncIterator/prototype/toArray/descriptor.js b/js/src/tests/non262/AsyncIterator/prototype/toArray/descriptor.js new file mode 100644 index 0000000000..58c7a13872 --- /dev/null +++ b/js/src/tests/non262/AsyncIterator/prototype/toArray/descriptor.js @@ -0,0 +1,13 @@ +// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator')) +/*--- + Descriptor property of AsyncIterator.prototype.toArray +---*/ + +const propDesc = Reflect.getOwnPropertyDescriptor(AsyncIterator.prototype, 'toArray'); +assertEq(typeof propDesc.value, 'function'); +assertEq(propDesc.writable, true); +assertEq(propDesc.enumerable, false); +assertEq(propDesc.configurable, true); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/AsyncIterator/prototype/toArray/interleaving-calls.js b/js/src/tests/non262/AsyncIterator/prototype/toArray/interleaving-calls.js new file mode 100644 index 0000000000..88e3fcd5e8 --- /dev/null +++ b/js/src/tests/non262/AsyncIterator/prototype/toArray/interleaving-calls.js @@ -0,0 +1,24 @@ +// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator')) + +const log = []; +async function* gen(n) { + log.push(`${n}`); + yield 1; + log.push(`${n}`); + yield 2; +} + +Promise.all([gen(1).toArray(), gen(2).toArray()]).then( + () => { + assertEq( + log.join(' '), + '1 2 1 2', + ); + }, + err => { + throw err; + } +); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/AsyncIterator/prototype/toArray/iterator-empty.js b/js/src/tests/non262/AsyncIterator/prototype/toArray/iterator-empty.js new file mode 100644 index 0000000000..d8eec0454d --- /dev/null +++ b/js/src/tests/non262/AsyncIterator/prototype/toArray/iterator-empty.js @@ -0,0 +1,10 @@ +// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator')) + +async function* gen() {} +gen().toArray().then(array => { + assertEq(Array.isArray(array), true); + assertEq(array.length, 0); +}); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/AsyncIterator/prototype/toArray/length.js b/js/src/tests/non262/AsyncIterator/prototype/toArray/length.js new file mode 100644 index 0000000000..f0467d5fc3 --- /dev/null +++ b/js/src/tests/non262/AsyncIterator/prototype/toArray/length.js @@ -0,0 +1,17 @@ +// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator')) +/*--- + The `length` property of AsyncIterator.prototype.toArray. +info: | + ES7 section 17: Unless otherwise specified, the length property of a built-in + Function object has the attributes { [[Writable]]: false, [[Enumerable]]: + false, [[Configurable]]: true }. +---*/ + +const propDesc = Reflect.getOwnPropertyDescriptor(AsyncIterator.prototype.toArray, 'length'); +assertEq(propDesc.value, 0); +assertEq(propDesc.writable, false); +assertEq(propDesc.enumerable, false); +assertEq(propDesc.configurable, true); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/AsyncIterator/prototype/toArray/name.js b/js/src/tests/non262/AsyncIterator/prototype/toArray/name.js new file mode 100644 index 0000000000..63cdf4c201 --- /dev/null +++ b/js/src/tests/non262/AsyncIterator/prototype/toArray/name.js @@ -0,0 +1,13 @@ +// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator')) +/*--- + `name` property of AsyncIterator.prototype.toArray. +---*/ + +const propDesc = Reflect.getOwnPropertyDescriptor(AsyncIterator.prototype.toArray, 'name'); +assertEq(propDesc.value, 'toArray'); +assertEq(propDesc.writable, false); +assertEq(propDesc.enumerable, false); +assertEq(propDesc.configurable, true); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/AsyncIterator/prototype/toArray/next-throws.js b/js/src/tests/non262/AsyncIterator/prototype/toArray/next-throws.js new file mode 100644 index 0000000000..d998f83e83 --- /dev/null +++ b/js/src/tests/non262/AsyncIterator/prototype/toArray/next-throws.js @@ -0,0 +1,23 @@ +// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator')) + +class TestIterator extends AsyncIterator { + next() { + throw new Error(); + } + + closed = false; + return() { + this.closed = true; + } +} + +const iter = new TestIterator(); + +assertEq(iter.closed, false); +iter.toArray().then(() => assertEq(true, false, 'expected error'), err => { + assertEq(err instanceof Error, true); + assertEq(iter.closed, false); +}); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/AsyncIterator/prototype/toArray/proxy.js b/js/src/tests/non262/AsyncIterator/prototype/toArray/proxy.js new file mode 100644 index 0000000000..b82af6628e --- /dev/null +++ b/js/src/tests/non262/AsyncIterator/prototype/toArray/proxy.js @@ -0,0 +1,44 @@ +// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator')) +// +// This test checks that %Iterator.prototype%.toArray only gets the `next` method off of the +// iterator once, and never accesses the @@iterator property. +const log = []; +const handlerProxy = new Proxy({}, { + get: (target, key, receiver) => (...args) => { + log.push(`${key}: ${args[1]?.toString()}`); + return Reflect[key](...args); + }, +}); + +class Counter extends AsyncIterator { + value = 0; + next() { + const value = this.value; + if (value < 2) { + this.value = value + 1; + return Promise.resolve({done: false, value}); + } + return Promise.resolve({done: true}); + } +} + +const iter = new Proxy(new Counter(), handlerProxy); +iter.toArray().then(() => { + assertEq( + log.join('\n'), + `get: toArray +get: next +get: value +set: value +getOwnPropertyDescriptor: value +defineProperty: value +get: value +set: value +getOwnPropertyDescriptor: value +defineProperty: value +get: value` + ); +}); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/AsyncIterator/prototype/toArray/this-not-iterator-throws.js b/js/src/tests/non262/AsyncIterator/prototype/toArray/this-not-iterator-throws.js new file mode 100644 index 0000000000..9846c33ee5 --- /dev/null +++ b/js/src/tests/non262/AsyncIterator/prototype/toArray/this-not-iterator-throws.js @@ -0,0 +1,21 @@ +// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator')) + + +function check(x) { + AsyncIterator.prototype.toArray.call(x).then( + () => { + throw new Error('check should have been rejected'); + }, + err => { + assertEq(err instanceof TypeError, true); + } + ); +} + +check(); +check(undefined); +check({}); +check({next: 0}); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/AsyncIterator/prototype/toArray/toArray.js b/js/src/tests/non262/AsyncIterator/prototype/toArray/toArray.js new file mode 100644 index 0000000000..0a91e4d95d --- /dev/null +++ b/js/src/tests/non262/AsyncIterator/prototype/toArray/toArray.js @@ -0,0 +1,22 @@ +// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator')) + +async function* gen() { + yield 1; + yield 2; + yield 3; +} +assertEq(Array.isArray(gen()), false); + +gen().toArray().then(array => { + assertEq(Array.isArray(array), true); + assertEq(array.length, 3); + + const expected = [1, 2, 3]; + for (const item of array) { + const expect = expected.shift(); + assertEq(item, expect); + } +}); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/AsyncIterator/prototype/toArray/value-throws-iterator-not-closed.js b/js/src/tests/non262/AsyncIterator/prototype/toArray/value-throws-iterator-not-closed.js new file mode 100644 index 0000000000..a00f780185 --- /dev/null +++ b/js/src/tests/non262/AsyncIterator/prototype/toArray/value-throws-iterator-not-closed.js @@ -0,0 +1,33 @@ +// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator')) + +class TestError extends Error {} +class TestIterator extends AsyncIterator { + next() { + return Promise.resolve({ + done: false, + get value() { + throw new TestError(); + } + }); + } + + closed = false; + return() { + closed = true; + } +} + +const iterator = new TestIterator(); +assertEq(iterator.closed, false, 'iterator starts unclosed'); +iterator.toArray().then( + () => { + throw new Error('toArray should have thrown'); + }, + err => { + assertEq(err instanceof TestError, true); + assertEq(iterator.closed, false, 'iterator remains unclosed'); + } +); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); |