From c6c61bd50dc67fb70a07f6ac9f7b2e5a620e1f92 Mon Sep 17 00:00:00 2001 From: Leonardo Balter Date: Thu, 9 Jul 2015 17:53:23 -0400 Subject: [PATCH] String.prototype.codePointAt --- .../prototype/codePointAt/codePointAt.js | 22 +++++++++++ .../String/prototype/codePointAt/length.js | 22 +++++++++++ .../String/prototype/codePointAt/name.js | 22 +++++++++++ ...eturn-abrupt-from-object-pos-to-integer.js | 25 ++++++++++++ ...eturn-abrupt-from-symbol-pos-to-integer.js | 22 +++++++++++ .../return-abrupt-from-this-as-symbol.js | 20 ++++++++++ .../codePointAt/return-abrupt-from-this.js | 23 +++++++++++ .../return-code-unit-coerced-position.js | 26 +++++++++++++ .../codePointAt/return-first-code-unit.js | 35 +++++++++++++++++ .../codePointAt/return-single-code-unit.js | 33 ++++++++++++++++ .../codePointAt/return-utf16-decode.js | 39 +++++++++++++++++++ ...ned-on-position-equal-or-more-than-size.js | 21 ++++++++++ ...ns-undefined-on-position-less-than-zero.js | 20 ++++++++++ .../codePointAt/this-is-null-throws.js | 16 ++++++++ .../codePointAt/this-is-undefined-throws.js | 16 ++++++++ 15 files changed, 362 insertions(+) create mode 100644 test/built-ins/String/prototype/codePointAt/codePointAt.js create mode 100644 test/built-ins/String/prototype/codePointAt/length.js create mode 100644 test/built-ins/String/prototype/codePointAt/name.js create mode 100644 test/built-ins/String/prototype/codePointAt/return-abrupt-from-object-pos-to-integer.js create mode 100644 test/built-ins/String/prototype/codePointAt/return-abrupt-from-symbol-pos-to-integer.js create mode 100644 test/built-ins/String/prototype/codePointAt/return-abrupt-from-this-as-symbol.js create mode 100644 test/built-ins/String/prototype/codePointAt/return-abrupt-from-this.js create mode 100644 test/built-ins/String/prototype/codePointAt/return-code-unit-coerced-position.js create mode 100644 test/built-ins/String/prototype/codePointAt/return-first-code-unit.js create mode 100644 test/built-ins/String/prototype/codePointAt/return-single-code-unit.js create mode 100644 test/built-ins/String/prototype/codePointAt/return-utf16-decode.js create mode 100644 test/built-ins/String/prototype/codePointAt/returns-undefined-on-position-equal-or-more-than-size.js create mode 100644 test/built-ins/String/prototype/codePointAt/returns-undefined-on-position-less-than-zero.js create mode 100644 test/built-ins/String/prototype/codePointAt/this-is-null-throws.js create mode 100644 test/built-ins/String/prototype/codePointAt/this-is-undefined-throws.js diff --git a/test/built-ins/String/prototype/codePointAt/codePointAt.js b/test/built-ins/String/prototype/codePointAt/codePointAt.js new file mode 100644 index 0000000000..62324d612e --- /dev/null +++ b/test/built-ins/String/prototype/codePointAt/codePointAt.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: 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'); diff --git a/test/built-ins/String/prototype/codePointAt/length.js b/test/built-ins/String/prototype/codePointAt/length.js new file mode 100644 index 0000000000..6dab7b836c --- /dev/null +++ b/test/built-ins/String/prototype/codePointAt/length.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: 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'); diff --git a/test/built-ins/String/prototype/codePointAt/name.js b/test/built-ins/String/prototype/codePointAt/name.js new file mode 100644 index 0000000000..e42c2bb518 --- /dev/null +++ b/test/built-ins/String/prototype/codePointAt/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: 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'); diff --git a/test/built-ins/String/prototype/codePointAt/return-abrupt-from-object-pos-to-integer.js b/test/built-ins/String/prototype/codePointAt/return-abrupt-from-object-pos-to-integer.js new file mode 100644 index 0000000000..a2fc781e73 --- /dev/null +++ b/test/built-ins/String/prototype/codePointAt/return-abrupt-from-object-pos-to-integer.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: 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); +}); diff --git a/test/built-ins/String/prototype/codePointAt/return-abrupt-from-symbol-pos-to-integer.js b/test/built-ins/String/prototype/codePointAt/return-abrupt-from-symbol-pos-to-integer.js new file mode 100644 index 0000000000..5bbdfa5285 --- /dev/null +++ b/test/built-ins/String/prototype/codePointAt/return-abrupt-from-symbol-pos-to-integer.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: 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); +}); diff --git a/test/built-ins/String/prototype/codePointAt/return-abrupt-from-this-as-symbol.js b/test/built-ins/String/prototype/codePointAt/return-abrupt-from-this-as-symbol.js new file mode 100644 index 0000000000..c8c27d960f --- /dev/null +++ b/test/built-ins/String/prototype/codePointAt/return-abrupt-from-this-as-symbol.js @@ -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); +}); diff --git a/test/built-ins/String/prototype/codePointAt/return-abrupt-from-this.js b/test/built-ins/String/prototype/codePointAt/return-abrupt-from-this.js new file mode 100644 index 0000000000..8088314b9a --- /dev/null +++ b/test/built-ins/String/prototype/codePointAt/return-abrupt-from-this.js @@ -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); +}); diff --git a/test/built-ins/String/prototype/codePointAt/return-code-unit-coerced-position.js b/test/built-ins/String/prototype/codePointAt/return-code-unit-coerced-position.js new file mode 100644 index 0000000000..ed8c343806 --- /dev/null +++ b/test/built-ins/String/prototype/codePointAt/return-code-unit-coerced-position.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: 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); diff --git a/test/built-ins/String/prototype/codePointAt/return-first-code-unit.js b/test/built-ins/String/prototype/codePointAt/return-first-code-unit.js new file mode 100644 index 0000000000..0546658e7a --- /dev/null +++ b/test/built-ins/String/prototype/codePointAt/return-first-code-unit.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: 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); diff --git a/test/built-ins/String/prototype/codePointAt/return-single-code-unit.js b/test/built-ins/String/prototype/codePointAt/return-single-code-unit.js new file mode 100644 index 0000000000..6f4ae90b00 --- /dev/null +++ b/test/built-ins/String/prototype/codePointAt/return-single-code-unit.js @@ -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); diff --git a/test/built-ins/String/prototype/codePointAt/return-utf16-decode.js b/test/built-ins/String/prototype/codePointAt/return-utf16-decode.js new file mode 100644 index 0000000000..873216b88e --- /dev/null +++ b/test/built-ins/String/prototype/codePointAt/return-utf16-decode.js @@ -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'); diff --git a/test/built-ins/String/prototype/codePointAt/returns-undefined-on-position-equal-or-more-than-size.js b/test/built-ins/String/prototype/codePointAt/returns-undefined-on-position-equal-or-more-than-size.js new file mode 100644 index 0000000000..216bf7029c --- /dev/null +++ b/test/built-ins/String/prototype/codePointAt/returns-undefined-on-position-equal-or-more-than-size.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: 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); diff --git a/test/built-ins/String/prototype/codePointAt/returns-undefined-on-position-less-than-zero.js b/test/built-ins/String/prototype/codePointAt/returns-undefined-on-position-less-than-zero.js new file mode 100644 index 0000000000..f20eb5544a --- /dev/null +++ b/test/built-ins/String/prototype/codePointAt/returns-undefined-on-position-less-than-zero.js @@ -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); diff --git a/test/built-ins/String/prototype/codePointAt/this-is-null-throws.js b/test/built-ins/String/prototype/codePointAt/this-is-null-throws.js new file mode 100644 index 0000000000..c092187c75 --- /dev/null +++ b/test/built-ins/String/prototype/codePointAt/this-is-null-throws.js @@ -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); +}); diff --git a/test/built-ins/String/prototype/codePointAt/this-is-undefined-throws.js b/test/built-ins/String/prototype/codePointAt/this-is-undefined-throws.js new file mode 100644 index 0000000000..961717a9c2 --- /dev/null +++ b/test/built-ins/String/prototype/codePointAt/this-is-undefined-throws.js @@ -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); +});