Merge pull request #376 from yanlxu/Array.from

update tests for Array.from
This commit is contained in:
Brian Terlson 2015-08-04 16:04:21 -07:00
commit 91e1a0b761
28 changed files with 591 additions and 3 deletions

View File

@ -0,0 +1,13 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: Testing descriptor property of Array.from
includes:
- propertyHelper.js
es6id: 22.1.2.1
---*/
verifyWritable(Array, "from");
verifyNotEnumerable(Array, "from");
verifyConfigurable(Array, "from");

View File

@ -0,0 +1,30 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
es6id: 22.1.2.1
description: '`name` property'
info: >
ES6 Section 17:
Every built-in Function object, including constructors, that is not
identified as an anonymous function has a name property whose value is a
String. Unless otherwise specified, this value is the name that is given to
the function in this specification.
[...]
Unless otherwise specified, the name property of a built-in Function
object, if it exists, has the attributes { [[Writable]]: false,
[[Enumerable]]: false, [[Configurable]]: true }.
includes: [propertyHelper.js]
---*/
assert.sameValue(
Array.from.name,
'from',
'The value of `Array.from.name` is `"from"`'
);
verifyNotEnumerable(Array.from, 'name');
verifyNotWritable(Array.from, 'name');
verifyConfigurable(Array.from, 'name');

View File

@ -4,9 +4,20 @@
/*---
es6id: 22.1.2.1
description: >
The Array.from() method creates a new Array instance
from an array-like or iterable object.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from
The length property of the Array.from method is 1.
info: >
ES6 Section 17:
Unless otherwise specified, the length property of a built-in Function
object has the attributes { [[Writable]]: false, [[Enumerable]]: false,
[[Configurable]]: true }.
includes: [propertyHelper.js]
---*/
assert.sameValue(Array.from.length, 1);
verifyNotEnumerable(Array.from, 'length');
verifyNotWritable(Array.from, 'length');
verifyConfigurable(Array.from, 'length');

View File

@ -0,0 +1,9 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: this argument is passed as null
es6id: 22.1.2.1
---*/
assert.throws(TypeError, function(){Array.from.call(null);});

View File

@ -0,0 +1,25 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: Calling from with a valid map function without thisArg use noStrict
es6id: 22.1.2.1
flags: [noStrict]
---*/
var array = [ 0, 1, -2, 4, -8, 16 ];
var arrayIndex = -1;
var globalThis = this;
function mapFn(value, k) {
arrayIndex++;
assert.sameValue(value, array[arrayIndex], "From mapFn Element mismatch for index " + arrayIndex + ".");
assert.sameValue(k, arrayIndex, "From mapFn index mismatch for " + arrayIndex + ".");
assert.sameValue(globalThis, this, "Wrong this value is passed to mapFn for index " + arrayIndex + ".");
return value;
}
var a = Array.from(array, mapFn);
for (var j = 0; j < array.length; j++) {
assert.sameValue(a[j], array[j], "Elements mismatch at " + j + ".");
}

View File

@ -0,0 +1,24 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: Calling from with a valid map function without thisArg use onlyStrict
es6id: 22.1.2.1
flags: [onlyStrict]
---*/
var array = [ 0, 1, -2, 4, -8, 16 ];
var arrayIndex = -1;
function mapFn(value, k) {
arrayIndex++;
assert.sameValue(value, array[arrayIndex], "From mapFn Element mismatch for index " + arrayIndex + ".");
assert.sameValue(k, arrayIndex, "From mapFn index mismatch for " + arrayIndex + ".");
return value;
}
var a = Array.from(array, mapFn);
for (var j = 0; j < array.length; j++) {
assert.sameValue(a[j], array[j], "Elements mismatch at " + j + ".");
}

View File

@ -0,0 +1,23 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: Calling from with a valid map function with thisArg
es6id: 22.1.2.1
---*/
var array = [ 0, 1, -2, 4, -8, 16 ];
var arrayIndex = -1;
function mapFn (value, k) {
arrayIndex++;
assert.sameValue(value, array[arrayIndex], "In mapFn element mismatch for index " + arrayIndex + " in the mapFn.");
assert.sameValue(k, arrayIndex, "From mapFn index mismatch for " + arrayIndex + ".");
return value;
}
var a = Array.from(array, mapFn, this);
for (var j = 0; j < a.length; j++) {
assert.sameValue(a[j], array[j], "Elements mismatch at " + j + ".");
}

View File

@ -0,0 +1,13 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: Create a TypedArray from another TypedArray
es6id: 22.1.2.1
---*/
var typedArray = Int32Array.from([1, 2, 4, 8, 16, 32, 64, 128]);
var a = Array.from(typedArray);
for (var j = 0; j < typedArray.length; j++) {
assert.sameValue(a[j], typedArray[j], "Elements mismatch at " + j + ".");
}

View File

@ -0,0 +1,37 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: Elements added after the call to from
es6id: 22.1.2.1
---*/
var arrayIndex = -1;
var originalLength = 7;
var obj = {
length: originalLength,
0: 2,
1: 4,
2: 8,
3: 16,
4: 32,
5: 64,
6: 128
};
var array = [ 2, 4, 8, 16, 32, 64, 128 ];
function mapFn(value, index) {
arrayIndex++;
assert.sameValue(value, obj[arrayIndex], "Value mismatch in mapFn at index " + index + ".");
assert.sameValue(index, arrayIndex, "Index mismatch in mapFn.");
obj[originalLength + arrayIndex] = 2 * arrayIndex + 1;
return obj[arrayIndex];
}
var a = Array.from(obj, mapFn);
assert.sameValue(a.length, array.length, "Length mismatch.");
for (var j = 0; j < a.length; j++) {
assert.sameValue(a[j], array[j], "Element mismatch for array at index " + j + ".");
}

View File

@ -0,0 +1,31 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: >
Elements deleted after the call started and before visited are not
visited
es6id: 22.1.2.1
---*/
var originalArray = [ 0, 1, -2, 4, -8, 16 ];
var array = [ 0, 1, -2, 4, -8, 16 ];
var a = [];
var arrayIndex = -1;
function mapFn(value, index) {
this.arrayIndex++;
assert.sameValue(value, array[this.arrayIndex], "Value mismatch in mapFn at index " + index + ".");
assert.sameValue(index, this.arrayIndex, "Index mismatch in mapFn.");
array.splice(array.length - 1, 1);
return 127;
}
a = Array.from(array, mapFn, this);
assert.sameValue(a.length, originalArray.length / 2, "Length mismatch. Old array : " + (originalArray.length / 2) + ". array : " + a.length + ".");
for (var j = 0; j < originalArray.length / 2; j++) {
assert.sameValue(a[j], 127, "Element mismatch for mapped array at index " + j + ".");
}

View File

@ -0,0 +1,26 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: Elements are updated after the call to from
es6id: 22.1.2.1
---*/
var array = [ 127, 4, 8, 16, 32, 64, 128 ];
var arrayIndex = -1;
function mapFn(value, index) {
arrayIndex++;
if (index + 1 < array.length) {
array[index + 1] = 127;
}
assert.sameValue(value, 127, "Value mismatch in mapFn at index " + index + ".");
assert.sameValue(index, arrayIndex, "Index mismatch in mapFn.");
return value;
}
var a = Array.from(array, mapFn);
assert.sameValue(a.length, array.length, "Length mismatch.");
for (var j = 0; j < a.length; j++) {
assert.sameValue(a[j], 127, "Element mismatch for mapped array.");
}

View File

@ -0,0 +1,25 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: map is called by passing null for thisArg
es6id: 22.1.2.1
---*/
var arrayIndex = -1;
var array = [ 2, 4, 8, 16, 32, 64, 128 ];
function mapFn(value, index) {
arrayIndex++;
assert.sameValue(value, array[arrayIndex], "Value mismatch in mapFn at index " + index + ".");
assert.sameValue(index, arrayIndex, "Index mismatch in mapFn.");
return array[arrayIndex];
}
var a = Array.from(array, mapFn, null);
assert.sameValue(a.length, array.length, "Length mismatch. array : " + a.length + ". Original array : " + array.length + ".");
for (var j = 0; j < a.length; j++) {
assert.sameValue(a[j], array[j], "Element mismatch for mapped array.");
}

View File

@ -0,0 +1,25 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: mapFn is attached to the Array itself
es6id: 22.1.2.1
---*/
var array = [ 2, 4, 8, 16, 32, 64, 128 ];
var a = [];
var i = 0;
Array.prototype.mapFn = function(value, index) {
assert.sameValue(value, array[i], "Value mismatch in mapFn at index " + index + ".");
assert.sameValue(index, i, "Index mismatch in mapFn.");
i++;
return 127;
};
a = Array.from(array, a.mapFn , a);
assert.sameValue(a.length, array.length, "Length mismatch. array : " + array.length + ". Mapped array : " + a.length + ".");
for (var j = 0; j < a.length; j++) {
assert.sameValue(a[j], 127, "Element mismatch for mapped array at index " + j + ".");
}

View File

@ -0,0 +1,9 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: mapFn is invalid causes a TypeError
es6id: 22.1.2.1
---*/
assert.throws(TypeError, function(){Array.from(null);});

View File

@ -0,0 +1,11 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: mapFn is invalid causes a TypeError
es6id: 22.1.2.1
---*/
var obj = {};
var array = [ ];
assert.throws(TypeError, function(){Array.from(array, obj);});

View File

@ -0,0 +1,29 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: mapFn returns a non-integer
es6id: 22.1.2.1
---*/
var array = [ 2, 4, 8, 16, 32, 64, 128 ];
var arrayIndex = -1;
function mapFn(value, index) {
this.arrayIndex++;
assert.sameValue(value, array[this.arrayIndex], "Value mismatch in mapFn at index " + index + ".");
assert.sameValue(index, this.arrayIndex, "Index mismatch in mapFn.");
return {
val : 127,
get value() {
return this.val;
}
};
}
var a = Array.from(array, mapFn, this);
assert.sameValue(a.length, array.length, "Length mismatch. array : " + array.length + ". Mapped array : " + a.length + ".");
for (var j = 0; j < a.length; j++) {
assert.sameValue(a[j].value, 127, "Element mismatch for mapped array at index " + j + ".");
}

View File

@ -0,0 +1,14 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: mapFn throws an exception
es6id: 22.1.2.1
---*/
var array = [ 2, 4, 8, 16, 32, 64, 128 ];
function mapFn(value, index, obj) {
throw new Test262Error();
}
assert.throws(Test262Error, function(){Array.from(array, mapFn);});

View File

@ -0,0 +1,13 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: Passing array Buffer
es6id: 22.1.2.1
---*/
var arrayBuffer = new ArrayBuffer([ 1, 2, 4, 8, 16, 32, 64, 128 ]);
var a = Array.from(arrayBuffer);
for (var j = 0; j < a.length; j++) {
assert.sameValue(a[j], arrayBuffer[j], "Elements mismatch at " + j + ".");
}

View File

@ -0,0 +1,13 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: Passing a valid array
es6id: 22.1.2.1
---*/
var array = [ 0, 1, -2, 4, -8, 16 ];
var a = Array.from(array);
for (var j = 0; j < a.length; j++) {
assert.sameValue(a[j], array[j], "Elements mismatch at " + j + ".");
}

View File

@ -0,0 +1,26 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: Source array with boundary values
es6id: 22.1.2.1
---*/
var array = [ Number.MAX_VALUE, Number.MIN_VALUE, Number.NaN, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY ];
var arrayIndex = -1;
function mapFn(value, index) {
this.arrayIndex++;
assert.sameValue(value, array[this.arrayIndex], "Value mismatch in mapFn at index " + index + ".");
assert.sameValue(index, this.arrayIndex, "Index mismatch in mapFn.");
return value;
}
var a = Array.from(array, mapFn, this);
assert.sameValue(a.length, array.length, "Length mismatch.");
assert.sameValue(a[0], Number.MAX_VALUE, "Element mismatch for mapped array at index 0.");
assert.sameValue(a[1], Number.MIN_VALUE, "Element mismatch for mapped array at index 1.");
assert.sameValue(a[2], Number.NaN, "Element mismatch for mapped array at index 2.");
assert.sameValue(a[3], Number.NEGATIVE_INFINITY, "Element mismatch for mapped array at index 3.");
assert.sameValue(a[4], Number.POSITIVE_INFINITY, "Element mismatch for mapped array at index 4.");

View File

@ -0,0 +1,10 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: >
Array.from uses a constructor other than Array.
es6id: 22.1.2.1
---*/
assert.sameValue(Array.from.call(Object, []).constructor, Object);

View File

@ -0,0 +1,28 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: Source object has iterator which throws
es6id: 22.1.2.1
---*/
var array = [ 2, 4, 8, 16, 32, 64, 128 ];
var obj = {
[Symbol.iterator]() {
return {
index: 0,
next() {
throw new Test262Error();
},
isDone : false,
get val() {
this.index++;
if (this.index > 7) {
this.isDone = true;
}
return 1 << this.index;
}
};
}
};
assert.throws(Test262Error, function(){Array.from(obj);});

View File

@ -0,0 +1,35 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: Source object has iterator
es6id: 22.1.2.1
---*/
var array = [ 2, 4, 8, 16, 32, 64, 128 ];
var obj = {
[Symbol.iterator]() {
return {
index: 0,
next() {
return {
value: this.val,
done: this.isDone
};
},
isDone : false,
get val() {
this.index++;
if (this.index > 7) {
this.isDone = true;
}
return 1 << this.index;
}
};
}
};
var a = Array.from.call(Object, obj);
assert.sameValue(typeof a, typeof {}, "The returned type is expected to be object.");
for (var j = 0; j < a.length; j++) {
assert.sameValue(a[j], array[j], "Elements mismatch at " + j + ".");
}

View File

@ -0,0 +1,24 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: >
Source is an object with length property and one item is deleted
from the source
es6id: 22.1.2.1
---*/
var array = [2, 4, 0, 16];
var expectedArray = [2, 4, , 16];
var obj = {
length : 4,
0 : 2,
1 : 4,
2 : 0,
3 : 16
};
delete obj[2];
var a = Array.from(obj);
for (var j = 0; j < expectedArray.length; j++) {
assert.sameValue(a[j], expectedArray[j], "Elements mismatch at " + j + ".");
}

View File

@ -0,0 +1,21 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: Source is an object with missing values
es6id: 22.1.2.1
---*/
var array = [2, 4, , 16];
var obj = {
length: 4,
0: 2,
1: 4,
3: 16
};
var a = Array.from.call(Object, obj);
assert.sameValue(typeof a, "object", "The returned type is expected to be object.");
for (var j = 0; j < a.length; j++) {
assert.sameValue(a[j], array[j], "Elements mismatch at " + j + ".");
}

View File

@ -0,0 +1,17 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: Source is an object without length property
es6id: 22.1.2.1
---*/
var obj = {
0: 2,
1: 4,
2: 8,
3: 16
}
var a = Array.from(obj);
assert.sameValue(a.length, 0, "Expected an array of length 0.");

View File

@ -0,0 +1,24 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: Source is a sparse array use noStrict
es6id: 22.1.2.1
flags: [noStrict]
---*/
var array = [0, 1, -2, 4, -8, , -32, 64, , 256, -512, 1024];
var globalThis = this;
var arrayIndex = -1;
function mapFn(value, k) {
arrayIndex++;
assert.sameValue(value, array[arrayIndex], "From mapFn Element mismatch for index " + arrayIndex + ".");
assert.sameValue(k, arrayIndex, "From mapFn index mismatch for " + arrayIndex + ".");
assert.sameValue(globalThis, this, "Wrong this value is passed to mapFn for index " + arrayIndex + ".");
return value;
}
var a = Array.from(array, mapFn);
for (var j = 0; j < a.length; j++) {
assert.sameValue(a[j], array[j], "Elements mismatch at " + j + ".");
}

View File

@ -0,0 +1,22 @@
// Copyright 2015 Microsoft Corporation. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
description: Source is a sparse array use onlyStrict
es6id: 22.1.2.1
flags: [onlyStrict]
---*/
var array = [0, 1, -2, 4, -8, , -32, 64, , 256, -512, 1024];
var arrayIndex = -1;
function mapFn(value, k) {
arrayIndex++;
assert.sameValue(value, array[arrayIndex], "From mapFn Element mismatch for index " + arrayIndex + ".");
assert.sameValue(k, arrayIndex, "From mapFn index mismatch for " + arrayIndex + ".");
return value;
}
var a = Array.from(array, mapFn);
for (var j = 0; j < a.length; j++) {
assert.sameValue(a[j], array[j], "Elements mismatch at " + j + ".");
}