From e3cf0acb1c0fa564f7e9b907fb17f913fadadf1f Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 3 Aug 2015 18:20:24 +0800 Subject: [PATCH] update tests for Array.from --- .../Array/from/Array.from-descriptor.js | 13 +++++++ test/built-ins/Array/from/Array.from-name.js | 30 +++++++++++++++ test/built-ins/Array/from/Array.from_arity.js | 17 +++++++-- .../Array/from/argument-passed-null.js | 9 +++++ .../from/calling-from-valid-1-noStrict.js | 25 +++++++++++++ .../from/calling-from-valid-1-onlyStrict.js | 24 ++++++++++++ .../Array/from/calling-from-valid-2.js | 23 ++++++++++++ .../Array/from/create-typedarray-from.js | 13 +++++++ .../Array/from/elements-added-after.js | 37 +++++++++++++++++++ .../Array/from/elements-deleted-after.js | 31 ++++++++++++++++ .../Array/from/elements-updated-after.js | 26 +++++++++++++ .../Array/from/map-called-passing.js | 25 +++++++++++++ .../Array/from/mapfn-attached-array.js | 25 +++++++++++++ .../Array/from/mapfn-invalid-typeerror-1.js | 9 +++++ .../Array/from/mapfn-invalid-typeerror-2.js | 11 ++++++ .../Array/from/mapfn-returns-non-integer.js | 29 +++++++++++++++ .../Array/from/mapfn-throws-exception.js | 14 +++++++ .../Array/from/passing-array-buffer.js | 13 +++++++ .../Array/from/passing-valid-array.js | 13 +++++++ .../Array/from/source-array-boundary.js | 26 +++++++++++++ .../Array/from/source-object-constructor.js | 10 +++++ .../Array/from/source-object-iterator-1.js | 28 ++++++++++++++ .../Array/from/source-object-iterator-2.js | 35 ++++++++++++++++++ .../Array/from/source-object-length.js | 24 ++++++++++++ .../Array/from/source-object-missing.js | 21 +++++++++++ .../Array/from/source-object-without.js | 17 +++++++++ .../from/source-sparse-array-noStirct.js | 24 ++++++++++++ .../from/source-sparse-array-onlyStrict.js | 22 +++++++++++ 28 files changed, 591 insertions(+), 3 deletions(-) create mode 100644 test/built-ins/Array/from/Array.from-descriptor.js create mode 100644 test/built-ins/Array/from/Array.from-name.js create mode 100644 test/built-ins/Array/from/argument-passed-null.js create mode 100644 test/built-ins/Array/from/calling-from-valid-1-noStrict.js create mode 100644 test/built-ins/Array/from/calling-from-valid-1-onlyStrict.js create mode 100644 test/built-ins/Array/from/calling-from-valid-2.js create mode 100644 test/built-ins/Array/from/create-typedarray-from.js create mode 100644 test/built-ins/Array/from/elements-added-after.js create mode 100644 test/built-ins/Array/from/elements-deleted-after.js create mode 100644 test/built-ins/Array/from/elements-updated-after.js create mode 100644 test/built-ins/Array/from/map-called-passing.js create mode 100644 test/built-ins/Array/from/mapfn-attached-array.js create mode 100644 test/built-ins/Array/from/mapfn-invalid-typeerror-1.js create mode 100644 test/built-ins/Array/from/mapfn-invalid-typeerror-2.js create mode 100644 test/built-ins/Array/from/mapfn-returns-non-integer.js create mode 100644 test/built-ins/Array/from/mapfn-throws-exception.js create mode 100644 test/built-ins/Array/from/passing-array-buffer.js create mode 100644 test/built-ins/Array/from/passing-valid-array.js create mode 100644 test/built-ins/Array/from/source-array-boundary.js create mode 100644 test/built-ins/Array/from/source-object-constructor.js create mode 100644 test/built-ins/Array/from/source-object-iterator-1.js create mode 100644 test/built-ins/Array/from/source-object-iterator-2.js create mode 100644 test/built-ins/Array/from/source-object-length.js create mode 100644 test/built-ins/Array/from/source-object-missing.js create mode 100644 test/built-ins/Array/from/source-object-without.js create mode 100644 test/built-ins/Array/from/source-sparse-array-noStirct.js create mode 100644 test/built-ins/Array/from/source-sparse-array-onlyStrict.js diff --git a/test/built-ins/Array/from/Array.from-descriptor.js b/test/built-ins/Array/from/Array.from-descriptor.js new file mode 100644 index 0000000000..7fcb331e09 --- /dev/null +++ b/test/built-ins/Array/from/Array.from-descriptor.js @@ -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"); diff --git a/test/built-ins/Array/from/Array.from-name.js b/test/built-ins/Array/from/Array.from-name.js new file mode 100644 index 0000000000..cb05ac2b5d --- /dev/null +++ b/test/built-ins/Array/from/Array.from-name.js @@ -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'); diff --git a/test/built-ins/Array/from/Array.from_arity.js b/test/built-ins/Array/from/Array.from_arity.js index 354f281ba4..0e5ef26bf8 100644 --- a/test/built-ins/Array/from/Array.from_arity.js +++ b/test/built-ins/Array/from/Array.from_arity.js @@ -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'); diff --git a/test/built-ins/Array/from/argument-passed-null.js b/test/built-ins/Array/from/argument-passed-null.js new file mode 100644 index 0000000000..c35cc79be2 --- /dev/null +++ b/test/built-ins/Array/from/argument-passed-null.js @@ -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);}); diff --git a/test/built-ins/Array/from/calling-from-valid-1-noStrict.js b/test/built-ins/Array/from/calling-from-valid-1-noStrict.js new file mode 100644 index 0000000000..6b94b2de39 --- /dev/null +++ b/test/built-ins/Array/from/calling-from-valid-1-noStrict.js @@ -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 + "."); +} diff --git a/test/built-ins/Array/from/calling-from-valid-1-onlyStrict.js b/test/built-ins/Array/from/calling-from-valid-1-onlyStrict.js new file mode 100644 index 0000000000..64994298ba --- /dev/null +++ b/test/built-ins/Array/from/calling-from-valid-1-onlyStrict.js @@ -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 + "."); +} diff --git a/test/built-ins/Array/from/calling-from-valid-2.js b/test/built-ins/Array/from/calling-from-valid-2.js new file mode 100644 index 0000000000..dad2398127 --- /dev/null +++ b/test/built-ins/Array/from/calling-from-valid-2.js @@ -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 + "."); +} diff --git a/test/built-ins/Array/from/create-typedarray-from.js b/test/built-ins/Array/from/create-typedarray-from.js new file mode 100644 index 0000000000..889211260a --- /dev/null +++ b/test/built-ins/Array/from/create-typedarray-from.js @@ -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 + "."); +} diff --git a/test/built-ins/Array/from/elements-added-after.js b/test/built-ins/Array/from/elements-added-after.js new file mode 100644 index 0000000000..52fa034899 --- /dev/null +++ b/test/built-ins/Array/from/elements-added-after.js @@ -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 + "."); +} diff --git a/test/built-ins/Array/from/elements-deleted-after.js b/test/built-ins/Array/from/elements-deleted-after.js new file mode 100644 index 0000000000..6edd6cd521 --- /dev/null +++ b/test/built-ins/Array/from/elements-deleted-after.js @@ -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 + "."); +} diff --git a/test/built-ins/Array/from/elements-updated-after.js b/test/built-ins/Array/from/elements-updated-after.js new file mode 100644 index 0000000000..7f475c6c08 --- /dev/null +++ b/test/built-ins/Array/from/elements-updated-after.js @@ -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."); +} diff --git a/test/built-ins/Array/from/map-called-passing.js b/test/built-ins/Array/from/map-called-passing.js new file mode 100644 index 0000000000..6a8a6e2a8d --- /dev/null +++ b/test/built-ins/Array/from/map-called-passing.js @@ -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."); +} diff --git a/test/built-ins/Array/from/mapfn-attached-array.js b/test/built-ins/Array/from/mapfn-attached-array.js new file mode 100644 index 0000000000..9053368960 --- /dev/null +++ b/test/built-ins/Array/from/mapfn-attached-array.js @@ -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 + "."); +} diff --git a/test/built-ins/Array/from/mapfn-invalid-typeerror-1.js b/test/built-ins/Array/from/mapfn-invalid-typeerror-1.js new file mode 100644 index 0000000000..1a618f95eb --- /dev/null +++ b/test/built-ins/Array/from/mapfn-invalid-typeerror-1.js @@ -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);}); diff --git a/test/built-ins/Array/from/mapfn-invalid-typeerror-2.js b/test/built-ins/Array/from/mapfn-invalid-typeerror-2.js new file mode 100644 index 0000000000..77aa5e6c3b --- /dev/null +++ b/test/built-ins/Array/from/mapfn-invalid-typeerror-2.js @@ -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);}); diff --git a/test/built-ins/Array/from/mapfn-returns-non-integer.js b/test/built-ins/Array/from/mapfn-returns-non-integer.js new file mode 100644 index 0000000000..51cf01ddd4 --- /dev/null +++ b/test/built-ins/Array/from/mapfn-returns-non-integer.js @@ -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 + "."); +} diff --git a/test/built-ins/Array/from/mapfn-throws-exception.js b/test/built-ins/Array/from/mapfn-throws-exception.js new file mode 100644 index 0000000000..0e7fce659c --- /dev/null +++ b/test/built-ins/Array/from/mapfn-throws-exception.js @@ -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);}); diff --git a/test/built-ins/Array/from/passing-array-buffer.js b/test/built-ins/Array/from/passing-array-buffer.js new file mode 100644 index 0000000000..b316fd799c --- /dev/null +++ b/test/built-ins/Array/from/passing-array-buffer.js @@ -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 + "."); +} diff --git a/test/built-ins/Array/from/passing-valid-array.js b/test/built-ins/Array/from/passing-valid-array.js new file mode 100644 index 0000000000..578828b134 --- /dev/null +++ b/test/built-ins/Array/from/passing-valid-array.js @@ -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 + "."); +} diff --git a/test/built-ins/Array/from/source-array-boundary.js b/test/built-ins/Array/from/source-array-boundary.js new file mode 100644 index 0000000000..09ce928a6e --- /dev/null +++ b/test/built-ins/Array/from/source-array-boundary.js @@ -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."); diff --git a/test/built-ins/Array/from/source-object-constructor.js b/test/built-ins/Array/from/source-object-constructor.js new file mode 100644 index 0000000000..2094766737 --- /dev/null +++ b/test/built-ins/Array/from/source-object-constructor.js @@ -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); diff --git a/test/built-ins/Array/from/source-object-iterator-1.js b/test/built-ins/Array/from/source-object-iterator-1.js new file mode 100644 index 0000000000..d128a36fe2 --- /dev/null +++ b/test/built-ins/Array/from/source-object-iterator-1.js @@ -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);}); diff --git a/test/built-ins/Array/from/source-object-iterator-2.js b/test/built-ins/Array/from/source-object-iterator-2.js new file mode 100644 index 0000000000..ff8e804045 --- /dev/null +++ b/test/built-ins/Array/from/source-object-iterator-2.js @@ -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 + "."); +} diff --git a/test/built-ins/Array/from/source-object-length.js b/test/built-ins/Array/from/source-object-length.js new file mode 100644 index 0000000000..b320affa8a --- /dev/null +++ b/test/built-ins/Array/from/source-object-length.js @@ -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 + "."); +} diff --git a/test/built-ins/Array/from/source-object-missing.js b/test/built-ins/Array/from/source-object-missing.js new file mode 100644 index 0000000000..fd2a4a9510 --- /dev/null +++ b/test/built-ins/Array/from/source-object-missing.js @@ -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 + "."); +} diff --git a/test/built-ins/Array/from/source-object-without.js b/test/built-ins/Array/from/source-object-without.js new file mode 100644 index 0000000000..2f6018ed7a --- /dev/null +++ b/test/built-ins/Array/from/source-object-without.js @@ -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."); diff --git a/test/built-ins/Array/from/source-sparse-array-noStirct.js b/test/built-ins/Array/from/source-sparse-array-noStirct.js new file mode 100644 index 0000000000..0bb093364e --- /dev/null +++ b/test/built-ins/Array/from/source-sparse-array-noStirct.js @@ -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 + "."); +} diff --git a/test/built-ins/Array/from/source-sparse-array-onlyStrict.js b/test/built-ins/Array/from/source-sparse-array-onlyStrict.js new file mode 100644 index 0000000000..8ad2fcc083 --- /dev/null +++ b/test/built-ins/Array/from/source-sparse-array-onlyStrict.js @@ -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 + "."); +}