summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Array/from_mapping.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /js/src/tests/non262/Array/from_mapping.js
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'js/src/tests/non262/Array/from_mapping.js')
-rw-r--r--js/src/tests/non262/Array/from_mapping.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/js/src/tests/non262/Array/from_mapping.js b/js/src/tests/non262/Array/from_mapping.js
new file mode 100644
index 0000000000..568b9d6d7a
--- /dev/null
+++ b/js/src/tests/non262/Array/from_mapping.js
@@ -0,0 +1,41 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/licenses/publicdomain/ */
+
+// If the mapfn argument to Array.from is undefined, don't map.
+assertDeepEq(Array.from([3, 4, 5], undefined), [3, 4, 5]);
+assertDeepEq(Array.from([4, 5, 6], undefined, Math), [4, 5, 6]);
+
+// mapfn is called with two arguments: value and index.
+var log = [];
+function f() {
+ log.push(Array.from(arguments));
+ return log.length;
+}
+assertDeepEq(Array.from(['a', 'e', 'i', 'o', 'u'], f), [1, 2, 3, 4, 5]);
+assertDeepEq(log, [['a', 0], ['e', 1], ['i', 2], ['o', 3], ['u', 4]]);
+
+// If the object to be copied is non-iterable, mapfn is still called with two
+// arguments.
+log = [];
+assertDeepEq(Array.from({0: "zero", 1: "one", length: 2}, f), [1, 2]);
+assertDeepEq(log, [["zero", 0], ["one", 1]]);
+
+// If the object to be copied is iterable and the constructor is not Array,
+// mapfn is still called with two arguments.
+log = [];
+function C() {}
+C.from = Array.from;
+var c = new C;
+c[0] = 1;
+c[1] = 2;
+c.length = 2;
+assertDeepEq(C.from(["zero", "one"], f), c);
+assertDeepEq(log, [["zero", 0], ["one", 1]]);
+
+// The mapfn is called even if the value to be mapped is undefined.
+assertDeepEq(Array.from([0, 1, , 3], String), ["0", "1", "undefined", "3"]);
+var arraylike = {length: 4, "0": 0, "1": 1, "3": 3};
+assertDeepEq(Array.from(arraylike, String), ["0", "1", "undefined", "3"]);
+
+if (typeof reportCompare === 'function')
+ reportCompare(0, 0);