Skip to content

Commit

Permalink
Add tests for labeled break/continue
Browse files Browse the repository at this point in the history
  • Loading branch information
CountBleck committed Dec 17, 2024
1 parent b47dc99 commit 692b388
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/compiler/labeled-break-continue-errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"stderr": [
"TS1116: A 'break' statement can only jump to a label of an enclosing statement",
"TS1114: Duplicate label 'duplicate'.",
"TS1115: A 'continue' statement can only jump to a label of an enclosing iteration statement.",
"TS1115: A 'continue' statement can only jump to a label of an enclosing iteration statement.",
"TS1115: A 'continue' statement can only jump to a label of an enclosing iteration statement.",
"EOF"
]
}
24 changes: 24 additions & 0 deletions tests/compiler/labeled-break-continue-errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
break nonexistent;

Check failure on line 2 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node lts/*)

Expected indentation of 2 spaces but found 4

Check failure on line 2 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node current)

Expected indentation of 2 spaces but found 4

Check failure on line 2 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node current)

Expected indentation of 2 spaces but found 4

Check failure on line 2 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node lts/*)

Expected indentation of 2 spaces but found 4
}

duplicate:

Check failure on line 5 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node lts/*)

'duplicate:' is defined but never used

Check failure on line 5 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node current)

'duplicate:' is defined but never used

Check failure on line 5 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node current)

'duplicate:' is defined but never used

Check failure on line 5 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node lts/*)

'duplicate:' is defined but never used
{
duplicate: {}

Check failure on line 7 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node lts/*)

Expected indentation of 2 spaces but found 4

Check failure on line 7 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node lts/*)

'duplicate:' is defined but never used

Check failure on line 7 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node current)

Expected indentation of 2 spaces but found 4

Check failure on line 7 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node current)

'duplicate:' is defined but never used

Check failure on line 7 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node current)

Expected indentation of 2 spaces but found 4

Check failure on line 7 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node current)

'duplicate:' is defined but never used

Check failure on line 7 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node lts/*)

Expected indentation of 2 spaces but found 4

Check failure on line 7 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node lts/*)

'duplicate:' is defined but never used
}

for (let i = 0; i < 3; i++) {
continue nonexistent;

Check failure on line 11 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node lts/*)

Expected indentation of 2 spaces but found 4

Check failure on line 11 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node current)

Expected indentation of 2 spaces but found 4

Check failure on line 11 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node current)

Expected indentation of 2 spaces but found 4

Check failure on line 11 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node lts/*)

Expected indentation of 2 spaces but found 4
}

labelA:
{
continue labelA;

Check failure on line 16 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node lts/*)

Expected indentation of 2 spaces but found 4

Check failure on line 16 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node current)

Expected indentation of 2 spaces but found 4

Check failure on line 16 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node current)

Expected indentation of 2 spaces but found 4

Check failure on line 16 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node lts/*)

Expected indentation of 2 spaces but found 4
}

labelB:
switch (0) {
case 0: continue labelB;

Check failure on line 21 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node lts/*)

Expected indentation of 2 spaces but found 4

Check failure on line 21 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node current)

Expected indentation of 2 spaces but found 4

Check failure on line 21 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node current)

Expected indentation of 2 spaces but found 4

Check failure on line 21 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node lts/*)

Expected indentation of 2 spaces but found 4
}

ERROR("EOF")

Check failure on line 24 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node lts/*)

Missing semicolon

Check failure on line 24 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (macos, node current)

Missing semicolon

Check failure on line 24 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node current)

Missing semicolon

Check failure on line 24 in tests/compiler/labeled-break-continue-errors.ts

View workflow job for this annotation

GitHub Actions / Compiler (ubuntu, node lts/*)

Missing semicolon
1 change: 1 addition & 0 deletions tests/compiler/labeled-break-continue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
78 changes: 78 additions & 0 deletions tests/compiler/labeled-break-continue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
function tests(): string[] {
const results: string[] = [];

label:
for (let i = 0; i < 6; i++) {
if (i == 2) {
results.push("continue!");
continue label;
}

if (i == 4) {
results.push("break!");
break label;
}

results.push(`first loop ${i}`);
}

sweetch:
switch (1) {
case 1:
for (let i = 0; i < 6; i++) {
results.push(`second ${i}`);
if (i == 3) break sweetch;
}
results.push("skipped");
}

escape:
{
results.push("hi");
break escape;
results.push("skipped");
}

outer:
for (let i = 0; i < 5; i++) {
exit:
if (i % 2 == 0) {
for (let j = 0; j < 4; j++) {
if (i == j) continue outer;
else if (j > i) break exit;

results.push(`did ${i} ${j}`);
}
}

results.push(`reached end of ${i}`);
}

return results;
}

const results = tests();
const expected = [
"first loop 0",
"first loop 1",
"continue!",
"first loop 3",
"break!",
"second 0",
"second 1",
"second 2",
"second 3",
"hi",
"reached end of 1",
"did 2 0",
"did 2 1",
"reached end of 3",
"did 4 0",
"did 4 1",
"did 4 2",
"did 4 3",
"reached end of 4"
];

assert(results.length == expected.length);
for (let i = 0; i < expected.length; i++) assert(results[i] == expected[i]);
26 changes: 26 additions & 0 deletions tests/parser/labels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
labelA:
{
a;
b;
}

labelB:
for (let i = 0; i < 3; i++) {}

labelC:
for (const x of y) {}

labelD:
do {} while (0)

labelE:
while (0) {}

labelF:
try {} catch (e) {}

labelG:
if (0) {} else {}

labelH: // ERROR 1344: "A label is not allowed here."
let x = 123;
21 changes: 21 additions & 0 deletions tests/parser/labels.ts.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
labelA:
{
a;
b;
}
labelB:
for (let i = 0; i < 3; i++) {}
labelC:
for (const x of y) {}
labelD:
do {} while (0);
labelE:
while (0) {}
labelF:
try {
} catch (e) {
}
labelG:
if (0) {} else {}
let x = 123;
// ERROR 1344: "A label is not allowed here." in labels.ts(25,1+6)

0 comments on commit 692b388

Please sign in to comment.