about summary refs log tree commit diff stats
path: root/js/baba-yaga/scratch/baba/test_pattern_guards.baba
diff options
context:
space:
mode:
Diffstat (limited to 'js/baba-yaga/scratch/baba/test_pattern_guards.baba')
-rw-r--r--js/baba-yaga/scratch/baba/test_pattern_guards.baba57
1 files changed, 57 insertions, 0 deletions
diff --git a/js/baba-yaga/scratch/baba/test_pattern_guards.baba b/js/baba-yaga/scratch/baba/test_pattern_guards.baba
new file mode 100644
index 0000000..8b8e2cd
--- /dev/null
+++ b/js/baba-yaga/scratch/baba/test_pattern_guards.baba
@@ -0,0 +1,57 @@
+// Test file for pattern guards
+io.out "=== Testing Pattern Guards ===";
+
+// Test basic guard with literal pattern
+classifyNumber : n ->
+  when n is
+    0 if n = 0 then "exactly zero"
+    n if n > 0 then "positive"
+    n if n < 0 then "negative"
+    _ then "unknown";
+
+io.out "classifyNumber 5:";
+io.out (classifyNumber 5); // Should be "positive"
+
+io.out "classifyNumber -3:";
+io.out (classifyNumber -3); // Should be "negative"
+
+io.out "classifyNumber 0:";
+io.out (classifyNumber 0); // Should be "exactly zero"
+
+// Test guard with range conditions (avoiding .. operator)
+categorizeAge : age ->
+  when age is
+    a if (a >= 0 and a < 18) then "minor"
+    a if (a >= 18 and a < 65) then "adult"
+    a if (a >= 65) then "senior"
+    _ then "invalid";
+
+io.out "categorizeAge 16:";
+io.out (categorizeAge 16); // Should be "minor"
+
+io.out "categorizeAge 30:";
+io.out (categorizeAge 30); // Should be "adult"
+
+io.out "categorizeAge 70:";
+io.out (categorizeAge 70); // Should be "senior"
+
+// Test guard with complex conditions
+gradeStudent : score ->
+  when score is
+    s if (s >= 90) then "A"
+    s if (s >= 80 and s < 90) then "B"
+    s if (s >= 70 and s < 80) then "C"
+    s if (s >= 60 and s < 70) then "D"
+    s if (s < 60) then "F"
+    _ then "Invalid score";
+
+io.out "gradeStudent 95:";
+io.out (gradeStudent 95); // Should be "A"
+
+io.out "gradeStudent 75:";
+io.out (gradeStudent 75); // Should be "C"
+
+io.out "gradeStudent 45:";
+io.out (gradeStudent 45); // Should be "F"
+
+io.out "=== Pattern Guards Tests Completed ===";