Skip to content

Commit

Permalink
Add tests for Array.prototype.includes
Browse files Browse the repository at this point in the history
  • Loading branch information
leobalter authored and jugglinmike committed Jun 1, 2016
1 parent 2cf968c commit c7f9b12
Show file tree
Hide file tree
Showing 26 changed files with 918 additions and 12 deletions.
32 changes: 20 additions & 12 deletions test/built-ins/Array/prototype/Symbol.unscopables/value.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
// Copyright (C) 2015 Mike Pennisi. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.3.31
esid: sec-array.prototype-@@unscopables
description: >
Initial value of `Symbol.unscopables` property
info: >
1. Let blackList be ObjectCreate(null).
2. Perform CreateDataProperty(blackList, "copyWithin", true).
3. Perform CreateDataProperty(blackList, "entries", true).
4. Perform CreateDataProperty(blackList, "fill", true).
5. Perform CreateDataProperty(blackList, "find", true).
6. Perform CreateDataProperty(blackList, "findIndex", true).
7. Perform CreateDataProperty(blackList, "keys", true).
8. Perform CreateDataProperty(blackList, "values", true).
9. Assert: Each of the above calls will return true.
10. Return blackList.
info: |
22.1.3.32 Array.prototype [ @@unscopables ]
1. Let unscopableList be ObjectCreate(null).
2. Perform CreateDataProperty(unscopableList, "copyWithin", true).
3. Perform CreateDataProperty(unscopableList, "entries", true).
4. Perform CreateDataProperty(unscopableList, "fill", true).
5. Perform CreateDataProperty(unscopableList, "find", true).
6. Perform CreateDataProperty(unscopableList, "findIndex", true).
7. Perform CreateDataProperty(unscopableList, "includes", true).
8. Perform CreateDataProperty(unscopableList, "keys", true).
9. Perform CreateDataProperty(unscopableList, "values", true).
10. Assert: Each of the above calls will return true.
11. Return unscopableList.
includes: [propertyHelper.js]
features: [Symbol.unscopables]
---*/
Expand Down Expand Up @@ -48,6 +51,11 @@ verifyEnumerable(unscopables, 'findIndex');
verifyWritable(unscopables, 'findIndex');
verifyConfigurable(unscopables, 'findIndex');

assert.sameValue(unscopables.includes, true, '`includes` property value');
verifyEnumerable(unscopables, 'includes');
verifyWritable(unscopables, 'includes');
verifyConfigurable(unscopables, 'includes');

assert.sameValue(unscopables.keys, true, '`keys` property value');
verifyEnumerable(unscopables, 'keys');
verifyWritable(unscopables, 'keys');
Expand Down
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 test/built-ins/Array/prototype/includes/fromIndex-infinity.js
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 test/built-ins/Array/prototype/includes/fromIndex-minus-zero.js
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]");
43 changes: 43 additions & 0 deletions test/built-ins/Array/prototype/includes/get-prop.js
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 test/built-ins/Array/prototype/includes/length-boundaries.js
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"
);
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)");
29 changes: 29 additions & 0 deletions test/built-ins/Array/prototype/includes/length.js
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");
26 changes: 26 additions & 0 deletions test/built-ins/Array/prototype/includes/name.js
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");
19 changes: 19 additions & 0 deletions test/built-ins/Array/prototype/includes/no-arg.js
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()");
16 changes: 16 additions & 0 deletions test/built-ins/Array/prototype/includes/prop-desc.js
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");
Loading

0 comments on commit c7f9b12

Please sign in to comment.