Chromium Code Reviews| 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 ); | |
|
arv (Not doing code reviews)
2014/09/05 18:04:54
long line here and elsewhere
| |
| 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 // Test applied in harmony/array-from-generators.js instead, to avoid TF testi ng | |
| 23 // assertArrayLikeEquals(Array.from.call(thisArg, generator()), ['a', 'b', 'c' ], constructor); | |
| 24 | |
| 25 // Mozilla: | |
| 26 // Array.from on a string handles surrogate pairs correctly. | |
| 27 var gclef = "\uD834\uDD1E"; // U+1D11E MUSICAL SYMBOL G CLEF | |
| 28 assertArrayLikeEquals(Array.from.call(thisArg, gclef), [gclef], constructor); | |
| 29 assertArrayLikeEquals(Array.from.call(thisArg, gclef + " G"), [gclef, " ", "G" ], constructor); | |
| 30 | |
| 31 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) { | |
| 32 return this.filter(x); | |
| 33 }, { | |
| 34 filter: function(x) { return x.toUpperCase(); } | |
| 35 }), ['T', 'E', 'S', 'T'], constructor); | |
| 36 assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) { | |
| 37 return x.toUpperCase(); | |
| 38 }), ['T', 'E', 'S', 'T'], constructor); | |
| 39 | |
| 40 this.thisArg = thisArg; | |
| 41 assertThrows('Array.from.call(thisArg, null)', TypeError); | |
| 42 assertThrows('Array.from.call(thisArg, undefined)', TypeError); | |
| 43 assertThrows('Array.from.call(thisArg, [], null)', TypeError); | |
| 44 assertThrows('Array.from.call(thisArg, [], "noncallable")', TypeError); | |
| 45 | |
| 46 this.nullIterator = {}; | |
| 47 nullIterator[Symbol.iterator] = null; | |
| 48 assertThrows('Array.from.call(thisArg, nullIterator)', TypeError); | |
| 49 | |
| 50 this.nonObjIterator = {}; | |
| 51 nonObjIterator[Symbol.iterator] = function() { return "nonObject"; }; | |
| 52 assertThrows('Array.from.call(thisArg, nonObjIterator)', TypeError); | |
| 53 | |
| 54 assertThrows('Array.from.call(thisArg, [], null)', TypeError); | |
| 55 } | |
| 56 | |
| 57 testArrayFrom(Array, Array); | |
| 58 testArrayFrom(null, Array); | |
| 59 testArrayFrom({}, Array); | |
| 60 testArrayFrom(Object, Object); | |
| 61 testArrayFrom(Other, Other); | |
| 62 | |
| 63 function Other() {} | |
| 64 | |
| 65 function* generator() { | |
| 66 yield 'a'; | |
| 67 yield 'b'; | |
| 68 yield 'c'; | |
| 69 } | |
| 70 | |
| 71 function assertArrayLikeEquals(value, expected, type) { | |
| 72 assertInstanceof(value, type); | |
| 73 assertEquals(expected.length, value.length); | |
| 74 for (var i=0; i<value.length; ++i) { | |
| 75 assertEquals(expected[i], value[i]); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 })(); | |
| OLD | NEW |