-
Notifications
You must be signed in to change notification settings - Fork 471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BigInt ToBoolean type coercion test #1241
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,6 +112,46 @@ function testCoercibleToIntegerFromInteger(nominalInteger, test) { | |
} | ||
} | ||
|
||
function testCoercibleToBooleanFalse(test) { | ||
test(undefined); | ||
test(null); | ||
test(false); | ||
test(0); | ||
test(-0); | ||
test(NaN); | ||
test(0n); | ||
test(""); | ||
} | ||
|
||
function testCoercibleToBooleanTrue(test) { | ||
test(true); | ||
test(1); | ||
test(-1); | ||
test(Infinity); | ||
test(-Infinity); | ||
test(1n); | ||
test("a"); | ||
test("true"); | ||
test("false"); | ||
test("0"); | ||
test(Symbol("1")); | ||
test({}); | ||
test([]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to my comment above, this would be sufficient in a single file: assert.sameValue(Boolean(true), true);
assert.sameValue(Boolean(1), true);
assert.sameValue(Boolean(-1), true);
assert.sameValue(Boolean(Infinity), true);
assert.sameValue(Boolean(-Infinity), true);
assert.sameValue(Boolean(1n), true);
assert.sameValue(Boolean("a"), true);
assert.sameValue(Boolean("true"), true);
assert.sameValue(Boolean("false"), true);
assert.sameValue(Boolean("0"), true);
assert.sameValue(Boolean(Symbol("1")), true);
assert.sameValue(Boolean({}), true);
assert.sameValue(Boolean([]), true); |
||
|
||
// ToBoolean does not call ToPrimitive. | ||
// In all of these cases, it's just an object, which is truthy. | ||
testPrimitiveWrappers(false, "number", test); | ||
testPrimitiveWrappers(true, "number", test); | ||
testPrimitiveWrappers(false, "string", test); | ||
testPrimitiveWrappers(true, "string", test); | ||
|
||
function notReallyAnError(error, value) { | ||
test(value); | ||
} | ||
testNotCoercibleToPrimitive("number", notReallyAnError); | ||
testNotCoercibleToPrimitive("string", notReallyAnError); | ||
} | ||
|
||
function testPrimitiveWrappers(primitiveValue, hint, test) { | ||
if (primitiveValue != null) { | ||
// null and undefined result in {} rather than a proper wrapper, | ||
|
@@ -231,10 +271,8 @@ function testNotCoercibleToNumber(test) { | |
// ToNumber: Symbol -> TypeError | ||
testPrimitiveValue(Symbol("1")); | ||
|
||
if (typeof BigInt !== "undefined") { | ||
// ToNumber: BigInt -> TypeError | ||
testPrimitiveValue(BigInt(0)); | ||
} | ||
// ToNumber: BigInt -> TypeError | ||
testPrimitiveValue(0n); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are we just throwing this away and assuming this file will only be used with BigInt? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to decide one way or another. In some places in this harness file, BigInt literals are used unconditionally. This check here was pointless, since the file would not parse without BigInt support already. Additionally, the This was also a concern when my |
||
|
||
// ToPrimitive | ||
testNotCoercibleToPrimitive("number", test); | ||
|
@@ -291,15 +329,9 @@ function testCoercibleToString(test) { | |
testPrimitiveValue(-0, "0"); | ||
testPrimitiveValue(Infinity, "Infinity"); | ||
testPrimitiveValue(-Infinity, "-Infinity"); | ||
testPrimitiveValue(123.456, "123.456"); | ||
testPrimitiveValue(-123.456, "-123.456"); | ||
testPrimitiveValue("", ""); | ||
testPrimitiveValue("foo", "foo"); | ||
|
||
if (typeof BigInt !== "undefined") { | ||
// BigInt -> TypeError | ||
testPrimitiveValue(BigInt(0), "0"); | ||
} | ||
testPrimitiveValue(0n, "0"); | ||
|
||
// toString of a few objects | ||
test([], ""); | ||
|
@@ -406,4 +438,6 @@ function testNotCoercibleToBigInt(test) { | |
testStringValue("0o8"); | ||
testStringValue("0xg"); | ||
testStringValue("1n"); | ||
|
||
testNotCoercibleToPrimitive("number", test); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (C) 2017 Josh Wolfe. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-boolean-constructor-boolean-value | ||
description: ToBoolean type coercion tests via Boolean() | ||
info: > | ||
Boolean ( value ) | ||
|
||
1. Let b be ToBoolean(value). | ||
2. If NewTarget is undefined, return b. | ||
|
||
features: [BigInt, Symbol, Symbol.toPrimitive] | ||
includes: [typeCoercion.js] | ||
---*/ | ||
|
||
testCoercibleToBooleanFalse(function(falsy) { | ||
assert.sameValue(Boolean(falsy), false); | ||
}); | ||
|
||
testCoercibleToBooleanTrue(function(truthy) { | ||
assert.sameValue(Boolean(truthy), true); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should also be placed in 2 different files, at least. We are not trying to condense everything in the same test file. This is also not helpful if anything is failing. |
||
|
||
// `Boolean(value)` (without `new`) never throws. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this line is not necessary |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that I agree with the benefit of the harness function here (which I recognize is similar to others that have been added for these coercion tests). It's not clear in the test file below what is actually being tested, without also seeking out this file and stepping through the layers that have been created. If the test file below contained the following, it would be both sufficient and more easily understood by those that would benefit from test262:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the code being more verbose helps communicating what it is actually doing, as mentioned. I'd avoid adding too many extras to the harness file so it doesn't become more complex.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to avoid ambiguity, I agree with @rwaldron