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

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: Add tests for calling mapfn with/without receiver in/out of sloppy mode Created 6 years, 3 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
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..31fc0d0266cde3cb5ab44edfec8d12c06494ed86
--- /dev/null
+++ b/test/mjsunit/harmony/array-from.js
@@ -0,0 +1,129 @@
+// 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() {
rossberg 2014/09/10 07:23:55 Nit: insert empty line before.
+
+assertEquals(1, Array.from.length);
+
+// Assert that constructor is called with "length" for array-like objects
+var myCollectionCalled = false;
+function MyCollection(length) {
+ myCollectionCalled = true;
+ assertEquals(1, arguments.length);
+ assertEquals(5, length);
+}
+
+Array.from.call(MyCollection, {length: 5});
+assertTrue(myCollectionCalled);
+
+// Assert that calling mapfn with / without thisArg in sloppy and strict modes
+// works as expected.
+var global = this;
+function non_strict(){ assertEquals(global, this); }
+function strict(){ "use strict"; assertEquals(void 0, this); }
+function strict_null(){ "use strict"; assertEquals(null, this); }
+Array.from([1], non_strict);
+Array.from([1], non_strict, undefined);
+Array.from([1], non_strict, null);
+Array.from([1], strict);
+Array.from([1], strict, undefined);
+Array.from([1], strict_null, null);
+
+function testArrayFrom(thisArg, constructor) {
+ assertArrayLikeEquals(Array.from.call(thisArg, [], undefined), [],
+ constructor);
rossberg 2014/09/10 07:23:56 Nit: style guide requires indent of 4 for line con
+ 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'
rossberg 2014/09/10 07:23:55 Nit: write this on one line
+ }
+ }), [{'foo': 'bar'}], constructor);
+
+ assertArrayLikeEquals(Array.from.call(thisArg, {
+ length: -1,
+ '0': {
+ 'foo': 'bar'
rossberg 2014/09/10 07:23:56 Same here
+ }
+ }), [], constructor);
+
+ assertArrayLikeEquals(Array.from.call(thisArg, [
+ 'foo', 'bar', 'baz'
+ ]), ['foo', 'bar', 'baz'], constructor);
+
+ var kSet = (new Set).add('foo').add('bar').add('baz');
rossberg 2014/09/10 07:23:55 The Set constructor can take an array argument.
+ assertArrayLikeEquals(Array.from.call(thisArg, kSet), ['foo', 'bar', 'baz'],
+ constructor);
+
+ var kMap = (new Map).set(0, 'foo').set(1, 'bar').set(2, 'baz');
rossberg 2014/09/10 07:23:55 Same here.
+ assertArrayLikeEquals(Array.from.call(thisArg, kMap),
+ [[0, 'foo'], [1, 'bar'], [2, 'baz']], constructor);
+
+ // TODO: re-enable generator test (failing with "Fatal error unimplemented code")
rossberg 2014/09/10 07:23:55 Nit: line lengths
+ // Test applied in harmony/array-from-generators.js instead, to avoid TF testing
+ // assertArrayLikeEquals(Array.from.call(thisArg, generator()), ['a', 'b', 'c'],
+ // constructor);
+
+ // 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"],
rossberg 2014/09/10 07:23:55 Line length
+ 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);
rossberg 2014/09/10 07:23:55 Cover more cases here, e.g. a function with @@iter
caitp (gmail) 2014/09/10 14:26:05 Can you provide examples of functions which we wou
rossberg 2014/09/10 14:32:35 Yes, a test with .bind would be good. Also, most E
arv (Not doing code reviews) 2014/09/10 14:33:58 Anything that is lacking a .prototype. For example
+
+function Other() {}
+
+function* generator() {
rossberg 2014/09/10 07:23:56 This seems unused here.
+ yield 'a';
+ yield 'b';
+ yield 'c';
+}
+
+function assertArrayLikeEquals(value, expected, type) {
rossberg 2014/09/10 07:23:55 Move this to the start of the file.
+ assertInstanceof(value, type);
+ assertEquals(expected.length, value.length);
+ for (var i=0; i<value.length; ++i) {
+ assertEquals(expected[i], value[i]);
+ }
+}
+
+})();

Powered by Google App Engine
This is Rietveld 408576698