blob: e7c9fef7d5d8a3826451e1c9d6fb133af26060cc [file] [log] [blame]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Flags: --harmony-arrays --harmony-generators
6(function() {
7
8assertEquals(1, Array.from.length);
9
10function assertArrayLikeEquals(value, expected, type) {
11 assertInstanceof(value, type);
12 assertEquals(expected.length, value.length);
13 for (var i=0; i<value.length; ++i) {
14 assertEquals(expected[i], value[i]);
15 }
16}
17
18// Assert that constructor is called with "length" for array-like objects
19var myCollectionCalled = false;
20function MyCollection(length) {
21 myCollectionCalled = true;
22 assertEquals(1, arguments.length);
23 assertEquals(5, length);
24}
25
26Array.from.call(MyCollection, {length: 5});
27assertTrue(myCollectionCalled);
28
29// Assert that calling mapfn with / without thisArg in sloppy and strict modes
30// works as expected.
31var global = this;
32function non_strict(){ assertEquals(global, this); }
33function strict(){ "use strict"; assertEquals(void 0, this); }
34function strict_null(){ "use strict"; assertEquals(null, this); }
35Array.from([1], non_strict);
36Array.from([1], non_strict, void 0);
37Array.from([1], non_strict, null);
38Array.from([1], strict);
39Array.from([1], strict, void 0);
40Array.from([1], strict_null, null);
41
42function testArrayFrom(thisArg, constructor) {
43 assertArrayLikeEquals(Array.from.call(thisArg, [], undefined), [],
44 constructor);
45 assertArrayLikeEquals(Array.from.call(thisArg, NaN), [], constructor);
46 assertArrayLikeEquals(Array.from.call(thisArg, Infinity), [], constructor);
47 assertArrayLikeEquals(Array.from.call(thisArg, 10000000), [], constructor);
48 assertArrayLikeEquals(Array.from.call(thisArg, 'test'), ['t', 'e', 's', 't'],
49 constructor);
50
51 assertArrayLikeEquals(Array.from.call(thisArg,
52 { length: 1, '0': { 'foo': 'bar' } }), [{'foo': 'bar'}], constructor);
53
54 assertArrayLikeEquals(Array.from.call(thisArg,
55 { length: -1, '0': { 'foo': 'bar' } }), [], constructor);
56
57 assertArrayLikeEquals(Array.from.call(thisArg,
58 [ 'foo', 'bar', 'baz' ]), ['foo', 'bar', 'baz'], constructor);
59
60 var kSet = new Set(['foo', 'bar', 'baz']);
61 assertArrayLikeEquals(Array.from.call(thisArg, kSet), ['foo', 'bar', 'baz'],
62 constructor);
63
64 var kMap = new Map(['foo', 'bar', 'baz'].entries());
65 assertArrayLikeEquals(Array.from.call(thisArg, kMap),
66 [[0, 'foo'], [1, 'bar'], [2, 'baz']], constructor);
67
68
69 function* generator() {
70 yield 'a';
71 yield 'b';
72 yield 'c';
73 }
74
75 assertArrayLikeEquals(Array.from.call(thisArg, generator()),
76 ['a', 'b', 'c'], constructor);
77
78 // Mozilla:
79 // Array.from on a string handles surrogate pairs correctly.
80 var gclef = "\uD834\uDD1E"; // U+1D11E MUSICAL SYMBOL G CLEF
81 assertArrayLikeEquals(Array.from.call(thisArg, gclef), [gclef], constructor);
82 assertArrayLikeEquals(Array.from.call(thisArg, gclef + " G"),
83 [gclef, " ", "G"], constructor);
84
85 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) {
86 return this.filter(x);
87 }, {
88 filter: function(x) { return x.toUpperCase(); }
89 }), ['T', 'E', 'S', 'T'], constructor);
90 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) {
91 return x.toUpperCase();
92 }), ['T', 'E', 'S', 'T'], constructor);
93
94 this.thisArg = thisArg;
95 assertThrows('Array.from.call(thisArg, null)', TypeError);
96 assertThrows('Array.from.call(thisArg, undefined)', TypeError);
97 assertThrows('Array.from.call(thisArg, [], null)', TypeError);
98 assertThrows('Array.from.call(thisArg, [], "noncallable")', TypeError);
99
100 this.nullIterator = {};
101 nullIterator[Symbol.iterator] = null;
102 assertThrows('Array.from.call(thisArg, nullIterator)', TypeError);
103
104 this.nonObjIterator = {};
105 nonObjIterator[Symbol.iterator] = function() { return "nonObject"; };
106 assertThrows('Array.from.call(thisArg, nonObjIterator)', TypeError);
107
108 assertThrows('Array.from.call(thisArg, [], null)', TypeError);
109}
110
111function Other() {}
112
113var boundFn = (function() {}).bind(Array, 27);
114
115testArrayFrom(Array, Array);
116testArrayFrom(null, Array);
117testArrayFrom({}, Array);
118testArrayFrom(Object, Object);
119testArrayFrom(Other, Other);
120testArrayFrom(Math.cos, Array);
121testArrayFrom(boundFn, Array);
122
123})();