erlang: pattern matching + case (+21 tests)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-24 17:36:44 +00:00
parent efbab24cb2
commit 4965be71ca
3 changed files with 170 additions and 15 deletions

View File

@@ -123,6 +123,57 @@
(ev "X = 5, if X > 0 -> 1; true -> 0 end")
1)
;; ── pattern matching ─────────────────────────────────────────────
(er-eval-test "match atom literal" (nm (ev "ok = ok, done")) "done")
(er-eval-test "match int literal" (ev "5 = 5, 42") 42)
(er-eval-test "match tuple bind"
(ev "{ok, V} = {ok, 99}, V") 99)
(er-eval-test "match tuple nested"
(ev "{A, {B, C}} = {1, {2, 3}}, A + B + C") 6)
(er-eval-test "match cons head"
(ev "[H|T] = [1, 2, 3], H") 1)
(er-eval-test "match cons tail head"
(ev "[_, H|_] = [1, 2, 3], H") 2)
(er-eval-test "match nil"
(ev "[] = [], 7") 7)
(er-eval-test "match wildcard always"
(ev "_ = 42, 7") 7)
(er-eval-test "match var reuse equal"
(ev "X = 5, X = 5, X") 5)
;; ── case ─────────────────────────────────────────────────────────
(er-eval-test "case bind" (ev "case 5 of N -> N end") 5)
(er-eval-test "case tuple"
(ev "case {ok, 42} of {ok, V} -> V end") 42)
(er-eval-test "case cons"
(ev "case [1, 2, 3] of [H|_] -> H end") 1)
(er-eval-test "case fallthrough"
(ev "case error of ok -> 1; error -> 2 end") 2)
(er-eval-test "case wildcard"
(nm (ev "case x of ok -> ok; _ -> err end"))
"err")
(er-eval-test "case guard"
(ev "case 5 of N when N > 0 -> pos; _ -> neg end")
(er-mk-atom "pos"))
(er-eval-test "case guard fallthrough"
(ev "case -3 of N when N > 0 -> pos; _ -> neg end")
(er-mk-atom "neg"))
(er-eval-test "case bound re-match"
(ev "X = 5, case 5 of X -> same; _ -> diff end")
(er-mk-atom "same"))
(er-eval-test "case bound re-match fail"
(ev "X = 5, case 6 of X -> same; _ -> diff end")
(er-mk-atom "diff"))
(er-eval-test "case nested tuple"
(ev "case {ok, {value, 42}} of {ok, {value, V}} -> V end")
42)
(er-eval-test "case multi-clause"
(ev "case 2 of 1 -> one; 2 -> two; _ -> other end")
(er-mk-atom "two"))
(er-eval-test "case leak binding"
(ev "case {ok, 7} of {ok, X} -> X end + 1")
8)
(define
er-eval-test-summary
(str "eval " er-eval-test-pass "/" er-eval-test-count))