Add tests for Annex B String.prototype.substr (#628)

This commit is contained in:
jugglinmike 2016-05-19 20:18:20 -04:00 committed by Leo Balter
parent c6b0864d2b
commit 2c33050732
10 changed files with 283 additions and 0 deletions

View File

@ -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');

View File

@ -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');

View File

@ -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');

View File

@ -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);
});

View File

@ -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'
);

View File

@ -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');

View File

@ -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);

View File

@ -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');

View File

@ -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');

View File

@ -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);
});