123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- test("Nested try/catch/finally with break", () => {
- const executionOrder = [];
- try {
- for (const i = 1337; ; expect().fail("Jumped to for loop update block")) {
- try {
- try {
- try {
- break;
- } finally {
- expect(i).toBe(1337);
- executionOrder.push(1);
- }
- } finally {
- expect(i).toBe(1337);
- executionOrder.push(2);
- }
- } finally {
- expect(i).toBe(1337);
- executionOrder.push(3);
- }
- expect().fail("Running code after second to most outer try");
- }
- } finally {
- expect(() => {
- i;
- }).toThrowWithMessage(ReferenceError, "'i' is not defined");
- executionOrder.push(4);
- }
- expect(() => {
- i;
- }).toThrowWithMessage(ReferenceError, "'i' is not defined");
- expect(executionOrder).toEqual([1, 2, 3, 4]);
- });
- test("Nested try/finally with labelled break", () => {
- const executionOrder = [];
- outer: try {
- const libjs = 0;
- expect(libjs).toBe(0);
- try {
- const serenity = 1;
- expect(libjs).toBe(0);
- expect(serenity).toBe(1);
- for (const i = 2; ; expect().fail("Jumped to for loop update block")) {
- const foo = 3;
- expect(libjs).toBe(0);
- expect(serenity).toBe(1);
- expect(i).toBe(2);
- expect(foo).toBe(3);
- try {
- const bar = 4;
- expect(libjs).toBe(0);
- expect(serenity).toBe(1);
- expect(i).toBe(2);
- expect(foo).toBe(3);
- expect(bar).toBe(4);
- try {
- const baz = 5;
- expect(libjs).toBe(0);
- expect(serenity).toBe(1);
- expect(i).toBe(2);
- expect(foo).toBe(3);
- expect(bar).toBe(4);
- expect(baz).toBe(5);
- try {
- const whf = 6;
- expect(libjs).toBe(0);
- expect(serenity).toBe(1);
- expect(i).toBe(2);
- expect(foo).toBe(3);
- expect(bar).toBe(4);
- expect(baz).toBe(5);
- expect(whf).toBe(6);
- break outer;
- } finally {
- const innerFinally1 = 7;
- expect(() => {
- whf;
- }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
- expect(libjs).toBe(0);
- expect(serenity).toBe(1);
- expect(i).toBe(2);
- expect(foo).toBe(3);
- expect(bar).toBe(4);
- expect(baz).toBe(5);
- expect(innerFinally1).toBe(7);
- executionOrder.push(1);
- }
- expect().fail("Running code after most inner try");
- } finally {
- const innerFinally2 = 8;
- expect(() => {
- baz;
- }).toThrowWithMessage(ReferenceError, "'baz' is not defined");
- expect(() => {
- whf;
- }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
- expect(() => {
- innerFinally1;
- }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
- expect(libjs).toBe(0);
- expect(serenity).toBe(1);
- expect(i).toBe(2);
- expect(foo).toBe(3);
- expect(bar).toBe(4);
- expect(innerFinally2).toBe(8);
- executionOrder.push(2);
- }
- expect().fail("Running code after second to most inner try");
- } finally {
- const innerFinally3 = 9;
- expect(() => {
- bar;
- }).toThrowWithMessage(ReferenceError, "'bar' is not defined");
- expect(() => {
- baz;
- }).toThrowWithMessage(ReferenceError, "'baz' is not defined");
- expect(() => {
- whf;
- }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
- expect(() => {
- innerFinally1;
- }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
- expect(() => {
- innerFinally2;
- }).toThrowWithMessage(ReferenceError, "'innerFinally2' is not defined");
- expect(libjs).toBe(0);
- expect(serenity).toBe(1);
- expect(i).toBe(2);
- expect(foo).toBe(3);
- expect(innerFinally3).toBe(9);
- executionOrder.push(3);
- }
- expect().fail("Running code after third to most inner try");
- }
- expect().fail("Running code after for loop");
- } finally {
- const innerFinally4 = 10;
- expect(() => {
- serenity;
- }).toThrowWithMessage(ReferenceError, "'serenity' is not defined");
- expect(() => {
- i;
- }).toThrowWithMessage(ReferenceError, "'i' is not defined");
- expect(() => {
- foo;
- }).toThrowWithMessage(ReferenceError, "'foo' is not defined");
- expect(() => {
- bar;
- }).toThrowWithMessage(ReferenceError, "'bar' is not defined");
- expect(() => {
- baz;
- }).toThrowWithMessage(ReferenceError, "'baz' is not defined");
- expect(() => {
- whf;
- }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
- expect(() => {
- innerFinally1;
- }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
- expect(() => {
- innerFinally2;
- }).toThrowWithMessage(ReferenceError, "'innerFinally2' is not defined");
- expect(() => {
- innerFinally3;
- }).toThrowWithMessage(ReferenceError, "'innerFinally3' is not defined");
- expect(libjs).toBe(0);
- expect(innerFinally4).toBe(10);
- executionOrder.push(4);
- }
- expect().fail("Running code after second to outer try");
- } finally {
- const innerFinally5 = 11;
- expect(() => {
- libjs;
- }).toThrowWithMessage(ReferenceError, "'libjs' is not defined");
- expect(() => {
- serenity;
- }).toThrowWithMessage(ReferenceError, "'serenity' is not defined");
- expect(() => {
- i;
- }).toThrowWithMessage(ReferenceError, "'i' is not defined");
- expect(() => {
- foo;
- }).toThrowWithMessage(ReferenceError, "'foo' is not defined");
- expect(() => {
- bar;
- }).toThrowWithMessage(ReferenceError, "'bar' is not defined");
- expect(() => {
- baz;
- }).toThrowWithMessage(ReferenceError, "'baz' is not defined");
- expect(() => {
- whf;
- }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
- expect(() => {
- innerFinally1;
- }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
- expect(() => {
- innerFinally2;
- }).toThrowWithMessage(ReferenceError, "'innerFinally2' is not defined");
- expect(() => {
- innerFinally3;
- }).toThrowWithMessage(ReferenceError, "'innerFinally3' is not defined");
- expect(() => {
- innerFinally4;
- }).toThrowWithMessage(ReferenceError, "'innerFinally4' is not defined");
- expect(innerFinally5).toBe(11);
- executionOrder.push(5);
- }
- expect(() => {
- libjs;
- }).toThrowWithMessage(ReferenceError, "'libjs' is not defined");
- expect(() => {
- serenity;
- }).toThrowWithMessage(ReferenceError, "'serenity' is not defined");
- expect(() => {
- i;
- }).toThrowWithMessage(ReferenceError, "'i' is not defined");
- expect(() => {
- foo;
- }).toThrowWithMessage(ReferenceError, "'foo' is not defined");
- expect(() => {
- bar;
- }).toThrowWithMessage(ReferenceError, "'bar' is not defined");
- expect(() => {
- baz;
- }).toThrowWithMessage(ReferenceError, "'baz' is not defined");
- expect(() => {
- whf;
- }).toThrowWithMessage(ReferenceError, "'whf' is not defined");
- expect(() => {
- innerFinally1;
- }).toThrowWithMessage(ReferenceError, "'innerFinally1' is not defined");
- expect(() => {
- innerFinally2;
- }).toThrowWithMessage(ReferenceError, "'innerFinally2' is not defined");
- expect(() => {
- innerFinally3;
- }).toThrowWithMessage(ReferenceError, "'innerFinally3' is not defined");
- expect(() => {
- innerFinally4;
- }).toThrowWithMessage(ReferenceError, "'innerFinally4' is not defined");
- expect(() => {
- innerFinally5;
- }).toThrowWithMessage(ReferenceError, "'innerFinally5' is not defined");
- expect(executionOrder).toEqual([1, 2, 3, 4, 5]);
- });
- test("labelled break in finally overrides labelled break in try", () => {
- const executionOrder = [];
- outer: for (const i = 1; ; expect().fail("Jumped to outer for loop update block")) {
- inner: for (const j = 2; ; expect().fail("Jumped to inner for loop update block")) {
- try {
- executionOrder.push(1);
- break inner;
- } finally {
- executionOrder.push(2);
- break outer;
- }
- expect().fail("Running code after try block");
- }
- expect().fail("Running code after inner for loop");
- }
- expect(executionOrder).toEqual([1, 2]);
- });
- test("Throw while breaking", () => {
- const executionOrder = [];
- try {
- for (const i = 1337; ; expect().fail("Jumped to for loop update block")) {
- try {
- executionOrder.push(1);
- break;
- } finally {
- executionOrder.push(2);
- throw 1;
- }
- }
- } finally {
- executionOrder.push(3);
- }
- expect(() => {
- i;
- }).toThrowWithMessage(ReferenceError, "'i' is not defined");
- expect(executionOrder).toEqual([1, 2, 3]);
- });
- test("Throw while breaking with nested try-catch in finalizer", () => {
- const executionOrder = [];
- try {
- for (const i = 1337; ; expect().fail("Jumped to for loop update block")) {
- try {
- executionOrder.push(1);
- break;
- } finally {
- try {
- throw 1;
- } catch {
- executionOrder.push(2);
- }
- executionOrder.push(3);
- }
- expect().fail("Jumped out of inner finally statement");
- }
- } finally {
- executionOrder.push(4);
- }
- expect(() => {
- i;
- }).toThrowWithMessage(ReferenceError, "'i' is not defined");
- expect(executionOrder).toEqual([1, 2, 3, 4]);
- });
- test("Throw while breaking with nested try-catch-finally in finalizer", () => {
- const executionOrder = [];
- try {
- for (const i = 1337; ; expect().fail("Jumped to for loop update block")) {
- try {
- executionOrder.push(1);
- break;
- } finally {
- try {
- executionOrder.push(2);
- } catch {
- expect.fail("Entered catch");
- } finally {
- executionOrder.push(3);
- }
- executionOrder.push(4);
- }
- expect().fail("Jumped out of inner finally statement");
- }
- } finally {
- executionOrder.push(5);
- }
- expect(() => {
- i;
- }).toThrowWithMessage(ReferenceError, "'i' is not defined");
- expect(executionOrder).toEqual([1, 2, 3, 4, 5]);
- });
- test("Throw while breaking with nested try-catch-finally with throw in finalizer", () => {
- const executionOrder = [];
- try {
- for (const i = 1337; ; expect().fail("Jumped to for loop update block")) {
- try {
- executionOrder.push(1);
- break;
- } finally {
- try {
- throw 1;
- } catch {
- executionOrder.push(2);
- } finally {
- executionOrder.push(3);
- }
- executionOrder.push(4);
- }
- expect().fail("Jumped out of inner finally statement");
- }
- } finally {
- executionOrder.push(5);
- }
- expect(() => {
- i;
- }).toThrowWithMessage(ReferenceError, "'i' is not defined");
- expect(executionOrder).toEqual([1, 2, 3, 4, 5]);
- });
|