From 9737a5ff73c487bd5b70850bc143f2e3c3207318 Mon Sep 17 00:00:00 2001
From: Leo Balter <leonardo.balter@gmail.com>
Date: Tue, 3 Oct 2017 16:51:07 -0400
Subject: [PATCH] Improve tests for BigInt.prototype.valueOf (#1256)

* Improve tests for BigInt.prototype.valueOf

* fixup! Improve tests for BigInt.prototype.valueOf

* fixup! Improve tests for BigInt.prototype.valueOf

* fixup! Improve tests for BigInt.prototype.valueOf
---
 harness/typeCoercion.js                       | 16 -----
 .../BigInt/prototype/valueOf/return.js        | 18 ++++++
 .../prototype/valueOf/this-value-err.js       | 17 -----
 .../this-value-invalid-object-throws.js       | 30 +++++++++
 .../this-value-invalid-primitive-throws.js    | 63 +++++++++++++++++++
 .../BigInt/prototype/valueOf/this-value.js    | 18 ------
 6 files changed, 111 insertions(+), 51 deletions(-)
 create mode 100644 test/built-ins/BigInt/prototype/valueOf/return.js
 delete mode 100644 test/built-ins/BigInt/prototype/valueOf/this-value-err.js
 create mode 100644 test/built-ins/BigInt/prototype/valueOf/this-value-invalid-object-throws.js
 create mode 100644 test/built-ins/BigInt/prototype/valueOf/this-value-invalid-primitive-throws.js
 delete mode 100644 test/built-ins/BigInt/prototype/valueOf/this-value.js

diff --git a/harness/typeCoercion.js b/harness/typeCoercion.js
index 3950d4b2cc..ed03805d7c 100644
--- a/harness/typeCoercion.js
+++ b/harness/typeCoercion.js
@@ -407,19 +407,3 @@ function testNotCoercibleToBigInt(test) {
   testStringValue("0xg");
   testStringValue("1n");
 }
-
-function testCoercibleToBigIntThisValue(value, test) {
-  test(value);
-  test(Object(value));
-}
-
-function testNotCoercibleToBigIntThisValue(test) {
-  test(undefined);
-  test(null);
-  test(true);
-  test(false);
-  test("");
-  test(Symbol(""));
-  test(0);
-  test({});
-}
diff --git a/test/built-ins/BigInt/prototype/valueOf/return.js b/test/built-ins/BigInt/prototype/valueOf/return.js
new file mode 100644
index 0000000000..de7336057c
--- /dev/null
+++ b/test/built-ins/BigInt/prototype/valueOf/return.js
@@ -0,0 +1,18 @@
+// Copyright (C) 2017 Robin Templeton. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+
+/*---
+esid: sec-bigint.prototype.valueof
+description: >
+  BigInt.prototype.valueOf returns the primitive BigInt value.
+info: |
+  BigInt.prototype.valueOf ( )
+
+  Return ? thisBigIntValue(this value). 
+features: [BigInt]
+---*/
+
+var valueOf = BigInt.prototype.valueOf;
+
+assert.sameValue(valueOf.call(0n), 0n, "0n");
+assert.sameValue(valueOf.call(Object(0n)), 0n, "Object(0n)");
diff --git a/test/built-ins/BigInt/prototype/valueOf/this-value-err.js b/test/built-ins/BigInt/prototype/valueOf/this-value-err.js
deleted file mode 100644
index b707455e64..0000000000
--- a/test/built-ins/BigInt/prototype/valueOf/this-value-err.js
+++ /dev/null
@@ -1,17 +0,0 @@
-// Copyright (C) 2017 Robin Templeton. All rights reserved.
-// This code is governed by the BSD license found in the LICENSE file.
-
-/*---
-esid: sec-bigint.prototype.valueof
-description: BigInt.prototype.valueOf this value coercion
-info: >
-  BigInt.prototype.valueOf ( )
-
-  Return ? thisBigIntValue(this value).
-includes: [typeCoercion.js]
-features: [BigInt, arrow-function, Symbol, Symbol.toPrimitive]
----*/
-
-testNotCoercibleToBigIntThisValue(
-  (x) => assert.throws(TypeError, () => BigInt.prototype.valueOf.call(x))
-);
diff --git a/test/built-ins/BigInt/prototype/valueOf/this-value-invalid-object-throws.js b/test/built-ins/BigInt/prototype/valueOf/this-value-invalid-object-throws.js
new file mode 100644
index 0000000000..90c5b71c18
--- /dev/null
+++ b/test/built-ins/BigInt/prototype/valueOf/this-value-invalid-object-throws.js
@@ -0,0 +1,30 @@
+// Copyright (C) 2017 Robin Templeton. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+
+/*---
+esid: sec-bigint.prototype.valueof
+description: >
+  Throws a TypeError if this is an Object without a [[BigIntData]] internal.
+info: |
+  BigInt.prototype.valueOf ( )
+
+  1. Return ? thisBigIntValue(this value).
+
+  The abstract operation thisBigIntValue(value) performs the following steps:
+
+  1. If Type(value) is BigInt, return value.
+  2. If Type(value) is Object and value has a [[BigIntData]] internal slot, then
+    ...
+  3. Throw a TypeError exception.
+features: [BigInt]
+---*/
+
+var valueOf = BigInt.prototype.valueOf;
+
+assert.throws(TypeError, function() {
+  valueOf.call({});
+}, "{}");
+
+assert.throws(TypeError, function() {
+  valueOf.call(Object(1));
+}, "Object(1)");
diff --git a/test/built-ins/BigInt/prototype/valueOf/this-value-invalid-primitive-throws.js b/test/built-ins/BigInt/prototype/valueOf/this-value-invalid-primitive-throws.js
new file mode 100644
index 0000000000..c7379385c7
--- /dev/null
+++ b/test/built-ins/BigInt/prototype/valueOf/this-value-invalid-primitive-throws.js
@@ -0,0 +1,63 @@
+// Copyright (C) 2017 Robin Templeton. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+
+/*---
+esid: sec-bigint.prototype.valueof
+description: >
+  Throws a TypeError if this is not a BigInt neither an Object.
+info: |
+  BigInt.prototype.valueOf ( )
+
+  1. Return ? thisBigIntValue(this value).
+
+  The abstract operation thisBigIntValue(value) performs the following steps:
+
+  1. If Type(value) is BigInt, return value.
+  2. If Type(value) is Object and value has a [[BigIntData]] internal slot, then
+    ...
+  3. Throw a TypeError exception.
+features: [BigInt, Symbol]
+---*/
+
+var valueOf = BigInt.prototype.valueOf;
+
+assert.throws(TypeError, function() {
+  valueOf.call(undefined);
+}, "undefined");
+
+assert.throws(TypeError, function() {
+  valueOf.call(null);
+}, "null");
+
+assert.throws(TypeError, function() {
+  valueOf.call(false);
+}, "false");
+
+assert.throws(TypeError, function() {
+  valueOf.call(true);
+}, "true");
+
+assert.throws(TypeError, function() {
+  valueOf.call("");
+}, "the empty string");
+
+assert.throws(TypeError, function() {
+  valueOf.call("1n");
+}, "string");
+
+assert.throws(TypeError, function() {
+  valueOf.call(0);
+}, "number (0)");
+
+assert.throws(TypeError, function() {
+  valueOf.call(1);
+}, "number (1)");
+
+assert.throws(TypeError, function() {
+  valueOf.call(NaN);
+}, "NaN");
+
+var s = Symbol();
+assert.throws(TypeError, function() {
+  valueOf.call(s);
+}, "symbol");
diff --git a/test/built-ins/BigInt/prototype/valueOf/this-value.js b/test/built-ins/BigInt/prototype/valueOf/this-value.js
deleted file mode 100644
index 4d383aa9cf..0000000000
--- a/test/built-ins/BigInt/prototype/valueOf/this-value.js
+++ /dev/null
@@ -1,18 +0,0 @@
-// Copyright (C) 2017 Robin Templeton. All rights reserved.
-// This code is governed by the BSD license found in the LICENSE file.
-
-/*---
-esid: sec-bigint.prototype.valueof
-description: BigInt.prototype.valueOf this value coercion
-info: >
-  BigInt.prototype.valueOf ( )
-
-  Return ? thisBigIntValue(this value). 
-includes: [typeCoercion.js]
-features: [BigInt, arrow-function, Symbol, Symbol.toPrimitive]
----*/
-
-testCoercibleToBigIntThisValue(
-  0n,
-  (x) => assert.sameValue(0n, BigInt.prototype.valueOf.call(x))
-);
-- 
GitLab