From 3ffca82742043b683a3c63e0b5d02ff05e4512bb Mon Sep 17 00:00:00 2001
From: Leonardo Balter <leonardo.balter@gmail.com>
Date: Tue, 14 Jul 2015 17:54:02 -0400
Subject: [PATCH] String.prototype.repeat

---
 ...nt-coerced-to-zero-returns-empty-string.js | 22 +++++++++++++++
 .../repeat/count-is-infinity-throws.js        | 15 ++++++++++
 .../count-is-zero-returns-empty-string.js     | 15 ++++++++++
 .../repeat/count-less-than-zero-throws.js     | 19 +++++++++++++
 .../repeat/empty-string-returns-empty.js      | 19 +++++++++++++
 .../String/prototype/repeat/length.js         | 22 +++++++++++++++
 .../built-ins/String/prototype/repeat/name.js | 22 +++++++++++++++
 .../prototype/repeat/repeat-string-n-times.js | 28 +++++++++++++++++++
 .../String/prototype/repeat/repeat.js         | 22 +++++++++++++++
 .../return-abrupt-from-count-as-symbol.js     | 19 +++++++++++++
 .../repeat/return-abrupt-from-count.js        | 22 +++++++++++++++
 .../return-abrupt-from-this-as-symbol.js      | 20 +++++++++++++
 .../repeat/return-abrupt-from-this.js         | 23 +++++++++++++++
 .../prototype/repeat/this-is-null-throws.js   | 16 +++++++++++
 .../repeat/this-is-undefined-throws.js        | 16 +++++++++++
 15 files changed, 300 insertions(+)
 create mode 100644 test/built-ins/String/prototype/repeat/count-coerced-to-zero-returns-empty-string.js
 create mode 100644 test/built-ins/String/prototype/repeat/count-is-infinity-throws.js
 create mode 100644 test/built-ins/String/prototype/repeat/count-is-zero-returns-empty-string.js
 create mode 100644 test/built-ins/String/prototype/repeat/count-less-than-zero-throws.js
 create mode 100644 test/built-ins/String/prototype/repeat/empty-string-returns-empty.js
 create mode 100644 test/built-ins/String/prototype/repeat/length.js
 create mode 100644 test/built-ins/String/prototype/repeat/name.js
 create mode 100644 test/built-ins/String/prototype/repeat/repeat-string-n-times.js
 create mode 100644 test/built-ins/String/prototype/repeat/repeat.js
 create mode 100644 test/built-ins/String/prototype/repeat/return-abrupt-from-count-as-symbol.js
 create mode 100644 test/built-ins/String/prototype/repeat/return-abrupt-from-count.js
 create mode 100644 test/built-ins/String/prototype/repeat/return-abrupt-from-this-as-symbol.js
 create mode 100644 test/built-ins/String/prototype/repeat/return-abrupt-from-this.js
 create mode 100644 test/built-ins/String/prototype/repeat/this-is-null-throws.js
 create mode 100644 test/built-ins/String/prototype/repeat/this-is-undefined-throws.js

diff --git a/test/built-ins/String/prototype/repeat/count-coerced-to-zero-returns-empty-string.js b/test/built-ins/String/prototype/repeat/count-coerced-to-zero-returns-empty-string.js
new file mode 100644
index 0000000000..f058f3bce1
--- /dev/null
+++ b/test/built-ins/String/prototype/repeat/count-coerced-to-zero-returns-empty-string.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.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 ""');
diff --git a/test/built-ins/String/prototype/repeat/count-is-infinity-throws.js b/test/built-ins/String/prototype/repeat/count-is-infinity-throws.js
new file mode 100644
index 0000000000..f9af5b5467
--- /dev/null
+++ b/test/built-ins/String/prototype/repeat/count-is-infinity-throws.js
@@ -0,0 +1,15 @@
+// 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);
+});
diff --git a/test/built-ins/String/prototype/repeat/count-is-zero-returns-empty-string.js b/test/built-ins/String/prototype/repeat/count-is-zero-returns-empty-string.js
new file mode 100644
index 0000000000..3183f21d49
--- /dev/null
+++ b/test/built-ins/String/prototype/repeat/count-is-zero-returns-empty-string.js
@@ -0,0 +1,15 @@
+// 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), '');
diff --git a/test/built-ins/String/prototype/repeat/count-less-than-zero-throws.js b/test/built-ins/String/prototype/repeat/count-less-than-zero-throws.js
new file mode 100644
index 0000000000..a23026483d
--- /dev/null
+++ b/test/built-ins/String/prototype/repeat/count-less-than-zero-throws.js
@@ -0,0 +1,19 @@
+// 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);
+});
diff --git a/test/built-ins/String/prototype/repeat/empty-string-returns-empty.js b/test/built-ins/String/prototype/repeat/empty-string-returns-empty.js
new file mode 100644
index 0000000000..31da7d90cd
--- /dev/null
+++ b/test/built-ins/String/prototype/repeat/empty-string-returns-empty.js
@@ -0,0 +1,19 @@
+// 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)');
diff --git a/test/built-ins/String/prototype/repeat/length.js b/test/built-ins/String/prototype/repeat/length.js
new file mode 100644
index 0000000000..2deb1799fe
--- /dev/null
+++ b/test/built-ins/String/prototype/repeat/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.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');
diff --git a/test/built-ins/String/prototype/repeat/name.js b/test/built-ins/String/prototype/repeat/name.js
new file mode 100644
index 0000000000..b4607d65a6
--- /dev/null
+++ b/test/built-ins/String/prototype/repeat/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.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');
diff --git a/test/built-ins/String/prototype/repeat/repeat-string-n-times.js b/test/built-ins/String/prototype/repeat/repeat-string-n-times.js
new file mode 100644
index 0000000000..db6a4929f3
--- /dev/null
+++ b/test/built-ins/String/prototype/repeat/repeat-string-n-times.js
@@ -0,0 +1,28 @@
+// 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);
diff --git a/test/built-ins/String/prototype/repeat/repeat.js b/test/built-ins/String/prototype/repeat/repeat.js
new file mode 100644
index 0000000000..5a632bd5a0
--- /dev/null
+++ b/test/built-ins/String/prototype/repeat/repeat.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.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');
diff --git a/test/built-ins/String/prototype/repeat/return-abrupt-from-count-as-symbol.js b/test/built-ins/String/prototype/repeat/return-abrupt-from-count-as-symbol.js
new file mode 100644
index 0000000000..96dc9ed95e
--- /dev/null
+++ b/test/built-ins/String/prototype/repeat/return-abrupt-from-count-as-symbol.js
@@ -0,0 +1,19 @@
+// 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);
+});
diff --git a/test/built-ins/String/prototype/repeat/return-abrupt-from-count.js b/test/built-ins/String/prototype/repeat/return-abrupt-from-count.js
new file mode 100644
index 0000000000..0b44ab825b
--- /dev/null
+++ b/test/built-ins/String/prototype/repeat/return-abrupt-from-count.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.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);
+});
diff --git a/test/built-ins/String/prototype/repeat/return-abrupt-from-this-as-symbol.js b/test/built-ins/String/prototype/repeat/return-abrupt-from-this-as-symbol.js
new file mode 100644
index 0000000000..a8ad73837a
--- /dev/null
+++ b/test/built-ins/String/prototype/repeat/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.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);
+});
diff --git a/test/built-ins/String/prototype/repeat/return-abrupt-from-this.js b/test/built-ins/String/prototype/repeat/return-abrupt-from-this.js
new file mode 100644
index 0000000000..d9316e12f5
--- /dev/null
+++ b/test/built-ins/String/prototype/repeat/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.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);
+});
diff --git a/test/built-ins/String/prototype/repeat/this-is-null-throws.js b/test/built-ins/String/prototype/repeat/this-is-null-throws.js
new file mode 100644
index 0000000000..70c351d151
--- /dev/null
+++ b/test/built-ins/String/prototype/repeat/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.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);
+});
diff --git a/test/built-ins/String/prototype/repeat/this-is-undefined-throws.js b/test/built-ins/String/prototype/repeat/this-is-undefined-throws.js
new file mode 100644
index 0000000000..18e6fb640a
--- /dev/null
+++ b/test/built-ins/String/prototype/repeat/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.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);
+});
-- 
GitLab