Debian Codegraph @

tests/auto/qml/ecmascripttests/test262/test/built-ins/Array/from/calling-from-valid-2.js

raw
1 // Copyright 2015 Microsoft Corporation. All rights reserved.
2 // This code is governed by the license found in the LICENSE file.
3
4 /*---
5 esid: sec-array.from
6 es6id: 22.1.2.1
7 description: Calling from with a valid map function with thisArg
8 info: |
9 22.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] )
10
11 ...
12 10. Let len be ToLength(Get(arrayLike, "length")).
13 11. ReturnIfAbrupt(len).
14 12. If IsConstructor(C) is true, then
15 a. Let A be Construct(C, «len»).
16 13. Else,
17 b. Let A be ArrayCreate(len).
18 14. ReturnIfAbrupt(A).
19 15. Let k be 0.
20 16. Repeat, while k < len
21 a. Let Pk be ToString(k).
22 b. Let kValue be Get(arrayLike, Pk).
23 c. ReturnIfAbrupt(kValue).
24 d. If mapping is true, then
25 i. Let mappedValue be Call(mapfn, T, «kValue, k»).
26 ...
27 ---*/
28
29 var list = {
30 '0': 41,
31 '1': 42,
32 '2': 43,
33 length: 3
34 };
35 var calls = [];
36 var thisArg = {};
37
38 function mapFn(value) {
39 calls.push({
40 args: arguments,
41 thisArg: this
42 });
43 return value * 2;
44 }
45
46 var result = Array.from(list, mapFn, thisArg);
47
48 assert.sameValue(result.length, 3, 'result.length');
49 assert.sameValue(result[0], 82, 'result[0]');
50 assert.sameValue(result[1], 84, 'result[1]');
51 assert.sameValue(result[2], 86, 'result[2]');
52
53 assert.sameValue(calls.length, 3, 'calls.length');
54
55 assert.sameValue(calls[0].args.length, 2, 'calls[0].args.length');
56 assert.sameValue(calls[0].args[0], 41, 'calls[0].args[0]');
57 assert.sameValue(calls[0].args[1], 0, 'calls[0].args[1]');
58 assert.sameValue(calls[0].thisArg, thisArg, 'calls[0].thisArg');
59
60 assert.sameValue(calls[1].args.length, 2, 'calls[1].args.length');
61 assert.sameValue(calls[1].args[0], 42, 'calls[1].args[0]');
62 assert.sameValue(calls[1].args[1], 1, 'calls[1].args[1]');
63 assert.sameValue(calls[1].thisArg, thisArg, 'calls[1].thisArg');
64
65 assert.sameValue(calls[2].args.length, 2, 'calls[2].args.length');
66 assert.sameValue(calls[2].args[0], 43, 'calls[2].args[0]');
67 assert.sameValue(calls[2].args[1], 2, 'calls[2].args[1]');
68 assert.sameValue(calls[2].thisArg, thisArg, 'calls[2].thisArg');