Index: test/mjsunit/harmony/array-species.js |
diff --git a/test/mjsunit/harmony/array-species.js b/test/mjsunit/harmony/array-species.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f07d9cddf1bcdbd43df3bf727a37f8a1a57fa24d |
--- /dev/null |
+++ b/test/mjsunit/harmony/array-species.js |
@@ -0,0 +1,98 @@ |
+// Copyright 2015 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-species |
+ |
+// Test the ES2015 @@species feature |
adamk
2016/01/06 00:10:12
More tests I'd like to see:
- Test that the lengt
Dan Ehrenberg
2016/01/07 00:42:58
Added those tests at the end
|
+ |
+'use strict'; |
+ |
+// Subclasses of Array construct themselves under map, etc |
+ |
+class MyArray extends Array { } |
+ |
+assertEquals(MyArray, new MyArray().map(()=>{}).constructor); |
+assertEquals(MyArray, new MyArray().filter(()=>{}).constructor); |
+assertEquals(MyArray, new MyArray().concat().constructor); |
+assertEquals(MyArray, new MyArray().slice().constructor); |
+assertEquals(MyArray, new MyArray().splice().constructor); |
+ |
+// Subclasses can override @@species to return the another class |
+ |
+class MyOtherArray extends Array { |
+ static get [Symbol.species]() { return MyArray; } |
+} |
+ |
+assertEquals(MyArray, new MyOtherArray().map(()=>{}).constructor); |
+assertEquals(MyArray, new MyOtherArray().filter(()=>{}).constructor); |
+assertEquals(MyArray, new MyOtherArray().concat().constructor); |
+assertEquals(MyArray, new MyOtherArray().slice().constructor); |
+assertEquals(MyArray, new MyOtherArray().splice().constructor); |
+ |
+// Array methods on non-arrays return arrays |
+ |
+class MyNonArray extends Array { |
+ static get [Symbol.species]() { return MyObject; } |
+} |
+ |
+class MyObject { } |
+ |
+assertEquals(MyObject, Array.prototype.map.call(new MyNonArray(), ()=>{}).constructor); |
adamk
2016/01/06 00:10:12
Please wrap at 80 chars here and below (dunno why
Dan Ehrenberg
2016/01/07 00:42:58
Fixed
|
+assertEquals(MyObject, Array.prototype.filter.call(new MyNonArray(), ()=>{}).constructor); |
+assertEquals(MyObject, Array.prototype.concat.call(new MyNonArray()).constructor); |
+assertEquals(MyObject, Array.prototype.slice.call(new MyNonArray()).constructor); |
+assertEquals(MyObject, Array.prototype.splice.call(new MyNonArray()).constructor); |
+ |
+assertEquals(undefined, Array.prototype.map.call(new MyNonArray(), ()=>{}).length); |
+assertEquals(undefined, Array.prototype.filter.call(new MyNonArray(), ()=>{}).length); |
+assertEquals(undefined, Array.prototype.concat.call(new MyNonArray()).length); |
+// slice and splice actually do explicitly define the length for some reason |
+assertEquals(0, Array.prototype.slice.call(new MyNonArray()).length); |
+assertEquals(0, Array.prototype.splice.call(new MyNonArray()).length); |
+ |
+// Cross-realm Arrays build same-realm arrays |
+ |
+var realm = Realm.create(); |
+assertEquals(Array, Array.prototype.map.call(Realm.eval(realm, "[]"), ()=>{}).constructor); |
+assertFalse(Array === Realm.eval(realm, "[]").map(()=>{}).constructor); |
+assertFalse(Array === Realm.eval(realm, "[].map(()=>{}).constructor")); |
+ |
+// Defaults when constructor or @@species is missing or non-constructor |
+ |
+class MyDefaultArray extends Array { |
+ static get [Symbol.species]() { return undefined; } |
+} |
+assertEquals(Array, new MyDefaultArray().map(()=>{}).constructor); |
+ |
+class MyOtherDefaultArray extends Array { } |
+assertEquals(MyOtherDefaultArray, new MyOtherDefaultArray().map(()=>{}).constructor); |
+MyOtherDefaultArray.prototype.constructor = undefined; |
+assertEquals(Array, new MyOtherDefaultArray().map(()=>{}).constructor); |
+ |
+// Exceptions propagated when getting constructor @@species throws |
+ |
+class SpeciesError extends Error { } |
+class ConstructorError extends Error { } |
+class MyThrowingArray extends Array { |
+ static get [Symbol.species]() { throw new SpeciesError; } |
+} |
+assertThrows(() => new MyThrowingArray().map(()=>{}), SpeciesError); |
+Object.defineProperty(MyThrowingArray.prototype, 'constructor', { |
+ get() { throw new ConstructorError; } |
+}); |
+assertThrows(() => new MyThrowingArray().map(()=>{}), ConstructorError); |
+ |
+// Previously unexpected errors from setting properties in arrays throw |
+ |
+class FrozenArray extends Array { |
+ constructor(...args) { |
+ super(...args); |
+ Object.freeze(this); |
+ } |
+} |
+assertThrows(() => new FrozenArray([1]).map(()=>0), TypeError); |
+assertThrows(() => new FrozenArray([1]).filter(()=>true), TypeError); |
+assertThrows(() => new FrozenArray([1]).slice(0, 1), TypeError); |
+assertThrows(() => new FrozenArray([1]).splice(0, 1), TypeError); |
+assertThrows(() => new FrozenArray([1]).concat(), TypeError); |