summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests/labels.js
blob: 1747e8d0bb620f3d9ae8583114535b694fd36f8e (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
test("labeled plain scope", () => {
    test: {
        let o = 1;
        expect(o).toBe(1);
        break test;
        expect().fail();
    }
});

test("break on plain scope from inner scope", () => {
    outer: {
        {
            break outer;
        }
        expect().fail();
    }
});

test("labeled for loop with break", () => {
    let counter = 0;
    outer: for (a of [1, 2, 3]) {
        for (b of [4, 5, 6]) {
            if (a === 2 && b === 5) break outer;
            counter++;
        }
    }
    expect(counter).toBe(4);
});

test("labeled for loop with continue", () => {
    let counter = 0;
    outer: for (a of [1, 2, 3]) {
        for (b of [4, 5, 6]) {
            if (b === 6) continue outer;
            counter++;
        }
    }
    expect(counter).toBe(6);
});

test("invalid label across scope", () => {
    expect(`
        label: {
            (() => { break label; });
        }
    `).not.toEval();
});