From 2c33050732941612870343e76fcd8a574fb4e8a4 Mon Sep 17 00:00:00 2001 From: jugglinmike Date: Thu, 19 May 2016 20:18:20 -0400 Subject: [PATCH] Add tests for Annex B String.prototype.substr (#628) --- .../String/prototype/substr/length-falsey.js | 37 +++++++++++++++++++ .../prototype/substr/length-negative.js | 31 ++++++++++++++++ .../prototype/substr/length-positive.js | 36 ++++++++++++++++++ .../prototype/substr/length-to-int-err.js | 27 ++++++++++++++ .../String/prototype/substr/length-undef.js | 34 +++++++++++++++++ .../String/prototype/substr/start-negative.js | 17 +++++++++ .../prototype/substr/start-to-int-err.js | 35 ++++++++++++++++++ .../prototype/substr/surrogate-pairs.js | 24 ++++++++++++ .../prototype/substr/this-non-obj-coerce.js | 21 +++++++++++ .../prototype/substr/this-to-str-err.js | 21 +++++++++++ 10 files changed, 283 insertions(+) create mode 100644 test/annexB/built-ins/String/prototype/substr/length-falsey.js create mode 100644 test/annexB/built-ins/String/prototype/substr/length-negative.js create mode 100644 test/annexB/built-ins/String/prototype/substr/length-positive.js create mode 100644 test/annexB/built-ins/String/prototype/substr/length-to-int-err.js create mode 100644 test/annexB/built-ins/String/prototype/substr/length-undef.js create mode 100644 test/annexB/built-ins/String/prototype/substr/start-negative.js create mode 100644 test/annexB/built-ins/String/prototype/substr/start-to-int-err.js create mode 100644 test/annexB/built-ins/String/prototype/substr/surrogate-pairs.js create mode 100644 test/annexB/built-ins/String/prototype/substr/this-non-obj-coerce.js create mode 100644 test/annexB/built-ins/String/prototype/substr/this-to-str-err.js diff --git a/test/annexB/built-ins/String/prototype/substr/length-falsey.js b/test/annexB/built-ins/String/prototype/substr/length-falsey.js new file mode 100644 index 0000000000..dfced7b74a --- /dev/null +++ b/test/annexB/built-ins/String/prototype/substr/length-falsey.js @@ -0,0 +1,37 @@ +// 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.substr +es6id: B.2.3.1 +description: > + Behavior when "length" is a false-converting value other than `undefined` +info: | + [...] + 4. If length is undefined, let end be +∞; otherwise let end be ? + ToInteger(length). + [...] + 7. Let resultLength be min(max(end, 0), size - intStart). + 8. If resultLength ≤ 0, return the empty String "". + 9. Return a String containing resultLength consecutive code units from S + beginning with the code unit at index intStart. +---*/ + +assert.sameValue('abc'.substr(0, false), '', 'start: 0, length: false'); +assert.sameValue('abc'.substr(1, false), '', 'start: 1, length: false'); +assert.sameValue('abc'.substr(2, false), '', 'start: 2, length: false'); +assert.sameValue('abc'.substr(3, false), '', 'start: 3, length: false'); + +assert.sameValue('abc'.substr(0, NaN), '', 'start: 0, length: NaN'); +assert.sameValue('abc'.substr(1, NaN), '', 'start: 1, length: NaN'); +assert.sameValue('abc'.substr(2, NaN), '', 'start: 2, length: NaN'); +assert.sameValue('abc'.substr(3, NaN), '', 'start: 3, length: NaN'); + +assert.sameValue('abc'.substr(0, ''), '', 'start: 0, length: ""'); +assert.sameValue('abc'.substr(1, ''), '', 'start: 1, length: ""'); +assert.sameValue('abc'.substr(2, ''), '', 'start: 2, length: ""'); +assert.sameValue('abc'.substr(3, ''), '', 'start: 3, length: ""'); + +assert.sameValue('abc'.substr(0, null), '', 'start: 0, length: null'); +assert.sameValue('abc'.substr(1, null), '', 'start: 1, length: null'); +assert.sameValue('abc'.substr(2, null), '', 'start: 2, length: null'); +assert.sameValue('abc'.substr(3, null), '', 'start: 3, length: null'); diff --git a/test/annexB/built-ins/String/prototype/substr/length-negative.js b/test/annexB/built-ins/String/prototype/substr/length-negative.js new file mode 100644 index 0000000000..de45d4a371 --- /dev/null +++ b/test/annexB/built-ins/String/prototype/substr/length-negative.js @@ -0,0 +1,31 @@ +// 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.substr +es6id: B.2.3.1 +description: Behavior when "length" is a negative number +info: | + [...] + 7. Let resultLength be min(max(end, 0), size - intStart). + 8. If resultLength ≤ 0, return the empty String "". +---*/ + +assert.sameValue('abc'.substr(0, -1), '', '0, -1'); +assert.sameValue('abc'.substr(0, -2), '', '0, -2'); +assert.sameValue('abc'.substr(0, -3), '', '0, -3'); +assert.sameValue('abc'.substr(0, -4), '', '0, -4'); + +assert.sameValue('abc'.substr(1, -1), '', '1, -1'); +assert.sameValue('abc'.substr(1, -2), '', '1, -2'); +assert.sameValue('abc'.substr(1, -3), '', '1, -3'); +assert.sameValue('abc'.substr(1, -4), '', '1, -4'); + +assert.sameValue('abc'.substr(2, -1), '', '2, -1'); +assert.sameValue('abc'.substr(2, -2), '', '2, -2'); +assert.sameValue('abc'.substr(2, -3), '', '2, -3'); +assert.sameValue('abc'.substr(2, -4), '', '2, -4'); + +assert.sameValue('abc'.substr(3, -1), '', '3, -1'); +assert.sameValue('abc'.substr(3, -2), '', '3, -2'); +assert.sameValue('abc'.substr(3, -3), '', '3, -3'); +assert.sameValue('abc'.substr(3, -4), '', '3, -4'); diff --git a/test/annexB/built-ins/String/prototype/substr/length-positive.js b/test/annexB/built-ins/String/prototype/substr/length-positive.js new file mode 100644 index 0000000000..82a9104703 --- /dev/null +++ b/test/annexB/built-ins/String/prototype/substr/length-positive.js @@ -0,0 +1,36 @@ +// 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.substr +es6id: B.2.3.1 +description: Behavior when "length" is a positive number +info: | + [...] + 4. If length is undefined, let end be +∞; otherwise let end be ? + ToInteger(length). + [...] + 7. Let resultLength be min(max(end, 0), size - intStart). + 8. If resultLength ≤ 0, return the empty String "". + 9. Return a String containing resultLength consecutive code units from S + beginning with the code unit at index intStart. +---*/ + +assert.sameValue('abc'.substr(0, 1), 'a', '0, 1'); +assert.sameValue('abc'.substr(0, 2), 'ab', '0, 1'); +assert.sameValue('abc'.substr(0, 3), 'abc', '0, 1'); +assert.sameValue('abc'.substr(0, 4), 'abc', '0, 1'); + +assert.sameValue('abc'.substr(1, 1), 'b', '1, 1'); +assert.sameValue('abc'.substr(1, 2), 'bc', '1, 1'); +assert.sameValue('abc'.substr(1, 3), 'bc', '1, 1'); +assert.sameValue('abc'.substr(1, 4), 'bc', '1, 1'); + +assert.sameValue('abc'.substr(2, 1), 'c', '2, 1'); +assert.sameValue('abc'.substr(2, 2), 'c', '2, 1'); +assert.sameValue('abc'.substr(2, 3), 'c', '2, 1'); +assert.sameValue('abc'.substr(2, 4), 'c', '2, 1'); + +assert.sameValue('abc'.substr(3, 1), '', '3, 1'); +assert.sameValue('abc'.substr(3, 2), '', '3, 1'); +assert.sameValue('abc'.substr(3, 3), '', '3, 1'); +assert.sameValue('abc'.substr(3, 4), '', '3, 1'); diff --git a/test/annexB/built-ins/String/prototype/substr/length-to-int-err.js b/test/annexB/built-ins/String/prototype/substr/length-to-int-err.js new file mode 100644 index 0000000000..6f90e1de80 --- /dev/null +++ b/test/annexB/built-ins/String/prototype/substr/length-to-int-err.js @@ -0,0 +1,27 @@ +// 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.substr +es6id: B.2.3.1 +description: > + Behavior when "length" integer conversion triggers an abrupt completion +info: | + [...] + 3. Let intStart be ? ToInteger(start). +features: [Symbol] +---*/ + +var symbol = Symbol(''); +var length = { + valueOf: function() { + throw new Test262Error(); + } +}; + +assert.throws(Test262Error, function() { + ''.substr(0, length); +}); + +assert.throws(TypeError, function() { + ''.substr(0, symbol); +}); diff --git a/test/annexB/built-ins/String/prototype/substr/length-undef.js b/test/annexB/built-ins/String/prototype/substr/length-undef.js new file mode 100644 index 0000000000..e677cd9752 --- /dev/null +++ b/test/annexB/built-ins/String/prototype/substr/length-undef.js @@ -0,0 +1,34 @@ +// 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.substr +es6id: B.2.3.1 +description: Behavior when "length" is not defined +info: | + [...] + 4. If length is undefined, let end be +∞; otherwise let end be ? + ToInteger(length). + [...] + 7. Let resultLength be min(max(end, 0), size - intStart). + 8. If resultLength ≤ 0, return the empty String "". + 9. Return a String containing resultLength consecutive code units from S + beginning with the code unit at index intStart. +---*/ + +assert.sameValue('abc'.substr(0), 'abc', 'start: 0, length: unspecified'); +assert.sameValue('abc'.substr(1), 'bc', 'start: 1, length: unspecified'); +assert.sameValue('abc'.substr(2), 'c', 'start: 2, length: unspecified'); +assert.sameValue('abc'.substr(3), '', 'start: 3, length: unspecified'); + +assert.sameValue( + 'abc'.substr(0, undefined), 'abc', 'start: 0, length: undefined' +); +assert.sameValue( + 'abc'.substr(1, undefined), 'bc', 'start: 1, length: undefined' +); +assert.sameValue( + 'abc'.substr(2, undefined), 'c', 'start: 2, length: undefined' +); +assert.sameValue( + 'abc'.substr(3, undefined), '', 'start: 3, length: undefined' +); diff --git a/test/annexB/built-ins/String/prototype/substr/start-negative.js b/test/annexB/built-ins/String/prototype/substr/start-negative.js new file mode 100644 index 0000000000..d0f51b8fd2 --- /dev/null +++ b/test/annexB/built-ins/String/prototype/substr/start-negative.js @@ -0,0 +1,17 @@ +// 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.substr +es6id: B.2.3.1 +description: Behavior when "start" is a negative number +info: | + [...] + 6. If intStart < 0, let intStart be max(size + intStart, 0). +---*/ + +assert.sameValue('abc'.substr(-1), 'c'); +assert.sameValue('abc'.substr(-2), 'bc'); +assert.sameValue('abc'.substr(-3), 'abc'); +assert.sameValue('abc'.substr(-4), 'abc', 'size + intStart < 0'); + +assert.sameValue('abc'.substr(-1.1), 'c', 'floating point rounding semantics'); diff --git a/test/annexB/built-ins/String/prototype/substr/start-to-int-err.js b/test/annexB/built-ins/String/prototype/substr/start-to-int-err.js new file mode 100644 index 0000000000..5dd4d4892c --- /dev/null +++ b/test/annexB/built-ins/String/prototype/substr/start-to-int-err.js @@ -0,0 +1,35 @@ +// 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.substr +es6id: B.2.3.1 +description: > + Behavior when "start" integer conversion triggers an abrupt completion +info: | + [...] + 3. Let intStart be ? ToInteger(start). +features: [Symbol] +---*/ + +var lengthCallCount = 0; +var symbol = Symbol(''); +var start = { + valueOf: function() { + throw new Test262Error(); + } +}; +var length = { + valueOf: function() { + lengthCallCount += 1; + } +}; + +assert.throws(Test262Error, function() { + ''.substr(start, length); +}); + +assert.throws(TypeError, function() { + ''.substr(symbol, length); +}); + +assert.sameValue(lengthCallCount, 0); diff --git a/test/annexB/built-ins/String/prototype/substr/surrogate-pairs.js b/test/annexB/built-ins/String/prototype/substr/surrogate-pairs.js new file mode 100644 index 0000000000..87cd1bfc69 --- /dev/null +++ b/test/annexB/built-ins/String/prototype/substr/surrogate-pairs.js @@ -0,0 +1,24 @@ +// 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.substr +es6id: B.2.3.1 +description: > + Behavior when input string contains a surrogate pair +info: | + [...] + 4. If length is undefined, let end be +∞; otherwise let end be ? + ToInteger(length). + [...] + 7. Let resultLength be min(max(end, 0), size - intStart). + 8. If resultLength ≤ 0, return the empty String "". + 9. Return a String containing resultLength consecutive code units from S + beginning with the code unit at index intStart. +---*/ + +assert.sameValue('\ud834\udf06'.substr(0), '\ud834\udf06', 'start: 0'); +assert.sameValue('\ud834\udf06'.substr(1), '\udf06', 'start: 1'); +assert.sameValue('\ud834\udf06'.substr(2), '', 'start: 2'); +assert.sameValue('\ud834\udf06'.substr(0, 0), '', 'end: 0'); +assert.sameValue('\ud834\udf06'.substr(0, 1), '\ud834', 'end: 1'); +assert.sameValue('\ud834\udf06'.substr(0, 2), '\ud834\udf06', 'end: 2'); diff --git a/test/annexB/built-ins/String/prototype/substr/this-non-obj-coerce.js b/test/annexB/built-ins/String/prototype/substr/this-non-obj-coerce.js new file mode 100644 index 0000000000..1ee03c3fd1 --- /dev/null +++ b/test/annexB/built-ins/String/prototype/substr/this-non-obj-coerce.js @@ -0,0 +1,21 @@ +// 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.substr +es6id: B.2.3.1 +description: Behavior when "this" value is not Object-coercible +info: | + 1. Let O be ? RequireObjectCoercible(this value). +---*/ + +var substr = String.prototype.substr; + +assert.sameValue(typeof substr, 'function'); + +assert.throws(TypeError, function() { + substr.call(undefined); +}, 'undefined'); + +assert.throws(TypeError, function() { + substr.call(null); +}, 'null'); diff --git a/test/annexB/built-ins/String/prototype/substr/this-to-str-err.js b/test/annexB/built-ins/String/prototype/substr/this-to-str-err.js new file mode 100644 index 0000000000..017f7092d8 --- /dev/null +++ b/test/annexB/built-ins/String/prototype/substr/this-to-str-err.js @@ -0,0 +1,21 @@ +// 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.substr +es6id: B.2.3.1 +description: Behavior when string conversion triggers an abrupt completion +info: | + 1. Let O be ? RequireObjectCoercible(this value). + 2. Let S be ? ToString(O). +---*/ + +var substr = String.prototype.substr; +var thisValue = { + toString: function() { + throw new Test262Error(); + } +}; + +assert.throws(Test262Error, function() { + substr.call(thisValue); +});