diff options
author | Jack Karamanian <karamanian.jack@gmail.com> | 2020-03-30 08:26:09 -0500 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-03-30 15:41:36 +0200 |
commit | 098f1cd0ca94b8025c2e0e8caacd94d62d18f272 (patch) | |
tree | d221a4f30712c5b9a4246d11543888c6eab99b42 /Libraries/LibJS/Tests | |
parent | f90da71d2856f86e76b654f4ee2c66f09d2afdcf (diff) | |
download | serenity-098f1cd0ca94b8025c2e0e8caacd94d62d18f272.zip |
LibJS: Add support for arrow functions
Diffstat (limited to 'Libraries/LibJS/Tests')
-rw-r--r-- | Libraries/LibJS/Tests/arrow-functions.js | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/Libraries/LibJS/Tests/arrow-functions.js b/Libraries/LibJS/Tests/arrow-functions.js new file mode 100644 index 0000000000..3d2569d099 --- /dev/null +++ b/Libraries/LibJS/Tests/arrow-functions.js @@ -0,0 +1,57 @@ +function assert(x) { if (!x) throw 1; } +try { + let getNumber = () => 42; + assert(getNumber() === 42); + + let add = (a, b) => a + b; + assert(add(2, 3) === 5); + + const addBlock = (a, b) => { + let res = a + b; + return res; + }; + assert(addBlock(5, 4) === 9); + + const makeObject = (a, b) => ({ a, b }); + const obj = makeObject(33, 44); + assert(typeof obj === "object"); + assert(obj.a === 33); + assert(obj.b === 44); + + let returnUndefined = () => {}; + assert(typeof returnUndefined() === "undefined"); + + const makeArray = (a, b) => [a, b]; + const array = makeArray("3", { foo: 4 }); + assert(array[0] === "3"); + assert(array[1].foo === 4); + + let square = x => x * x; + assert(square(3) === 9); + + let squareBlock = x => { + return x * x; + }; + assert(squareBlock(4) === 16); + + const message = (who => "Hello " + who)("friends!"); + assert(message === "Hello friends!"); + + const sum = ((x, y, z) => x + y + z)(1, 2, 3); + assert(sum === 6); + + const product = ((x, y, z) => { + let res = x * y * z; + return res; + })(5, 4, 2); + assert(product === 40); + + const half = (x => { + return x / 2; + })(10); + assert(half === 5); + + console.log("PASS"); +} catch { + console.log("FAIL"); +} |