Merge pull request #404 from bocoup/update-array-from

Update tests for Array.from
This commit is contained in:
Rick Waldron 2015-09-02 17:37:30 -04:00
commit e80b669a37
25 changed files with 343 additions and 305 deletions

View File

@ -4,14 +4,13 @@
/*---
es6id: 22.1.2.1
description: >
The length property of the Array.from method is 1.
The length property of the Array.from method is 1.
info: >
ES6 Section 17:
22.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] )
Unless otherwise specified, the length property of a built-in Function
object has the attributes { [[Writable]]: false, [[Enumerable]]: false,
[[Configurable]]: true }.
...
The length property of the from method is 1.
includes: [propertyHelper.js]
---*/

View File

@ -4,16 +4,31 @@
/*---
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
If this is a constructor, and items doesn't have an @@iterator,
returns a new instance of this
info: >
22.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] )
4. Let usingIterator be GetMethod(items, @@iterator).
...
6. If usingIterator is not undefined, then
...
12. If IsConstructor(C) is true, then
a. Let A be Construct(C, «len»).
13. Else,
a. Let A be ArrayCreate(len).
...
19. Return A.
---*/
var myCollectionCalled = false;
function MyCollection(length) {
myCollectionCalled = true;
assert.sameValue(1, arguments.length);
assert.sameValue(5, length);
var result;
function MyCollection() {
this.args = arguments;
}
Array.from.call(MyCollection, {length: 5});
assert(myCollectionCalled);
result = Array.from.call(MyCollection, {length: 42});
assert.sameValue(result.args.length, 1);
assert.sameValue(result.args[0], 42);
assert(result instanceof MyCollection);

View File

@ -1,20 +0,0 @@
// Copyright (c) 2014 Hank Yates. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.2.1_T1
description: Testing Array.from when passed a String
author: Hank Yates (hankyates@gmail.com)
includes: [runTestCase.js]
---*/
runTestCase(function () {
var arrLikeSource = 'testValue',
testArr = Array.from(arrLikeSource);
if (testArr.length != 9) {
return false;
}
return true;
});

View File

@ -1,25 +0,0 @@
// Copyright (c) 2014 Hank Yates. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.2.1_T2
description: Testing Array.from when passed an Object is passed
author: Hank Yates (hankyates@gmail.com)
includes: [runTestCase.js]
---*/
runTestCase(function () {
var testArr = Array.from({
'a': 1,
'b': '2',
'c': 'three',
'length': '3'
});
if (testArr.length != 3) {
return false;
}
return true;
});

View File

@ -1,20 +0,0 @@
// Copyright (c) 2014 Hank Yates. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.2.1_T3
description: Testing Array.from when passed an undefined
author: Hank Yates (hankyates@gmail.com)
includes: [runTestCase.js]
---*/
runTestCase(function () {
try {
Array.from(undefined);
} catch (e) {
return e instanceof TypeError;
}
return false;
});

View File

@ -1,9 +0,0 @@
// 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

@ -1,25 +1,66 @@
// 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
description: Map function without thisArg on non strict mode
info: >
22.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] )
...
10. Let len be ToLength(Get(arrayLike, "length")).
11. ReturnIfAbrupt(len).
12. If IsConstructor(C) is true, then
a. Let A be Construct(C, «len»).
13. Else,
b. Let A be ArrayCreate(len).
14. ReturnIfAbrupt(A).
15. Let k be 0.
16. Repeat, while k < len
a. Let Pk be ToString(k).
b. Let kValue be Get(arrayLike, Pk).
c. ReturnIfAbrupt(kValue).
d. If mapping is true, then
i. Let mappedValue be Call(mapfn, T, «kValue, k»).
...
flags: [noStrict]
---*/
var array = [ 0, 1, -2, 4, -8, 16 ];
var arrayIndex = -1;
var globalThis = this;
function mapFn(value, k) {
var list = {
'0': 41,
'1': 42,
'2': 43,
length: 3
};
var calls = [];
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 + ".");
function mapFn (value) {
calls.push({
args: arguments,
thisArg: this
});
return value * 2;
}
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 + ".");
}
var result = Array.from(list, mapFn);
assert.sameValue(result.length, 3, 'result.length');
assert.sameValue(result[0], 82, 'result[0]');
assert.sameValue(result[1], 84, 'result[1]');
assert.sameValue(result[2], 86, 'result[2]');
assert.sameValue(calls.length, 3, 'calls.length');
assert.sameValue(calls[0].args.length, 2, 'calls[0].args.length');
assert.sameValue(calls[0].args[0], 41, 'calls[0].args[0]');
assert.sameValue(calls[0].args[1], 0, 'calls[0].args[1]');
assert.sameValue(calls[0].thisArg, this, 'calls[0].thisArg');
assert.sameValue(calls[1].args.length, 2, 'calls[1].args.length');
assert.sameValue(calls[1].args[0], 42, 'calls[1].args[0]');
assert.sameValue(calls[1].args[1], 1, 'calls[1].args[1]');
assert.sameValue(calls[1].thisArg, this, 'calls[1].thisArg');
assert.sameValue(calls[2].args.length, 2, 'calls[2].args.length');
assert.sameValue(calls[2].args[0], 43, 'calls[2].args[0]');
assert.sameValue(calls[2].args[1], 2, 'calls[2].args[1]');
assert.sameValue(calls[2].thisArg, this, 'calls[2].thisArg');

View File

@ -1,24 +1,66 @@
// 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
description: Map function without thisArg on strict mode
info: >
22.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] )
...
10. Let len be ToLength(Get(arrayLike, "length")).
11. ReturnIfAbrupt(len).
12. If IsConstructor(C) is true, then
a. Let A be Construct(C, «len»).
13. Else,
b. Let A be ArrayCreate(len).
14. ReturnIfAbrupt(A).
15. Let k be 0.
16. Repeat, while k < len
a. Let Pk be ToString(k).
b. Let kValue be Get(arrayLike, Pk).
c. ReturnIfAbrupt(kValue).
d. If mapping is true, then
i. Let mappedValue be Call(mapfn, T, «kValue, k»).
...
flags: [onlyStrict]
---*/
var array = [ 0, 1, -2, 4, -8, 16 ];
var arrayIndex = -1;
var list = {
'0': 41,
'1': 42,
'2': 43,
length: 3
};
var calls = [];
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 + ".");
function mapFn (value) {
calls.push({
args: arguments,
thisArg: this
});
return value * 2;
}
var result = Array.from(list, mapFn);
assert.sameValue(result.length, 3, 'result.length');
assert.sameValue(result[0], 82, 'result[0]');
assert.sameValue(result[1], 84, 'result[1]');
assert.sameValue(result[2], 86, 'result[2]');
assert.sameValue(calls.length, 3, 'calls.length');
assert.sameValue(calls[0].args.length, 2, 'calls[0].args.length');
assert.sameValue(calls[0].args[0], 41, 'calls[0].args[0]');
assert.sameValue(calls[0].args[1], 0, 'calls[0].args[1]');
assert.sameValue(calls[0].thisArg, undefined, 'calls[0].thisArg');
assert.sameValue(calls[1].args.length, 2, 'calls[1].args.length');
assert.sameValue(calls[1].args[0], 42, 'calls[1].args[0]');
assert.sameValue(calls[1].args[1], 1, 'calls[1].args[1]');
assert.sameValue(calls[1].thisArg, undefined, 'calls[1].thisArg');
assert.sameValue(calls[2].args.length, 2, 'calls[2].args.length');
assert.sameValue(calls[2].args[0], 43, 'calls[2].args[0]');
assert.sameValue(calls[2].args[1], 2, 'calls[2].args[1]');
assert.sameValue(calls[2].thisArg, undefined, 'calls[2].thisArg');

View File

@ -2,22 +2,66 @@
// 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
description: Calling from with a valid map function with thisArg
info: >
22.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] )
...
10. Let len be ToLength(Get(arrayLike, "length")).
11. ReturnIfAbrupt(len).
12. If IsConstructor(C) is true, then
a. Let A be Construct(C, «len»).
13. Else,
b. Let A be ArrayCreate(len).
14. ReturnIfAbrupt(A).
15. Let k be 0.
16. Repeat, while k < len
a. Let Pk be ToString(k).
b. Let kValue be Get(arrayLike, Pk).
c. ReturnIfAbrupt(kValue).
d. If mapping is true, then
i. Let mappedValue be Call(mapfn, T, «kValue, k»).
...
---*/
var array = [ 0, 1, -2, 4, -8, 16 ];
var arrayIndex = -1;
function mapFn (value, k) {
var list = {
'0': 41,
'1': 42,
'2': 43,
length: 3
};
var calls = [];
var thisArg = {};
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;
function mapFn (value) {
calls.push({
args: arguments,
thisArg: this
});
return value * 2;
}
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 + ".");
}
var result = Array.from(list, mapFn, thisArg);
assert.sameValue(result.length, 3, 'result.length');
assert.sameValue(result[0], 82, 'result[0]');
assert.sameValue(result[1], 84, 'result[1]');
assert.sameValue(result[2], 86, 'result[2]');
assert.sameValue(calls.length, 3, 'calls.length');
assert.sameValue(calls[0].args.length, 2, 'calls[0].args.length');
assert.sameValue(calls[0].args[0], 41, 'calls[0].args[0]');
assert.sameValue(calls[0].args[1], 0, 'calls[0].args[1]');
assert.sameValue(calls[0].thisArg, thisArg, 'calls[0].thisArg');
assert.sameValue(calls[1].args.length, 2, 'calls[1].args.length');
assert.sameValue(calls[1].args[0], 42, 'calls[1].args[0]');
assert.sameValue(calls[1].args[1], 1, 'calls[1].args[1]');
assert.sameValue(calls[1].thisArg, thisArg, 'calls[1].thisArg');
assert.sameValue(calls[2].args.length, 2, 'calls[2].args.length');
assert.sameValue(calls[2].args[0], 43, 'calls[2].args[0]');
assert.sameValue(calls[2].args[1], 2, 'calls[2].args[1]');
assert.sameValue(calls[2].thisArg, thisArg, 'calls[2].thisArg');

View File

@ -1,13 +0,0 @@
// 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,23 @@
// 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, 'foo', , Infinity];
var result = Array.from(array);
assert.sameValue(result.length, 4, 'result.length');
assert.sameValue(result[0], 0, 'result[0]');
assert.sameValue(result[1], 'foo', 'result[1]');
assert.sameValue(result[2], undefined, 'result[2]');
assert.sameValue(result[3], Infinity, 'result[3]');
assert.notSameValue(
result, array,
'result is not the object from items argument'
);
assert(result instanceof Array, 'result instanceof Array');

View File

@ -0,0 +1,18 @@
// Copyright (c) 2014 Hank Yates. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.2.1_T1
description: Testing Array.from when passed a String
author: Hank Yates (hankyates@gmail.com)
includes: [runTestCase.js]
---*/
var arrLikeSource = 'Test';
var result = Array.from(arrLikeSource);
assert.sameValue(result.length, 4, 'result.length');
assert.sameValue(result[0], 'T', 'result[0]');
assert.sameValue(result[1], 'e', 'result[1]');
assert.sameValue(result[2], 's', 'result[2]');
assert.sameValue(result[3], 't', 'result[3]');

View File

@ -0,0 +1,19 @@
// Copyright 2015 Leonardo Balter. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
es6id: 22.1.2.1
description: Return empty array if items argument is an ArrayBuffer
info: >
22.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] )
...
4. Let usingIterator be GetMethod(items, @@iterator).
5. ReturnIfAbrupt(usingIterator).
...
---*/
var arrayBuffer = new ArrayBuffer(7);
var result = Array.from(arrayBuffer);
assert.sameValue(result.length, 0);

View File

@ -0,0 +1,17 @@
// Copyright 2015 Leonardo Balter. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
es6id: 22.1.2.1
description: Throws a TypeError if items argument is null
info: >
22.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] )
...
4. Let usingIterator be GetMethod(items, @@iterator).
5. ReturnIfAbrupt(usingIterator).
...
---*/
assert.throws(TypeError, function() {
Array.from(null);
});

View File

@ -1,25 +0,0 @@
// 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

@ -1,25 +0,0 @@
// 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

@ -1,9 +0,0 @@
// 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

@ -1,11 +0,0 @@
// 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,34 @@
// Copyright 2015 Leonardo Balter. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
es6id: 22.1.2.1
description: Throws a TypeError if mapFn is not callable
info: >
22.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] )
...
2. If mapfn is undefined, let mapping be false.
3. else
a. If IsCallable(mapfn) is false, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
Array.from([], null);
});
assert.throws(TypeError, function() {
Array.from([], {});
});
assert.throws(TypeError, function() {
Array.from([], 'string');
});
assert.throws(TypeError, function() {
Array.from([], true);
});
assert.throws(TypeError, function() {
Array.from([], 42);
});

View File

@ -0,0 +1,20 @@
// Copyright 2015 Leonardo Balter. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
es6id: 22.1.2.1
description: Throws a TypeError if mapFn is not callable (Symbol)
info: >
22.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] )
...
2. If mapfn is undefined, let mapping be false.
3. else
a. If IsCallable(mapfn) is false, throw a TypeError exception.
...
features:
- Symbol
---*/
assert.throws(TypeError, function() {
Array.from([], Symbol('1'));
});

View File

@ -1,29 +0,0 @@
// 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

@ -1,13 +0,0 @@
// 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

@ -1,24 +0,0 @@
// 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

@ -1,22 +0,0 @@
// 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 + ".");
}

View File

@ -0,0 +1,11 @@
// Copyright 2015 Leonardo Balter. All rights reserved.
// This code is governed by the license found in the LICENSE file.
/*---
es6id: 22.1.2.1
description: Does not throw if this is null
---*/
var result = Array.from.call(null, []);
assert(result instanceof Array, 'Does not throw if this is null');
assert.sameValue(result.length, 0, 'result.length');