diff options
author | davidot <david.tuin@gmail.com> | 2021-09-29 15:51:30 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-09-30 08:16:32 +0100 |
commit | e5d48ee238b58e82660edf286a1942e800493621 (patch) | |
tree | e3cc07437c24308e45d98df0e621a0127de41ad2 /Userland/Libraries/LibJS/Tests/switch-basic.js | |
parent | 830ea0414cc6e4686514ff7de337787dbb6c3b71 (diff) | |
download | serenity-e5d48ee238b58e82660edf286a1942e800493621.zip |
LibJS: Fix switch skipping case evaluation when hitting the default case
When no case match we should not just execute the statements of the
default case but also of any cases below the default case.
Diffstat (limited to 'Userland/Libraries/LibJS/Tests/switch-basic.js')
-rw-r--r-- | Userland/Libraries/LibJS/Tests/switch-basic.js | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/switch-basic.js b/Userland/Libraries/LibJS/Tests/switch-basic.js index 36210dda17..bab19cfc0d 100644 --- a/Userland/Libraries/LibJS/Tests/switch-basic.js +++ b/Userland/Libraries/LibJS/Tests/switch-basic.js @@ -67,11 +67,43 @@ describe("basic switch tests", () => { } expect(i).toBe(5); }); + + test("default branch is not taken if more exact branch exists", () => { + function switchTest(i) { + let result = 0; + switch (i) { + case 1: + result += 1; + break; + case 1: + expect().fail(); + case 2: + result += 2; + default: + result += 4; + case 3: + result += 8; + break; + case 2: + expect().fail(); + } + return result; + } + + expect(switchTest(1)).toBe(1); + expect(switchTest(2)).toBe(14); + expect(switchTest(3)).toBe(8); + expect(switchTest(4)).toBe(12); + }); }); describe("errors", () => { test("syntax errors", () => { expect("switch () {}").not.toEval(); + expect("switch () { case 1: continue; }").not.toEval(); + expect("switch () { case 1: break doesnotexist; }").not.toEval(); + expect("label: switch () { case 1: break not_the_right_label; }").not.toEval(); + expect("label: switch () { case 1: continue label; }").not.toEval(); expect("switch (foo) { bar }").not.toEval(); expect("switch (foo) { default: default: }").not.toEval(); }); |