Update tests for Array.prototype.findIndex

This commit is contained in:
Leonardo Balter 2015-07-22 18:07:12 -04:00
parent 88435c5201
commit 6a440ee616
31 changed files with 561 additions and 436 deletions

View File

@ -1,13 +0,0 @@
// Copyright (c) 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
---*/
assert.sameValue(1, Array.prototype.findIndex.length);

View File

@ -1,17 +0,0 @@
// Copyright (c) 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
---*/
var a = [21, 22, 23, 24];
assert.sameValue(a.findIndex(function() { return false; }), -1);
assert.sameValue(a.findIndex(function(val) { return 121 === val; }), -1);
assert.sameValue(a.findIndex(function() { return true; }), 0);
assert.sameValue(a.findIndex(function(val) { return 22 === val; }), 1);
assert.sameValue(a.findIndex(function(val) { return 23 === val; }), 2);
assert.sameValue(a.findIndex(function(val) { return 24 === val; }), 3);

View File

@ -1,24 +0,0 @@
// Copyright (c) 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
---*/
//
// Test predicate is called array.length times
//
(function() {
var a = [1, 2, 3, 4, 5];
var l = 0;
a.findIndex(function() {
l++;
return false;
});
assert.sameValue(a.length, l);
})();

View File

@ -1,107 +0,0 @@
// Copyright (c) 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
---*/
// Test exceptions
assert.throws(TypeError, function() {
Array.prototype.findIndex.call(null, function() { });
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call(undefined, function() { });
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply(null, function() { }, []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply(undefined, function() { }, []);
});
assert.throws(TypeError, function() {
[].findIndex(null);
});
assert.throws(TypeError, function() {
[].findIndex(undefined);
});
assert.throws(TypeError, function() {
[].findIndex(0);
});
assert.throws(TypeError, function() {
[].findIndex(true);
});
assert.throws(TypeError, function() {
[].findIndex(false);
});
assert.throws(TypeError, function() {
[].findIndex("");
});
assert.throws(TypeError, function() {
[].findIndex({});
});
assert.throws(TypeError, function() {
[].findIndex([]);
});
assert.throws(TypeError, function() {
[].findIndex(/\d+/);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call({}, null);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call({}, undefined);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call({}, 0);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call({}, true);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call({}, false);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call({}, "");
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call({}, {});
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call({}, []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call({}, /\d+/);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply({}, null, []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply({}, undefined, []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply({}, 0, []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply({}, true, []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply({}, false, []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply({}, "", []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply({}, {}, []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply({}, [], []);
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.apply({}, /\d+/, []);
});

View File

@ -1,42 +0,0 @@
// Copyright (c) 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
includes: [compareArray.js]
---*/
//
// Test Array.prototype.findIndex works with exotic object
//
var l = -1;
var o = -1;
var v = -1;
var k = -1;
var a = {
prop1: "val1",
prop2: "val2",
isValid: function() {
return this.prop1 === "val1" && this.prop2 === "val2";
}
};
Array.prototype.push.apply(a, [30, 31, 32]);
var index = Array.prototype.findIndex.call(a, function(val, key, obj) {
o = obj;
l = obj.length;
v = val;
k = key;
return !obj.isValid();
});
assert(compareArray(o, a));
assert.sameValue(l, 3);
assert.sameValue(v, 32);
assert.sameValue(k, 2);
assert.sameValue(index, -1);

View File

@ -1,23 +0,0 @@
// Copyright (c) 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
includes: [compareArray.js]
---*/
//
// Test array modifications
//
var a = [1, 2, 3];
a.findIndex(function(val) { a.push(val); return false; });
assert(compareArray(a, [1, 2, 3, 1, 2, 3]));
assert.sameValue(a.length, 6);
a = [1, 2, 3];
a.findIndex(function(val, key) { a[key] = ++val; return false; });
assert(compareArray(a, [2, 3, 4]));
assert.sameValue(a.length, 3);

View File

@ -1,32 +0,0 @@
// Copyright (c) 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
---*/
//
// Test predicate is not called when array is empty
//
var a = [];
var l = -1;
var o = -1;
var v = -1;
var k = -1;
a.findIndex(function(val, key, obj) {
o = obj;
l = obj.length;
v = val;
k = key;
return false;
});
assert.sameValue(-1, l);
assert.sameValue(-1, o);
assert.sameValue(-1, v);
assert.sameValue(-1, k);

View File

@ -1,34 +0,0 @@
// Copyright (c) 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
Test predicate is called with correct arguments
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
includes: [compareArray.js]
---*/
var a = ["b"];
var l = -1;
var o = -1;
var v = -1;
var k = -1;
var index = a.findIndex(function(val, key, obj) {
o = obj;
l = obj.length;
v = val;
k = key;
return false;
});
assert(compareArray(o, a));
assert.sameValue(l, a.length);
assert.sameValue(v, "b");
assert.sameValue(k, 0);
assert.sameValue(index, -1);

View File

@ -1,13 +0,0 @@
// Copyright (c) 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
In strict mode primitive value thisArg should not be coerced to an object.
flags: [onlyStrict]
---*/
var a = [];
[1, 2].findIndex(function() { a.push(this); }, "");
assert.sameValue(a[0], "");
assert.sameValue(a[1], a[0]);

View File

@ -1,17 +0,0 @@
// Copyright (c) 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
Create a new object in each function call when receiver is a
primitive value. See ECMA-262, Annex C.
flags: [noStrict]
---*/
var a = [];
[1, 2].findIndex(function() { a.push(this) }, "");
assert(a[0] !== a[1]);
var b = [];
[1, 2].findIndex(function() { b.push(this) }, 1);
assert(b[0] !== b[1]);

View File

@ -1,12 +0,0 @@
// Copyright (c) 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
Do not create a new object in each function call when receiver is a
non-primitive value. See ECMA-262, Annex C.
---*/
var a = [];
[1, 2].findIndex(function() { a.push(this) }, {});
assert.sameValue(a[1], a[0]);

View File

@ -1,42 +0,0 @@
// Copyright (c) 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
---*/
//
// Test thisArg
//
// Test String as a thisArg
var index = [1, 2, 3].findIndex(function(val, key) {
return this.charAt(Number(key)) === String(val);
}, "321");
assert.sameValue(index, 1);
// Test object as a thisArg
var thisArg = {
elementAt: function(key) {
return this[key];
}
};
Array.prototype.push.apply(thisArg, ["c", "b", "a"]);
index = ["a", "b", "c"].findIndex(function(val, key) {
return this.elementAt(key) === val;
}, thisArg);
assert.sameValue(index, 1);
// Check thisArg parameter does not change.
var a = [];
[1, 2].findIndex(function() { a.push(this) }, {});
assert.sameValue(a[1], a[0]);
// In strict mode primitive values should not be coerced to an object.
a = [];
[1, 2].findIndex(function() { 'use strict'; a.push(this); }, "");
assert.sameValue(a[0], "");
assert.sameValue(a[1], a[0]);

View File

@ -1,11 +0,0 @@
// Copyright (c) 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
Holes are not skipped
---*/
var count = 0;
[,,,,,].find(function() { count++; return false; });
assert.sameValue(count, 5);

View File

@ -1,49 +0,0 @@
// Copyright (c) 2013 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
The findIndex() method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
Test Array.prototype.findIndex works with String
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
---*/
var a = "abcd";
var l = -1;
var o = -1;
var v = -1;
var k = -1;
var index = Array.prototype.findIndex.call(a, function(val, key, obj) {
o = obj.toString();
l = obj.length;
v = val;
k = key;
return false;
});
assert.sameValue(o, a);
assert.sameValue(l, a.length);
assert.sameValue(v, "d");
assert.sameValue(k, 3);
assert.sameValue(index, -1);
index = Array.prototype.findIndex.apply(a, [function(val, key, obj) {
o = obj.toString();
l = obj.length;
v = val;
k = key;
return true;
}]);
assert.sameValue(o, a);
assert.sameValue(l, a.length);
assert.sameValue(v, "a");
assert.sameValue(k, 0);
assert.sameValue(index, 0);

View File

@ -0,0 +1,47 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
The range of elements processed is set before the first call to `predicate`.
info: >
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
6. If thisArg was supplied, let T be thisArg; else let T be undefined.
7. Let k be 0.
8. Repeat, while k < len
...
d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)).
...
---*/
var arr = ['Shoes', 'Car', 'Bike'];
var results = [];
arr.findIndex(function(kValue) {
if (results.length === 0) {
arr.splice(1, 1);
}
results.push(kValue);
});
assert.sameValue(results.length, 3, 'predicate called three times');
assert.sameValue(results[0], 'Shoes');
assert.sameValue(results[1], 'Bike');
assert.sameValue(results[2], undefined);
results = [];
arr = ['Skateboard', 'Barefoot'];
arr.findIndex(function(kValue) {
if (results.length === 0) {
arr.push('Motorcycle');
arr[1] = 'Magic Carpet';
}
results.push(kValue);
});
assert.sameValue(results.length, 2, 'predicate called twice');
assert.sameValue(results[0], 'Skateboard');
assert.sameValue(results[1], 'Magic Carpet');

View File

@ -0,0 +1,19 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: Property type and descriptor.
info: >
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
assert.sameValue(
typeof Array.prototype.findIndex,
'function',
'`typeof Array.prototype.findIndex` is `function`'
);
verifyNotEnumerable(Array.prototype, 'findIndex');
verifyWritable(Array.prototype, 'findIndex');
verifyConfigurable(Array.prototype, 'findIndex');

View File

@ -0,0 +1,18 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: Array.prototype.findIndex.length value and descriptor.
info: >
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
assert.sameValue(
Array.prototype.findIndex.length, 1,
'The value of `Array.prototype.findIndex.length` is `1`'
);
verifyNotEnumerable(Array.prototype.findIndex, 'length');
verifyNotWritable(Array.prototype.findIndex, 'length');
verifyConfigurable(Array.prototype.findIndex, 'length');

View File

@ -0,0 +1,22 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
Array.prototype.findIndex.name value and descriptor.
info: >
22.1.3.9 Array.prototype.findIndex ( predicate [ , thisArg ] )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
assert.sameValue(
Array.prototype.findIndex.name, 'findIndex',
'The value of `Array.prototype.findIndex.name` is `"findIndex"`'
);
verifyNotEnumerable(Array.prototype.findIndex, 'name');
verifyNotWritable(Array.prototype.findIndex, 'name');
verifyConfigurable(Array.prototype.findIndex, 'name');

View File

@ -0,0 +1,45 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
Predicate called as F.call( thisArg, kValue, k, O ) for each array entry.
info: >
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
6. If thisArg was supplied, let T be thisArg; else let T be undefined.
7. Let k be 0.
8. Repeat, while k < len
...
d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)).
...
---*/
var arr = ['Mike', 'Rick', 'Leo'];
var results = [];
arr.findIndex(function(kValue, k, O) {
results.push(arguments);
});
assert.sameValue(results.length, 3);
var result = results[0];
assert.sameValue(result[0], 'Mike');
assert.sameValue(result[1], 0);
assert.sameValue(result[2], arr);
assert.sameValue(result.length, 3);
result = results[1];
assert.sameValue(result[0], 'Rick');
assert.sameValue(result[1], 1);
assert.sameValue(result[2], arr);
assert.sameValue(result.length, 3);
result = results[2];
assert.sameValue(result[0], 'Leo');
assert.sameValue(result[1], 2);
assert.sameValue(result[2], arr);
assert.sameValue(result.length, 3);

View File

@ -0,0 +1,32 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
Predicate thisArg as F.call( thisArg, kValue, k, O ) for each array entry.
info: >
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
8. Repeat, while k < len
...
d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)).
e. ReturnIfAbrupt(testResult).
...
flags: [noStrict]
---*/
var result;
[1].find(function(kValue, k, O) {
result = this;
});
assert.sameValue(result, this);
var o = {};
[1].find(function() {
result = this;
}, o);
assert.sameValue(result, o);

View File

@ -0,0 +1,32 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
Predicate thisArg as F.call( thisArg, kValue, k, O ) for each array entry.
info: >
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
8. Repeat, while k < len
...
d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)).
e. ReturnIfAbrupt(testResult).
...
flags: [onlyStrict]
---*/
var result;
[1].find(function(kValue, k, O) {
result = this;
});
assert.sameValue(result, undefined);
var o = {};
[1].find(function() {
result = this;
}, o);
assert.sameValue(result, o);

View File

@ -0,0 +1,26 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
Predicate is called for each array property.
info: >
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
6. If thisArg was supplied, let T be thisArg; else let T be undefined.
7. Let k be 0.
8. Repeat, while k < len
...
d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)).
...
---*/
var arr = [undefined, , , 'foo'];
var called = 0;
arr.findIndex(function() {
called++;
});
assert.sameValue(called, 4);

View File

@ -0,0 +1,49 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
Throws a TypeError exception if predicate is not callable.
info: >
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
5. If IsCallable(predicate) is false, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
[].findIndex({});
});
assert.throws(TypeError, function() {
[].findIndex(null);
});
assert.throws(TypeError, function() {
[].findIndex(undefined);
});
assert.throws(TypeError, function() {
[].findIndex(true);
});
assert.throws(TypeError, function() {
[].findIndex(1);
});
assert.throws(TypeError, function() {
[].findIndex('');
});
assert.throws(TypeError, function() {
[].findIndex(1);
});
assert.throws(TypeError, function() {
[].findIndex([]);
});
assert.throws(TypeError, function() {
[].findIndex(/./);
});

View File

@ -0,0 +1,35 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
Predicate is only called if this.length is > 0.
info: >
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
7. Let k be 0.
8. Repeat, while k < len
...
d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)).
...
9. Return -1.
---*/
var called = false;
var predicate = function() {
called = true;
return true;
};
var result = [].findIndex(predicate);
assert.sameValue(
called, false,
'[].findIndex(predicate) does not call predicate'
);
assert.sameValue(
result, -1,
'[].findIndex(predicate) returned undefined'
);

View File

@ -0,0 +1,25 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
Return abrupt from predicate call.
info: >
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
7. Let k be 0.
8. Repeat, while k < len
...
d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)).
e. ReturnIfAbrupt(testResult).
...
---*/
var predicate = function() {
throw new Test262Error();
};
assert.throws(Test262Error, function() {
[1].findIndex(predicate);
});

View File

@ -0,0 +1,31 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
Returns abrupt from getting property value from `this`.
info: >
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
7. Let k be 0.
8. Repeat, while k < len
a. Let Pk be ToString(k).
b. Let kValue be Get(O, Pk).
c. ReturnIfAbrupt(kValue).
...
---*/
var o = {
length: 1
};
Object.defineProperty(o, 0, {
get: function() {
throw new Test262Error();
}
});
assert.throws(Test262Error, function() {
[].findIndex.call(o, function() {});
});

View File

@ -0,0 +1,25 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
Return abrupt from ToLength(Get(O, "length")) where length is a Symbol.
info: >
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
1. Let O be ToObject(this value).
2. ReturnIfAbrupt(O).
3. Let len be ToLength(Get(O, "length")).
4. ReturnIfAbrupt(len).
...
features: [Symbol]
---*/
var o = {};
o.length = Symbol(1);
// predicate fn is given to avoid false positives
assert.throws(TypeError, function() {
[].findIndex.call(o, function() {});
});

View File

@ -0,0 +1,38 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
Return abrupt from ToLength(Get(O, "length")).
info: >
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
1. Let O be ToObject(this value).
2. ReturnIfAbrupt(O).
3. Let len be ToLength(Get(O, "length")).
4. ReturnIfAbrupt(len).
---*/
var o1 = {};
Object.defineProperty(o1, 'length', {
get: function() {
throw new Test262Error();
},
configurable: true
});
// predicate fn is given to avoid false positives
assert.throws(Test262Error, function() {
[].findIndex.call(o1, function() {});
});
var o2 = {
length: {
valueOf: function() {
throw new Test262Error();
}
}
};
assert.throws(Test262Error, function() {
[].findIndex.call(o2, function() {});
});

View File

@ -0,0 +1,21 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
Return abrupt from ToObject(this value).
info: >
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
1. Let O be ToObject(this value).
2. ReturnIfAbrupt(O).
---*/
// predicate fn is given to avoid false positives
assert.throws(TypeError, function() {
Array.prototype.findIndex.call(undefined, function() {});
});
assert.throws(TypeError, function() {
Array.prototype.findIndex.call(null, function() {});
});

View File

@ -0,0 +1,53 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
Return index if predicate return a boolean true value.
info: >
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
8. Repeat, while k < len
...
d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)).
e. ReturnIfAbrupt(testResult).
f. If testResult is true, return k.
...
features: [Symbol]
---*/
var arr = ['Shoes', 'Car', 'Bike'];
var called = 0;
var result = arr.findIndex(function(val) {
called++;
return true;
});
assert.sameValue(result, 0);
assert.sameValue(called, 1, 'predicate was called once');
called = 0;
result = arr.findIndex(function(val) {
called++;
return val === 'Bike';
});
assert.sameValue(called, 3, 'predicate was called three times');
assert.sameValue(result, 2);
result = arr.findIndex(function(val) { return 'string'; });
assert.sameValue(result, 0, 'coerced string');
result = arr.findIndex(function(val) { return {}; });
assert.sameValue(result, 0, 'coerced object');
result = arr.findIndex(function(val) { return Symbol(''); });
assert.sameValue(result, 0, 'coerced Symbol');
result = arr.findIndex(function(val) { return 1; });
assert.sameValue(result, 0, 'coerced number');
result = arr.findIndex(function(val) { return -1; });
assert.sameValue(result, 0, 'coerced negative number');

View File

@ -0,0 +1,43 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 22.1.3.9
description: >
Return -1 if predicate always returns a boolean false value.
info: >
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
8. Repeat, while k < len
...
d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)).
...
9. Return -1.
features: [Symbol]
---*/
var arr = ['Shoes', 'Car', 'Bike'];
var called = 0;
var result = arr.findIndex(function(val) {
called++;
return false;
});
assert.sameValue(called, 3, 'predicate was called three times');
assert.sameValue(result, -1);
result = arr.findIndex(function(val) { return ''; });
assert.sameValue(result, -1, 'coerced string');
result = arr.findIndex(function(val) { return undefined; });
assert.sameValue(result, -1, 'coerced undefined');
result = arr.findIndex(function(val) { return null; });
assert.sameValue(result, -1, 'coerced null');
result = arr.findIndex(function(val) { return 0; });
assert.sameValue(result, -1, 'coerced 0');
result = arr.findIndex(function(val) { return NaN; });
assert.sameValue(result, -1, 'coerced NaN');