Merge pull request #365 from bocoup/codePointAt

Add tests for String.prototype.codePointAt
This commit is contained in:
Brian Terlson 2015-07-17 11:01:58 -07:00
commit 28089fe079
15 changed files with 362 additions and 0 deletions

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

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

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.3
description: >
Returns abrupt from ToInteger(pos)
info: >
21.1.3.3 String.prototype.codePointAt ( pos )
1. Let O be RequireObjectCoercible(this value).
2. Let S be ToString(O).
3. ReturnIfAbrupt(S).
4. Let position be ToInteger(pos).
5. ReturnIfAbrupt(position).
---*/
var o = {
valueOf: function() {
throw new Test262Error();
}
}
assert.throws(Test262Error, function() {
'abc'.codePointAt(o);
});

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.3
description: >
Returns abrupt from ToInteger(pos)
info: >
21.1.3.3 String.prototype.codePointAt ( pos )
1. Let O be RequireObjectCoercible(this value).
2. Let S be ToString(O).
3. ReturnIfAbrupt(S).
4. Let position be ToInteger(pos).
5. ReturnIfAbrupt(position).
features: [Symbol]
---*/
var s = Symbol(1);
assert.throws(TypeError, function() {
'abc'.codePointAt(s);
});

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.
/*---
es6id: 21.1.3.3
description: >
Returns abrupt from ToString(this) where this is a Symbol
info: >
21.1.3.3 String.prototype.codePointAt ( pos )
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.codePointAt.call(s, 1);
});

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.3
description: >
Returns abrupt from ToString(this)
info: >
21.1.3.3 String.prototype.codePointAt ( pos )
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.codePointAt.call(o, 1);
});

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: 21.1.3.3
description: >
Return value on coerced values on ToInteger(position).
info: >
21.1.3.3 String.prototype.codePointAt ( pos )
...
4. Let position be ToInteger(pos).
...
---*/
assert.sameValue('\uD800\uDC00'.codePointAt(''), 65536);
assert.sameValue('\uD800\uDC00'.codePointAt('0'), 65536);
assert.sameValue('\uD800\uDC00'.codePointAt(NaN), 65536);
assert.sameValue('\uD800\uDC00'.codePointAt(false), 65536);
assert.sameValue('\uD800\uDC00'.codePointAt(null), 65536);
assert.sameValue('\uD800\uDC00'.codePointAt(undefined), 65536);
assert.sameValue('\uD800\uDC00'.codePointAt([]), 65536);
assert.sameValue('\uD800\uDC00'.codePointAt('1'), 56320);
assert.sameValue('\uD800\uDC00'.codePointAt(true), 56320);
assert.sameValue('\uD800\uDC00'.codePointAt([1]), 56320);

View File

@ -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: 21.1.3.3
description: >
Returns code point of LeadSurrogate if not followed by a valid TrailSurrogate.
info: >
21.1.3.3 String.prototype.codePointAt ( pos )
...
8. Let first be the code unit value of the element at index position in the
String S.
9. If first < 0xD800 or first > 0xDBFF or position+1 = size, return first.
10. Let second be the code unit value of the element at index position+1 in
the String S.
11. If second < 0xDC00 or second > 0xDFFF, return first.
---*/
assert.sameValue('\uD800\uDBFF'.codePointAt(0), 0xD800);
assert.sameValue('\uD800\uE000'.codePointAt(0), 0xD800);
assert.sameValue('\uDAAA\uDBFF'.codePointAt(0), 0xDAAA);
assert.sameValue('\uDAAA\uE000'.codePointAt(0), 0xDAAA);
assert.sameValue('\uDBFF\uDBFF'.codePointAt(0), 0xDBFF);
assert.sameValue('\uDBFF\uE000'.codePointAt(0), 0xDBFF);
assert.sameValue('\uD800\u0000'.codePointAt(0), 0xD800);
assert.sameValue('\uD800\uFFFF'.codePointAt(0), 0xD800);
assert.sameValue('\uDAAA\u0000'.codePointAt(0), 0xDAAA);
assert.sameValue('\uDAAA\uFFFF'.codePointAt(0), 0xDAAA);
assert.sameValue('\uDBFF\uDBFF'.codePointAt(0), 0xDBFF);
assert.sameValue('\uDBFF\uFFFF'.codePointAt(0), 0xDBFF);

View File

@ -0,0 +1,33 @@
// 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.3
description: >
Return single code unit value of the element at index position.
info: >
21.1.3.3 String.prototype.codePointAt ( pos )
1. Let O be RequireObjectCoercible(this value).
2. Let S be ToString(O).
3. ReturnIfAbrupt(S).
4. Let position be ToInteger(pos).
5. ReturnIfAbrupt(position).
6. Let size be the number of elements in S.
7. If position < 0 or position size, return undefined.
8. Let first be the code unit value of the element at index position in the
String S.
9. If first < 0xD800 or first > 0xDBFF or position+1 = size, return first.
---*/
assert.sameValue('abc'.codePointAt(0), 97);
assert.sameValue('abc'.codePointAt(1), 98);
assert.sameValue('abc'.codePointAt(2), 99);
assert.sameValue('\uAAAA\uBBBB'.codePointAt(0), 0xAAAA);
assert.sameValue('\uD7FF\uAAAA'.codePointAt(0), 0xD7FF);
assert.sameValue('\uDC00\uAAAA'.codePointAt(0), 0xDC00);
assert.sameValue('\uAAAA\uBBBB'.codePointAt(0), 0xAAAA);
assert.sameValue('123\uD800'.codePointAt(3), 0xD800);
assert.sameValue('123\uDAAA'.codePointAt(3), 0xDAAA);
assert.sameValue('123\uDBFF'.codePointAt(3), 0xDBFF);

View File

@ -0,0 +1,39 @@
// 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.3
description: >
Return UTF16 Decode value of the lead and trail elements at index position.
info: >
21.1.3.3 String.prototype.codePointAt ( pos )
...
8. Let first be the code unit value of the element at index position in the
String S.
9. If first < 0xD800 or first > 0xDBFF or position+1 = size, return first.
10. Let second be the code unit value of the element at index position+1 in
the String S.
11. If second < 0xDC00 or second > 0xDFFF, return first.
12. Return UTF16Decode(first, second).
10.1.2 Static Semantics: UTF16Decode( lead, trail )
Two code units, lead and trail, that form a UTF-16 surrogate pair are
converted to a code point by performing the following steps:
1. Assert: 0xD800 lead 0xDBFF and 0xDC00 trail 0xDFFF.
2. Let cp be (lead 0xD800) × 1024 + (trail 0xDC00) + 0x10000.
3. Return the code point cp.
---*/
assert.sameValue('\uD800\uDC00'.codePointAt(0), 65536, 'U+10000');
assert.sameValue('\uD800\uDDD0'.codePointAt(0), 66000, 'U+101D0');
assert.sameValue('\uD800\uDFFF'.codePointAt(0), 66559, 'U+103FF');
assert.sameValue('\uDAAA\uDC00'.codePointAt(0), 763904, 'U+BA800');
assert.sameValue('\uDAAA\uDDD0'.codePointAt(0), 764368, 'U+BA9D0');
assert.sameValue('\uDAAA\uDFFF'.codePointAt(0), 764927, 'U+BABFF');
assert.sameValue('\uDBFF\uDC00'.codePointAt(0), 1113088, 'U+10FC00');
assert.sameValue('\uDBFF\uDDD0'.codePointAt(0), 1113552, 'U+10FDD0');
assert.sameValue('\uDBFF\uDFFF'.codePointAt(0), 1114111, 'U+10FFFF');

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.3
description: >
If pos >= size, return undefined
info: >
21.1.3.3 String.prototype.codePointAt ( pos )
1. Let O be RequireObjectCoercible(this value).
2. Let S be ToString(O).
3. ReturnIfAbrupt(S).
4. Let position be ToInteger(pos).
5. ReturnIfAbrupt(position).
6. Let size be the number of elements in S.
7. If position < 0 or position size, return undefined.
---*/
assert.sameValue('abc'.codePointAt(3), undefined);
assert.sameValue('abc'.codePointAt(4), undefined);
assert.sameValue('abc'.codePointAt(Infinity), undefined);

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.
/*---
es6id: 21.1.3.3
description: >
If pos < size, return undefined
info: >
21.1.3.3 String.prototype.codePointAt ( pos )
1. Let O be RequireObjectCoercible(this value).
2. Let S be ToString(O).
3. ReturnIfAbrupt(S).
4. Let position be ToInteger(pos).
5. ReturnIfAbrupt(position).
6. Let size be the number of elements in S.
7. If position < 0 or position size, return undefined.
---*/
assert.sameValue('abc'.codePointAt(-1), undefined);
assert.sameValue('abc'.codePointAt(-Infinity), undefined);

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.3
description: >
Throws TypeError when `this` is null
info: >
21.1.3.3 String.prototype.codePointAt ( pos )
1. Let O be RequireObjectCoercible(this value).
2. Let S be ToString(O).
---*/
assert.throws(TypeError, function() {
String.prototype.codePointAt.call(null, 1);
});

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.3
description: >
Throws TypeError when `this` is undefined
info: >
21.1.3.3 String.prototype.codePointAt ( pos )
1. Let O be RequireObjectCoercible(this value).
2. Let S be ToString(O).
---*/
assert.throws(TypeError, function() {
String.prototype.codePointAt.call(undefined, 1);
});