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

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: Add tests for calling mapfn with/without receiver in/out of sloppy mode Created 6 years, 3 months 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
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() {
rossberg 2014/09/10 07:23:55 Nit: insert empty line before.
7
8 assertEquals(1, Array.from.length);
9
10 // Assert that constructor is called with "length" for array-like objects
11 var myCollectionCalled = false;
12 function MyCollection(length) {
13 myCollectionCalled = true;
14 assertEquals(1, arguments.length);
15 assertEquals(5, length);
16 }
17
18 Array.from.call(MyCollection, {length: 5});
19 assertTrue(myCollectionCalled);
20
21 // Assert that calling mapfn with / without thisArg in sloppy and strict modes
22 // works as expected.
23 var global = this;
24 function non_strict(){ assertEquals(global, this); }
25 function strict(){ "use strict"; assertEquals(void 0, this); }
26 function strict_null(){ "use strict"; assertEquals(null, this); }
27 Array.from([1], non_strict);
28 Array.from([1], non_strict, undefined);
29 Array.from([1], non_strict, null);
30 Array.from([1], strict);
31 Array.from([1], strict, undefined);
32 Array.from([1], strict_null, null);
33
34 function testArrayFrom(thisArg, constructor) {
35 assertArrayLikeEquals(Array.from.call(thisArg, [], undefined), [],
36 constructor);
rossberg 2014/09/10 07:23:56 Nit: style guide requires indent of 4 for line con
37 assertArrayLikeEquals(Array.from.call(thisArg, NaN), [], constructor);
38 assertArrayLikeEquals(Array.from.call(thisArg, Infinity), [], constructor);
39 assertArrayLikeEquals(Array.from.call(thisArg, 10000000), [], constructor);
40 assertArrayLikeEquals(Array.from.call(thisArg, 'test'), ['t', 'e', 's', 't'],
41 constructor);
42
43 assertArrayLikeEquals(Array.from.call(thisArg, {
44 length: 1,
45 '0': {
46 'foo': 'bar'
rossberg 2014/09/10 07:23:55 Nit: write this on one line
47 }
48 }), [{'foo': 'bar'}], constructor);
49
50 assertArrayLikeEquals(Array.from.call(thisArg, {
51 length: -1,
52 '0': {
53 'foo': 'bar'
rossberg 2014/09/10 07:23:56 Same here
54 }
55 }), [], constructor);
56
57 assertArrayLikeEquals(Array.from.call(thisArg, [
58 'foo', 'bar', 'baz'
59 ]), ['foo', 'bar', 'baz'], constructor);
60
61 var kSet = (new Set).add('foo').add('bar').add('baz');
rossberg 2014/09/10 07:23:55 The Set constructor can take an array argument.
62 assertArrayLikeEquals(Array.from.call(thisArg, kSet), ['foo', 'bar', 'baz'],
63 constructor);
64
65 var kMap = (new Map).set(0, 'foo').set(1, 'bar').set(2, 'baz');
rossberg 2014/09/10 07:23:55 Same here.
66 assertArrayLikeEquals(Array.from.call(thisArg, kMap),
67 [[0, 'foo'], [1, 'bar'], [2, 'baz']], constructor);
68
69 // TODO: re-enable generator test (failing with "Fatal error unimplemented cod e")
rossberg 2014/09/10 07:23:55 Nit: line lengths
70 // Test applied in harmony/array-from-generators.js instead, to avoid TF testi ng
71 // assertArrayLikeEquals(Array.from.call(thisArg, generator()), ['a', 'b', 'c' ],
72 // constructor);
73
74 // Mozilla:
75 // Array.from on a string handles surrogate pairs correctly.
76 var gclef = "\uD834\uDD1E"; // U+1D11E MUSICAL SYMBOL G CLEF
77 assertArrayLikeEquals(Array.from.call(thisArg, gclef), [gclef], constructor);
78 assertArrayLikeEquals(Array.from.call(thisArg, gclef + " G"), [gclef, " ", "G" ],
rossberg 2014/09/10 07:23:55 Line length
79 constructor);
80
81 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) {
82 return this.filter(x);
83 }, {
84 filter: function(x) { return x.toUpperCase(); }
85 }), ['T', 'E', 'S', 'T'], constructor);
86 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) {
87 return x.toUpperCase();
88 }), ['T', 'E', 'S', 'T'], constructor);
89
90 this.thisArg = thisArg;
91 assertThrows('Array.from.call(thisArg, null)', TypeError);
92 assertThrows('Array.from.call(thisArg, undefined)', TypeError);
93 assertThrows('Array.from.call(thisArg, [], null)', TypeError);
94 assertThrows('Array.from.call(thisArg, [], "noncallable")', TypeError);
95
96 this.nullIterator = {};
97 nullIterator[Symbol.iterator] = null;
98 assertThrows('Array.from.call(thisArg, nullIterator)', TypeError);
99
100 this.nonObjIterator = {};
101 nonObjIterator[Symbol.iterator] = function() { return "nonObject"; };
102 assertThrows('Array.from.call(thisArg, nonObjIterator)', TypeError);
103
104 assertThrows('Array.from.call(thisArg, [], null)', TypeError);
105 }
106
107 testArrayFrom(Array, Array);
108 testArrayFrom(null, Array);
109 testArrayFrom({}, Array);
110 testArrayFrom(Object, Object);
111 testArrayFrom(Other, Other);
rossberg 2014/09/10 07:23:55 Cover more cases here, e.g. a function with @@iter
caitp (gmail) 2014/09/10 14:26:05 Can you provide examples of functions which we wou
rossberg 2014/09/10 14:32:35 Yes, a test with .bind would be good. Also, most E
arv (Not doing code reviews) 2014/09/10 14:33:58 Anything that is lacking a .prototype. For example
112
113 function Other() {}
114
115 function* generator() {
rossberg 2014/09/10 07:23:56 This seems unused here.
116 yield 'a';
117 yield 'b';
118 yield 'c';
119 }
120
121 function assertArrayLikeEquals(value, expected, type) {
rossberg 2014/09/10 07:23:55 Move this to the start of the file.
122 assertInstanceof(value, type);
123 assertEquals(expected.length, value.length);
124 for (var i=0; i<value.length; ++i) {
125 assertEquals(expected[i], value[i]);
126 }
127 }
128
129 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698