-
Notifications
You must be signed in to change notification settings - Fork 472
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
Add generic type coercion utility functions #1200
Changes from 2 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 |
---|---|---|
@@ -0,0 +1,224 @@ | ||
// Copyright (C) 2017 Josh Wolfe. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
description: | | ||
Functions to help generate test cases for testing type coercion abstract | ||
operations like ToNumber. | ||
---*/ | ||
|
||
function testCoercibleToIntegerZero(test) { | ||
function testPrimitiveValue(value) { | ||
test(value); | ||
// ToPrimitive | ||
testPrimitiveWrappers(value, "number", test); | ||
} | ||
|
||
// ToNumber | ||
testPrimitiveValue(null); | ||
testPrimitiveValue(false); | ||
testPrimitiveValue(0); | ||
testPrimitiveValue("0"); | ||
|
||
// ToInteger: NaN -> +0 | ||
testPrimitiveValue(undefined); | ||
testPrimitiveValue(NaN); | ||
testPrimitiveValue(""); | ||
testPrimitiveValue("foo"); | ||
testPrimitiveValue("true"); | ||
|
||
// ToInteger: floor(abs(number)) | ||
testPrimitiveValue(0.9); | ||
testPrimitiveValue(-0); | ||
testPrimitiveValue(-0.9); | ||
testPrimitiveValue("0.9"); | ||
testPrimitiveValue("-0"); | ||
testPrimitiveValue("-0.9"); | ||
|
||
// Non-primitive values that coerce to 0: | ||
// toString() returns a string that parses to NaN. | ||
test({}); | ||
test([]); | ||
} | ||
|
||
function testCoercibleToIntegerOne(test) { | ||
function testPrimitiveValue(value) { | ||
test(value); | ||
// ToPrimitive | ||
testPrimitiveWrappers(value, "number", test); | ||
} | ||
|
||
// ToNumber | ||
testPrimitiveValue(true); | ||
testPrimitiveValue(1); | ||
testPrimitiveValue("1"); | ||
|
||
// ToInteger: floor(abs(number)) | ||
testPrimitiveValue(1.9); | ||
testPrimitiveValue("1.9"); | ||
|
||
// Non-primitive values that coerce to 1: | ||
// toString() returns a string that parses to 1. | ||
test([1]); | ||
test(["1"]); | ||
} | ||
|
||
function testCoercibleToIntegerFromInteger(nominalInteger, test) { | ||
assert(Number.isInteger(nominalInteger)); | ||
|
||
function testPrimitiveValue(value) { | ||
test(value); | ||
// ToPrimitive | ||
testPrimitiveWrappers(value, "number", test); | ||
|
||
// Non-primitive values that coerce to the nominal integer: | ||
// toString() returns a string that parsers to a primitive value. | ||
test([value]); | ||
} | ||
|
||
function testPrimitiveNumber(number) { | ||
testPrimitiveValue(number); | ||
// ToNumber: String -> Number | ||
testPrimitiveValue(number.toString()); | ||
} | ||
|
||
testPrimitiveNumber(nominalInteger); | ||
|
||
// ToInteger: floor(abs(number)) | ||
if (nominalInteger >= 0) { | ||
testPrimitiveNumber(nominalInteger + 0.9); | ||
} | ||
if (nominalInteger <= 0) { | ||
testPrimitiveNumber(nominalInteger - 0.9); | ||
} | ||
} | ||
|
||
function testPrimitiveWrappers(primitiveValue, hint, test) { | ||
if (primitiveValue != null) { | ||
// null and undefined result in {} rather than a proper wrapper, | ||
// so skip this case for those values. | ||
test(Object(primitiveValue)); | ||
} | ||
|
||
testCoercibleToPrimitiveWithMethod(hint, function() { | ||
return primitiveValue; | ||
}, test); | ||
} | ||
|
||
function testCoercibleToPrimitiveWithMethod(hint, method, test) { | ||
var methodNames; | ||
if (hint === "number") { | ||
methodNames = ["valueOf", "toString"]; | ||
} else if (hint === "string") { | ||
methodNames = ["toString", "valueOf"]; | ||
} else { | ||
throw new Test262Error(); | ||
} | ||
// precedence order | ||
test({ | ||
[Symbol.toPrimitive]: method, | ||
[methodNames[0]]: function() { throw new Test262Error(); }, | ||
[methodNames[1]]: function() { throw new Test262Error(); }, | ||
}); | ||
test({ | ||
[methodNames[0]]: method, | ||
[methodNames[1]]: function() { throw new Test262Error(); }, | ||
}); | ||
test({ | ||
[methodNames[1]]: method, | ||
}); | ||
|
||
// GetMethod: if func is undefined or null, return undefined. | ||
test({ | ||
[Symbol.toPrimitive]: undefined, | ||
[methodNames[0]]: method, | ||
[methodNames[1]]: method, | ||
}); | ||
test({ | ||
[Symbol.toPrimitive]: null, | ||
[methodNames[0]]: method, | ||
[methodNames[1]]: method, | ||
}); | ||
|
||
// if methodNames[0] is not callable, fallback to methodNames[1] | ||
test({ | ||
[methodNames[0]]: null, | ||
[methodNames[1]]: method, | ||
}); | ||
test({ | ||
[methodNames[0]]: 1, | ||
[methodNames[1]]: method, | ||
}); | ||
test({ | ||
[methodNames[0]]: {}, | ||
[methodNames[1]]: method, | ||
}); | ||
|
||
// if methodNames[0] returns an object, fallback to methodNames[1] | ||
test({ | ||
[methodNames[0]]: function() { return {}; }, | ||
[methodNames[1]]: method, | ||
}); | ||
test({ | ||
[methodNames[0]]: function() { return Object(1); }, | ||
[methodNames[1]]: method, | ||
}); | ||
} | ||
|
||
function testNotCoercibleToInteger(test) { | ||
// ToInteger only throws from ToNumber. | ||
return testNotCoercibleToNumber(test); | ||
} | ||
function testNotCoercibleToNumber(test) { | ||
function testPrimitiveValue(value) { | ||
test(TypeError, value); | ||
// ToPrimitive | ||
testPrimitiveWrappers(value, "number", function(value) { | ||
test(TypeError, value); | ||
}); | ||
} | ||
|
||
// ToNumber: Symbol -> TypeError | ||
testPrimitiveValue(Symbol("1")); | ||
|
||
if (typeof BigInt !== "undefined") { | ||
// ToNumber: BigInt -> TypeError | ||
testPrimitiveValue(BigInt(0)); | ||
} | ||
|
||
// ToPrimitive | ||
testNotCoercibleToPrimitive("number", test); | ||
} | ||
|
||
function testNotCoercibleToPrimitive(hint, test) { | ||
function MyError() {} | ||
|
||
// ToPrimitive: input[@@toPrimitive] is not callable (and non-null) | ||
test(TypeError, {[Symbol.toPrimitive]: 1}); | ||
test(TypeError, {[Symbol.toPrimitive]: {}}); | ||
|
||
// ToPrimitive: input[@@toPrimitive] returns object | ||
test(TypeError, {[Symbol.toPrimitive]: function() { return Object(1); }}); | ||
test(TypeError, {[Symbol.toPrimitive]: function() { return {}; }}); | ||
|
||
// ToPrimitive: input[@@toPrimitive] throws | ||
test(MyError, {[Symbol.toPrimitive]: function() { throw new MyError(); }}); | ||
|
||
// OrdinaryToPrimitive: method throws | ||
testCoercibleToPrimitiveWithMethod(hint, function() { | ||
throw new MyError(); | ||
}, function(value) { | ||
test(MyError, value); | ||
}); | ||
|
||
// OrdinaryToPrimitive: both methods are unsuitable | ||
function testUnsuitableMethod(method) { | ||
test(TypeError, {valueOf:method, toString:method}); | ||
} | ||
// not callable: | ||
testUnsuitableMethod(null); | ||
testUnsuitableMethod(1); | ||
testUnsuitableMethod({}); | ||
// returns object: | ||
testUnsuitableMethod(function() { return Object(1); }); | ||
testUnsuitableMethod(function() { return {}; }); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (C) 2017 Josh Wolfe. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
/*--- | ||
esid: sec-string.prototype.indexof | ||
description: String.prototype.indexOf type coercion for position parameter | ||
info: > | ||
String.prototype.indexOf ( searchString [ , position ] ) | ||
|
||
4. Let pos be ? ToInteger(position). | ||
|
||
includes: [typeCoercion.js] | ||
---*/ | ||
|
||
testCoercibleToIntegerZero(function(zero) { | ||
assert.sameValue("aaaa".indexOf("aa", zero), 0); | ||
}); | ||
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 is interesting. I like it when I read this code and this PR is an evidence I was wrong and not sure this would work. It also seems like What do you think? 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. As an alternative, calling i.e.: valuesCoercibleToIntegerZero(function(zero) {
assert.sameValue("aaaa".indexOf("aa", zero), 0, "with value " + zero);
}); 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. I like the idea of passing in a callback instead of using I'm a huge fan of long and clear variable names, but I know I'm an outlier in that regard. One concern here is that the functions are exposed at global scope, so I want their full purpose to be in the variable name. I'm also a bit concerned with publishing the helper functions I wrote in the harness file. So perhaps a solution could be exposing an exports object where the name of the object gives context. Perhaps something like |
||
|
||
testCoercibleToIntegerOne(function(one) { | ||
assert.sameValue("aaaa".indexOf("aa", one), 1); | ||
}); | ||
|
||
testCoercibleToIntegerFromInteger(2, function(two) { | ||
assert.sameValue("aaaa".indexOf("aa", two), 2); | ||
}); | ||
|
||
testNotCoercibleToInteger(function(error, value) { | ||
assert.throws(error, function() { "".indexOf("", value); }); | ||
}); |
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.
@rwaldron this is what I mean by checking if the feature is enabled at runtime. Something similar could be done for
typeof Symbol !== "undefined"
and eventypeof Symbol.toPrimitive !== "undefined"
perhaps. And in the event they are undefined, then skip the parts of the test that use the features.