ladybird/Libraries/LibJS/Tests/to-number-basic.js
Linus Groh da0ab16f01 LibJS: Don't handle arrays separately in Value::to_number()
Now that Array.prototype.join() is producing the correct results we
can remove the separate code path for arrays in Value::to_number()
and treat them like all other objects - using to_primitive() with
number as the preferred type and then calling to_number() on the
result.

This is how the spec descibes it.

This also means we don't crash anymore when trying to coerce
[<empty>] to a number - it now does the following:

[<empty>] - to string - "" - to number - 0
[<empty>, <empty>] - to string - "," - to number - NaN
2020-04-29 01:30:59 +02:00

60 lines
1.6 KiB
JavaScript

load("test-common.js");
try {
assert(+false === 0);
assert(-false === 0);
assert(+true === 1);
assert(-true === -1);
assert(+null === 0);
assert(-null === 0);
assert(+[] === 0);
assert(-[] === 0);
assert(+[,] === 0);
assert(-[,] === 0);
assert(+[null] === 0);
assert(-[null] === 0);
assert(+[undefined] === 0);
assert(-[undefined] === 0);
assert(+[[[[[]]]]] === 0);
assert(-[[[[[]]]]] === 0);
assert(+[[[[[42]]]]] === 42);
assert(-[[[[[42]]]]] === -42);
assert(+"" === 0);
assert(-"" === 0);
assert(+"42" === 42);
assert(-"42" === -42);
assert(+42 === 42);
assert(-42 === -42);
assert(+1.23 === 1.23);
assert(-1.23 === -1.23);
// FIXME: returns NaN
// assert(+"1.23" === 1.23)
// assert(-"1.23" === -1.23)
assert(+"Infinity" === Infinity);
assert(+"+Infinity" === Infinity);
assert(+"-Infinity" === -Infinity);
assert(-"Infinity" === -Infinity);
assert(-"+Infinity" === -Infinity);
assert(-"-Infinity" === Infinity);
assert(isNaN(+undefined));
assert(isNaN(-undefined));
assert(isNaN(+{}));
assert(isNaN(-{}));
assert(isNaN(+{ a: 1 }));
assert(isNaN(-{ a: 1 }));
assert(isNaN(+[, , ,]));
assert(isNaN(-[, , ,]));
assert(isNaN(+[undefined, undefined]));
assert(isNaN(-[undefined, undefined]));
assert(isNaN(+[1, 2, 3]));
assert(isNaN(-[1, 2, 3]));
assert(isNaN(+[[[["foo"]]]]));
assert(isNaN(-[[[["foo"]]]]));
assert(isNaN(+"foo"));
assert(isNaN(-"foo"));
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}