From 6a440ee61600e11aaca65079c79342b030f09b43 Mon Sep 17 00:00:00 2001 From: Leonardo Balter <leonardo.balter@gmail.com> Date: Wed, 22 Jul 2015 18:07:12 -0400 Subject: [PATCH] Update tests for Array.prototype.findIndex --- .../Array.prototype.findIndex_arity.js | 13 --- .../Array.prototype.findIndex_basic.js | 17 --- ...prototype.findIndex_called-length-times.js | 24 ---- .../Array.prototype.findIndex_exceptions.js | 107 ------------------ ...Array.prototype.findIndex_exotic-object.js | 42 ------- ...Array.prototype.findIndex_modifications.js | 23 ---- ...ype.findIndex_not-called-on-empty-array.js | 32 ------ ...prototype.findIndex_predicate-arguments.js | 34 ------ ...Index_this-arg-receiver-coercion-strict.js | 13 --- ...e.findIndex_this-arg-receiver-primitive.js | 17 --- ...e.findIndex_this-arg-receiver-reference.js | 12 -- .../Array.prototype.findIndex_this-arg.js | 42 ------- ...x_treats-holes-as-undefined-not-skipped.js | 11 -- .../Array.prototype.findIndex_with-string.js | 49 -------- .../findIndex/array-altered-during-loop.js | 47 ++++++++ .../Array/prototype/findIndex/findIndex.js | 19 ++++ .../Array/prototype/findIndex/length.js | 18 +++ .../Array/prototype/findIndex/name.js | 22 ++++ .../findIndex/predicate-call-parameters.js | 45 ++++++++ .../predicate-call-this-non-strict.js | 32 ++++++ .../findIndex/predicate-call-this-strict.js | 32 ++++++ ...redicate-called-for-each-array-property.js | 26 +++++ .../predicate-is-not-callable-throws.js | 49 ++++++++ .../predicate-not-called-on-empty-array.js | 35 ++++++ .../return-abrupt-from-predicate-call.js | 25 ++++ .../findIndex/return-abrupt-from-property.js | 31 +++++ ...eturn-abrupt-from-this-length-as-symbol.js | 25 ++++ .../return-abrupt-from-this-length.js | 38 +++++++ .../findIndex/return-abrupt-from-this.js | 21 ++++ .../return-index-predicate-result-is-true.js | 53 +++++++++ ...ve-one-if-predicate-returns-false-value.js | 43 +++++++ 31 files changed, 561 insertions(+), 436 deletions(-) delete mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_arity.js delete mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_basic.js delete mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_called-length-times.js delete mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_exceptions.js delete mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_exotic-object.js delete mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_modifications.js delete mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_not-called-on-empty-array.js delete mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_predicate-arguments.js delete mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-coercion-strict.js delete mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-primitive.js delete mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-reference.js delete mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg.js delete mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_treats-holes-as-undefined-not-skipped.js delete mode 100644 test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_with-string.js create mode 100644 test/built-ins/Array/prototype/findIndex/array-altered-during-loop.js create mode 100644 test/built-ins/Array/prototype/findIndex/findIndex.js create mode 100644 test/built-ins/Array/prototype/findIndex/length.js create mode 100644 test/built-ins/Array/prototype/findIndex/name.js create mode 100644 test/built-ins/Array/prototype/findIndex/predicate-call-parameters.js create mode 100644 test/built-ins/Array/prototype/findIndex/predicate-call-this-non-strict.js create mode 100644 test/built-ins/Array/prototype/findIndex/predicate-call-this-strict.js create mode 100644 test/built-ins/Array/prototype/findIndex/predicate-called-for-each-array-property.js create mode 100644 test/built-ins/Array/prototype/findIndex/predicate-is-not-callable-throws.js create mode 100644 test/built-ins/Array/prototype/findIndex/predicate-not-called-on-empty-array.js create mode 100644 test/built-ins/Array/prototype/findIndex/return-abrupt-from-predicate-call.js create mode 100644 test/built-ins/Array/prototype/findIndex/return-abrupt-from-property.js create mode 100644 test/built-ins/Array/prototype/findIndex/return-abrupt-from-this-length-as-symbol.js create mode 100644 test/built-ins/Array/prototype/findIndex/return-abrupt-from-this-length.js create mode 100644 test/built-ins/Array/prototype/findIndex/return-abrupt-from-this.js create mode 100644 test/built-ins/Array/prototype/findIndex/return-index-predicate-result-is-true.js create mode 100644 test/built-ins/Array/prototype/findIndex/return-negative-one-if-predicate-returns-false-value.js diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_arity.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_arity.js deleted file mode 100644 index 622ceb5623..0000000000 --- a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_arity.js +++ /dev/null @@ -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); - diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_basic.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_basic.js deleted file mode 100644 index 2b9db5c681..0000000000 --- a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_basic.js +++ /dev/null @@ -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); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_called-length-times.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_called-length-times.js deleted file mode 100644 index dc3cbe85e7..0000000000 --- a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_called-length-times.js +++ /dev/null @@ -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); -})(); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_exceptions.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_exceptions.js deleted file mode 100644 index 11abc79b1c..0000000000 --- a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_exceptions.js +++ /dev/null @@ -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+/, []); -}); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_exotic-object.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_exotic-object.js deleted file mode 100644 index 888b17e179..0000000000 --- a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_exotic-object.js +++ /dev/null @@ -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); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_modifications.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_modifications.js deleted file mode 100644 index a1c8d28b41..0000000000 --- a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_modifications.js +++ /dev/null @@ -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); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_not-called-on-empty-array.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_not-called-on-empty-array.js deleted file mode 100644 index 9f9db8b324..0000000000 --- a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_not-called-on-empty-array.js +++ /dev/null @@ -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); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_predicate-arguments.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_predicate-arguments.js deleted file mode 100644 index 08c4a7dee1..0000000000 --- a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_predicate-arguments.js +++ /dev/null @@ -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); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-coercion-strict.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-coercion-strict.js deleted file mode 100644 index f343507f0b..0000000000 --- a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-coercion-strict.js +++ /dev/null @@ -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]); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-primitive.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-primitive.js deleted file mode 100644 index 3f2c44f831..0000000000 --- a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-primitive.js +++ /dev/null @@ -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]); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-reference.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-reference.js deleted file mode 100644 index 4c89bd813b..0000000000 --- a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg-receiver-reference.js +++ /dev/null @@ -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]); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg.js deleted file mode 100644 index d166f8592f..0000000000 --- a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_this-arg.js +++ /dev/null @@ -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]); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_treats-holes-as-undefined-not-skipped.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_treats-holes-as-undefined-not-skipped.js deleted file mode 100644 index 489d41c02f..0000000000 --- a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_treats-holes-as-undefined-not-skipped.js +++ /dev/null @@ -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); diff --git a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_with-string.js b/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_with-string.js deleted file mode 100644 index a48fc0504e..0000000000 --- a/test/built-ins/Array/prototype/findIndex/Array.prototype.findIndex_with-string.js +++ /dev/null @@ -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); - diff --git a/test/built-ins/Array/prototype/findIndex/array-altered-during-loop.js b/test/built-ins/Array/prototype/findIndex/array-altered-during-loop.js new file mode 100644 index 0000000000..e490021939 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/array-altered-during-loop.js @@ -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'); diff --git a/test/built-ins/Array/prototype/findIndex/findIndex.js b/test/built-ins/Array/prototype/findIndex/findIndex.js new file mode 100644 index 0000000000..69557fb7f9 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/findIndex.js @@ -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'); diff --git a/test/built-ins/Array/prototype/findIndex/length.js b/test/built-ins/Array/prototype/findIndex/length.js new file mode 100644 index 0000000000..bdb946494e --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/length.js @@ -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'); diff --git a/test/built-ins/Array/prototype/findIndex/name.js b/test/built-ins/Array/prototype/findIndex/name.js new file mode 100644 index 0000000000..6258ff467f --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/name.js @@ -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'); diff --git a/test/built-ins/Array/prototype/findIndex/predicate-call-parameters.js b/test/built-ins/Array/prototype/findIndex/predicate-call-parameters.js new file mode 100644 index 0000000000..60a6dc1407 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/predicate-call-parameters.js @@ -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); diff --git a/test/built-ins/Array/prototype/findIndex/predicate-call-this-non-strict.js b/test/built-ins/Array/prototype/findIndex/predicate-call-this-non-strict.js new file mode 100644 index 0000000000..0e702987eb --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/predicate-call-this-non-strict.js @@ -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); diff --git a/test/built-ins/Array/prototype/findIndex/predicate-call-this-strict.js b/test/built-ins/Array/prototype/findIndex/predicate-call-this-strict.js new file mode 100644 index 0000000000..2febc6a41e --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/predicate-call-this-strict.js @@ -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); diff --git a/test/built-ins/Array/prototype/findIndex/predicate-called-for-each-array-property.js b/test/built-ins/Array/prototype/findIndex/predicate-called-for-each-array-property.js new file mode 100644 index 0000000000..a9413f9544 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/predicate-called-for-each-array-property.js @@ -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); diff --git a/test/built-ins/Array/prototype/findIndex/predicate-is-not-callable-throws.js b/test/built-ins/Array/prototype/findIndex/predicate-is-not-callable-throws.js new file mode 100644 index 0000000000..e7da5795ed --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/predicate-is-not-callable-throws.js @@ -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(/./); +}); diff --git a/test/built-ins/Array/prototype/findIndex/predicate-not-called-on-empty-array.js b/test/built-ins/Array/prototype/findIndex/predicate-not-called-on-empty-array.js new file mode 100644 index 0000000000..58788f9385 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/predicate-not-called-on-empty-array.js @@ -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' +); diff --git a/test/built-ins/Array/prototype/findIndex/return-abrupt-from-predicate-call.js b/test/built-ins/Array/prototype/findIndex/return-abrupt-from-predicate-call.js new file mode 100644 index 0000000000..c5b42e6ac2 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/return-abrupt-from-predicate-call.js @@ -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); +}); diff --git a/test/built-ins/Array/prototype/findIndex/return-abrupt-from-property.js b/test/built-ins/Array/prototype/findIndex/return-abrupt-from-property.js new file mode 100644 index 0000000000..88c729b244 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/return-abrupt-from-property.js @@ -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() {}); +}); diff --git a/test/built-ins/Array/prototype/findIndex/return-abrupt-from-this-length-as-symbol.js b/test/built-ins/Array/prototype/findIndex/return-abrupt-from-this-length-as-symbol.js new file mode 100644 index 0000000000..0489657bb0 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/return-abrupt-from-this-length-as-symbol.js @@ -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() {}); +}); diff --git a/test/built-ins/Array/prototype/findIndex/return-abrupt-from-this-length.js b/test/built-ins/Array/prototype/findIndex/return-abrupt-from-this-length.js new file mode 100644 index 0000000000..294fbd9902 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/return-abrupt-from-this-length.js @@ -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() {}); +}); diff --git a/test/built-ins/Array/prototype/findIndex/return-abrupt-from-this.js b/test/built-ins/Array/prototype/findIndex/return-abrupt-from-this.js new file mode 100644 index 0000000000..35853a8279 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/return-abrupt-from-this.js @@ -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() {}); +}); diff --git a/test/built-ins/Array/prototype/findIndex/return-index-predicate-result-is-true.js b/test/built-ins/Array/prototype/findIndex/return-index-predicate-result-is-true.js new file mode 100644 index 0000000000..349559e048 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/return-index-predicate-result-is-true.js @@ -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'); diff --git a/test/built-ins/Array/prototype/findIndex/return-negative-one-if-predicate-returns-false-value.js b/test/built-ins/Array/prototype/findIndex/return-negative-one-if-predicate-returns-false-value.js new file mode 100644 index 0000000000..f54e5bb496 --- /dev/null +++ b/test/built-ins/Array/prototype/findIndex/return-negative-one-if-predicate-returns-false-value.js @@ -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'); -- GitLab