From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- js/src/tests/non262/Tuple/from/from.js | 89 ++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 js/src/tests/non262/Tuple/from/from.js (limited to 'js/src/tests/non262/Tuple/from/from.js') diff --git a/js/src/tests/non262/Tuple/from/from.js b/js/src/tests/non262/Tuple/from/from.js new file mode 100644 index 0000000000..ee79cdd33f --- /dev/null +++ b/js/src/tests/non262/Tuple/from/from.js @@ -0,0 +1,89 @@ +// |reftest| skip-if(!this.hasOwnProperty("Tuple")) +/* +8.2.2.2 Tuple.from ( items [ , mapFn [, thisArg ] ] ) +When the from method is called with argument items and optional arguments mapfn and thisArg, the following steps are taken: + +1. If mapfn is undefined, let mapping be false. +2. Else, +a. If IsCallable(mapfn) is false, throw a TypeError exception. +b. Let mapping be true. +3. Let list be a new empty List. +4. Let k be 0. +5. Let usingIterator be ? GetMethod(items, @@iterator). +6. If usingIterator is not undefined, then +a. Let adder be a new Abstract Closure with parameters (key, value) that captures (list, mapFn, thisArg, mapping, k) and performs the following steps when called: +i. If mapping is true, then +1. Let mappedValue be ? Call(mapfn, thisArg, « value, k »). +ii. Else, let mappedValue be value. +iii. If Type(mappedValue) is Object, throw a TypeError exception. +iv. Append mappedValue to list. +v. Set k to k + 1. +b. Perform ! AddEntriesFromIterable(undefined, iterable, adder). +c. Return a new Tuple value whose [[Sequence]] is list. +7. NOTE: items is not an Iterable so assume it is an array-like object. +8. Let arrayLike be ! ToObject(items). +9. Let len be ? LengthOfArrayLike(arrayLike). +10. Repeat, while k < len, +a. Let Pk be ! ToString(k). +b. Let kValue be ? Get(arrayLike, Pk). +c. If mapping is true, then +i. Let mappedValue be ? Call(mapfn, thisArg, « kValue, k »). +d. Else, let mappedValue be kValue. +e. If Type(mappedValue) is Object, throw a TypeError exception. +f. Append mappedValue to list. +g. Set k to k + 1. +11. Return a new Tuple value whose [[Sequence]] is list. +*/ + +/* Step 1: mapfn explicitly undefined */ +assertEq(Tuple.from([1,2],undefined),#[1,2]); + +/* Step 2a: If IsCallable(mapfn) is false, throw a TypeError exception. */ +assertThrowsInstanceOf(() => Tuple.from([1,2,3],"monkeys"), TypeError, "mapfn not callable"); + +/* Step 5 */ +assertThrowsInstanceOf(() => Tuple.from(undefined), TypeError, + "can't access property Symbol.iterator of undefined"); +assertThrowsInstanceOf(() => Tuple.from(null), TypeError, + "can't access property Symbol.iterator of null"); +assertEq(Tuple.from(1), #[]); +var obj = {}; +obj[Symbol.iterator] = null; +assertEq(Tuple.from(obj), #[]); +obj[Symbol.iterator] = 5; +assertThrowsInstanceOf(() => Tuple.from(obj), TypeError, + "number is not a function"); + + +/* Step 6a.i. mapping exists */ +assertEq(Tuple.from([1,2,3],x => x+1), #[2,3,4]); +assertEq(Tuple.from(#[1,2,3],x => x+1), #[2,3,4]); +assertEq(Tuple.from("xyz", c => String.fromCharCode(c.charCodeAt(0) + 1)), #['y', 'z', '{']); +/* Step 6a.ii. mapping does not exist */ +assertEq(Tuple.from([1,2,3,4,5]), #[1,2,3,4,5]); +assertEq(Tuple.from(#[1,2,3,4,5]), #[1,2,3,4,5]); +assertEq(Tuple.from("xyz"), #['x', 'y', 'z']); + +/* Step 6a.iii. if mapfn returns Object, throw */ +assertThrowsInstanceOf(() => Tuple.from([1,2], x => [x]), TypeError, "Tuple cannot contain object"); + +/* Step 7 -- array-like but not iterable */ +var obj = { length: 3, 0: "a", 1: "b", 2: "c" }; +assertEq(Tuple.from(obj), #["a","b","c"]); +assertEq(Tuple.from(obj, s => s + s), #["aa","bb","cc"]); + +obj = { 0: "a", 1: "b", 2: "c" }; +assertEq(Tuple.from(obj), #[]); + +obj = { length: 3, 0: 1, 2: 3 }; +assertEq(Tuple.from(obj), #[1, undefined, 3]); + +obj = { length: 3, 0: 1, 1: [2], 2: 3 }; +assertThrowsInstanceOf(() => Tuple.from(obj), TypeError, "Tuple cannot contain object"); + +/* Step 10e */ +assertThrowsInstanceOf(() => Tuple.from([1, 2, [3,4]]), TypeError, + "Tuple can't contain Object"); + + +reportCompare(0, 0); -- cgit v1.2.3