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