Skip to content

Commit

Permalink
more separation of ToInteger from ToNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Wolfe committed Aug 30, 2017
1 parent f58e962 commit 2bcee86
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions harness/typeCoercion.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,65 @@ description: |
---*/

function testCoercibleToIntegerZero(test) {
testCoercibleToNumberZero(test);

testCoercibleToIntegerFromInteger(0, test);

// NaN -> +0
testCoercibleToNumberNan(test);

// When toString() returns a string that parses to NaN:
test({});
test([]);
}

function testCoercibleToIntegerOne(test) {
testCoercibleToNumberOne(test);

testCoercibleToIntegerFromInteger(1, test);

// When toString() returns "1"
test([1]);
test(["1"]);
}

function testCoercibleToNumberZero(test) {
function testPrimitiveValue(value) {
test(value);
// ToPrimitive
testPrimitiveWrappers(value, "number", test);
}

// ToNumber
testPrimitiveValue(null);
testPrimitiveValue(false);
testPrimitiveValue(0);
testPrimitiveValue("0");
}

function testCoercibleToNumberNan(test) {
function testPrimitiveValue(value) {
test(value);
// ToPrimitive
testPrimitiveWrappers(value, "number", test);
}

// 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 testCoercibleToNumberOne(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) {
Expand Down

0 comments on commit 2bcee86

Please sign in to comment.