summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests/try-catch-finally-nested.js
blob: da9ad2d438a167d4ff1bd2a3b6c44642b7fa913b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
test("Nested try/catch/finally with exceptions", () => {
    // This test uses a combination of boolean "checkpoint" flags
    // and expect().fail() to ensure certain code paths have been
    // reached and others haven't.
    var level1TryHasBeenExecuted = false;
    var level1CatchHasBeenExecuted = false;
    var level1FinallyHasBeenExecuted = false;
    var level2TryHasBeenExecuted = false;
    var level2CatchHasBeenExecuted = false;
    var level3TryHasBeenExecuted = false;
    var level3CatchHasBeenExecuted = false;
    var level3FinallyHasBeenExecuted = false;
    expect(() => {
        try {
            level1TryHasBeenExecuted = true;
            foo();
            expect().fail();
        } catch (e) {
            level1CatchHasBeenExecuted = true;
            try {
                level2TryHasBeenExecuted = true;
                try {
                    level3TryHasBeenExecuted = true;
                    bar();
                    expect().fail();
                } catch (e) {
                    level3CatchHasBeenExecuted = true;
                } finally {
                    level3FinallyHasBeenExecuted = true;
                    baz();
                    expect().fail();
                }
                expect().fail();
            } catch (e) {
                level2CatchHasBeenExecuted = true;
                qux();
                expect().fail();
            }
            expect().fail();
        } finally {
            level1FinallyHasBeenExecuted = true;
            throw Error("Error in final finally");
            expect().fail();
        }
        expect().fail();
    }).toThrow(Error, "Error in final finally");
    expect(level1TryHasBeenExecuted).toBeTrue();
    expect(level1CatchHasBeenExecuted).toBeTrue();
    expect(level1FinallyHasBeenExecuted).toBeTrue();
    expect(level2TryHasBeenExecuted).toBeTrue();
    expect(level2CatchHasBeenExecuted).toBeTrue();
    expect(level3TryHasBeenExecuted).toBeTrue();
    expect(level3CatchHasBeenExecuted).toBeTrue();
    expect(level3FinallyHasBeenExecuted).toBeTrue();
});