diff options
author | davidot <davidot@serenityos.org> | 2021-11-23 16:09:28 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-11-29 15:20:07 +0000 |
commit | e69276e704b4c3eb56d0ad6108a3de16cc8dd175 (patch) | |
tree | 42fefe53683699df18a56e5149db05606bba29e5 /Userland/Libraries/LibJS/Tests | |
parent | b3699029e2d91ae3bb2e023e8e83d3afc6c19232 (diff) | |
download | serenity-e69276e704b4c3eb56d0ad6108a3de16cc8dd175.zip |
LibJS: Implement parsing and executing for-await-of loops
Diffstat (limited to 'Userland/Libraries/LibJS/Tests')
-rw-r--r-- | Userland/Libraries/LibJS/Tests/loops/for-await-of.js | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/loops/for-await-of.js b/Userland/Libraries/LibJS/Tests/loops/for-await-of.js new file mode 100644 index 0000000000..f590e7462f --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/loops/for-await-of.js @@ -0,0 +1,71 @@ +describe("basic behavior", () => { + test("empty array", () => { + var enteredFunction = false; + var rejected = false; + async function f() { + enteredFunction = true; + for await (const v of []) { + expect().fail("Should not enter loop"); + } + } + + f().then( + () => { + expect(enteredFunction).toBeTrue(); + }, + () => { + rejected = true; + } + ); + runQueuedPromiseJobs(); + expect(enteredFunction).toBeTrue(); + expect(rejected).toBeFalse(); + }); + + test("sync iterator", () => { + var loopIterations = 0; + var rejected = false; + async function f() { + for await (const v of [1]) { + expect(v).toBe(1); + loopIterations++; + } + } + + f().then( + () => { + expect(loopIterations).toBe(1); + }, + () => { + rejected = true; + } + ); + runQueuedPromiseJobs(); + expect(loopIterations).toBe(1); + expect(rejected).toBeFalse(); + }); +}); + +describe("only allowed in async functions", () => { + test("async functions", () => { + expect("async function foo() { for await (const v of []) return v; }").toEval(); + expect("(async function () { for await (const v of []) return v; })").toEval(); + expect("async () => { for await (const v of []) return v; }").toEval(); + }); + + test("regular functions", () => { + expect("function foo() { for await (const v of []) return v; }").not.toEval(); + expect("(function () { for await (const v of []) return v; })").not.toEval(); + expect("() => { for await (const v of []) return v; }").not.toEval(); + }); + + test("generator functions", () => { + expect("function* foo() { for await (const v of []) return v; }").not.toEval(); + expect("(function* () { for await (const v of []) return v; })").not.toEval(); + }); + + test("async genrator functions", () => { + expect("async function* foo() { for await (const v of []) yield v; }").toEval(); + expect("(async function* () { for await (const v of []) yield v; })").toEval(); + }); +}); |