summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/02_runtime/04_switch_case
diff options
context:
space:
mode:
Diffstat (limited to 'tests/custom/02_runtime/04_switch_case')
-rw-r--r--tests/custom/02_runtime/04_switch_case325
1 files changed, 325 insertions, 0 deletions
diff --git a/tests/custom/02_runtime/04_switch_case b/tests/custom/02_runtime/04_switch_case
new file mode 100644
index 0000000..4c1fc57
--- /dev/null
+++ b/tests/custom/02_runtime/04_switch_case
@@ -0,0 +1,325 @@
+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 a single default case matches.
+
+-- Expect stdout --
+default
+default
+-- End --
+
+-- Testcase --
+{%
+ for (n in [1, 3]) {
+ switch (n) {
+ default:
+ print("default\n");
+ }
+ }
+%}
+-- End --
+
+
+5. 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 3:
+
+ ` default:`
+ ^-- Near here
+
+
+Syntax error: Expecting expression
+In line 8, byte 2:
+
+ ` }`
+ ^-- Near here
+
+
+-- End --
+
+-- Testcase --
+{%
+ switch (1) {
+ default:
+ print("default1\n");
+
+ default:
+ print("default2\n");
+ }
+%}
+-- End --
+
+
+6. 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 --
+
+
+7. 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 --
+
+
+8. 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 --
+
+
+9. 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 --
+
+
+10. 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 --
+
+
+11. Ensure that exceptions break out of switch statements.
+
+-- Expect stdout --
+one
+-- End --
+
+-- Expect stderr --
+Died
+In test(), line 6, byte 8:
+ called from anonymous function ([stdin]:17:14)
+
+ ` 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 --
+
+
+12. Ensure that consecutive cases values are properly handled.
+
+-- Expect stdout --
+three and four
+-- End --
+
+-- Testcase --
+{%
+ switch (3) {
+ case 1:
+ case 2:
+ print("one and two\n");
+ break;
+
+ case 3:
+ case 4:
+ print("three and four\n");
+ break;
+
+ default:
+ print("five\n");
+ }
+%}
+-- End --