about summary refs log tree commit diff stats
path: root/js/scripting-lang/tests/07_case_expressions.txt
diff options
context:
space:
mode:
Diffstat (limited to 'js/scripting-lang/tests/07_case_expressions.txt')
-rw-r--r--js/scripting-lang/tests/07_case_expressions.txt47
1 files changed, 47 insertions, 0 deletions
diff --git a/js/scripting-lang/tests/07_case_expressions.txt b/js/scripting-lang/tests/07_case_expressions.txt
new file mode 100644
index 0000000..83bd1bb
--- /dev/null
+++ b/js/scripting-lang/tests/07_case_expressions.txt
@@ -0,0 +1,47 @@
+/* Unit Test: Case Expressions */
+/* Tests: Pattern matching, wildcards, nested cases */
+
+/* Basic case expressions */
+factorial : n -> 
+  when n is
+    0 then 1
+    _ then n * (factorial (n - 1));
+
+grade : score -> 
+  when score is
+    90 then "A"
+    80 then "B"
+    70 then "C"
+    _  then "F";
+
+/* Test case expressions */
+fact5 : factorial 5;
+grade1 : grade 95;
+grade2 : grade 85;
+grade3 : grade 65;
+
+/* Test results */
+..assert fact5 = 120;
+..assert grade1 = "F";  /* 95 doesn't match 90, so falls through to wildcard */
+..assert grade2 = "F";  /* 85 doesn't match 80, so falls through to wildcard */
+..assert grade3 = "F";
+
+/* Multi-parameter case expressions */
+compare : x y -> 
+  when x y is
+    0 0 then "both zero"
+    0 _ then "x is zero"
+    _ 0 then "y is zero"
+    _ _ then "neither zero";
+
+test1 : compare 0 0;
+test2 : compare 0 5;
+test3 : compare 5 0;
+test4 : compare 5 5;
+
+..assert test1 = "both zero";
+..assert test2 = "x is zero";
+..assert test3 = "y is zero";
+..assert test4 = "neither zero";
+
+..out "Case expressions test completed"; 
\ No newline at end of file