summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/AsyncIterator/prototype/forEach
diff options
context:
space:
mode:
Diffstat (limited to 'js/src/tests/non262/AsyncIterator/prototype/forEach')
-rw-r--r--js/src/tests/non262/AsyncIterator/prototype/forEach/async-writes.js20
-rw-r--r--js/src/tests/non262/AsyncIterator/prototype/forEach/check-fn-after-getting-iterator.js27
-rw-r--r--js/src/tests/non262/AsyncIterator/prototype/forEach/descriptor.js13
-rw-r--r--js/src/tests/non262/AsyncIterator/prototype/forEach/error-from-correct-realm.js21
-rw-r--r--js/src/tests/non262/AsyncIterator/prototype/forEach/fn-not-callable-throws.js24
-rw-r--r--js/src/tests/non262/AsyncIterator/prototype/forEach/fn-throws-close-iterator.js24
-rw-r--r--js/src/tests/non262/AsyncIterator/prototype/forEach/forEach.js11
-rw-r--r--js/src/tests/non262/AsyncIterator/prototype/forEach/interleaving-calls.js24
-rw-r--r--js/src/tests/non262/AsyncIterator/prototype/forEach/length.js17
-rw-r--r--js/src/tests/non262/AsyncIterator/prototype/forEach/name.js13
-rw-r--r--js/src/tests/non262/AsyncIterator/prototype/forEach/next-throws-iterator-not-closed.js24
-rw-r--r--js/src/tests/non262/AsyncIterator/prototype/forEach/proxy.js44
-rw-r--r--js/src/tests/non262/AsyncIterator/prototype/forEach/this-not-iterator-throws.js19
-rw-r--r--js/src/tests/non262/AsyncIterator/prototype/forEach/value-throws-iterator-not-closed.js28
14 files changed, 309 insertions, 0 deletions
diff --git a/js/src/tests/non262/AsyncIterator/prototype/forEach/async-writes.js b/js/src/tests/non262/AsyncIterator/prototype/forEach/async-writes.js
new file mode 100644
index 0000000000..25c4302ade
--- /dev/null
+++ b/js/src/tests/non262/AsyncIterator/prototype/forEach/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().forEach(() => {}).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/forEach/check-fn-after-getting-iterator.js b/js/src/tests/non262/AsyncIterator/prototype/forEach/check-fn-after-getting-iterator.js
new file mode 100644
index 0000000000..bcf77e56d1
--- /dev/null
+++ b/js/src/tests/non262/AsyncIterator/prototype/forEach/check-fn-after-getting-iterator.js
@@ -0,0 +1,27 @@
+// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator'))
+const log = [];
+const handlerProxy = new Proxy({}, {
+ get: (target, key, receiver) => (...args) => {
+ log.push(`${key}: ${args[1]?.toString()}`);
+ return Reflect[key](...args);
+ },
+});
+
+class TestIterator extends AsyncIterator {
+ next() {
+ return Promise.resolve({done: true});
+ }
+}
+
+const iter = new Proxy(new TestIterator(), handlerProxy);
+iter.forEach(1).then(() => assertEq(true, false, 'expected error'), err => {
+ assertEq(err instanceof TypeError, true);
+ assertEq(
+ log.join('\n'),
+ `get: forEach
+get: next`
+ );
+});
+
+if (typeof reportCompare === 'function')
+ reportCompare(0, 0);
diff --git a/js/src/tests/non262/AsyncIterator/prototype/forEach/descriptor.js b/js/src/tests/non262/AsyncIterator/prototype/forEach/descriptor.js
new file mode 100644
index 0000000000..8825422a28
--- /dev/null
+++ b/js/src/tests/non262/AsyncIterator/prototype/forEach/descriptor.js
@@ -0,0 +1,13 @@
+// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator'))
+/*---
+ Descriptor property of AsyncIterator.prototype.forEach
+---*/
+
+const propDesc = Reflect.getOwnPropertyDescriptor(AsyncIterator.prototype, 'forEach');
+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/forEach/error-from-correct-realm.js b/js/src/tests/non262/AsyncIterator/prototype/forEach/error-from-correct-realm.js
new file mode 100644
index 0000000000..32797fe750
--- /dev/null
+++ b/js/src/tests/non262/AsyncIterator/prototype/forEach/error-from-correct-realm.js
@@ -0,0 +1,21 @@
+// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator'))
+
+const otherGlobal = newGlobal({newCompartment: true});
+assertEq(TypeError !== otherGlobal.TypeError, true);
+
+async function *gen() {}
+
+gen().forEach().then(() => assertEq(true, false, 'expected error'), err => {
+ assertEq(err instanceof TypeError, true);
+});
+
+otherGlobal.AsyncIterator.prototype.forEach.call(gen()).then(() => assertEq(true, false, 'expected error'), err => {
+ assertEq(
+ err instanceof otherGlobal.TypeError,
+ true,
+ 'TypeError comes from the realm of the method.',
+ );
+});
+
+if (typeof reportCompare === 'function')
+ reportCompare(0, 0);
diff --git a/js/src/tests/non262/AsyncIterator/prototype/forEach/fn-not-callable-throws.js b/js/src/tests/non262/AsyncIterator/prototype/forEach/fn-not-callable-throws.js
new file mode 100644
index 0000000000..c70e336bff
--- /dev/null
+++ b/js/src/tests/non262/AsyncIterator/prototype/forEach/fn-not-callable-throws.js
@@ -0,0 +1,24 @@
+// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator'))
+
+async function *gen() {}
+
+function check(fn) {
+ gen().forEach(fn).then(() => {
+ throw new Error('every should have thrown');
+ },
+ (err) => {
+ assertEq(err instanceof TypeError, true);
+ });
+}
+
+check();
+check(undefined);
+check(null);
+check(0);
+check(false);
+check('');
+check(Symbol(''));
+check({});
+
+if (typeof reportCompare === 'function')
+ reportCompare(0, 0);
diff --git a/js/src/tests/non262/AsyncIterator/prototype/forEach/fn-throws-close-iterator.js b/js/src/tests/non262/AsyncIterator/prototype/forEach/fn-throws-close-iterator.js
new file mode 100644
index 0000000000..4e7d60412e
--- /dev/null
+++ b/js/src/tests/non262/AsyncIterator/prototype/forEach/fn-throws-close-iterator.js
@@ -0,0 +1,24 @@
+// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator'))
+
+class TestIterator extends AsyncIterator {
+ next() {
+ return Promise.resolve({ done: this.closed });
+ }
+
+ closed = false;
+ return() {
+ this.closed = true;
+ }
+}
+
+const fn = () => { throw new Error(); };
+const iter = new TestIterator();
+
+assertEq(iter.closed, false);
+iter.forEach(fn).then(() => assertEq(true, false, 'expected error'), err => {
+ assertEq(err instanceof Error, true);
+ assertEq(iter.closed, true);
+});
+
+if (typeof reportCompare === 'function')
+ reportCompare(0, 0);
diff --git a/js/src/tests/non262/AsyncIterator/prototype/forEach/forEach.js b/js/src/tests/non262/AsyncIterator/prototype/forEach/forEach.js
new file mode 100644
index 0000000000..2109be5fd6
--- /dev/null
+++ b/js/src/tests/non262/AsyncIterator/prototype/forEach/forEach.js
@@ -0,0 +1,11 @@
+// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator'))
+
+const log = [];
+const fn = (value) => log.push(value);
+const iter = [1, 2, 3].values();
+
+assertEq(iter.forEach(fn), undefined);
+assertEq(log.join(','), '1,2,3');
+
+if (typeof reportCompare === 'function')
+ reportCompare(0, 0);
diff --git a/js/src/tests/non262/AsyncIterator/prototype/forEach/interleaving-calls.js b/js/src/tests/non262/AsyncIterator/prototype/forEach/interleaving-calls.js
new file mode 100644
index 0000000000..c8c47bde45
--- /dev/null
+++ b/js/src/tests/non262/AsyncIterator/prototype/forEach/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).forEach(() => {}), gen(2).forEach(() => {})]).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/forEach/length.js b/js/src/tests/non262/AsyncIterator/prototype/forEach/length.js
new file mode 100644
index 0000000000..5e59af9fea
--- /dev/null
+++ b/js/src/tests/non262/AsyncIterator/prototype/forEach/length.js
@@ -0,0 +1,17 @@
+// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator'))
+/*---
+ The `length` property of AsyncIterator.prototype.forEach.
+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.forEach, 'length');
+assertEq(propDesc.value, 1);
+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/forEach/name.js b/js/src/tests/non262/AsyncIterator/prototype/forEach/name.js
new file mode 100644
index 0000000000..3053c3967f
--- /dev/null
+++ b/js/src/tests/non262/AsyncIterator/prototype/forEach/name.js
@@ -0,0 +1,13 @@
+// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator'))
+/*---
+ `name` property of AsyncIterator.prototype.forEach.
+---*/
+
+const propDesc = Reflect.getOwnPropertyDescriptor(AsyncIterator.prototype.forEach, 'name');
+assertEq(propDesc.value, 'forEach');
+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/forEach/next-throws-iterator-not-closed.js b/js/src/tests/non262/AsyncIterator/prototype/forEach/next-throws-iterator-not-closed.js
new file mode 100644
index 0000000000..0c72c700e2
--- /dev/null
+++ b/js/src/tests/non262/AsyncIterator/prototype/forEach/next-throws-iterator-not-closed.js
@@ -0,0 +1,24 @@
+// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator'))
+
+class TestIterator extends AsyncIterator {
+ next() {
+ throw new Error();
+ }
+
+ closed = false;
+ return() {
+ this.closed = true;
+ }
+}
+
+const fn = () => {};
+const iter = new TestIterator();
+
+assertEq(iter.closed, false);
+iter.forEach(fn).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/forEach/proxy.js b/js/src/tests/non262/AsyncIterator/prototype/forEach/proxy.js
new file mode 100644
index 0000000000..bab7a2e864
--- /dev/null
+++ b/js/src/tests/non262/AsyncIterator/prototype/forEach/proxy.js
@@ -0,0 +1,44 @@
+// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator'))
+//
+// This test checks that %Iterator.prototype%.forEach 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.forEach(x => x).then(() => {
+ assertEq(
+ log.join('\n'),
+ `get: forEach
+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/forEach/this-not-iterator-throws.js b/js/src/tests/non262/AsyncIterator/prototype/forEach/this-not-iterator-throws.js
new file mode 100644
index 0000000000..c92536fbe6
--- /dev/null
+++ b/js/src/tests/non262/AsyncIterator/prototype/forEach/this-not-iterator-throws.js
@@ -0,0 +1,19 @@
+// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator'))
+
+const fn = x => x;
+function check(x) {
+ AsyncIterator.prototype.forEach.call(x, fn).then(
+ () => assertEq(true, false, 'expected error'),
+ 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/forEach/value-throws-iterator-not-closed.js b/js/src/tests/non262/AsyncIterator/prototype/forEach/value-throws-iterator-not-closed.js
new file mode 100644
index 0000000000..8611a0100f
--- /dev/null
+++ b/js/src/tests/non262/AsyncIterator/prototype/forEach/value-throws-iterator-not-closed.js
@@ -0,0 +1,28 @@
+// |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.forEach(x => x).then(() => assertEq(true, false, 'expected error'), err => {
+ assertEq(err instanceof TestError, true);
+ assertEq(iterator.closed, false, 'iterator remains unclosed');
+});
+
+if (typeof reportCompare === 'function')
+ reportCompare(0, 0);