String.prototype.endsWith

This commit is contained in:
Leonardo Balter 2015-07-16 17:38:39 -04:00
parent bdd84fb6ae
commit cc4230564a
20 changed files with 548 additions and 0 deletions

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.
/*---
es6id: 21.1.3.6
description: >
Returns based on coerced values of endPosition.
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
...
10. If endPosition is undefined, let pos be len, else let pos be
ToInteger(endPosition).
11. ReturnIfAbrupt(pos).
12. Let end be min(max(pos, 0), len).
13. Let searchLength be the number of elements in searchStr.
14. Let start be end - searchLength.
15. If start is less than 0, return false.
16. If the sequence of elements of S starting at start of length searchLength
is the same as the full element sequence of searchStr, return true.
17. Otherwise, return false.
...
---*/
var str = 'The future is cool!';
assert(str.endsWith('', NaN), 'NaN coerced to 0');
assert(str.endsWith('', null), 'null coerced to 0');
assert(str.endsWith('', false), 'false coerced to 0');
assert(str.endsWith('', ''), '"" coerced to 0');
assert(str.endsWith('', '0'), '"0" coerced to 0');
assert(str.endsWith('', undefined), 'undefined coerced to 0');
assert(str.endsWith('The future', 10.4), '10.4 coerced to 10');
assert(str.endsWith('T', true), 'true coerced to 1');
assert(str.endsWith('The future', '10'), '"10" coerced to 10');

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

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: 21.1.3.6
description: >
String.prototype.endsWith.length value and descriptor.
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
The length property of the endsWith method is 1.
includes: [propertyHelper.js]
---*/
assert.sameValue(
String.prototype.endsWith.length, 1,
'The value of `String.prototype.endsWith.length` is `1`'
);
verifyNotEnumerable(String.prototype.endsWith, 'length');
verifyNotWritable(String.prototype.endsWith, 'length');
verifyConfigurable(String.prototype.endsWith, '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: 21.1.3.6
description: >
String.prototype.endsWith.name value and descriptor.
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
assert.sameValue(
String.prototype.endsWith.name, 'endsWith',
'The value of `String.prototype.endsWith.name` is `"endsWith"`'
);
verifyNotEnumerable(String.prototype.endsWith, 'name');
verifyNotWritable(String.prototype.endsWith, 'name');
verifyConfigurable(String.prototype.endsWith, 'name');

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: 21.1.3.6
description: >
Returns abrupt from ToInteger(endPosition) as a Symbol.
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
...
10. If endPosition is undefined, let pos be len, else let pos be
ToInteger(endPosition).
11. ReturnIfAbrupt(pos).
...
features: [Symbol]
---*/
var position = Symbol();
assert.throws(TypeError, function() {
''.endsWith('', position);
});

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: 21.1.3.6
description: >
Returns abrupt from ToInteger(endPosition).
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
...
10. If endPosition is undefined, let pos be len, else let pos be
ToInteger(endPosition).
11. ReturnIfAbrupt(pos).
...
---*/
var position = {
valueOf: function() {
throw new Test262Error();
}
};
assert.throws(Test262Error, function() {
''.endsWith('', position);
});

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: 21.1.3.6
description: >
Returns abrupt from ToString(searchString) as a Symbol
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
...
7. Let searchStr be ToString(searchString).
8. ReturnIfAbrupt(searchStr).
...
features: [Symbol]
---*/
var s = Symbol();
assert.throws(TypeError, function() {
''.endsWith(s);
});

View File

@ -0,0 +1,42 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 21.1.3.6
description: >
Returns abrupt from IsRegExp(searchString).
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
...
4. Let isRegExp be IsRegExp(searchString).
5. ReturnIfAbrupt(isRegExp).
...
7.2.8 IsRegExp ( argument )
2. Let isRegExp be Get(argument, @@match).
3. ReturnIfAbrupt(isRegExp).
features: [Symbol.match]
---*/
var obj = {};
Object.defineProperty(obj, Symbol.match, {
get: function() {
throw new Test262Error();
}
});
assert.throws(Test262Error, function() {
''.endsWith(obj);
});
var regexp = /./;
Object.defineProperty(regexp, Symbol.match, {
get: function() {
throw new Test262Error();
}
});
assert.throws(Test262Error, function() {
''.endsWith(regexp);
});

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: 21.1.3.6
description: >
Returns abrupt from ToString(searchString)
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
...
7. Let searchStr be ToString(searchString).
8. ReturnIfAbrupt(searchStr).
...
---*/
var obj = {
toString: function() {
throw new Test262Error();
}
};
assert.throws(Test262Error, function() {
''.endsWith(obj);
});

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: 21.1.3.6
description: >
Returns abrupt from ToString(this) where this is a Symbol
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
1. Let O be RequireObjectCoercible(this value).
2. Let S be ToString(O).
3. ReturnIfAbrupt(S).
...
features: [Symbol]
---*/
var s = Symbol('');
assert.throws(TypeError, function() {
String.prototype.endsWith.call(s, '');
});

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.
/*---
es6id: 21.1.3.6
description: >
Returns abrupt from ToString(this)
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
1. Let O be RequireObjectCoercible(this value).
2. Let S be ToString(O).
3. ReturnIfAbrupt(S).
---*/
var o = {
toString: function() {
throw new Test262Error();
}
};
assert.throws(Test262Error, function() {
String.prototype.endsWith.call(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: 21.1.3.6
description: >
Returns false if search start is less than 0.
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
...
9. Let len be the number of elements in S.
10. If endPosition is undefined, let pos be len, else let pos be
ToInteger(endPosition).
11. ReturnIfAbrupt(pos).
12. Let end be min(max(pos, 0), len).
13. Let searchLength be the number of elements in searchStr.
14. Let start be end - searchLength.
15. If start is less than 0, return false.
...
Note: (min(max(pos, 0), len) - searchString.length) < 0;
---*/
assert.sameValue(
'web'.endsWith('w', 0), false,
'"web".endsWith("w", 0) returns false'
);
assert.sameValue(
'Bob'.endsWith(' Bob'), false,
'"Bob".endsWith(" Bob") returns false'
);

View File

@ -0,0 +1,48 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 21.1.3.6
description: >
Returns true if searchString.length == 0.
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
...
10. If endPosition is undefined, let pos be len, else let pos be
ToInteger(endPosition).
11. ReturnIfAbrupt(pos).
12. Let end be min(max(pos, 0), len).
13. Let searchLength be the number of elements in searchStr.
14. Let start be end - searchLength.
15. If start is less than 0, return false.
16. If the sequence of elements of S starting at start of length searchLength
is the same as the full element sequence of searchStr, return true.
...
---*/
var str = 'The future is cool!';
assert(
str.endsWith(''),
'str.endsWith("") returns true'
);
assert(
str.endsWith('', str.length),
'str.endsWith("", str.length) returns true'
);
assert(
str.endsWith('', Infinity),
'str.endsWith("", Infinity) returns true'
);
assert(
str.endsWith('', -1),
'str.endsWith("", -1) returns true'
);
assert(
str.endsWith('', -Infinity),
'str.endsWith("", -Infinity) returns true'
);

View File

@ -0,0 +1,37 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 21.1.3.6
description: >
Returns true if searchString appears as a substring of the given string with a
given position.
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
...
10. If endPosition is undefined, let pos be len, else let pos be
ToInteger(endPosition).
11. ReturnIfAbrupt(pos).
12. Let end be min(max(pos, 0), len).
13. Let searchLength be the number of elements in searchStr.
14. Let start be end - searchLength.
15. If start is less than 0, return false.
16. If the sequence of elements of S starting at start of length searchLength
is the same as the full element sequence of searchStr, return true.
...
---*/
var str = 'The future is cool!';
assert(
str.endsWith('The future', 10),
'str.endsWith("The future", 10) === true'
);
assert(
str.endsWith('future', 10),
'str.endsWith("future", 10) === true'
);
assert(
str.endsWith(' is cool!', str.length),
'str.endsWith(" is cool!", str.length) === true'
);

View File

@ -0,0 +1,27 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 21.1.3.6
description: >
Returns true if searchString appears as a substring of the given string.
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
...
10. If endPosition is undefined, let pos be len, else let pos be
ToInteger(endPosition).
11. ReturnIfAbrupt(pos).
12. Let end be min(max(pos, 0), len).
13. Let searchLength be the number of elements in searchStr.
14. Let start be end - searchLength.
15. If start is less than 0, return false.
16. If the sequence of elements of S starting at start of length searchLength
is the same as the full element sequence of searchStr, return true.
...
---*/
var str = 'The future is cool!';
assert(str.endsWith('cool!'), 'str.endsWith("cool!") === true');
assert(str.endsWith('!'), 'str.endsWith("!") === true');
assert(str.endsWith(str), 'str.endsWith(str) === true');

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: 21.1.3.6
description: >
Throws a TypeError if searchString is a RegExp.
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
...
4. Let isRegExp be IsRegExp(searchString).
5. ReturnIfAbrupt(isRegExp).
6. If isRegExp is true, throw a TypeError exception.
...
---*/
var searchString = /./;
assert.throws(TypeError, function() {
''.endsWith(searchString);
});

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.
/*---
es6id: 21.1.3.6
description: >
Returns false if searchString is not found with a given position.
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
...
10. If endPosition is undefined, let pos be len, else let pos be
ToInteger(endPosition).
11. ReturnIfAbrupt(pos).
12. Let end be min(max(pos, 0), len).
13. Let searchLength be the number of elements in searchStr.
14. Let start be end - searchLength.
15. If start is less than 0, return false.
16. If the sequence of elements of S starting at start of length searchLength
is the same as the full element sequence of searchStr, return true.
17. Otherwise, return false.
...
---*/
var str = 'The future is cool!';
assert.sameValue(
str.endsWith('is cool!', str.length - 1), false,
'str.endsWith("is cool!", str.length - 1) === false'
);
assert.sameValue(
str.endsWith('!', 1), false,
'str.endsWith("!", 1) === false'
);

View File

@ -0,0 +1,37 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 21.1.3.6
description: >
Returns false if searchString is not found.
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
...
10. If endPosition is undefined, let pos be len, else let pos be
ToInteger(endPosition).
11. ReturnIfAbrupt(pos).
12. Let end be min(max(pos, 0), len).
13. Let searchLength be the number of elements in searchStr.
14. Let start be end - searchLength.
15. If start is less than 0, return false.
16. If the sequence of elements of S starting at start of length searchLength
is the same as the full element sequence of searchStr, return true.
17. Otherwise, return false.
...
---*/
var str = 'The future is cool!';
assert.sameValue(
str.endsWith('is Flash!'), false,
'str.endsWith("is Flash!") === false'
);
assert.sameValue(
str.endsWith('IS COOL!'), false,
'endsWith is case sensitive'
);
assert.sameValue(
str.endsWith('The future'), false,
'str.endsWith("The future") === false'
);

View File

@ -0,0 +1,16 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 21.1.3.6
description: >
Throws TypeError when `this` is null
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
1. Let O be RequireObjectCoercible(this value).
2. Let S be ToString(O).
---*/
assert.throws(TypeError, function() {
String.prototype.endsWith.call(null, '');
});

View File

@ -0,0 +1,16 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 21.1.3.6
description: >
Throws TypeError when `this` is undefined
info: >
21.1.3.6 String.prototype.endsWith ( searchString [ , endPosition] )
1. Let O be RequireObjectCoercible(this value).
2. Let S be ToString(O).
---*/
assert.throws(TypeError, function() {
String.prototype.endsWith.call(undefined, '');
});