Skip to content
Snippets Groups Projects
Commit f554f68a authored by jugglinmike's avatar jugglinmike Committed by Tom Care
Browse files

Improve coverage for section 21: String (#712)

This changeset increases coverage for section 21, specifically "21.1 String
Objects".

* Add tests for "this" validation of String methods

* Add tests for ToNumber as used by String methods

* Add test for `length` prop of exotic String objs

* fixup! Add test for `length` prop of exotic String objs
parent 021d4482
No related branches found
No related tags found
No related merge requests found
Showing
with 441 additions and 0 deletions
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-properties-of-string-instances-length
es6id: 21.1.4.1
description: The "length" property of String objects
info: |
[...]
4. Return ? StringCreate(s, ? GetPrototypeFromConstructor(NewTarget,
"%StringPrototype%")).
includes: [propertyHelper.js]
---*/
var str = new String('');
verifyNotEnumerable(str, 'length');
verifyNotWritable(str, 'length');
verifyNotConfigurable(str, 'length');
assert.sameValue(str.length, 0, 'empty string');
str = new String(' ');
assert.sameValue(str.length, 1, 'whitespace');
str = new String(' \b ');
assert.sameValue(str.length, 3, 'character escape (U+008, "backspace")');
str = new String('\ud834\udf06');
assert.sameValue(str.length, 2, 'Unicode escape (surrogate pair)');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.charat
es6id: 21.1.3.1
description: Error when attempting to coerce providec "pos" to a Number
info: |
[...]
3. Let position be ? ToInteger(pos).
[...]
7.1.4 ToInteger
1. Let number be ? ToNumber(argument).
---*/
var noCoerce = Object.create(null);
assert.throws(TypeError, function() {
''.charAt(noCoerce);
});
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.charat
es6id: 21.1.3.1
description: Coercion of "pos" string value into number
info: |
[...]
3. Let position be ? ToInteger(pos).
[...]
7.1.4 ToInteger
1. Let number be ? ToNumber(argument).
---*/
assert.sameValue('abcd'.charAt(' +00200.0000E-0002 '), 'c');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.charat
es6id: 21.1.3.1
description: Rounding of the provided "pos" number
info: |
[...]
3. Let position be ? ToInteger(pos).
[...]
7.1.4 ToInteger
1. Let number be ? ToNumber(argument).
2. If number is NaN, return +0.
3. If number is +0, -0, +∞, or -∞, return number.
4. Return the number value that is the same sign as number and whose
magnitude is floor(abs(number)).
---*/
assert.sameValue('abc'.charAt(-0.99999), 'a', '-0.99999');
assert.sameValue('abc'.charAt(-0.00001), 'a', '-0.00001');
assert.sameValue('abc'.charAt(0.00001), 'a', '0.00001');
assert.sameValue('abc'.charAt(0.99999), 'a', '0.99999');
assert.sameValue('abc'.charAt(1.00001), 'b', '1.00001');
assert.sameValue('abc'.charAt(1.99999), 'b', '1.99999');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.charat
es6id: 21.1.3.1
description: The "this" value must be object-coercible
info: |
1. Let O be ? RequireObjectCoercible(this value).
---*/
var charAt = String.prototype.charAt;
assert.sameValue(typeof charAt, 'function');
assert.throws(TypeError, function() {
charAt.call(undefined, 0);
}, 'undefined');
assert.throws(TypeError, function() {
charAt.call(null, 0);
}, 'null');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.charcodeat
es6id: 21.1.3.2
description: Error when attempting to coerce providec "pos" to a Number
info: |
[...]
3. Let position be ? ToInteger(pos).
[...]
7.1.4 ToInteger
1. Let number be ? ToNumber(argument).
---*/
var noCoerce = Object.create(null);
assert.throws(TypeError, function() {
''.charCodeAt(noCoerce);
});
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.charcodeat
es6id: 21.1.3.2
description: Coercion of "pos" string value into number
info: |
[...]
3. Let position be ? ToInteger(pos).
[...]
7.1.4 ToInteger
1. Let number be ? ToNumber(argument).
---*/
var cCode = 99;
assert.sameValue('abcd'.charCodeAt(' +00200.0000E-0002 '), cCode);
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.charcodeat
es6id: 21.1.3.2
description: Rounding of the provided "pos" number
info: |
[...]
3. Let position be ? ToInteger(pos).
[...]
7.1.4 ToInteger
1. Let number be ? ToNumber(argument).
2. If number is NaN, return +0.
3. If number is +0, -0, +∞, or -∞, return number.
4. Return the number value that is the same sign as number and whose
magnitude is floor(abs(number)).
---*/
var aCode = 97;
var bCode = 98;
assert.sameValue('abc'.charCodeAt(-0.99999), aCode, '-0.99999');
assert.sameValue('abc'.charCodeAt(-0.00001), aCode, '-0.00001');
assert.sameValue('abc'.charCodeAt(0.00001), aCode, '0.00001');
assert.sameValue('abc'.charCodeAt(0.99999), aCode, '0.99999');
assert.sameValue('abc'.charCodeAt(1.00001), bCode, '1.00001');
assert.sameValue('abc'.charCodeAt(1.99999), bCode, '1.99999');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.charcodeat
es6id: 21.1.3.2
description: The "this" value must be object-coercible
info: |
1. Let O be ? RequireObjectCoercible(this value).
---*/
var charCodeAt = String.prototype.charCodeAt;
assert.sameValue(typeof charCodeAt, 'function');
assert.throws(TypeError, function() {
charCodeAt.call(undefined, 0);
}, 'undefined');
assert.throws(TypeError, function() {
charCodeAt.call(null, 0);
}, 'null');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.concat
es6id: 21.1.3.4
description: The "this" value must be object-coercible
info: |
1. Let O be ? RequireObjectCoercible(this value).
---*/
var concat = String.prototype.concat;
assert.sameValue(typeof concat, 'function');
assert.throws(TypeError, function() {
concat.call(undefined, '');
}, 'undefined');
assert.throws(TypeError, function() {
concat.call(null, '');
}, 'null');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.indexof
es6id: 21.1.3.8
description: The "this" value must be object-coercible
info: |
1. Let O be ? RequireObjectCoercible(this value).
---*/
var indexOf = String.prototype.indexOf;
assert.sameValue(typeof indexOf, 'function');
assert.throws(TypeError, function() {
indexOf.call(undefined, '');
}, 'undefined');
assert.throws(TypeError, function() {
indexOf.call(null, '');
}, 'null');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.lastindexof
es6id: 21.1.3.9
description: The "this" value must be object-coercible
info: |
1. Let O be ? RequireObjectCoercible(this value).
---*/
var lastIndexOf = String.prototype.lastIndexOf;
assert.sameValue(typeof lastIndexOf, 'function');
assert.throws(TypeError, function() {
lastIndexOf.call(undefined, '');
}, 'undefined');
assert.throws(TypeError, function() {
lastIndexOf.call(null, '');
}, 'null');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.localecompare
es6id: 21.1.3.10
description: The "this" value must be object-coercible
info: |
1. Let O be ? RequireObjectCoercible(this value).
---*/
var localeCompare = String.prototype.localeCompare;
assert.sameValue(typeof localeCompare, 'function');
assert.throws(TypeError, function() {
localeCompare.call(undefined, '');
}, 'undefined');
assert.throws(TypeError, function() {
localeCompare.call(null, '');
}, 'null');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.match
es6id: 21.1.3.11
description: The "this" value must be object-coercible
info: |
1. Let O be ? RequireObjectCoercible(this value).
---*/
var match = String.prototype.match;
assert.sameValue(typeof match, 'function');
assert.throws(TypeError, function() {
match.call(undefined, /./);
}, 'undefined');
assert.throws(TypeError, function() {
match.call(null, /./);
}, 'null');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.replace
es6id: 21.1.3.14
description: The "this" value must be object-coercible
info: |
1. Let O be ? RequireObjectCoercible(this value).
---*/
var replace = String.prototype.replace;
assert.sameValue(typeof replace, 'function');
assert.throws(TypeError, function() {
replace.call(undefined, '', '');
}, 'undefined');
assert.throws(TypeError, function() {
replace.call(null, '', '');
}, 'null');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.search
es6id: 21.1.3.15
description: The "this" value must be object-coercible
info: |
1. Let O be ? RequireObjectCoercible(this value).
---*/
var search = String.prototype.search;
assert.sameValue(typeof search, 'function');
assert.throws(TypeError, function() {
search.call(undefined, /./);
}, 'undefined');
assert.throws(TypeError, function() {
search.call(null, /./);
}, 'null');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.slice
es6id: 21.1.3.16
description: The "this" value must be object-coercible
info: |
1. Let O be ? RequireObjectCoercible(this value).
---*/
var slice = String.prototype.slice;
assert.sameValue(typeof slice, 'function');
assert.throws(TypeError, function() {
slice.call(undefined, 0);
}, 'undefined');
assert.throws(TypeError, function() {
slice.call(null, 0);
}, 'null');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.split
es6id: 21.1.3.17
description: The "this" value must be object-coercible
info: |
1. Let O be ? RequireObjectCoercible(this value).
---*/
var split = String.prototype.split;
assert.sameValue(typeof split, 'function');
assert.throws(TypeError, function() {
split.call(undefined, '');
}, 'undefined');
assert.throws(TypeError, function() {
split.call(null, '');
}, 'null');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.substring
es6id: 21.1.3.19
description: The "this" value must be object-coercible
info: |
1. Let O be ? RequireObjectCoercible(this value).
---*/
var substring = String.prototype.substring;
assert.sameValue(typeof substring, 'function');
assert.throws(TypeError, function() {
substring.call(undefined, 0, 1);
}, 'undefined');
assert.throws(TypeError, function() {
substring.call(null, 0, 1);
}, 'null');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.tolocalelowercase
es6id: 21.1.3.20
description: The "this" value must be object-coercible
info: |
This function works exactly the same as toLowerCase except that its result is
intended to yield the correct result for the host environment's current
locale, rather than a locale-independent result.
21.1.3.24 String.prototype.toLowerCase
1. Let O be ? RequireObjectCoercible(this value).
---*/
var toLocaleLowerCase = String.prototype.toLocaleLowerCase;
assert.sameValue(typeof toLocaleLowerCase, 'function');
assert.throws(TypeError, function() {
toLocaleLowerCase.call(undefined);
}, 'undefined');
assert.throws(TypeError, function() {
toLocaleLowerCase.call(null);
}, 'null');
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment