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 // 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 function testArrayFrom(thisArg, constructor) { |
| 22 assertArrayLikeEquals(Array.from.call(thisArg, [], undefined), [], |
| 23 constructor); |
| 24 assertArrayLikeEquals(Array.from.call(thisArg, NaN), [], constructor); |
| 25 assertArrayLikeEquals(Array.from.call(thisArg, Infinity), [], constructor); |
| 26 assertArrayLikeEquals(Array.from.call(thisArg, 10000000), [], constructor); |
| 27 assertArrayLikeEquals(Array.from.call(thisArg, 'test'), ['t', 'e', 's', 't'], |
| 28 constructor); |
| 29 |
| 30 assertArrayLikeEquals(Array.from.call(thisArg, { |
| 31 length: 1, |
| 32 '0': { |
| 33 'foo': 'bar' |
| 34 } |
| 35 }), [{'foo': 'bar'}], constructor); |
| 36 |
| 37 assertArrayLikeEquals(Array.from.call(thisArg, { |
| 38 length: -1, |
| 39 '0': { |
| 40 'foo': 'bar' |
| 41 } |
| 42 }), [], constructor); |
| 43 |
| 44 assertArrayLikeEquals(Array.from.call(thisArg, [ |
| 45 'foo', 'bar', 'baz' |
| 46 ]), ['foo', 'bar', 'baz'], constructor); |
| 47 |
| 48 var kSet = (new Set).add('foo').add('bar').add('baz'); |
| 49 assertArrayLikeEquals(Array.from.call(thisArg, kSet), ['foo', 'bar', 'baz'], |
| 50 constructor); |
| 51 |
| 52 var kMap = (new Map).set(0, 'foo').set(1, 'bar').set(2, 'baz'); |
| 53 assertArrayLikeEquals(Array.from.call(thisArg, kMap), |
| 54 [[0, 'foo'], [1, 'bar'], [2, 'baz']], constructor); |
| 55 |
| 56 // TODO: re-enable generator test (failing with "Fatal error unimplemented cod
e") |
| 57 // Test applied in harmony/array-from-generators.js instead, to avoid TF testi
ng |
| 58 // assertArrayLikeEquals(Array.from.call(thisArg, generator()), ['a', 'b', 'c'
], |
| 59 // constructor); |
| 60 |
| 61 // Mozilla: |
| 62 // Array.from on a string handles surrogate pairs correctly. |
| 63 var gclef = "\uD834\uDD1E"; // U+1D11E MUSICAL SYMBOL G CLEF |
| 64 assertArrayLikeEquals(Array.from.call(thisArg, gclef), [gclef], constructor); |
| 65 assertArrayLikeEquals(Array.from.call(thisArg, gclef + " G"), [gclef, " ", "G"
], |
| 66 constructor); |
| 67 |
| 68 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) { |
| 69 return this.filter(x); |
| 70 }, { |
| 71 filter: function(x) { return x.toUpperCase(); } |
| 72 }), ['T', 'E', 'S', 'T'], constructor); |
| 73 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) { |
| 74 return x.toUpperCase(); |
| 75 }), ['T', 'E', 'S', 'T'], constructor); |
| 76 |
| 77 this.thisArg = thisArg; |
| 78 assertThrows('Array.from.call(thisArg, null)', TypeError); |
| 79 assertThrows('Array.from.call(thisArg, undefined)', TypeError); |
| 80 assertThrows('Array.from.call(thisArg, [], null)', TypeError); |
| 81 assertThrows('Array.from.call(thisArg, [], "noncallable")', TypeError); |
| 82 |
| 83 this.nullIterator = {}; |
| 84 nullIterator[Symbol.iterator] = null; |
| 85 assertThrows('Array.from.call(thisArg, nullIterator)', TypeError); |
| 86 |
| 87 this.nonObjIterator = {}; |
| 88 nonObjIterator[Symbol.iterator] = function() { return "nonObject"; }; |
| 89 assertThrows('Array.from.call(thisArg, nonObjIterator)', TypeError); |
| 90 |
| 91 assertThrows('Array.from.call(thisArg, [], null)', TypeError); |
| 92 } |
| 93 |
| 94 testArrayFrom(Array, Array); |
| 95 testArrayFrom(null, Array); |
| 96 testArrayFrom({}, Array); |
| 97 testArrayFrom(Object, Object); |
| 98 testArrayFrom(Other, Other); |
| 99 |
| 100 function Other() {} |
| 101 |
| 102 function* generator() { |
| 103 yield 'a'; |
| 104 yield 'b'; |
| 105 yield 'c'; |
| 106 } |
| 107 |
| 108 function assertArrayLikeEquals(value, expected, type) { |
| 109 assertInstanceof(value, type); |
| 110 assertEquals(expected.length, value.length); |
| 111 for (var i=0; i<value.length; ++i) { |
| 112 assertEquals(expected[i], value[i]); |
| 113 } |
| 114 } |
| 115 |
| 116 })(); |
OLD | NEW |