about summary refs log tree commit diff stats
path: root/js/baba-yaga/scratch/baba/test_pattern_guards.baba
blob: 8b8e2cd87280394a774fac3b0cb59e8e2d714892 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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 ===";