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

Unified 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: Rebased, rewrote algorithm to closer match spec Created 6 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/runtime.js ('k') | tools/generate-runtime-tests.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/mjsunit/harmony/array-from.js
diff --git a/test/mjsunit/harmony/array-from.js b/test/mjsunit/harmony/array-from.js
new file mode 100644
index 0000000000000000000000000000000000000000..b3895645dbb5fb4617ca43b129005cd54a5e45f2
--- /dev/null
+++ b/test/mjsunit/harmony/array-from.js
@@ -0,0 +1,78 @@
+// Copyright 2014 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Flags: --harmony-arrays --harmony-generators
+(function() {
+
+assertEquals(1, Array.from.length);
+
+function testArrayFrom(thisArg, constructor) {
+ assertArrayLikeEquals(Array.from.call(thisArg, [], undefined), [], constructor);
+ assertArrayLikeEquals(Array.from.call(thisArg, NaN), [], constructor);
+ assertArrayLikeEquals(Array.from.call(thisArg, Infinity), [], constructor);
+ assertArrayLikeEquals(Array.from.call(thisArg, 10000000), [], constructor);
+ assertArrayLikeEquals(Array.from.call(thisArg, 'test'), ['t', 'e', 's', 't'], constructor);
+ assertArrayLikeEquals(Array.from.call(thisArg, {length: 1, '0': { 'foo': 'bar' }}), [{'foo': 'bar'}], constructor);
+ assertArrayLikeEquals(Array.from.call(thisArg, {length: -1, '0': { 'foo': 'bar' }}), [], constructor);
+ assertArrayLikeEquals(Array.from.call(thisArg, ['foo', 'bar', 'baz']), ['foo', 'bar', 'baz'], constructor);
+ assertArrayLikeEquals(Array.from.call(thisArg, (new Set).add('foo').add('bar').add('baz')), ['foo', 'bar', 'baz'], constructor);
+ assertArrayLikeEquals(Array.from.call(thisArg, (new Map).set(0, 'foo').set(1, 'bar').set(2, 'baz')), [[0, 'foo'], [1, 'bar'], [2, 'baz']], constructor);
+ // TODO: re-enable generator test (failing with "Fatal error unimplemented code")
+ // 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
+
+ // Mozilla:
+ // Array.from on a string handles surrogate pairs correctly.
+ var gclef = "\uD834\uDD1E"; // U+1D11E MUSICAL SYMBOL G CLEF
+ assertArrayLikeEquals(Array.from.call(thisArg, gclef), [gclef], constructor);
+ assertArrayLikeEquals(Array.from.call(thisArg, gclef + " G"), [gclef, " ", "G"], constructor);
+
+ assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) {
+ return this.filter(x);
+ }, {
+ filter: function(x) { return x.toUpperCase(); }
+ }), ['T', 'E', 'S', 'T'], constructor);
+ assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) {
+ return x.toUpperCase();
+ }), ['T', 'E', 'S', 'T'], constructor);
+
+ this.thisArg = thisArg;
+ assertThrows('Array.from.call(thisArg, null)', TypeError);
+ assertThrows('Array.from.call(thisArg, undefined)', TypeError);
+ assertThrows('Array.from.call(thisArg, [], null)', TypeError);
+ assertThrows('Array.from.call(thisArg, [], "noncallable")', TypeError);
+
+ this.nullIterator = {};
+ nullIterator[Symbol.iterator] = null;
+ assertThrows('Array.from.call(thisArg, nullIterator)', TypeError);
+
+ this.nonObjIterator = {};
+ nonObjIterator[Symbol.iterator] = function() { return "nonObject"; };
+ assertThrows('Array.from.call(thisArg, nonObjIterator)', TypeError);
+
+ assertThrows('Array.from.call(thisArg, [], null)', TypeError);
+}
+
+testArrayFrom(Array, Array);
+testArrayFrom(null, Array);
+testArrayFrom({}, Array);
+testArrayFrom(Object, Object);
+testArrayFrom(Other, Other);
+
+function Other() {}
+
+function* generator() {
+ yield 'a';
+ yield 'b';
+ yield 'c';
+}
+
+function assertArrayLikeEquals(value, expected, type) {
+ assertInstanceof(value, type);
+ assertEquals(expected.length, value.length);
+ for (var i=0; i<value.length; ++i) {
+ assertEquals(expected[i], value[i]);
+ }
+}
+
+})();
« no previous file with comments | « src/runtime.js ('k') | tools/generate-runtime-tests.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698