summaryrefslogtreecommitdiffhomepage
path: root/tests/02_runtime/04_switch_case
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2020-09-22 14:23:01 +0200
committerJo-Philipp Wich <jo@mein.io>2020-09-22 14:23:01 +0200
commitb4c605e8ab49e35dfbb7caa179eaa273c696ac9f (patch)
tree1b9e58a2c473709f7ef18f7746284c92c1218d43 /tests/02_runtime/04_switch_case
parentd36709f63e4bae54d5c9c3defd35eb3e23e64260 (diff)
syntax: allow empty switch statements
Also add testcases for switch/case blocks. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests/02_runtime/04_switch_case')
-rw-r--r--tests/02_runtime/04_switch_case179
1 files changed, 179 insertions, 0 deletions
diff --git a/tests/02_runtime/04_switch_case b/tests/02_runtime/04_switch_case
new file mode 100644
index 0000000..9a871fd
--- /dev/null
+++ b/tests/02_runtime/04_switch_case
@@ -0,0 +1,179 @@
+Testing utpl switch statements.
+
+
+1. Ensure that execution starts at the first matching case.
+
+-- Expect stdout --
+1a
+-- End --
+
+-- Testcase --
+{%
+ switch (1) {
+ case 1:
+ print("1a\n");
+ break;
+
+ case 1:
+ print("1b\n");
+ break;
+
+ case 2:
+ print("2\n");
+ break;
+ }
+%}
+-- End --
+
+
+2. Ensure that default case is only used if no case matches,
+ even if declared first.
+
+-- Expect stdout --
+1
+default
+-- End --
+
+-- Testcase --
+{%
+ for (n in [1, 3]) {
+ switch (n) {
+ default:
+ print("default\n");
+ break;
+
+ case 1:
+ print("1\n");
+ break;
+
+ case 2:
+ print("2\n");
+ break;
+ }
+ }
+%}
+-- End --
+
+
+3. Ensure that cases without break fall through into
+ subsequent cases.
+
+-- Expect stdout --
+1
+2
+default
+1
+2
+-- End --
+
+-- Testcase --
+{%
+ for (n in [1, 3]) {
+ switch (n) {
+ default:
+ print("default\n");
+
+ case 1:
+ print("1\n");
+
+ case 2:
+ print("2\n");
+ }
+ }
+%}
+-- End --
+
+
+4. Ensure that duplicate default cases emit a syntax
+ error during parsing.
+
+-- Expect stderr --
+Syntax error: more than one switch default case
+In line 6, byte 9:
+
+ ` default:`
+ Near here -----^
+
+
+-- End --
+
+-- Testcase --
+{%
+ switch (1) {
+ default:
+ print("default1\n");
+
+ default:
+ print("default2\n");
+ }
+%}
+-- End --
+
+
+5. Ensure that case values use strict comparison.
+
+-- Expect stdout --
+b
+b
+-- End --
+
+-- Testcase --
+{%
+ switch (1.0) {
+ case 1:
+ print("a\n");
+ break;
+
+ case 1.0:
+ print("b\n");
+ break;
+ }
+
+ switch ("123") {
+ case 123:
+ print("a\n");
+ break;
+
+ case "123":
+ print("b\n");
+ break;
+ }
+%}
+-- End --
+
+
+6. Ensure that case values may be complex expressions.
+
+-- Expect stdout --
+2, 3, 1
+-- End --
+
+-- Testcase --
+{%
+ switch (1) {
+ case a = 2, b = 3, c = 1:
+ print(join(", ", [ a, b, c ]), "\n");
+ break;
+ }
+%}
+-- End --
+
+
+7. Ensure that empty switch statements are accepted by the
+ parser and that the test expression is evaluated.
+
+-- Expect stdout --
+true
+-- End --
+
+-- Testcase --
+{%
+ x = false;
+
+ switch (x = true) {
+
+ }
+
+ print(x, "\n");
+%}
+-- End --