test262/test/built-ins/Array/from/calling-from-valid-2.js
Leonardo Balter 3bdc467c51 Update tests for Array.from
- fixed a false positive where argument-passed-null.js was returning a TypeError from 22.1.2.1 step 5
- created 2 tests from argument-passed-null to assert this as null and items as null
- fixed reference info on Array.from_arity.js
- fix tests at Array.from_forwards-length-for-array-likes
- static checks on calling-from-valid-1-onlyStrict.js
- static checks on calling-from-valid-1-noStrict.js
- static checks on calling-from-valid-2.js
- removed create-typedarray-from.js, that test belong to es6id 22.2.2.1
- removed mapfn-invalid-typeerror-1.js - false positive from 22.1.2.1 step 5
- moved mapfn-invalid-typeerror-2.js to more spread tests where mapfn is not callable
- ...
2015-09-02 17:36:11 -04:00

68 lines
2.0 KiB
JavaScript

// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
es6id: 22.1.2.1
description: Calling from with a valid map function with thisArg
info: >
22.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] )
...
10. Let len be ToLength(Get(arrayLike, "length")).
11. ReturnIfAbrupt(len).
12. If IsConstructor(C) is true, then
a. Let A be Construct(C, «len»).
13. Else,
b. Let A be ArrayCreate(len).
14. ReturnIfAbrupt(A).
15. Let k be 0.
16. Repeat, while k < len
a. Let Pk be ToString(k).
b. Let kValue be Get(arrayLike, Pk).
c. ReturnIfAbrupt(kValue).
d. If mapping is true, then
i. Let mappedValue be Call(mapfn, T, «kValue, k»).
...
---*/
var list = {
'0': 41,
'1': 42,
'2': 43,
length: 3
};
var calls = [];
var thisArg = {};
function mapFn (value) {
calls.push({
args: arguments,
thisArg: this
});
return value * 2;
}
var result = Array.from(list, mapFn, thisArg);
assert.sameValue(result.length, 3, 'result.length');
assert.sameValue(result[0], 82, 'result[0]');
assert.sameValue(result[1], 84, 'result[1]');
assert.sameValue(result[2], 86, 'result[2]');
assert.sameValue(calls.length, 3, 'calls.length');
assert.sameValue(calls[0].args.length, 2, 'calls[0].args.length');
assert.sameValue(calls[0].args[0], 41, 'calls[0].args[0]');
assert.sameValue(calls[0].args[1], 0, 'calls[0].args[1]');
assert.sameValue(calls[0].thisArg, thisArg, 'calls[0].thisArg');
assert.sameValue(calls[1].args.length, 2, 'calls[1].args.length');
assert.sameValue(calls[1].args[0], 42, 'calls[1].args[0]');
assert.sameValue(calls[1].args[1], 1, 'calls[1].args[1]');
assert.sameValue(calls[1].thisArg, thisArg, 'calls[1].thisArg');
assert.sameValue(calls[2].args.length, 2, 'calls[2].args.length');
assert.sameValue(calls[2].args[0], 43, 'calls[2].args[0]');
assert.sameValue(calls[2].args[1], 2, 'calls[2].args[1]');
assert.sameValue(calls[2].thisArg, thisArg, 'calls[2].thisArg');