forked from tc39/test262
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for Array.prototype.includes
- Loading branch information
1 parent
2cf968c
commit c7f9b12
Showing
26 changed files
with
918 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
test/built-ins/Array/prototype/includes/fromIndex-equal-or-greater-length-returns-false.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (C) 2016 the V8 project authors. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-array.prototype.includes | ||
description: Return false if fromIndex >= ArrayLength | ||
info: | | ||
22.1.3.11 Array.prototype.includes ( searchElement [ , fromIndex ] ) | ||
... | ||
4. Let n be ? ToInteger(fromIndex). (If fromIndex is undefined, this step | ||
produces the value 0.) | ||
5. If n ≥ 0, then | ||
a. Let k be n. | ||
... | ||
7. Repeat, while k < len | ||
... | ||
8. Return false. | ||
---*/ | ||
|
||
var sample = [ 7, 7, 7, 7 ]; | ||
assert.sameValue(sample.includes(7, 4), false, "length"); | ||
assert.sameValue(sample.includes(7, 5), false, "length + 1"); |
34 changes: 34 additions & 0 deletions
34
test/built-ins/Array/prototype/includes/fromIndex-infinity.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (C) 2016 the V8 project authors. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-array.prototype.includes | ||
description: handle Infinity values for fromIndex | ||
info: | | ||
22.1.3.11 Array.prototype.includes ( searchElement [ , fromIndex ] ) | ||
... | ||
4. Let n be ? ToInteger(fromIndex). (If fromIndex is undefined, this step | ||
produces the value 0.) | ||
5. If n ≥ 0, then | ||
a. Let k be n. | ||
6. Else n < 0, | ||
a. Let k be len + n. | ||
b. If k < 0, let k be 0. | ||
7. Repeat, while k < len | ||
... | ||
8. Return false. | ||
---*/ | ||
|
||
var sample = [42, 43, 43, 41]; | ||
|
||
assert.sameValue( | ||
sample.includes(43, Infinity), | ||
false, | ||
"includes(43, Infinity)" | ||
); | ||
assert.sameValue( | ||
sample.includes(43, -Infinity), | ||
true, | ||
"includes(43, -Infinity)" | ||
); |
21 changes: 21 additions & 0 deletions
21
test/built-ins/Array/prototype/includes/fromIndex-minus-zero.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (C) 2016 the V8 project authors. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-array.prototype.includes | ||
description: -0 fromIndex becomes 0 | ||
info: | | ||
22.1.3.11 Array.prototype.includes ( searchElement [ , fromIndex ] ) | ||
... | ||
5. If n ≥ 0, then | ||
a. Let k be n. | ||
... | ||
7. Repeat, while k < len | ||
... | ||
---*/ | ||
|
||
var sample = [42, 43]; | ||
assert.sameValue(sample.includes(42, -0), true, "-0 [0]"); | ||
assert.sameValue(sample.includes(43, -0), true, "-0 [1]"); | ||
assert.sameValue(sample.includes(44, -0), false, "-0 [2]"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (C) 2016 the V8 project authors. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-array.prototype.includes | ||
description: get array-like indexed properties | ||
info: | | ||
22.1.3.11 Array.prototype.includes ( searchElement [ , fromIndex ] ) | ||
... | ||
7. Repeat, while k < len | ||
a. Let elementK be the result of ? Get(O, ! ToString(k)). | ||
... | ||
includes: [compareArray.js] | ||
features: [Proxy] | ||
---*/ | ||
|
||
var calls; | ||
|
||
var obj = {}; | ||
|
||
var p = new Proxy(obj, { | ||
get: function(_, key) { | ||
calls.push(key); | ||
|
||
if (key === "length") { | ||
return 4; | ||
} | ||
|
||
return key * 10; | ||
} | ||
}); | ||
|
||
calls = []; | ||
assert.sameValue([].includes.call(p, 42), false); | ||
assert( | ||
compareArray(calls, ["length", "0", "1", "2", "3"]), | ||
"loops through all indexes" | ||
); | ||
|
||
calls = []; | ||
assert.sameValue([].includes.call(p, 10), true, "uses the returned value"); | ||
assert(compareArray(calls, ["length", "0", "1"]), "loops until value is found"); |
82 changes: 82 additions & 0 deletions
82
test/built-ins/Array/prototype/includes/length-boundaries.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (C) 2016 the V8 project authors. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-array.prototype.includes | ||
description: length boundaries from ToLength operation | ||
info: | | ||
22.1.3.11 Array.prototype.includes ( searchElement [ , fromIndex ] ) | ||
... | ||
2. Let len be ? ToLength(? Get(O, "length")). | ||
... | ||
7.1.15 ToLength ( argument ) | ||
1. Let len be ? ToInteger(argument). | ||
2. If len ≤ +0, return +0. | ||
3. If len is +∞, return 2**53-1. | ||
4. Return min(len, 2**53-1). | ||
---*/ | ||
|
||
var obj = { | ||
"0": "a", | ||
"1": "b", | ||
"9007199254740990": "c", // 2 ** 53 - 2 | ||
"9007199254740991": "d", // 2 ** 53 - 1 | ||
"9007199254740992": "e", // 2 ** 53 | ||
}; | ||
|
||
obj.length = -0; | ||
assert.sameValue([].includes.call(obj, "a"), false, "-0"); | ||
|
||
obj.length = -1; | ||
assert.sameValue([].includes.call(obj, "a"), false, "-1"); | ||
|
||
obj.length = -0.1; | ||
assert.sameValue([].includes.call(obj, "a"), false, "-0.1"); | ||
|
||
obj.length = -Infinity; | ||
assert.sameValue([].includes.call(obj, "a"), false, "-Infinity"); | ||
|
||
var fromIndex = 9007199254740990; | ||
|
||
obj.length = 9007199254740991; | ||
assert.sameValue( | ||
[].includes.call(obj, "c", fromIndex), | ||
true, | ||
"2**53-1, found value at 2**53-2" | ||
); | ||
|
||
obj.length = 9007199254740991; | ||
assert.sameValue( | ||
[].includes.call(obj, "d", fromIndex), | ||
false, | ||
"2**53-1, ignores indexes >= 2**53-1" | ||
); | ||
|
||
obj.length = 9007199254740992; | ||
assert.sameValue( | ||
[].includes.call(obj, "d", fromIndex), | ||
false, | ||
"2**53, ignores indexes >= 2**53-1" | ||
); | ||
|
||
obj.length = 9007199254740993; | ||
assert.sameValue( | ||
[].includes.call(obj, "d", fromIndex), | ||
false, | ||
"2**53+1, ignores indexes >= 2**53-1" | ||
); | ||
|
||
obj.length = Infinity; | ||
assert.sameValue( | ||
[].includes.call(obj, "c", fromIndex), | ||
true, | ||
"Infinity, found item" | ||
); | ||
assert.sameValue( | ||
[].includes.call(obj, "d", fromIndex), | ||
false, | ||
"Infinity, ignores indexes >= 2**53-1" | ||
); |
27 changes: 27 additions & 0 deletions
27
test/built-ins/Array/prototype/includes/length-zero-returns-false.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (C) 2016 the V8 project authors. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-array.prototype.includes | ||
description: Returns false if length is 0 | ||
info: | | ||
22.1.3.11 Array.prototype.includes ( searchElement [ , fromIndex ] ) | ||
... | ||
2. Let len be ? ToLength(? Get(O, "length")). | ||
3. If len is 0, return false. | ||
... | ||
---*/ | ||
|
||
var calls = 0; | ||
var fromIndex = { | ||
valueOf: function() { | ||
calls++; | ||
} | ||
}; | ||
|
||
var sample = []; | ||
assert.sameValue(sample.includes(0), false, "returns false"); | ||
assert.sameValue(sample.includes(), false, "returns false - no arg"); | ||
assert.sameValue(sample.includes(0, fromIndex), false, "using fromIndex"); | ||
assert.sameValue(calls, 0, "length is checked before ToInteger(fromIndex)"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (C) 2016 the V8 project authors. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-array.prototype.includes | ||
description: > | ||
Array.prototype.includes.length is 1. | ||
info: | | ||
Array.prototype.includes ( searchElement [ , fromIndex ] ) | ||
17 ECMAScript Standard Built-in Objects: | ||
Every built-in Function object, including constructors, has a length | ||
property whose value is an integer. Unless otherwise specified, this | ||
value is equal to the largest number of named arguments shown in the | ||
subclause headings for the function description, including optional | ||
parameters. However, rest parameters shown using the form “...name” | ||
are not included in the default argument count. | ||
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.prototype.includes.length, 1); | ||
|
||
verifyNotEnumerable(Array.prototype.includes, "length"); | ||
verifyNotWritable(Array.prototype.includes, "length"); | ||
verifyConfigurable(Array.prototype.includes, "length"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (C) 2016 the V8 project authors. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-array.prototype.includes | ||
description: > | ||
Array.prototype.includes.name is "includes". | ||
info: | | ||
Array.prototype.includes (searchElement [ , fromIndex ] ) | ||
17 ECMAScript Standard Built-in Objects: | ||
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, 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.prototype.includes.name, "includes"); | ||
|
||
verifyNotEnumerable(Array.prototype.includes, "name"); | ||
verifyNotWritable(Array.prototype.includes, "name"); | ||
verifyConfigurable(Array.prototype.includes, "name"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (C) 2016 the V8 project authors. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-array.prototype.includes | ||
description: no argument searches for a undefined value | ||
info: | | ||
22.1.3.11 Array.prototype.includes ( searchElement [ , fromIndex ] ) | ||
... | ||
7. Repeat, while k < len | ||
a. Let elementK be the result of ? Get(O, ! ToString(k)). | ||
b. If SameValueZero(searchElement, elementK) is true, return true. | ||
c. Increase k by 1. | ||
... | ||
---*/ | ||
|
||
assert.sameValue([0].includes(), false, "[0].includes()"); | ||
assert.sameValue([undefined].includes(), true, "[undefined].includes()"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (C) 2016 the V8 project authors. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-array.prototype.includes | ||
description: > | ||
"includes" property of Array.prototype | ||
info: | | ||
ES6 section 17: Every other data property described in clauses 18 through 26 | ||
and in Annex B.2 has the attributes { [[Writable]]: true, | ||
[[Enumerable]]: false, [[Configurable]]: true } unless otherwise specified. | ||
includes: [propertyHelper.js] | ||
---*/ | ||
|
||
verifyNotEnumerable(Array.prototype, "includes"); | ||
verifyWritable(Array.prototype, "includes"); | ||
verifyConfigurable(Array.prototype, "includes"); |
Oops, something went wrong.