Skip to content

Commit 692b388

Browse files
committed
Add tests for labeled break/continue
1 parent b47dc99 commit 692b388

6 files changed

+160
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"stderr": [
3+
"TS1116: A 'break' statement can only jump to a label of an enclosing statement",
4+
"TS1114: Duplicate label 'duplicate'.",
5+
"TS1115: A 'continue' statement can only jump to a label of an enclosing iteration statement.",
6+
"TS1115: A 'continue' statement can only jump to a label of an enclosing iteration statement.",
7+
"TS1115: A 'continue' statement can only jump to a label of an enclosing iteration statement.",
8+
"EOF"
9+
]
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
break nonexistent;
3+
}
4+
5+
duplicate:
6+
{
7+
duplicate: {}
8+
}
9+
10+
for (let i = 0; i < 3; i++) {
11+
continue nonexistent;
12+
}
13+
14+
labelA:
15+
{
16+
continue labelA;
17+
}
18+
19+
labelB:
20+
switch (0) {
21+
case 0: continue labelB;
22+
}
23+
24+
ERROR("EOF")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
function tests(): string[] {
2+
const results: string[] = [];
3+
4+
label:
5+
for (let i = 0; i < 6; i++) {
6+
if (i == 2) {
7+
results.push("continue!");
8+
continue label;
9+
}
10+
11+
if (i == 4) {
12+
results.push("break!");
13+
break label;
14+
}
15+
16+
results.push(`first loop ${i}`);
17+
}
18+
19+
sweetch:
20+
switch (1) {
21+
case 1:
22+
for (let i = 0; i < 6; i++) {
23+
results.push(`second ${i}`);
24+
if (i == 3) break sweetch;
25+
}
26+
results.push("skipped");
27+
}
28+
29+
escape:
30+
{
31+
results.push("hi");
32+
break escape;
33+
results.push("skipped");
34+
}
35+
36+
outer:
37+
for (let i = 0; i < 5; i++) {
38+
exit:
39+
if (i % 2 == 0) {
40+
for (let j = 0; j < 4; j++) {
41+
if (i == j) continue outer;
42+
else if (j > i) break exit;
43+
44+
results.push(`did ${i} ${j}`);
45+
}
46+
}
47+
48+
results.push(`reached end of ${i}`);
49+
}
50+
51+
return results;
52+
}
53+
54+
const results = tests();
55+
const expected = [
56+
"first loop 0",
57+
"first loop 1",
58+
"continue!",
59+
"first loop 3",
60+
"break!",
61+
"second 0",
62+
"second 1",
63+
"second 2",
64+
"second 3",
65+
"hi",
66+
"reached end of 1",
67+
"did 2 0",
68+
"did 2 1",
69+
"reached end of 3",
70+
"did 4 0",
71+
"did 4 1",
72+
"did 4 2",
73+
"did 4 3",
74+
"reached end of 4"
75+
];
76+
77+
assert(results.length == expected.length);
78+
for (let i = 0; i < expected.length; i++) assert(results[i] == expected[i]);

tests/parser/labels.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
labelA:
2+
{
3+
a;
4+
b;
5+
}
6+
7+
labelB:
8+
for (let i = 0; i < 3; i++) {}
9+
10+
labelC:
11+
for (const x of y) {}
12+
13+
labelD:
14+
do {} while (0)
15+
16+
labelE:
17+
while (0) {}
18+
19+
labelF:
20+
try {} catch (e) {}
21+
22+
labelG:
23+
if (0) {} else {}
24+
25+
labelH: // ERROR 1344: "A label is not allowed here."
26+
let x = 123;

tests/parser/labels.ts.fixture.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
labelA:
2+
{
3+
a;
4+
b;
5+
}
6+
labelB:
7+
for (let i = 0; i < 3; i++) {}
8+
labelC:
9+
for (const x of y) {}
10+
labelD:
11+
do {} while (0);
12+
labelE:
13+
while (0) {}
14+
labelF:
15+
try {
16+
} catch (e) {
17+
}
18+
labelG:
19+
if (0) {} else {}
20+
let x = 123;
21+
// ERROR 1344: "A label is not allowed here." in labels.ts(25,1+6)

0 commit comments

Comments
 (0)