summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Iterator/prototype/find
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /js/src/tests/non262/Iterator/prototype/find
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--js/src/tests/non262/Iterator/prototype/find/check-fn-after-getting-iterator.js26
-rw-r--r--js/src/tests/non262/Iterator/prototype/find/coerce-result-to-boolean.js27
-rw-r--r--js/src/tests/non262/Iterator/prototype/find/descriptor.js13
-rw-r--r--js/src/tests/non262/Iterator/prototype/find/error-from-correct-realm.js16
-rw-r--r--js/src/tests/non262/Iterator/prototype/find/fn-not-callable-throws.js15
-rw-r--r--js/src/tests/non262/Iterator/prototype/find/fn-throws-close-iterator.js22
-rw-r--r--js/src/tests/non262/Iterator/prototype/find/length.js17
-rw-r--r--js/src/tests/non262/Iterator/prototype/find/name.js13
-rw-r--r--js/src/tests/non262/Iterator/prototype/find/next-throws-iterator-not-closed.js22
-rw-r--r--js/src/tests/non262/Iterator/prototype/find/proxy.js44
-rw-r--r--js/src/tests/non262/Iterator/prototype/find/return-undefined-if-none-match.js11
-rw-r--r--js/src/tests/non262/Iterator/prototype/find/short-circuit-on-match.js14
-rw-r--r--js/src/tests/non262/Iterator/prototype/find/this-not-iterator-throws.js9
-rw-r--r--js/src/tests/non262/Iterator/prototype/find/value-throws-iterator-not-closed.js25
14 files changed, 274 insertions, 0 deletions
diff --git a/js/src/tests/non262/Iterator/prototype/find/check-fn-after-getting-iterator.js b/js/src/tests/non262/Iterator/prototype/find/check-fn-after-getting-iterator.js
new file mode 100644
index 0000000000..0417360cc9
--- /dev/null
+++ b/js/src/tests/non262/Iterator/prototype/find/check-fn-after-getting-iterator.js
@@ -0,0 +1,26 @@
+// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally
+const log = [];
+const handlerProxy = new Proxy({}, {
+ get: (target, key, receiver) => (...args) => {
+ log.push(`${key}: ${args[1]?.toString()}`);
+ return Reflect[key](...args);
+ },
+});
+
+class TestIterator extends Iterator {
+ next() {
+ return {done: true};
+ }
+}
+
+const iter = new Proxy(new TestIterator(), handlerProxy);
+assertThrowsInstanceOf(() => iter.find(1), TypeError);
+
+assertEq(
+ log.join('\n'),
+ `get: find
+get: next`
+);
+
+if (typeof reportCompare === 'function')
+ reportCompare(0, 0);
diff --git a/js/src/tests/non262/Iterator/prototype/find/coerce-result-to-boolean.js b/js/src/tests/non262/Iterator/prototype/find/coerce-result-to-boolean.js
new file mode 100644
index 0000000000..7818e5a58d
--- /dev/null
+++ b/js/src/tests/non262/Iterator/prototype/find/coerce-result-to-boolean.js
@@ -0,0 +1,27 @@
+// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally
+
+const fn = (value) => value;
+assertEq([true].values().find(fn), true);
+assertEq([1].values().find(fn), 1);
+assertEq(['test'].values().find(fn), 'test');
+
+assertEq([false].values().find(fn), undefined);
+assertEq([0].values().find(fn), undefined);
+assertEq([''].values().find(fn), undefined);
+assertEq([null].values().find(fn), undefined);
+assertEq([undefined].values().find(fn), undefined);
+assertEq([NaN].values().find(fn), undefined);
+assertEq([-0].values().find(fn), undefined);
+assertEq([0n].values().find(fn), undefined);
+
+let array = [];
+assertEq([array].values().find(fn), array);
+
+let object = {};
+assertEq([object].values().find(fn), object);
+
+const htmlDDA = createIsHTMLDDA();
+assertEq([htmlDDA].values().find(fn), undefined);
+
+if (typeof reportCompare === 'function')
+ reportCompare(0, 0);
diff --git a/js/src/tests/non262/Iterator/prototype/find/descriptor.js b/js/src/tests/non262/Iterator/prototype/find/descriptor.js
new file mode 100644
index 0000000000..45699dff85
--- /dev/null
+++ b/js/src/tests/non262/Iterator/prototype/find/descriptor.js
@@ -0,0 +1,13 @@
+// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally
+/*---
+ Descriptor property of Iterator.prototype.find
+---*/
+
+const propDesc = Reflect.getOwnPropertyDescriptor(Iterator.prototype, 'find');
+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/Iterator/prototype/find/error-from-correct-realm.js b/js/src/tests/non262/Iterator/prototype/find/error-from-correct-realm.js
new file mode 100644
index 0000000000..b34408e853
--- /dev/null
+++ b/js/src/tests/non262/Iterator/prototype/find/error-from-correct-realm.js
@@ -0,0 +1,16 @@
+// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally
+
+const otherGlobal = newGlobal({newCompartment: true});
+assertEq(TypeError !== otherGlobal.TypeError, true);
+
+const iter = [].values();
+
+assertThrowsInstanceOf(() => iter.find(), TypeError);
+assertThrowsInstanceOf(
+ otherGlobal.Iterator.prototype.find.bind(iter),
+ otherGlobal.TypeError,
+ 'TypeError comes from the realm of the method.',
+);
+
+if (typeof reportCompare === 'function')
+ reportCompare(0, 0);
diff --git a/js/src/tests/non262/Iterator/prototype/find/fn-not-callable-throws.js b/js/src/tests/non262/Iterator/prototype/find/fn-not-callable-throws.js
new file mode 100644
index 0000000000..33d18b49cc
--- /dev/null
+++ b/js/src/tests/non262/Iterator/prototype/find/fn-not-callable-throws.js
@@ -0,0 +1,15 @@
+// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally
+
+const iter = [].values();
+
+assertThrowsInstanceOf(() => iter.find(), TypeError);
+assertThrowsInstanceOf(() => iter.find(undefined), TypeError);
+assertThrowsInstanceOf(() => iter.find(null), TypeError);
+assertThrowsInstanceOf(() => iter.find(0), TypeError);
+assertThrowsInstanceOf(() => iter.find(false), TypeError);
+assertThrowsInstanceOf(() => iter.find(''), TypeError);
+assertThrowsInstanceOf(() => iter.find(Symbol('')), TypeError);
+assertThrowsInstanceOf(() => iter.find({}), TypeError);
+
+if (typeof reportCompare === 'function')
+ reportCompare(0, 0);
diff --git a/js/src/tests/non262/Iterator/prototype/find/fn-throws-close-iterator.js b/js/src/tests/non262/Iterator/prototype/find/fn-throws-close-iterator.js
new file mode 100644
index 0000000000..1393bee356
--- /dev/null
+++ b/js/src/tests/non262/Iterator/prototype/find/fn-throws-close-iterator.js
@@ -0,0 +1,22 @@
+// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally
+
+class TestIterator extends Iterator {
+ next() {
+ return { done: this.closed };
+ }
+
+ closed = false;
+ return() {
+ this.closed = true;
+ }
+}
+
+const fn = (value) => { throw new Error(); };
+const iter = new TestIterator();
+
+assertEq(iter.closed, false);
+assertThrowsInstanceOf(() => iter.find(fn), Error);
+assertEq(iter.closed, true);
+
+if (typeof reportCompare === 'function')
+ reportCompare(0, 0);
diff --git a/js/src/tests/non262/Iterator/prototype/find/length.js b/js/src/tests/non262/Iterator/prototype/find/length.js
new file mode 100644
index 0000000000..b182ca0e78
--- /dev/null
+++ b/js/src/tests/non262/Iterator/prototype/find/length.js
@@ -0,0 +1,17 @@
+// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally
+/*---
+ The `length` property of Iterator.prototype.find.
+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(Iterator.prototype.find, '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/Iterator/prototype/find/name.js b/js/src/tests/non262/Iterator/prototype/find/name.js
new file mode 100644
index 0000000000..36d903b44c
--- /dev/null
+++ b/js/src/tests/non262/Iterator/prototype/find/name.js
@@ -0,0 +1,13 @@
+// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally
+/*---
+ `name` property of Iterator.prototype.find.
+---*/
+
+const propDesc = Reflect.getOwnPropertyDescriptor(Iterator.prototype.find, 'name');
+assertEq(propDesc.value, 'find');
+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/Iterator/prototype/find/next-throws-iterator-not-closed.js b/js/src/tests/non262/Iterator/prototype/find/next-throws-iterator-not-closed.js
new file mode 100644
index 0000000000..13c3195654
--- /dev/null
+++ b/js/src/tests/non262/Iterator/prototype/find/next-throws-iterator-not-closed.js
@@ -0,0 +1,22 @@
+// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally
+
+class TestIterator extends Iterator {
+ next() {
+ throw new Error();
+ }
+
+ closed = false;
+ return() {
+ this.closed = true;
+ }
+}
+
+const fn = x => x;
+const iter = new TestIterator();
+
+assertEq(iter.closed, false);
+assertThrowsInstanceOf(() => iter.find(fn), Error);
+assertEq(iter.closed, false);
+
+if (typeof reportCompare === 'function')
+ reportCompare(0, 0);
diff --git a/js/src/tests/non262/Iterator/prototype/find/proxy.js b/js/src/tests/non262/Iterator/prototype/find/proxy.js
new file mode 100644
index 0000000000..c24d787710
--- /dev/null
+++ b/js/src/tests/non262/Iterator/prototype/find/proxy.js
@@ -0,0 +1,44 @@
+// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally
+//
+// This test checks that %Iterator.prototype%.find 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 Iterator {
+ value = 0;
+ next() {
+ const value = this.value;
+ if (value < 2) {
+ this.value = value + 1;
+ return {done: false, value};
+ }
+ return {done: true};
+ }
+}
+
+const iter = new Proxy(new Counter(), handlerProxy);
+assertEq(iter.find(x => x % 2 == 1), 1);
+
+assertEq(
+ log.join('\n'),
+ `get: find
+get: next
+get: value
+set: value
+getOwnPropertyDescriptor: value
+defineProperty: value
+get: value
+set: value
+getOwnPropertyDescriptor: value
+defineProperty: value
+get: return`
+);
+
+if (typeof reportCompare === 'function')
+ reportCompare(0, 0);
diff --git a/js/src/tests/non262/Iterator/prototype/find/return-undefined-if-none-match.js b/js/src/tests/non262/Iterator/prototype/find/return-undefined-if-none-match.js
new file mode 100644
index 0000000000..3e7364fa95
--- /dev/null
+++ b/js/src/tests/non262/Iterator/prototype/find/return-undefined-if-none-match.js
@@ -0,0 +1,11 @@
+// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally
+
+const iter = [1, 3, 5].values();
+const fn = (value) => value % 2 == 0;
+
+assertEq(iter.find(fn), undefined);
+
+assertEq([].values().find(x => x), undefined);
+
+if (typeof reportCompare === 'function')
+ reportCompare(0, 0);
diff --git a/js/src/tests/non262/Iterator/prototype/find/short-circuit-on-match.js b/js/src/tests/non262/Iterator/prototype/find/short-circuit-on-match.js
new file mode 100644
index 0000000000..8ce7c5a354
--- /dev/null
+++ b/js/src/tests/non262/Iterator/prototype/find/short-circuit-on-match.js
@@ -0,0 +1,14 @@
+// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally
+
+const iter = [1, 2, 3].values();
+const log = [];
+const fn = (value) => {
+ log.push(value.toString());
+ return value % 2 == 0;
+};
+
+assertEq(iter.find(fn), 2);
+assertEq(log.join(','), '1,2');
+
+if (typeof reportCompare === 'function')
+ reportCompare(0, 0);
diff --git a/js/src/tests/non262/Iterator/prototype/find/this-not-iterator-throws.js b/js/src/tests/non262/Iterator/prototype/find/this-not-iterator-throws.js
new file mode 100644
index 0000000000..8e1d446705
--- /dev/null
+++ b/js/src/tests/non262/Iterator/prototype/find/this-not-iterator-throws.js
@@ -0,0 +1,9 @@
+// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally
+
+const fn = x => x;
+assertThrowsInstanceOf(Iterator.prototype.find.bind(undefined, fn), TypeError);
+assertThrowsInstanceOf(Iterator.prototype.find.bind({}, fn), TypeError);
+assertThrowsInstanceOf(Iterator.prototype.find.bind({next: 0}, fn), TypeError);
+
+if (typeof reportCompare === 'function')
+ reportCompare(0, 0);
diff --git a/js/src/tests/non262/Iterator/prototype/find/value-throws-iterator-not-closed.js b/js/src/tests/non262/Iterator/prototype/find/value-throws-iterator-not-closed.js
new file mode 100644
index 0000000000..23873c618a
--- /dev/null
+++ b/js/src/tests/non262/Iterator/prototype/find/value-throws-iterator-not-closed.js
@@ -0,0 +1,25 @@
+// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally
+
+class TestError extends Error {}
+class TestIterator extends Iterator {
+ next() {
+ return new Proxy({done: false}, {get: (target, key, receiver) => {
+ if (key === 'value')
+ throw new TestError();
+ return 0;
+ }});
+ }
+
+ closed = false;
+ return() {
+ closed = true;
+ }
+}
+
+const iterator = new TestIterator();
+assertEq(iterator.closed, false, 'iterator starts unclosed');
+assertThrowsInstanceOf(() => iterator.find(x => x), TestError);
+assertEq(iterator.closed, false, 'iterator remains unclosed');
+
+if (typeof reportCompare === 'function')
+ reportCompare(0, 0);