summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--eval.c3
-rw-r--r--tests/02_runtime/04_switch_case93
2 files changed, 96 insertions, 0 deletions
diff --git a/eval.c b/eval.c
index 074baa5..fdc88b9 100644
--- a/eval.c
+++ b/eval.c
@@ -1449,6 +1449,9 @@ ut_execute_switch_case(struct ut_state *state, uint32_t off)
rv = NULL;
break;
}
+ else if (ut_is_type(rv, T_RETURN) || ut_is_type(rv, T_EXCEPTION) || ut_is_type(rv, T_CONTINUE)) {
+ break;
+ }
}
json_object_put(v[0]);
diff --git a/tests/02_runtime/04_switch_case b/tests/02_runtime/04_switch_case
index 9a871fd..bc8b80e 100644
--- a/tests/02_runtime/04_switch_case
+++ b/tests/02_runtime/04_switch_case
@@ -177,3 +177,96 @@ true
print(x, "\n");
%}
-- End --
+
+
+8. Ensure that `return` breaks out of switch statements.
+
+-- Expect stdout --
+one
+two
+-- End --
+
+-- Testcase --
+{%
+ function test(n) {
+ switch (n) {
+ case 1:
+ return "one";
+
+ case 2:
+ return "two";
+
+ default:
+ return "three";
+ }
+ }
+
+ print(test(1), "\n");
+ print(test(2), "\n");
+%}
+-- End --
+
+
+9. Ensure that `continue` breaks out of switch statements.
+
+-- Expect stdout --
+one
+two
+-- End --
+
+-- Testcase --
+{%
+ for (n in [1,2]) {
+ switch (n) {
+ case 1:
+ print("one\n");
+ continue;
+
+ case 2:
+ print("two\n");
+ continue;
+
+ default:
+ print("three\n");
+ }
+ }
+%}
+-- End --
+
+
+10. Ensure that exceptions break out of switch statements.
+
+-- Expect stdout --
+one
+-- End --
+
+-- Expect stderr --
+Died
+In line 6, byte 7:
+
+ ` die();`
+ Near here ------^
+
+
+-- End --
+
+-- Testcase --
+{%
+ function test(n) {
+ switch (n) {
+ case 1:
+ print("one\n");
+ die();
+
+ case 2:
+ print("two\n");
+ die();
+
+ default:
+ print("three\n");
+ }
+ }
+
+ print(test(1), "\n");
+%}
+-- End --