Update tests for Array.prototype.find

This commit is contained in:
Leonardo Balter 2015-07-22 14:54:07 -04:00
parent 02fbde769c
commit 88435c5201
46 changed files with 555 additions and 764 deletions

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.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
includes: [compareArray.js]
---*/
var a = [1, 2, 3];
var found = a.find(function(val) { a.push(val); return false; });
assert(compareArray(a, [1, 2, 3, 1, 2, 3]));
assert.sameValue(a.length, 6);
assert.sameValue(found, undefined);
a = [1, 2, 3];
found = a.find(function(val, key) { a[key] = ++val; return false; });
assert(compareArray(a, [2, 3, 4]));
assert.sameValue(a.length, 3);
assert.sameValue(found, undefined);

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.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
---*/
assert.sameValue(1, Array.prototype.find.length);
var a = [21, 22, 23, 24];
assert.sameValue(a.find(function() { return false; }), undefined);
assert.sameValue(a.find(function() { return true; }), 21);
assert.sameValue(a.find(function(val) { return 121 === val; }), undefined);
assert.sameValue(a.find(function(val) { return 24 === val; }), 24);
assert.sameValue(a.find(function(val) { return 23 === val; }), 23);
assert.sameValue(a.find(function(val) { return 22 === val; }), 22);

View File

@ -1,13 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Array.prototype.find shouldn't throw a TypeError if
IsCallable(predicate) is true; Proxy is callable
features: [Array#find, Proxy]
---*/
[].find(Proxy);

View File

@ -1,13 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Array.prototype.find shouldn't throw a TypeError if
IsCallable(predicate) is true; a new Proxy object is callable
features: [Array#find, Proxy]
---*/
[].find(new Proxy(function () {}, function () {}));

View File

@ -1,13 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Array.prototype.find shouldn't throw a TypeError if
IsCallable(predicate) is true; arrow functions are callable
features: [Array#find, arrow-function]
---*/
[].find(x => x);

View File

@ -1,13 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Array.prototype.find shouldn't throw a TypeError if
IsCallable(predicate) is true; Array#forEach is callable
features: [Array#find]
---*/
[].find(Array.prototype.forEach);

View File

@ -1,13 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Array.prototype.find shouldn't throw a TypeError if
IsCallable(predicate) is true; a function is callable
features: [Array#find]
---*/
[].find(function () {});

View File

@ -1,22 +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.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
---*/
var a = [1, 2, 3, 4, 5];
var l = 0;
var found = a.find(function() {
l++;
return false;
});
assert.sameValue(l, a.length);
assert.sameValue(found, undefined);

View File

@ -1,15 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Find on empty array should return undefined
features: [Array#find]
---*/
var a = [].find(function () {
return true;
});
if (a !== undefined) {
$ERROR('#1: a !== undefined. Actual: ' + typeof a);
}

View File

@ -1,111 +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.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
includes: [compareArray.js]
---*/
// Test exceptions
assert.throws(TypeError, function() {
Array.prototype.find.call(null, function() { });
});
assert.throws(TypeError, function() {
Array.prototype.find.call(undefined, function() { });
});
assert.throws(TypeError, function() {
Array.prototype.find.apply(null, function() { }, []);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply(undefined, function() { }, []);
});
assert.throws(TypeError, function() {
[].find(null);
});
assert.throws(TypeError, function() {
[].find(undefined);
});
assert.throws(TypeError, function() {
[].find(0);
});
assert.throws(TypeError, function() {
[].find(true);
});
assert.throws(TypeError, function() {
[].find(false);
});
assert.throws(TypeError, function() {
[].find("");
});
assert.throws(TypeError, function() {
[].find({});
});
assert.throws(TypeError, function() {
[].find([]);
});
assert.throws(TypeError, function() {
[].find(/\d+/);
});
assert.throws(TypeError, function() {
Array.prototype.find.call({}, null);
});
assert.throws(TypeError, function() {
Array.prototype.find.call({}, undefined);
});
assert.throws(TypeError, function() {
Array.prototype.find.call({}, 0);
});
assert.throws(TypeError, function() {
Array.prototype.find.call({}, true);
});
assert.throws(TypeError, function() {
Array.prototype.find.call({}, false);
});
assert.throws(TypeError, function() {
Array.prototype.find.call({}, "");
});
assert.throws(TypeError, function() {
Array.prototype.find.call({}, {});
});
assert.throws(TypeError, function() {
Array.prototype.find.call({}, []);
});
assert.throws(TypeError, function() {
Array.prototype.find.call({}, /\d+/);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply({}, null, []);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply({}, undefined, []);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply({}, 0, []);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply({}, true, []);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply({}, false, []);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply({}, "", []);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply({}, {}, []);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply({}, [], []);
});
assert.throws(TypeError, function() {
Array.prototype.find.apply({}, /\d+/, []);
});

View File

@ -1,40 +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.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
includes: [compareArray.js]
---*/
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 found = Array.prototype.find.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(found, undefined);

View File

@ -1,11 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: The length property of the find method is 1
features: [Array#find]
---*/
if ([].find.length !== 1) {
$ERROR('1: [].find.length !== 1. Actual: ' + [].find.length);
}

View File

@ -1,28 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Array may be mutated by calls to the predicate
features: [Array#find]
---*/
[1, 2, 3].find(function (v, i, arr) {
arr[i + 1] = arr[i + 1] + 1;
switch (i) {
case 0:
if (arr[i] !== 1) {
$ERROR('#1: arr[0] !== 1. Actual: ' + arr[i]);
}
break;
case 1:
if (arr[i] !== 3) {
$ERROR('#2: arr[1] !== 3. Actual: ' + arr[i]);
}
break;
case 2:
if (arr[i] !== 4) {
$ERROR('#3: arr[1] !== 4. Actual: ' + arr[i]);
}
break;
}
});

View File

@ -1,13 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Find with a predicate with no return value should return undefined
features: [Array#find]
---*/
var a = [1, 2, 3].find(function () {});
if (a !== undefined) {
$ERROR('#1: a !== undefined. Actual: ' + typeof a);
}

View File

@ -1,36 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Array.prototype.find should throw a TypeError if
IsCallable(predicate) is false
includes: [runTestCase.js]
features: [Array#find]
---*/
var uncallableValues = [
undefined,
null,
true,
this,
{},
'string',
0
];
function testcase() {
for (var i = 0, len = uncallableValues.length; i < len; i++) {
try {
[].find(uncallableValues[i]);
return false;
} catch (e) {
if (!(e instanceof TypeError)) {
return false;
}
}
}
return true;
}
runTestCase(testcase);

View File

@ -1,33 +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.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
includes: [compareArray.js]
---*/
var a = [];
var l = -1;
var o = -1;
var v = -1;
var k = -1;
a.find(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,26 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
predicate is called with three arguments: the value of the
element, the index of the element, and the object being traversed.
features: [Array#find]
---*/
var a = [1];
var b = a.find(function (v, i, arr) {
if (arguments.length !== 3) {
$ERROR('#1: arguments.length !== 3. Actual: ' + arguments.length);
}
if (v !== 1) {
$ERROR('#2: element value !== 1. Actual: ' + v);
}
if (i !== 0) {
$ERROR('#3: index !== 0. Actual: ' + i);
}
if (arr !== a) {
$ERROR('#4: object being traversed !== a');
}
});

View File

@ -1,33 +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.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
includes: [compareArray.js]
---*/
var a = ["b"];
var l = -1;
var o = -1;
var v = -1;
var k = -1;
var found = a.find(function(val, key, obj) {
o = obj;
l = obj.length;
v = val;
k = key;
return false;
});
assert(compareArray(o, a));
assert.sameValue(a.length, l);
assert.sameValue(v, "b");
assert.sameValue(k, 0);
assert.sameValue(found, undefined);

View File

@ -1,16 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Elements added to array after find has been called should not be
visited
features: [Array#find]
---*/
[1].find(function (v, i, arr) {
arr.push('string');
if (v === 'string') {
$ERROR('#' + i + ': \'string\' should not be visited');
}
});

View File

@ -1,41 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Elements removed from array after find has been called should be
visited
features: [Array#find]
---*/
var elementsVisited;
elementsVisited = 0;
[1, 'string', 2].find(function (v, i, arr) {
elementsVisited++;
var stringIndex = arr.indexOf('string');
if (stringIndex !== -1) delete arr[stringIndex];
if (v === 'string') {
$ERROR('#1: \'string\' should not exist, it has been deleted');
}
});
if (elementsVisited !== 3) {
$ERROR('#2: deleted elements should be visited');
}
elementsVisited = 0;
[1, 'string', 2].find(function (v, i, arr) {
elementsVisited++;
var stringIndex = arr.indexOf('string');
if (stringIndex !== -1) arr.splice(stringIndex, 1);
if (v === 'string') {
$ERROR('#3: \'string\' should not exist, it has been deleted');
}
});
if (elementsVisited !== 3) {
$ERROR('#4: deleted elements should be visited');
}

View File

@ -1,29 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Find should return value if predicate returns true
features: [Array#find]
---*/
var testVals = [
undefined,
null,
0,
'string',
String,
this,
true,
[1,2,3]
];
var a;
for (var i = 0, len = testVals.length; i < len; i++) {
a = testVals.find(function (v) {
return v === testVals[i];
});
if (a !== testVals[i]) {
$ERROR('#' + (i + 1) + ': a !== testVals[' + i + ']. Actual: ' + a);
}
}

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.8
description: >
Create a new object in each function call when receiver is a
primitive value. See ECMA-262, Annex C.
flags: [noStrict]
includes: [compareArray.js]
---*/
// Create a new object in each function call when receiver is a
// primitive value. See ECMA-262, Annex C.
var a = [];
[1, 2].find(function() { a.push(this) }, "");
assert(a[0] !== a[1]);

View File

@ -1,43 +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.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
includes: [compareArray.js]
---*/
// Test String as a thisArg
var found = [1, 2, 3].find(function(val, key) {
return this.charAt(Number(key)) === String(val);
}, "321");
assert.sameValue(2, found);
// Test object as a thisArg
var thisArg = {
elementAt: function(key) {
return this[key];
}
};
Array.prototype.push.apply(thisArg, ["c", "b", "a"]);
found = ["a", "b", "c"].find(function(val, key) {
return this.elementAt(key) === val;
}, thisArg);
assert.sameValue("b", found);
// Check thisArg parameter does not change.
var a = [];
[1, 2].find(function() { a.push(this) }, {});
assert.sameValue(a[0], a[1]);
// In strict mode primitive values should not be coerced to an object.
a = [];
[1, 2].find(function() { "use strict"; a.push(this); }, "");
assert.sameValue("", a[0]);
assert.sameValue(a[0], a[1]);

View File

@ -1,14 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: thisArg should be bound to this if provided
features: [Array#find]
---*/
var globalThis = this;
[1].find(function () {
assert.sameValue(this, Array, 'this should equal Array');
assert.notSameValue(this, globalThis, 'this should not equal globalThis');
}, Array);

View File

@ -1,16 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: thisArg should be global object if not provided (not Strict mode)
flags: [noStrict]
includes: [fnGlobalObject.js]
features: [Array#find]
---*/
[1].find(function () {
if (this !== fnGlobalObject()) {
$ERROR('#1: this !== global object');
}
});

View File

@ -1,27 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Array.prototype.find should convert thisArg into an object
flags: [noStrict]
features: [Array#find]
---*/
var dataTypes = [
undefined,
null,
true,
this,
{},
'string',
0,
function () {}
]
for (var i = 0, len = dataTypes.length; i < len; i++) {
[1].find(function () {
if (!(this instanceof Object)) {
$ERROR('#' + i + ': !(this instanceof Object). Actual: ' + typeof this);
}
}, dataTypes[i])
}

View File

@ -1,15 +0,0 @@
// Copyright (c) 2014 Matthew Meyers. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: thisArg should be undefined if not provided (Strict mode)
flags: [onlyStrict]
features: [Array#find]
---*/
[1].find(function () {
if (this !== undefined) {
$ERROR('#1: this !== undefined');
}
});

View File

@ -1,18 +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.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
Does not skip holes
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
---*/
var count = 0;
[,,,,,].find(function() { count++; return false; });
assert.sameValue(count, 5);

View File

@ -1,48 +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.8
description: >
The find() method returns a value in the array, if an
element in the array satisfies the provided testing function.
Otherwise undefined is returned.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
includes: [compareArray.js]
---*/
var a = "abcd";
var l = -1;
var o = -1;
var v = -1;
var k = -1;
var found = Array.prototype.find.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(found, undefined);
found = Array.prototype.find.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(found, "a");

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.8
description: >
The range of elements processed is set before the first call to `predicate`.
info: >
22.1.3.8 Array.prototype.find ( 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.find(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.find(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.8
description: Property type and descriptor.
info: >
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
assert.sameValue(
typeof Array.prototype.find,
'function',
'`typeof Array.prototype.find` is `function`'
);
verifyNotEnumerable(Array.prototype, 'find');
verifyWritable(Array.prototype, 'find');
verifyConfigurable(Array.prototype, 'find');

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.8
description: Array.prototype.find.length value and descriptor.
info: >
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
assert.sameValue(
Array.prototype.find.length, 1,
'The value of `Array.prototype.find.length` is `1`'
);
verifyNotEnumerable(Array.prototype.find, 'length');
verifyNotWritable(Array.prototype.find, 'length');
verifyConfigurable(Array.prototype.find, '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.8
description: >
Array.prototype.find.name value and descriptor.
info: >
22.1.3.8 Array.prototype.find ( predicate [ , thisArg ] )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
assert.sameValue(
Array.prototype.find.name, 'find',
'The value of `Array.prototype.find.name` is `"find"`'
);
verifyNotEnumerable(Array.prototype.find, 'name');
verifyNotWritable(Array.prototype.find, 'name');
verifyConfigurable(Array.prototype.find, 'name');

View File

@ -0,0 +1,46 @@
// 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.8
description: >
Predicate called as F.call( thisArg, kValue, k, O ) for each array entry.
info: >
22.1.3.8 Array.prototype.find ( 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»)).
e. ReturnIfAbrupt(testResult).
...
---*/
var arr = ['Mike', 'Rick', 'Leo'];
var results = [];
arr.find(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.8
description: >
Predicate thisArg as F.call( thisArg, kValue, k, O ) for each array entry.
info: >
22.1.3.8 Array.prototype.find ( 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.8
description: >
Predicate thisArg as F.call( thisArg, kValue, k, O ) for each array entry.
info: >
22.1.3.8 Array.prototype.find ( 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.8
description: >
Predicate is called for each array property.
info: >
22.1.3.8 Array.prototype.find ( 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.find(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.8
description: >
Throws a TypeError exception if predicate is not callable.
info: >
22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] )
...
5. If IsCallable(predicate) is false, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
[].find({});
});
assert.throws(TypeError, function() {
[].find(null);
});
assert.throws(TypeError, function() {
[].find(undefined);
});
assert.throws(TypeError, function() {
[].find(true);
});
assert.throws(TypeError, function() {
[].find(1);
});
assert.throws(TypeError, function() {
[].find('');
});
assert.throws(TypeError, function() {
[].find(1);
});
assert.throws(TypeError, function() {
[].find([]);
});
assert.throws(TypeError, function() {
[].find(/./);
});

View File

@ -0,0 +1,29 @@
// 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.8
description: >
Predicate is only called if this.length is > 0.
info: >
22.1.3.8 Array.prototype.find ( 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 undefined.
---*/
var called = false;
var predicate = function() {
called = true;
return true;
};
var result = [].find(predicate);
assert.sameValue(called, false, '[].find(predicate) does not call predicate');
assert.sameValue(result, undefined, '[].find(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.8
description: >
Return abrupt from predicate call.
info: >
22.1.3.8 Array.prototype.find ( 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].find(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.8
description: >
Returns abrupt from getting property value from `this`.
info: >
22.1.3.8 Array.prototype.find ( 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() {
[].find.call(o, function() {});
});

View File

@ -0,0 +1,24 @@
// 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.8
description: >
Return abrupt from ToLength(Get(O, "length")) where length is a Symbol.
info: >
22.1.3.8 Array.prototype.find ( 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() {
[].find.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.8
description: >
Return abrupt from ToLength(Get(O, "length")).
info: >
22.1.3.8 Array.prototype.find ( 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
});
assert.throws(Test262Error, function() {
[].find.call(o1);
});
var o2 = {
length: {
valueOf: function() {
throw new Test262Error();
}
}
};
assert.throws(Test262Error, function() {
[].find.call(o2);
});

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.8
description: >
Return abrupt from ToObject(this value).
info: >
22.1.3.8 Array.prototype.find ( 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.find.call(undefined, function() {});
});
assert.throws(TypeError, function() {
Array.prototype.find.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.8
description: >
Return found value if predicate return a boolean true value.
info: >
22.1.3.8 Array.prototype.find ( 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 kValue.
...
features: [Symbol]
---*/
var arr = ['Shoes', 'Car', 'Bike'];
var called = 0;
var result = arr.find(function(val) {
called++;
return true;
});
assert.sameValue(result, 'Shoes');
assert.sameValue(called, 1, 'predicate was called once');
called = 0;
result = arr.find(function(val) {
called++;
return val === 'Bike';
});
assert.sameValue(called, 3, 'predicate was called three times');
assert.sameValue(result, 'Bike');
result = arr.find(function(val) { return 'string'; });
assert.sameValue(result, 'Shoes', 'coerced string');
result = arr.find(function(val) { return {}; });
assert.sameValue(result, 'Shoes', 'coerced object');
result = arr.find(function(val) { return Symbol(''); });
assert.sameValue(result, 'Shoes', 'coerced Symbol');
result = arr.find(function(val) { return 1; });
assert.sameValue(result, 'Shoes', 'coerced number');
result = arr.find(function(val) { return -1; });
assert.sameValue(result, 'Shoes', '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.8
description: >
Return undefined if predicate always returns a boolean false value.
info: >
22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] )
...
8. Repeat, while k < len
...
d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)).
...
9. Return undefined.
features: [Symbol]
---*/
var arr = ['Shoes', 'Car', 'Bike'];
var called = 0;
var result = arr.find(function(val) {
called++;
return false;
});
assert.sameValue(called, 3, 'predicate was called three times');
assert.sameValue(result, undefined);
result = arr.find(function(val) { return ''; });
assert.sameValue(result, undefined, 'coerced string');
result = arr.find(function(val) { return undefined; });
assert.sameValue(result, undefined, 'coerced undefined');
result = arr.find(function(val) { return null; });
assert.sameValue(result, undefined, 'coerced null');
result = arr.find(function(val) { return 0; });
assert.sameValue(result, undefined, 'coerced 0');
result = arr.find(function(val) { return NaN; });
assert.sameValue(result, undefined, 'coerced NaN');