Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: test/mjsunit/harmony/array-from.js

Issue 363833006: Implement Array.from() (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Rebase it Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/harmony-array.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // 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
8 assertEquals(1, Array.from.length);
9
10 function 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
19 var myCollectionCalled = false;
20 function MyCollection(length) {
21 myCollectionCalled = true;
22 assertEquals(1, arguments.length);
23 assertEquals(5, length);
24 }
25
26 Array.from.call(MyCollection, {length: 5});
27 assertTrue(myCollectionCalled);
28
29 // Assert that calling mapfn with / without thisArg in sloppy and strict modes
30 // works as expected.
31 var global = this;
32 function non_strict(){ assertEquals(global, this); }
33 function strict(){ "use strict"; assertEquals(void 0, this); }
34 function strict_null(){ "use strict"; assertEquals(null, this); }
35 Array.from([1], non_strict);
36 Array.from([1], non_strict, void 0);
37 Array.from([1], non_strict, null);
38 Array.from([1], strict);
39 Array.from([1], strict, void 0);
40 Array.from([1], strict_null, null);
41
42 function 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
111 function Other() {}
112
113 var boundFn = (function() {}).bind(Array, 27);
114
115 testArrayFrom(Array, Array);
116 testArrayFrom(null, Array);
117 testArrayFrom({}, Array);
118 testArrayFrom(Object, Object);
119 testArrayFrom(Other, Other);
120 testArrayFrom(Math.cos, Array);
121 testArrayFrom(boundFn, Array);
122
123 })();
OLDNEW
« no previous file with comments | « src/harmony-array.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698