mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 21:47:43 +00:00
LibJS: Add tests for value to number conversion
This commit is contained in:
parent
bad0556a59
commit
c698cc899d
1 changed files with 33 additions and 0 deletions
33
Libraries/LibJS/Tests/to-number-basic.js
Normal file
33
Libraries/LibJS/Tests/to-number-basic.js
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
function assert(x) { if (!x) throw 1; }
|
||||||
|
|
||||||
|
// FIXME: Just "+x" doesn't parse currently,
|
||||||
|
// so we use "x - 0", which is effectively the same.
|
||||||
|
// "0 + x" would _not_ work in all cases.
|
||||||
|
function n(x) { return x - 0; }
|
||||||
|
|
||||||
|
try {
|
||||||
|
assert(n(false) === 0);
|
||||||
|
assert(n(true) === 1);
|
||||||
|
assert(n(null) === 0);
|
||||||
|
assert(n([]) === 0);
|
||||||
|
assert(n([[[[[]]]]]) === 0);
|
||||||
|
assert(n([[[[[42]]]]]) === 42);
|
||||||
|
assert(n("") === 0);
|
||||||
|
assert(n("42") === 42);
|
||||||
|
assert(n(42) === 42);
|
||||||
|
// FIXME: returns NaN
|
||||||
|
// assert(n("1.23") === 1.23)
|
||||||
|
// FIXME: chokes on ASSERT
|
||||||
|
// assert(n(1.23) === 1.23);
|
||||||
|
|
||||||
|
assert(isNaN(n(undefined)));
|
||||||
|
assert(isNaN(n({})));
|
||||||
|
assert(isNaN(n({a: 1})));
|
||||||
|
assert(isNaN(n([1, 2, 3])));
|
||||||
|
assert(isNaN(n([[[["foo"]]]])));
|
||||||
|
assert(isNaN(n("foo")));
|
||||||
|
|
||||||
|
console.log("PASS");
|
||||||
|
} catch (e) {
|
||||||
|
console.log("FAIL: " + e);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue