Skip to content
Snippets Groups Projects
Commit 3ffca827 authored by Leonardo Balter's avatar Leonardo Balter
Browse files

String.prototype.repeat

parent f25d690a
No related branches found
No related tags found
No related merge requests found
Showing
with 300 additions and 0 deletions
// 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.13
description: >
If ToInteger(count) is zero, returns an empty String.
info: >
21.1.3.13 String.prototype.repeat ( count )
8. Let T be a String value that is made from n copies of S appended together.
If n is 0, T is the empty String.
9. Return T.
---*/
var str = 'ES2015';
assert.sameValue(str.repeat(NaN), '', 'str.repeat(NaN) returns ""');
assert.sameValue(str.repeat(null), '', 'str.repeat(null) returns ""');
assert.sameValue(str.repeat(undefined), '', 'str.repeat(undefined) returns ""');
assert.sameValue(str.repeat(false), '', 'str.repeat(false) returns ""');
assert.sameValue(str.repeat('0'), '', 'str.repeat("0") returns ""');
assert.sameValue(str.repeat(0.9), '', 'str.repeat(0.9) returns ""');
// 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.13
description: >
Throws a RangeError if count < 0
info: >
21.1.3.13 String.prototype.repeat ( count )
7. If n is +∞, throw a RangeError exception.
---*/
assert.throws(RangeError, function() {
''.repeat(Infinity);
});
// 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.13
description: >
If count is zero, returns an empty String.
info: >
21.1.3.13 String.prototype.repeat ( count )
8. Let T be a String value that is made from n copies of S appended together.
If n is 0, T is the empty String.
9. Return T.
---*/
assert.sameValue('foo'.repeat(0), '');
// 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.13
description: >
Throws a RangeError if count < 0
info: >
21.1.3.13 String.prototype.repeat ( count )
6. If n < 0, throw a RangeError exception.
---*/
assert.throws(RangeError, function() {
''.repeat(-1);
});
assert.throws(RangeError, function() {
''.repeat(-Infinity);
});
// 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.13
description: >
An empty repeated n times will return an empty string.
info: >
21.1.3.13 String.prototype.repeat ( count )
8. Let T be a String value that is made from n copies of S appended together.
If n is 0, T is the empty String.
9. Return T.
---*/
assert.sameValue(''.repeat(1), '', '"".repeat(1)');
assert.sameValue(''.repeat(3), '', '"".repeat(3)');
var maxSafe32bitInt = 2147483647;
assert.sameValue(''.repeat(maxSafe32bitInt), '', '"".repeat(maxSafe32bitInt)');
// 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.13
description: >
String.prototype.repeat.length value and descriptor.
info: >
21.1.3.13 String.prototype.repeat ( count )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
assert.sameValue(
String.prototype.repeat.length, 1,
'The value of `String.prototype.repeat.length` is `1`'
);
verifyNotEnumerable(String.prototype.repeat, 'length');
verifyNotWritable(String.prototype.repeat, 'length');
verifyConfigurable(String.prototype.repeat, 'length');
// 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.13
description: >
String.prototype.repeat.name value and descriptor.
info: >
21.1.3.13 String.prototype.repeat ( count )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
assert.sameValue(
String.prototype.repeat.name, 'repeat',
'The value of `String.prototype.repeat.name` is `"repeat"`'
);
verifyNotEnumerable(String.prototype.repeat, 'name');
verifyNotWritable(String.prototype.repeat, 'name');
verifyConfigurable(String.prototype.repeat, 'name');
// 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.13
description: >
Returns a String made from n copies of the original String appended together.
info: >
21.1.3.13 String.prototype.repeat ( count )
8. Let T be a String value that is made from n copies of S appended together.
If n is 0, T is the empty String.
9. Return T.
---*/
var str = 'abc';
assert.sameValue(str.repeat(1), str, 'str.repeat(1) === str');
assert.sameValue(str.repeat(3), 'abcabcabc', 'str.repeat(3) === "abcabcabc"');
str = '';
var i = 0;
var count = 10000;
while (i < count) {
str += '.';
i++;
}
assert.sameValue('.'.repeat(count), str);
// 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.13
description: >
Property type and descriptor.
info: >
21.1.3.13 String.prototype.repeat ( count )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
assert.sameValue(
typeof String.prototype.repeat,
'function',
'`typeof String.prototype.repeat` is `function`'
);
verifyNotEnumerable(String.prototype, 'repeat');
verifyWritable(String.prototype, 'repeat');
verifyConfigurable(String.prototype, 'repeat');
// 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.13
description: >
Returns abrupt from ToInteger(count) where count is a Symbol
info: >
21.1.3.13 String.prototype.repeat ( count )
4. Let n be ToInteger(count).
5. ReturnIfAbrupt(n).
features: [Symbol]
---*/
var s = Symbol('');
assert.throws(TypeError, function() {
''.repeat(s);
});
// 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.13
description: >
Returns abrupt from ToInteger(count)
info: >
21.1.3.13 String.prototype.repeat ( count )
4. Let n be ToInteger(count).
5. ReturnIfAbrupt(n).
---*/
var o = {
toString: function() {
throw new Test262Error();
}
}
assert.throws(Test262Error, function() {
''.repeat(o);
});
// 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.13
description: >
Returns abrupt from ToString(this) where this is a Symbol
info: >
21.1.3.13 String.prototype.repeat ( count )
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.repeat.call(s);
});
// 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.13
description: >
Returns abrupt from ToString(this)
info: >
21.1.3.13 String.prototype.repeat ( count )
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.repeat.call(o);
});
// 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.13
description: >
Throws TypeError when `this` is null
info: >
21.1.3.13 String.prototype.repeat ( count )
1. Let O be RequireObjectCoercible(this value).
2. Let S be ToString(O).
---*/
assert.throws(TypeError, function() {
String.prototype.repeat.call(null);
});
// 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.13
description: >
Throws TypeError when `this` is undefined
info: >
21.1.3.13 String.prototype.repeat ( count )
1. Let O be RequireObjectCoercible(this value).
2. Let S be ToString(O).
---*/
assert.throws(TypeError, function() {
String.prototype.repeat.call(undefined);
});
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