Add findLastIndex tests

This commit is contained in:
Wenlu Wang 2021-07-16 14:03:58 +08:00 committed by rwaldron
parent 275e7f1595
commit 3bb6d6480e
19 changed files with 621 additions and 0 deletions

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.
/*---
esid: sec-array.prototype.findlastindex
description: >
The range of elements processed is set before the first call to `predicate`.
info: |
Array.prototype.findLastIndex ( predicate[ , thisArg ] )
...
4. Let k be len - 1.
5. Repeat, while k 0,
...
c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)).
...
---*/
var arr = ['Shoes', 'Car', 'Bike'];
var results = [];
arr.findLastIndex(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], 'Bike');
assert.sameValue(results[1], 'Bike');
assert.sameValue(results[2], 'Shoes');
results = [];
arr = ['Skateboard', 'Barefoot'];
arr.findLastIndex(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], 'Barefoot');
assert.sameValue(results[1], 'Magic Carpet');

View File

@ -0,0 +1,18 @@
// Copyright (c) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.findlastindex
description: Array.prototype.findLastIndex applied to boolean primitive
---*/
assert.sameValue(
Array.prototype.findLastIndex.call(true, () => {}),
-1,
'Array.prototype.findLastIndex.call(true, () => {}) must return -1'
);
assert.sameValue(
Array.prototype.findLastIndex.call(false, () => {}),
-1,
'Array.prototype.findLastIndex.call(false, () => {}) must return -1'
);

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

View File

@ -0,0 +1,33 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-ecmascript-standard-built-in-objects
description: >
Array.prototype.findLastIndex does not implement [[Construct]], is not new-able
info: |
ECMAScript Function Objects
Built-in function objects that are not identified as constructors do not
implement the [[Construct]] internal method unless otherwise specified in
the description of a particular function.
sec-evaluatenew
...
7. If IsConstructor(constructor) is false, throw a TypeError exception.
...
includes: [isConstructor.js]
features: [Reflect.construct, arrow-function]
---*/
assert.sameValue(
isConstructor(Array.prototype.findLastIndex),
false,
'isConstructor(Array.prototype.findLastIndex) must return false'
);
assert.throws(TypeError, () => {
new Array.prototype.findLastIndex(() => {});
}, '`new Array.prototype.findLastIndex(() => {})` throws TypeError');

View File

@ -0,0 +1,44 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.findlastindex
description: >
Predicate called as F.call( thisArg, kValue, k, O ) for each array entry.
info: |
Array.prototype.findLastIndex ( predicate[ , thisArg ] )
...
4. Let k be len - 1.
5. Repeat, while k 0,
...
c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)).
...
---*/
var arr = ['Mike', 'Rick', 'Leo'];
var results = [];
arr.findLastIndex(function(kValue, k, O) {
results.push(arguments);
});
assert.sameValue(results.length, 3);
var result = results[0];
assert.sameValue(result[0], 'Leo');
assert.sameValue(result[1], 2);
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], 'Mike');
assert.sameValue(result[1], 0);
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.
/*---
esid: sec-array.prototype.findlastindex
description: >
Predicate thisArg as F.call( thisArg, kValue, k, O ) for each array entry.
info: |
Array.prototype.findLastIndex ( predicate[ , thisArg ] )
...
5. Repeat, while k 0,
...
c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)).
d. If testResult is true, return 𝔽(k).
...
flags: [noStrict]
---*/
var result;
[1].findLastIndex(function(kValue, k, O) {
result = this;
});
assert.sameValue(result, this);
var o = {};
[1].findLastIndex(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.
/*---
esid: sec-array.prototype.findlastindex
description: >
Predicate thisArg as F.call( thisArg, kValue, k, O ) for each array entry.
info: |
Array.prototype.findLastIndex ( predicate[ , thisArg ] )
...
5. Repeat, while k 0,
...
c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)).
d. If testResult is true, return 𝔽(k).
...
flags: [onlyStrict]
---*/
var result;
[1].findLastIndex(function(kValue, k, O) {
result = this;
});
assert.sameValue(result, undefined);
var o = {};
[1].findLastIndex(function() {
result = this;
}, o);
assert.sameValue(result, o);

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.
/*---
esid: sec-array.prototype.findlastindex
description: >
Predicate is called for each array property.
info: |
Array.prototype.findLastIndex ( predicate[ , thisArg ] )
...
4. Let k be len - 1.
5. Repeat, while k 0,
...
c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)).
...
---*/
var arr = [undefined, , , 'foo'];
var called = 0;
arr.findLastIndex(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.
/*---
esid: sec-array.prototype.findlastindex
description: >
Throws a TypeError exception if predicate is not callable.
info: |
Array.prototype.findLastIndex ( predicate[ , thisArg ] )
...
3. If IsCallable(predicate) is false, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
[].findLastIndex({});
});
assert.throws(TypeError, function() {
[].findLastIndex(null);
});
assert.throws(TypeError, function() {
[].findLastIndex(undefined);
});
assert.throws(TypeError, function() {
[].findLastIndex(true);
});
assert.throws(TypeError, function() {
[].findLastIndex(1);
});
assert.throws(TypeError, function() {
[].findLastIndex('');
});
assert.throws(TypeError, function() {
[].findLastIndex(1);
});
assert.throws(TypeError, function() {
[].findLastIndex([]);
});
assert.throws(TypeError, function() {
[].findLastIndex(/./);
});

View File

@ -0,0 +1,34 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.findlastindex
description: >
Predicate is only called if this.length is > 0.
info: |
Array.prototype.findLastIndex ( predicate[ , thisArg ] )
...
4. Let k be len - 1.
5. Repeat, while k 0,
...
c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)).
6. Return -1.
---*/
var called = false;
var predicate = function() {
called = true;
return true;
};
var result = [].findLastIndex(predicate);
assert.sameValue(
called, false,
'[].findLastIndex(predicate) does not call predicate'
);
assert.sameValue(
result, -1,
'[].findLastIndex(predicate) returned undefined'
);

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.
/*---
esid: sec-array.prototype.findlastindex
description: Property type and descriptor.
info: |
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
assert.sameValue(
typeof Array.prototype.findLastIndex,
'function',
'`typeof Array.prototype.findLastIndex` is `function`'
);
verifyNotEnumerable(Array.prototype, 'findLastIndex');
verifyWritable(Array.prototype, 'findLastIndex');
verifyConfigurable(Array.prototype, 'findLastIndex');

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.
/*---
esid: sec-array.prototype.findlastindex
description: >
Return abrupt from predicate call.
info: |
Array.prototype.findLastIndex ( predicate[ , thisArg ] )
...
4. Let k be len - 1.
5. Repeat, while k 0,
...
c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)).
d. If testResult is true, return 𝔽(k).
...
---*/
var predicate = function() {
throw new Test262Error();
};
assert.throws(Test262Error, function() {
[1].findLastIndex(predicate);
});

View File

@ -0,0 +1,30 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.findlastindex
description: >
Returns abrupt from getting property value from `this`.
info: |
Array.prototype.findLastIndex ( predicate[ , thisArg ] )
...
4. Let k be len - 1.
5. Repeat, while k 0,
a. Let Pk be ! ToString(𝔽(k)).
b. Let kValue be ? Get(O, Pk).
...
---*/
var o = {
length: 1
};
Object.defineProperty(o, 0, {
get: function() {
throw new Test262Error();
}
});
assert.throws(Test262Error, function() {
[].findLastIndex.call(o, function() {});
});

View File

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

View File

@ -0,0 +1,36 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.findlastindex
description: >
Return abrupt from ToLength(Get(O, "length")).
info: |
Array.prototype.findLastIndex ( predicate[ , thisArg ] )
1. Let O be ? ToObject(this value).
2. Let len be ? LengthOfArrayLike(O).
---*/
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() {
[].findLastIndex.call(o1, function() {});
});
var o2 = {
length: {
valueOf: function() {
throw new Test262Error();
}
}
};
assert.throws(Test262Error, function() {
[].findLastIndex.call(o2, function() {});
});

View File

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

View File

@ -0,0 +1,62 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.findlastindex
description: >
Return index if predicate return a boolean true value.
info: |
Array.prototype.findLastIndex ( predicate[ , thisArg ] )
...
5. Repeat, while k 0,
...
c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)).
d. If testResult is true, return 𝔽(k).
...
features: [Symbol]
---*/
var arr = ['Shoes', 'Car', 'Bike'];
var called = 0;
var result = arr.findLastIndex(function(val) {
called++;
return true;
});
assert.sameValue(result, 2);
assert.sameValue(called, 1, 'predicate was called once');
called = 0;
result = arr.findLastIndex(function(val) {
called++;
return val === 'Bike';
});
assert.sameValue(called, 1, 'predicate was called three times');
assert.sameValue(result, 2);
result = arr.findLastIndex(function(val) {
return 'string';
});
assert.sameValue(result, 2, 'coerced string');
result = arr.findLastIndex(function(val) {
return {};
});
assert.sameValue(result, 2, 'coerced object');
result = arr.findLastIndex(function(val) {
return Symbol('');
});
assert.sameValue(result, 2, 'coerced Symbol');
result = arr.findLastIndex(function(val) {
return 1;
});
assert.sameValue(result, 2, 'coerced number');
result = arr.findLastIndex(function(val) {
return -1;
});
assert.sameValue(result, 2, 'coerced negative number');

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.
/*---
esid: sec-array.prototype.findlastindex
description: >
Return -1 if predicate always returns a boolean false value.
info: |
Array.prototype.findLastIndex ( predicate[ , thisArg ] )
...
5. Repeat, while k 0,
...
c. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)).
...
6. Return -1.
features: [Symbol]
---*/
var arr = ['Shoes', 'Car', 'Bike'];
var called = 0;
var result = arr.findLastIndex(function(val) {
called++;
return false;
});
assert.sameValue(called, 3, 'predicate was called three times');
assert.sameValue(result, -1);
result = arr.findLastIndex(function(val) {
return '';
});
assert.sameValue(result, -1, 'coerced string');
result = arr.findLastIndex(function(val) {
return undefined;
});
assert.sameValue(result, -1, 'coerced undefined');
result = arr.findLastIndex(function(val) {
return null;
});
assert.sameValue(result, -1, 'coerced null');
result = arr.findLastIndex(function(val) {
return 0;
});
assert.sameValue(result, -1, 'coerced 0');
result = arr.findLastIndex(function(val) {
return NaN;
});
assert.sameValue(result, -1, 'coerced NaN');