Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Array.prototype.includes tests #95

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions harness/testTypedArrays.js
@@ -0,0 +1,21 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing required frontmatter description tag.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, sorry for the confusion there, I was scanning through all of the files quickly. I'll update my main comment accordingly.

function testTypedArrays(callback) {
[
Uint8Array,
Int8Array,
Uint16Array,
Int16Array,
Uint32Array,
Int32Array,
Uint8ClampedArray,
Float32Array,
Float64Array
]
.forEach(callback);
}

testTypedArrays.floatOnly = function (callback) {
[Float32Array, Float64Array].forEach(callback);
};
5 changes: 5 additions & 0 deletions test/built-ins/Array/prototype/Symbol.unscopables/value.js
Expand Up @@ -48,6 +48,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
19 changes: 19 additions & 0 deletions test/built-ins/Array/prototype/includes/adding-getter.js
@@ -0,0 +1,19 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Array.prototype.includes sees a new element added by a getter that is hit during iteration
author: Domenic Denicola
---*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


var arrayLike = {
length: 5,
0: 'a',
get 1() {
this[2] = 'c';
return 'b';
}
};

assert(Array.prototype.includes.call(arrayLike, 'c'),
'Expected array-like to contain "c", which was added by the getter for the 1st element');
17 changes: 17 additions & 0 deletions test/built-ins/Array/prototype/includes/array-like.js
@@ -0,0 +1,17 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Array.prototype.includes works on array-like objects
author: Domenic Denicola
---*/

var arrayLike1 = { length: 5, 0: "a", 1: "b" };

assert.sameValue(Array.prototype.includes.call(arrayLike1, "a"), true, 'Expected array-like to contain "a"');
assert.sameValue(Array.prototype.includes.call(arrayLike1, "c"), false, 'Expected array-like not to contain "c"');

var arrayLike2 = { length: 2, 0: "a", 1: "b", 2: "c" };

assert.sameValue(Array.prototype.includes.call(arrayLike2, "b"), true, 'Expected array-like to contain "b"');
assert.sameValue(Array.prototype.includes.call(arrayLike2, "c"), false, 'Expected array-like to not contain "c"');
14 changes: 14 additions & 0 deletions test/built-ins/Array/prototype/includes/exception-bad-this.js
@@ -0,0 +1,14 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Array.prototype.includes should fail if used on a null or undefined this
---*/

assert.throws(TypeError, function () {
Array.prototype.includes.call(null, 'a');
});

assert.throws(TypeError, function () {
Array.prototype.includes.call(undefined, 'a');
});
@@ -0,0 +1,21 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Array.prototype.includes should terminate if getting an index throws an exception
includes: [Test262Error.js]
---*/

var trappedZero = {
length: 2,
get 0() {
throw new Test262Error('This error should be re-thrown');
},
get 1() {
$ERROR('Should not try to get the first element');
}
};

assert.throws(Test262Error, function () {
Array.prototype.includes.call(trappedZero, 'a');
});
@@ -0,0 +1,17 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Array.prototype.includes should terminate if ToNumber ends up being called on a symbol fromIndex
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although not consistently enforced until mid-this-year, the frontmatter needs to include features: [Symbol]

---*/

var trappedZero = {
length: 1,
get 0() {
$ERROR('Should not try to get the zeroth element');
}
};

assert.throws(TypeError, function () {
Array.prototype.includes.call(trappedZero, 'a', Symbol());
});
@@ -0,0 +1,24 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Array.prototype.includes should terminate if an exception occurs converting the fromIndex to a number
includes: [Test262Error.js]
---*/

var fromIndex = {
valueOf: function () {
throw new Test262Error('This error should be re-thrown');
}
};

var trappedZero = {
length: 1,
get 0() {
$ERROR('Should not try to get the zeroth element');
}
};

assert.throws(Test262Error, function () {
Array.prototype.includes.call(trappedZero, 'a', fromIndex);
});
26 changes: 26 additions & 0 deletions test/built-ins/Array/prototype/includes/exception-get-length.js
@@ -0,0 +1,26 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Array.prototype.includes should terminate if an exception occurs getting the length
includes: [Test262Error.js]
---*/

var fromIndexTrap = {
valueOf: function () {
$ERROR('Should not try to call ToInteger on valueOf');
}
}

var throwingLength = {
get length() {
throw new Test262Error('This error should be re-thrown');
},
get 0() {
$ERROR('Should not try to get the zeroth element');
}
};

assert.throws(Test262Error, function () {
Array.prototype.includes.call(throwingLength, 'a', fromIndexTrap);
});
@@ -0,0 +1,23 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Array.prototype.includes should terminate if ToLength ends up being called on a symbol length
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to include features: [Symbol]

---*/

var fromIndexTrap = {
valueOf: function () {
$ERROR('Should not try to call ToInteger on valueOf');
}
};

var badLength = {
length: Symbol(),
get 0() {
$ERROR('Should not try to get the zeroth element');
}
};

assert.throws(TypeError, function () {
Array.prototype.includes.call(badLength, 'a', fromIndexTrap);
});
@@ -0,0 +1,28 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Array.prototype.includes should terminate if an exception occurs converting the length to a number
includes: [Test262Error.js]
---*/

var fromIndexTrap = {
valueOf: function () {
$ERROR('Should not try to call ToInteger on valueOf');
}
};

var badLength = {
length: {
valueOf: function () {
throw new Test262Error('This error should be re-thrown');
}
},
get 0() {
$ERROR('Should not try to get the zeroth element');
}
};

assert.throws(Test262Error, function () {
Array.prototype.includes.call(badLength, 'a', fromIndexTrap);
});
24 changes: 24 additions & 0 deletions test/built-ins/Array/prototype/includes/from-index-default.js
@@ -0,0 +1,24 @@
// Copyright (C) 2014 Robert Kowalski. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
Array.prototype.includes should search the whole array, as the optional second argument fromIndex defaults to 0
author: Robert Kowalski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this info correct? (also the same name is on the first line)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes; why would it not be?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a safe check. We've had other PRs with wrong authorship info.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes; why would it not be?

Because the name shown is not the name of the author of this commit—that's not an unreasonable inquiry.

@bterlson Do we have confirmation that the contributing author has signed http://tc39.github.io/test262-cla/ ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: I reviewed the CLA responses and there is no "Robert Kowalski". Please have the author sign http://tc39.github.io/test262-cla/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

submitted! :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@robertkowalski confirmed—thank you very much!

---*/

assert([10, 11].includes(10), 'Expected that the whole array was searched');
assert([10, 11].includes(11), 'Expected that the whole array was searched');

var arrayLike = {
length: 2,
get 0() {
return '1';
},
get 1() {
return '2';
}
};

assert(Array.prototype.includes.call(arrayLike, '1'), 'Expected that the whole array-like was searched');
assert(Array.prototype.includes.call(arrayLike, '2'), 'Expected that the whole array-like was searched');
@@ -0,0 +1,25 @@
// Copyright (C) 2014 Robert Kowalski. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Array.prototype.includes returns false if fromIndex is greater or equal to the length of the array
author: Robert Kowalski
---*/

assert.sameValue([1, 2].includes(2, 3), false, 'Expected that the array was not searched');
assert.sameValue([1, 2].includes(2, 2), false, 'Expected that the array was not searched');

var arrayLikeWithTrap = {
length: 2,
get 0() {
$ERROR('Getter for 0 was called');
},
get 1() {
$ERROR('Getter for 1 was called');
}
};

assert.sameValue(Array.prototype.includes.call(arrayLikeWithTrap, 'c', 2), false,
'Expected that the array-like was not searched');
assert.sameValue(Array.prototype.includes.call(arrayLikeWithTrap, 'c', 3), false,
'Expected that the array-like was not searched');
@@ -0,0 +1,26 @@
// Copyright (C) 2014 Robert Kowalski. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
Array.prototype.includes searches the whole array if the computed index from the given negative fromIndex argument
is less than 0
author: Robert Kowalski
---*/

assert([1, 3].includes(1, -4), 'Expected that the whole array was searched');
assert([1, 3].includes(3, -4), 'Expected that the whole array was searched');

var arrayLike = {
length: 2,
0: 'a',
get 1() {
return 'b';
},
get '-1'() {
$ERROR('Should not try to get the element at index -1');
}
};

assert(Array.prototype.includes.call(arrayLike, 'a', -4), 'Expected that the whole array-like was searched');
assert(Array.prototype.includes.call(arrayLike, 'b', -4), 'Expected that the whole array-like was searched');
@@ -0,0 +1,26 @@
// Copyright (C) 2014 Robert Kowalski. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
Array.prototype.includes should use a negative value as the offset from the end of the array to compute fromIndex
author: Robert Kowalski
---*/

assert.sameValue([12, 13].includes(13, -1), true, 'Expected to find 13');
assert.sameValue([12, 13].includes(12, -1), false, 'Should not find 12');
assert.sameValue([12, 13].includes(12, -2), true, 'Should find 12');

var arrayLike = {
length: 2,
get 0() {
return 'a';
},
get 1() {
return 'b';
}
};

assert.sameValue(Array.prototype.includes.call(arrayLike, 'b', -1), true, 'Expected to find b');
assert.sameValue(Array.prototype.includes.call(arrayLike, 'a', -1), false, 'Should not find a');
assert.sameValue(Array.prototype.includes.call(arrayLike, 'a', -2), true, 'Should find a');
@@ -0,0 +1,55 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Array.prototype.includes converts its fromIndex parameter to an integer
author: Domenic Denicola
---*/

assert.sameValue(['a', 'b'].includes('a', 2.3), false, 'Expected that the array was not searched');

var arrayLikeWithTraps = {
length: 2,
get 0() {
$ERROR('Getter for 0 was called');
},
get 1() {
$ERROR('Getter for 1 was called');
}
};

assert.sameValue(Array.prototype.includes.call(arrayLikeWithTraps, 'c', 2.1), false,
'Expected the array to be searched for a fromIndex fractionally above the length');
assert.sameValue(Array.prototype.includes.call(arrayLikeWithTraps, 'c', +Infinity), false,
'Expected the array not to be searched for a fromIndex of +Infinity');

assert.sameValue(['a', 'b', 'c'].includes('a', -Infinity), true,
'Expected the array to be searched for a fromIndex of -Infinity');
assert.sameValue(['a', 'b', 'c'].includes('c', 2.9), true,
'Expected the fromIndex to be rounded down and thus the element to be found');
assert.sameValue(['a', 'b', 'c'].includes('c', NaN), true,
'Expected a fromIndex of NaN to be treated as 0 for an array');

var arrayLikeWithTrapAfterZero = {
length: 2,
get 0() {
return 'a';
},
get 1() {
$ERROR('Getter for 1 was called');
}
};

assert.sameValue(Array.prototype.includes.call(arrayLikeWithTrapAfterZero, 'a', NaN), true,
'Expected a fromIndex of NaN to be treated as 0 for an array-like');

var numberLike = { valueOf: function () { return 2; } };

assert.sameValue(['a', 'b', 'c'].includes('a', numberLike), false,
'Expected the element not to be found with the given number-like fromIndex');
assert.sameValue(['a', 'b', 'c'].includes('a', '2'), false,
'Expected the element not to be found with the given string fromIndex');
assert.sameValue(['a', 'b', 'c'].includes('c', numberLike), true,
'Expected the element to be found with the given number-like fromIndex');
assert.sameValue(['a', 'b', 'c'].includes('c', '2'), true,
'Expected the element to be found with the given string fromIndex');