Skip to content

Commit

Permalink
Add Array.prototype.contains tests
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Sep 18, 2014
1 parent 9bd6686 commit d87401e
Show file tree
Hide file tree
Showing 29 changed files with 898 additions and 0 deletions.
@@ -0,0 +1,22 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

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

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

var result = Array.prototype.contains.call(arrayLike, 'c');

if (result !== true) {
$ERROR('Expected array-like to contain "c", which was added by the getter for the 1st element');
}
@@ -0,0 +1,31 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

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

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

var result1 = Array.prototype.contains.call(arrayLike1, "a");
if (result1 !== true) {
$ERROR('Expected array-like to contain "a"');
}

var result2 = Array.prototype.contains.call(arrayLike1, "c");
if (result2 !== false) {
$ERROR('Expected array-like not to contain "c"');
}

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

var result3 = Array.prototype.contains.call(arrayLike2, "b");
if (result3 !== true) {
$ERROR('Expected array-like to contain "b"');
}

var result4 = Array.prototype.contains.call(arrayLike2, "c");
if (result4 !== false) {
$ERROR('Expected array-like to not contain "c"');
}
@@ -0,0 +1,20 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Array.prototype.contains should terminate if getting an index throws an exception
negative: Test262Error
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');
}
};

Array.prototype.contains.call(trappedZero, 'a');
@@ -0,0 +1,16 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Array.prototype.contains should terminate if ToNumber ends up being called on a symbol fromIndex
negative: TypeError
---*/

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

Array.prototype.contains.call(trappedZero, 'a', Symbol());
@@ -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.contains should terminate if an exception occurs converting the fromIndex to a number
negative: Test262Error
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');
}
};

Array.prototype.contains.call(trappedZero, 'a', fromIndex);
@@ -0,0 +1,25 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Array.prototype.contains should terminate if an exception occurs getting the length
negative: Test262Error
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');
}
};

Array.prototype.contains.call(throwingLength, 'a', fromIndexTrap);
@@ -0,0 +1,22 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Array.prototype.contains should terminate if ToLength ends up being called on a symbol length
negative: TypeError
---*/

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');
}
};

Array.prototype.contains.call(badLength, 'a', fromIndexTrap);
@@ -0,0 +1,27 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Array.prototype.contains should terminate if an exception occurs converting the length to a number
negative: Test262Error
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');
}
};

Array.prototype.contains.call(badLength, 'a', fromIndexTrap);
@@ -0,0 +1,34 @@
// Copyright (C) 2014 Robert Kowalski. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

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

if ([10, 11].contains(10) !== true) {
$ERROR('Expected that the whole array was searched');
}

if ([10, 11].contains(11) !== true) {
$ERROR('Expected that the whole array was searched');
}

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

if (Array.prototype.contains.call(arrayLike, '1') !== true) {
$ERROR('Expected that the whole array-like was searched');
}

if (Array.prototype.contains.call(arrayLike, '2') !== true) {
$ERROR('Expected that the whole array-like was searched');
}
@@ -0,0 +1,34 @@
// Copyright (C) 2014 Robert Kowalski. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

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

if ([1, 2].contains(2, 3) !== false) {
$ERROR('Expected that the array was not searched');
}

if ([1, 2].contains(2, 2) !== false) {
$ERROR('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');
}
};

if (Array.prototype.contains.call(arrayLikeWithTrap, 'c', 2) !== false) {
$ERROR('Expected that the array was not searched');
}

if (Array.prototype.contains.call(arrayLikeWithTrap, 'c', 3) !== false) {
$ERROR('Expected that the array was not searched');
}
@@ -0,0 +1,33 @@
// Copyright (C) 2014 Robert Kowalski. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

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

if ([1, 3].contains(1, -4) !== true) {
$ERROR('Expected that the whole array was searched');
}

if ([1, 3].contains(3, -4) !== true) {
$ERROR('Expected that the whole array was searched');
}

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

if (Array.prototype.contains.call(arrayLike, 'a', -4) !== true) {
$ERROR('Expected that the whole array-like was searched');
}

if (Array.prototype.contains.call(arrayLike, 'b', -4) !== true) {
$ERROR('Expected that the whole array-like was searched');
}
@@ -0,0 +1,42 @@
// Copyright (C) 2014 Robert Kowalski. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

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

if ([12, 13].contains(13, -1) !== true) {
$ERROR('Expected to find 13');
}

if ([12, 13].contains(12, -1) !== false) {
$ERROR('Should not find 12');
}

if ([12, 13].contains(12, -2) !== true) {
$ERROR('Should find 12');
}

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

if (Array.prototype.contains.call(arrayLike, 'b', -1) !== true) {
$ERROR('Expected to find b');
}

if (Array.prototype.contains.call(arrayLike, 'a', -1) !== false) {
$ERROR('Should not find a');
}

if (Array.prototype.contains.call(arrayLike, 'a', -2) !== true) {
$ERROR('Should find a');
}

0 comments on commit d87401e

Please sign in to comment.