mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +00:00
LibJS: Add tests for value to number conversion
This commit is contained in:
parent
bad0556a59
commit
c698cc899d
Notes:
sideshowbarker
2024-07-19 08:04:48 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/c698cc899d6 Pull-request: https://github.com/SerenityOS/serenity/pull/1528
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
Reference in a new issue