diff --git a/tests/compiler/labeled-break-continue-errors.json b/tests/compiler/labeled-break-continue-errors.json new file mode 100644 index 0000000000..bc85e1c85c --- /dev/null +++ b/tests/compiler/labeled-break-continue-errors.json @@ -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" + ] +} \ No newline at end of file diff --git a/tests/compiler/labeled-break-continue-errors.ts b/tests/compiler/labeled-break-continue-errors.ts new file mode 100644 index 0000000000..c6ae629ba6 --- /dev/null +++ b/tests/compiler/labeled-break-continue-errors.ts @@ -0,0 +1,24 @@ +{ + break nonexistent; +} + +duplicate: +{ + duplicate: {} +} + +for (let i = 0; i < 3; i++) { + continue nonexistent; +} + +labelA: +{ + continue labelA; +} + +labelB: +switch (0) { + case 0: continue labelB; +} + +ERROR("EOF") \ No newline at end of file diff --git a/tests/compiler/labeled-break-continue.json b/tests/compiler/labeled-break-continue.json new file mode 100644 index 0000000000..9e26dfeeb6 --- /dev/null +++ b/tests/compiler/labeled-break-continue.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/tests/compiler/labeled-break-continue.ts b/tests/compiler/labeled-break-continue.ts new file mode 100644 index 0000000000..15120ce250 --- /dev/null +++ b/tests/compiler/labeled-break-continue.ts @@ -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]); \ No newline at end of file diff --git a/tests/parser/labels.ts b/tests/parser/labels.ts new file mode 100644 index 0000000000..23dec9a7e1 --- /dev/null +++ b/tests/parser/labels.ts @@ -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; \ No newline at end of file diff --git a/tests/parser/labels.ts.fixture.ts b/tests/parser/labels.ts.fixture.ts new file mode 100644 index 0000000000..640e7dfe60 --- /dev/null +++ b/tests/parser/labels.ts.fixture.ts @@ -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)