OLD | NEW |
---|---|
(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 testArrayFrom(thisArg, constructor) { | |
11 assertArrayLikeEquals(Array.from.call(thisArg, [], undefined), [], constructor ); | |
12 assertArrayLikeEquals(Array.from.call(thisArg, NaN), [], constructor); | |
13 assertArrayLikeEquals(Array.from.call(thisArg, Infinity), [], constructor); | |
14 assertArrayLikeEquals(Array.from.call(thisArg, 10000000), [], constructor); | |
15 assertArrayLikeEquals(Array.from.call(thisArg, 'test'), ['t', 'e', 's', 't'], constructor); | |
16 assertArrayLikeEquals(Array.from.call(thisArg, {length: 1, '0': { 'foo': 'bar' }}), [{'foo': 'bar'}], constructor); | |
17 assertArrayLikeEquals(Array.from.call(thisArg, {length: -1, '0': { 'foo': 'bar ' }}), [], constructor); | |
18 assertArrayLikeEquals(Array.from.call(thisArg, ['foo', 'bar', 'baz']), ['foo', 'bar', 'baz'], constructor); | |
19 assertArrayLikeEquals(Array.from.call(thisArg, (new Set).add('foo').add('bar') .add('baz')), ['foo', 'bar', 'baz'], constructor); | |
20 assertArrayLikeEquals(Array.from.call(thisArg, (new Map).set(0, 'foo').set(1, 'bar').set(2, 'baz')), [[0, 'foo'], [1, 'bar'], [2, 'baz']], constructor); | |
21 // TODO: re-enable generator test (failing with "Fatal error unimplemented cod e") | |
22 // assertArrayLikeEquals(Array.from.call(thisArg, generator()), ['a', 'b', 'c' ], constructor); | |
caitp (gmail)
2014/08/21 16:56:34
It looks like one of the flags added for mjsunit t
| |
23 | |
24 // Mozilla: | |
25 // Array.from on a string handles surrogate pairs correctly. | |
26 var gclef = "\uD834\uDD1E"; // U+1D11E MUSICAL SYMBOL G CLEF | |
27 assertArrayLikeEquals(Array.from.call(thisArg, gclef), [gclef], constructor); | |
28 assertArrayLikeEquals(Array.from.call(thisArg, gclef + " G"), [gclef, " ", "G" ], constructor); | |
29 | |
30 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) { | |
31 return this.filter(x); | |
32 }, { | |
33 filter: function(x) { return x.toUpperCase(); } | |
34 }), ['T', 'E', 'S', 'T'], constructor); | |
35 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) { | |
36 return x.toUpperCase(); | |
37 }), ['T', 'E', 'S', 'T'], constructor); | |
38 | |
39 this.thisArg = thisArg; | |
40 assertThrows('Array.from.call(thisArg, null)', TypeError); | |
41 assertThrows('Array.from.call(thisArg, undefined)', TypeError); | |
42 assertThrows('Array.from.call(thisArg, [], null)', TypeError); | |
43 assertThrows('Array.from.call(thisArg, [], "noncallable")', TypeError); | |
44 | |
45 this.nullIterator = {}; | |
46 nullIterator[Symbol.iterator] = null; | |
47 assertThrows('Array.from.call(thisArg, nullIterator)', TypeError); | |
48 | |
49 this.nonObjIterator = {}; | |
50 nonObjIterator[Symbol.iterator] = function() { return "nonObject"; }; | |
51 assertThrows('Array.from.call(thisArg, nonObjIterator)', TypeError); | |
52 | |
53 assertThrows('Array.from.call(thisArg, [], null)', TypeError); | |
54 } | |
55 | |
56 testArrayFrom(Array, Array); | |
57 testArrayFrom(null, Array); | |
58 testArrayFrom({}, Array); | |
59 testArrayFrom(Object, Object); | |
60 testArrayFrom(Other, Other); | |
61 | |
62 function Other() {} | |
63 | |
64 function* generator() { | |
65 yield 'a'; | |
66 yield 'b'; | |
67 yield 'c'; | |
68 } | |
69 | |
70 function assertArrayLikeEquals(value, expected, type) { | |
71 assertInstanceof(value, type); | |
72 assertEquals(expected.length, value.length); | |
73 for (var i=0; i<value.length; ++i) { | |
74 assertEquals(expected[i], value[i]); | |
75 } | |
76 } | |
77 | |
78 })(); | |
OLD | NEW |